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

Bug 1686668: [csc] Ignore invalid values when reading csc.Spec.Packages #138

Merged

Conversation

kevinrizza
Copy link
Member

Problem:
In some cases, when the Openshift UI updates the package list of a csc
after removing a subscription, the update does not properly format the
package list. For example:

packages: ',strimzi-kafka-operator-test'

In this example, the package list accidentally includes a leading comma.

Right now, the marketplace doesn't handle poorly formatted package
lists in the csc spec. In general, if a user accidentally leaves a comma
behind the operator should ignore it and proceed as normal with the
list of valid packages.

Solution:
Any time the operator reads the package list, that read happens
from a public method that formats the package list and excludes invalid
packages (specifically empty string).
Added a private method getValidPackageSliceFromString() that takes a
package string and removes invalid elements from it.
Updated the two GetPackages() methods on the csc and spec types to
call this method before returning.
Added a new method on the csc type called GetPackages() that uses the
validation logic to reconstruct the Spec.Packages string.
Updated every case where we were referencing the Spec.Packages field
directly and replaced it with one of these methods.

@openshift-ci-robot openshift-ci-robot added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Mar 19, 2019
@@ -96,7 +96,7 @@ func (r *registry) GetAddress() string {
// ensureDeployment ensures that registry Deployment is present for serving
// the the grpc interface for the packages from the given operatorSources
func (r *registry) ensureDeployment(operatorSources string) error {
registryCommand := getCommand(r.csc.Spec.Packages, operatorSources)
registryCommand := getCommand(r.csc.GetPackages(), operatorSources)
Copy link
Member

Choose a reason for hiding this comment

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

You should also fix getCommand() and have all that string fixup happen in just one place.

Copy link
Member Author

Choose a reason for hiding this comment

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

Addressed.

pkg/catalogsourceconfig/cache.go Show resolved Hide resolved

pkgSlice := strings.Split(strings.Replace(pkgs, " ", "", -1), ",")

for _, pkg := range pkgSlice {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would recommend trimming white spaces from each package name. After trimming, if the package name is not empty we add it.

Copy link
Member Author

Choose a reason for hiding this comment

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

See below.

func getValidPackageSliceFromString(pkgs string) []string {
pkgIds := make([]string, 0)

pkgSlice := strings.Split(strings.Replace(pkgs, " ", "", -1), ",")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is strings.Replace required If we sanitize each package name by trimming white spaces? For a leading comma strings.Split returns a package name with a space.

Copy link
Member Author

Choose a reason for hiding this comment

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

If we do it to the initial string once that will remove any extra whitespace, meaning we don't need to call strings.Replace() on each token.

Copy link
Member Author

Choose a reason for hiding this comment

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

Additionally I prefer this approach because removing all of the whitespace is needed anyway. This also avoids the issue of spaces in package names.

reading `csc.Spec.Packages`

Problem:
In some cases, when the Openshift UI updates the package list of a csc
after removing a subscription, the update does not properly format the
package list. For example:

`packages: ',strimzi-kafka-operator-test'`

In this example, the package list accidentally includes a leading comma.

Right now, the marketplace doesn't handle poorly formatted package
lists in the csc spec. In general, if a user accidentally leaves a comma
behind the operator should ignore it and proceed as normal with the
list of valid packages.

Solution:
Any time the operator reads the package list, that read happens
from a public method that formats the package list and excludes invalid
packages (specifically empty string).
Added a private method `getValidPackageSliceFromString()` that takes a
package string and removes invalid elements from it.
Updated the two `GetPackages()` methods on the csc and spec types to
call this method before returning.
Added a new method on the csc type called `GetPackages()` that uses the
validation logic to reconstruct the `Spec.Packages` string.
Updated every case where we were referencing the `Spec.Packages` field
directly and replaced it with one of these methods.

Fixes https://bugzilla.redhat.com/show_bug.cgi?id=1686668
@kevinrizza
Copy link
Member Author

@tkashem @aravindhp @awgreene Please take a look when you get a chance. Thanks.

Copy link
Member

@aravindhp aravindhp left a comment

Choose a reason for hiding this comment

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

/approve

@openshift-ci-robot openshift-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Mar 20, 2019
Copy link
Member

@awgreene awgreene left a comment

Choose a reason for hiding this comment

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

/lgtm

@openshift-ci-robot openshift-ci-robot added the lgtm Indicates that a PR is ready to be merged. label Mar 20, 2019
@openshift-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: aravindhp, awgreene, kevinrizza

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

@openshift-merge-robot openshift-merge-robot merged commit 164d997 into operator-framework:master Mar 20, 2019
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. lgtm Indicates that a PR is ready to be merged. size/S Denotes a PR that changes 10-29 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants