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

fix(validation): Remove default channel validation from bundle lib #408

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 2 additions & 38 deletions pkg/lib/bundle/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,32 +293,6 @@ func ValidateAnnotations(existing, expected []byte) error {
return utilerrors.NewAggregate(errs)
}

// ValidateChannelDefault validates provided default channel to ensure it exists in
// provided channel list.
func ValidateChannelDefault(channels, channelDefault string) (string, error) {
var chanDefault string
var chanErr error
channelList := strings.Split(channels, ",")

if containsString(channelList, "") {
return chanDefault, fmt.Errorf("invalid channels are provided: %s", channels)
}

if channelDefault != "" {
for _, channel := range channelList {
if channel == channelDefault {
chanDefault = channelDefault
break
}
}
if chanDefault == "" {
chanDefault = channelList[0]
chanErr = fmt.Errorf(`The channel list "%s" doesn't contain channelDefault "%s"`, channels, channelDefault)
}
}
return chanDefault, chanErr
}

// GenerateAnnotations builds annotations.yaml with mediatype, manifests &
// metadata directories in bundle image, package name, channels and default
// channels information.
Expand All @@ -334,12 +308,7 @@ func GenerateAnnotations(mediaType, manifests, metadata, packageName, channels,
},
}

chanDefault, err := ValidateChannelDefault(channels, channelDefault)
if err != nil {
return nil, err
}

annotations.Annotations[ChannelDefaultLabel] = chanDefault
annotations.Annotations[ChannelDefaultLabel] = channelDefault

afile, err := yaml.Marshal(annotations)
if err != nil {
Expand All @@ -355,11 +324,6 @@ func GenerateAnnotations(mediaType, manifests, metadata, packageName, channels,
func GenerateDockerfile(mediaType, manifests, metadata, copyManifestDir, copyMetadataDir, workingDir, packageName, channels, channelDefault string) ([]byte, error) {
var fileContent string

chanDefault, err := ValidateChannelDefault(channels, channelDefault)
Copy link
Member

Choose a reason for hiding this comment

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

If channelDefault is not an empty string, we still want to verify that it exists in channels.

Copy link
Member Author

Choose a reason for hiding this comment

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

There are cases when that is totally fine to have default channel not on the list. @shawn-hurley Can you explain this further?

Copy link
Member

Choose a reason for hiding this comment

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

Default channel mentioned in the bundle != Default channel on the package

if err != nil {
return nil, err
}

relativeManifestDirectory, err := filepath.Rel(workingDir, copyManifestDir)
if err != nil {
return nil, err
Expand All @@ -379,7 +343,7 @@ func GenerateDockerfile(mediaType, manifests, metadata, copyManifestDir, copyMet
fileContent += fmt.Sprintf("LABEL %s=%s\n", MetadataLabel, metadata)
fileContent += fmt.Sprintf("LABEL %s=%s\n", PackageLabel, packageName)
fileContent += fmt.Sprintf("LABEL %s=%s\n", ChannelsLabel, channels)
fileContent += fmt.Sprintf("LABEL %s=%s\n\n", ChannelDefaultLabel, chanDefault)
Copy link
Member

Choose a reason for hiding this comment

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

If channelDefault is an empty string, this label will be empty. Is that ok/desirable?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think it is OK to not have a defaultChannel value AKA empty string because in annotation generation function, there is a case where inferred defaultChannel is set to empty string. @Bowenislandsong Can you double-confirm this?

Copy link
Member

Choose a reason for hiding this comment

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

Wouldn't it be better to just not set this label at all?

Copy link
Member

Choose a reason for hiding this comment

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

yes. Later when added to Index, the default behavior will kick in (right now the default behavior is to add the first channel as default).

Copy link
Member

@estroz estroz Jul 29, 2020

Choose a reason for hiding this comment

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

In that case, wouldn't it be better/more obvious to the user to write a label containing the first channel as default if a default isn't explicitly set? https://github.com/operator-framework/operator-registry/pull/408/files#r462470459 implies that this isn't desirable.

fileContent += fmt.Sprintf("LABEL %s=%s\n\n", ChannelDefaultLabel, channelDefault)

// CONTENT
fileContent += fmt.Sprintf("COPY %s %s\n", relativeManifestDirectory, "/manifests/")
Expand Down
37 changes: 0 additions & 37 deletions pkg/lib/bundle/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,43 +49,6 @@ func TestGetMediaType(t *testing.T) {
}
}

func TestValidateChannelDefault(t *testing.T) {
tests := []struct {
channels string
channelDefault string
result string
errorMsg string
}{
{
"test5,test6",
"",
"",
"",
},
{
"test5,test6",
"test7",
"test5",
`The channel list "test5,test6" doesn't contain channelDefault "test7"`,
},
{
",",
"",
"",
`invalid channels are provided: ,`,
},
}

for _, item := range tests {
output, err := ValidateChannelDefault(item.channels, item.channelDefault)
if item.errorMsg == "" {
require.Equal(t, item.result, output)
} else {
require.Equal(t, item.errorMsg, err.Error())
}
}
}

func TestValidateAnnotations(t *testing.T) {
tests := []struct {
existing []byte
Expand Down
8 changes: 3 additions & 5 deletions pkg/lib/bundle/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,20 +201,18 @@ func validateAnnotations(mediaType string, fileAnnotations *AnnotationMetadata)
aErr := fmt.Errorf("Expecting annotation %q to have value %q instead of %q", label, MetadataDir, val)
validationErrors = append(validationErrors, aErr)
}
case ChannelsLabel, ChannelDefaultLabel:
case ChannelsLabel:
if val == "" {
aErr := fmt.Errorf("Expecting annotation %q to have non-empty value", label)
validationErrors = append(validationErrors, aErr)
} else {
annotations[label] = val
}
case ChannelDefaultLabel:
annotations[label] = val
}
}

_, err := ValidateChannelDefault(annotations[ChannelsLabel], annotations[ChannelDefaultLabel])
if err != nil {
validationErrors = append(validationErrors, err)
}
return validationErrors
}

Expand Down