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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 21 additions & 2 deletions pkg/apis/marketplace/v1alpha1/catalogsourceconfig_types.go
Expand Up @@ -100,9 +100,14 @@ func (csc *CatalogSourceConfig) EnsurePublisher() {
}
}

func (csc *CatalogSourceConfig) GetPackages() string {
pkgs := getValidPackageSliceFromString(csc.Spec.Packages)
return strings.Join(pkgs, ",")
}

// GetPackageIDs returns the list of package(s) specified.
func (csc *CatalogSourceConfig) GetPackageIDs() []string {
return strings.Split(strings.Replace(csc.Spec.Packages, " ", "", -1), ",")
return getValidPackageSliceFromString(csc.Spec.Packages)
}

// GetTargetNamespace returns the TargetNamespace where the OLM resources will
Expand All @@ -129,5 +134,19 @@ func (csc *CatalogSourceConfig) RemoveOwner(ownerUID types.UID) {

// GetPackageIDs returns the list of package(s) specified.
func (spec *CatalogSourceConfigSpec) GetPackageIDs() []string {
return strings.Split(strings.Replace(spec.Packages, " ", "", -1), ",")
return getValidPackageSliceFromString(spec.Packages)
}

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.


for _, pkg := range pkgSlice {
aravindhp marked this conversation as resolved.
Show resolved Hide resolved
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.

if pkg != "" {
pkgIds = append(pkgIds, pkg)
}
}

return pkgIds
}
2 changes: 1 addition & 1 deletion pkg/catalogsourceconfig/cache.go
Expand Up @@ -89,7 +89,7 @@ func (c *cache) Evict(csc *v1alpha1.CatalogSourceConfig) {

func (c *cache) Set(csc *v1alpha1.CatalogSourceConfig) {
c.entries[csc.ObjectMeta.UID] = &v1alpha1.CatalogSourceConfigSpec{
Packages: csc.Spec.Packages,
Packages: csc.GetPackages(),
aravindhp marked this conversation as resolved.
Show resolved Hide resolved
TargetNamespace: csc.Spec.TargetNamespace,
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/catalogsourceconfig/registry.go
Expand Up @@ -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.

deployment := new(DeploymentBuilder).WithTypeMeta().Deployment()
if err := r.client.Get(context.TODO(), r.csc.key(), deployment); err != nil {
deployment = r.newDeployment(registryCommand)
Expand Down Expand Up @@ -402,7 +402,7 @@ func (r *registry) waitForDeploymentScaleDown(retryInterval, timeout time.Durati

// getCommand returns the command used to launch the registry server
func getCommand(packages string, sources string) []string {
return []string{"appregistry-server", "-s", sources, "-o", strings.Replace(packages, " ", "", -1)}
return []string{"appregistry-server", "-s", sources, "-o", packages}
}

// getRules returns the PolicyRule needed to access the given operatorSources and secrets
Expand Down