Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor + fix: Cleanup proxy bundle #526

Merged
merged 3 commits into from Mar 24, 2023

Conversation

akrejcir
Copy link
Collaborator

What this PR does / why we need it:

  • Cleaned up code in vm-console-proxy-bundle module
  • Added validation that the bundle contains needed all resources

Release note:

None

@kubevirt-bot kubevirt-bot added release-note-none Denotes a PR that doesn't merit a release note. dco-signoff: yes Indicates the PR's author has DCO signed all their commits. labels Mar 16, 2023
@akrejcir
Copy link
Collaborator Author

/cc @codingben @0xFelix

@akrejcir akrejcir changed the title Cleanup proxy bundle chore: Cleanup proxy bundle Mar 16, 2023
@codingben
Copy link
Member

codingben commented Mar 16, 2023

@akrejcir IMHO it's code refactor: and not chore:, I think it'd be better to have the commit message as refactor: cleanup vm-console-proxy bundle. Personally I think that chore means non-code changes.

E.g. chore: Refactor proxy bundle code -> refactor: proxy bundle.

o, err := yamlv2.Marshal(&obj)
if err != nil {
return err
func validateBundle(bundle *Bundle) error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think validateBundle it's too general naming and validateBundleObjects would be better.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disagree. It validates that the Bundle struct is correct and can be used.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but it didn't mention what kind of validation it does. Maybe validateBundleParameters.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is only one function that does validation on Bundle, so it does all the validation. IMHO it's not needed to be more specific. If there would be multiple functions, then yes.

}
}
err = runtime.DefaultUnstructuredConverter.FromUnstructured(obj.UnstructuredContent(), destObj)
if err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about make it shorter:

if err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.UnstructuredContent(), destObj); err != nil {
	return nil, err
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The line is already quite long. I prefer it this way.

for {
obj := &unstructured.Unstructured{}
err := decoder.Decode(&obj)
if err == io.EOF {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you like it:

if err := decoder.Decode(&obj); err == io.EOF {
	return bundle, nil
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The err is checked multiple times, and this change would limit its scope only to the inside of the if statement.

@akrejcir akrejcir force-pushed the cleanup-proxy-bundle branch 2 times, most recently from 9de100c to 9ccb818 Compare March 16, 2023 16:08
@akrejcir
Copy link
Collaborator Author

/retest

1 similar comment
@akrejcir
Copy link
Collaborator Author

/retest

Copy link
Member

@0xFelix 0xFelix left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it really necessary to "half-hardcode" the expected bundle resources into the operand and then to load their values from the bundle YAML? Can't we either define the resources completely in code or load them just from the YAML?

internal/vm-console-proxy-bundle/bundle.go Show resolved Hide resolved
internal/vm-console-proxy-bundle/bundle.go Outdated Show resolved Hide resolved
@@ -91,3 +99,29 @@ func loadBundleFromBytes(data []byte) (*Bundle, error) {
}
}
}

func validateBundle(bundle *Bundle) error {
missingFields := make([]string, 0, 6)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we gain something by explicitly sizing the slice?

Copy link
Collaborator Author

@akrejcir akrejcir Mar 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We gain a little bit of performance for no change in readability.

internal/vm-console-proxy-bundle/bundle.go Show resolved Hide resolved
@0xFelix
Copy link
Member

0xFelix commented Mar 17, 2023

Also, the commit messages are a bit sparse. Could you give some more context, please?

@akrejcir
Copy link
Collaborator Author

Is it really necessary to "half-hardcode" the expected bundle resources into the operand and then to load their values from the bundle YAML? Can't we either define the resources completely in code or load them just from the YAML?

We cannot define the resources in ssp operator code, because they can change when a new version of vm-console-proxy is released. Loading them only from yaml would be good, but the reconciliation code needs to know the types of resources, so it can use the correct update functions.

I think we can think of a more generic solution as part of the SSP cleanup epic.

@codingben
Copy link
Member

Can you please change PR title from chore: Cleanup proxy bundle to refactor: Cleanup proxy bundle?

Copy link
Member

@codingben codingben left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd maybe move bundle-test.yaml and bundle-test-incorrect.yaml to test-yamls folder or something. It's up to you.

/lgtm

@kubevirt-bot kubevirt-bot added the lgtm Indicates that a PR is ready to be merged. label Mar 20, 2023
@akrejcir akrejcir changed the title chore: Cleanup proxy bundle refactor + fix: Cleanup proxy bundle Mar 21, 2023
Copy link
Member

@0xFelix 0xFelix left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some comments.

We cannot define the resources in ssp operator code, because they can change when a new version of vm-console-proxy is released. Loading them only from yaml would be good, but the reconciliation code needs to know the types of resources, so it can use the correct update functions.

TTO can load resources from the pipeline / task manifests without knowing exactly which resources it will encounter. It only has a list of known kinds it can handle / reconcile. Can't you do the same?

kind := obj.GetKind()

var destObj any
switch obj.GetKind() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

switch kind := obj.GetKind(); kind?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or if you need the kind var later, use it here too?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@0xFelix Done

@ksimon1 That code was used originally here, and I just refactored it to make it smaller.

internal/vm-console-proxy-bundle/bundle.go Outdated Show resolved Hide resolved
Reading of the vm-console-proxy bundle fails, if the yaml
bundle contains an object with unexpected kind.

Signed-off-by: Andrej Krejcir <akrejcir@redhat.com>
Simplify code that loads vm-console-proxy bundle.

Signed-off-by: Andrej Krejcir <akrejcir@redhat.com>
Validate that no field in Bundle is nil.

Signed-off-by: Andrej Krejcir <akrejcir@redhat.com>
@kubevirt-bot kubevirt-bot added size/XL and removed lgtm Indicates that a PR is ready to be merged. size/L labels Mar 21, 2023
@akrejcir
Copy link
Collaborator Author

TTO can load resources from the pipeline / task manifests without knowing exactly which resources it will encounter. It only has a list of known kinds it can handle / reconcile. Can't you do the same?

It will be some work, so we can add an Issue.

@sonarcloud
Copy link

sonarcloud bot commented Mar 21, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

Copy link
Member

@0xFelix 0xFelix left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm

@kubevirt-bot kubevirt-bot added the lgtm Indicates that a PR is ready to be merged. label Mar 24, 2023
@akrejcir
Copy link
Collaborator Author

/approve

@kubevirt-bot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: akrejcir

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@kubevirt-bot kubevirt-bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Mar 24, 2023
@kubevirt-bot kubevirt-bot merged commit 37dfdaf into kubevirt:master Mar 24, 2023
3 checks passed
@akrejcir akrejcir deleted the cleanup-proxy-bundle branch March 24, 2023 12:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. dco-signoff: yes Indicates the PR's author has DCO signed all their commits. lgtm Indicates that a PR is ready to be merged. release-note-none Denotes a PR that doesn't merit a release note. size/XL
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants