Skip to content

Commit

Permalink
fix: Ignore extra labels in CodebaseImageStream for auto-deploy (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
zmotso authored and MykolaMarusenko committed Nov 5, 2024
1 parent ceb90f7 commit 117bba5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
16 changes: 9 additions & 7 deletions controllers/codebaseimagestream/chain/put_cd_stage_deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,17 @@ func (h PutCDStageDeploy) handleCodebaseImageStreamEnvLabels(ctx context.Context
return nil
}

if errs := validateCbis(imageStream); len(errs) != 0 {
return errors.New(strings.Join(errs, "; "))
}

labelValueRegexp := regexp.MustCompile("^[-A-Za-z0-9_.]+/[-A-Za-z0-9_.]+$")

for envLabel := range imageStream.ObjectMeta.Labels {
if errs := validateCbis(imageStream, envLabel, labelValueRegexp); len(errs) != 0 {
return errors.New(strings.Join(errs, "; "))
if !labelValueRegexp.MatchString(envLabel) {
l.Info("Label value does not match the pattern cd-pipeline-name/stage-name. Skip CDStageDeploy creating.")

continue
}

if err := h.putCDStageDeploy(ctx, envLabel, imageStream.Namespace, imageStream.Spec); err != nil {
Expand All @@ -69,7 +75,7 @@ func (h PutCDStageDeploy) handleCodebaseImageStreamEnvLabels(ctx context.Context
return nil
}

func validateCbis(imageStream *codebaseApi.CodebaseImageStream, envLabel string, labelValueRegexp *regexp.Regexp) []string {
func validateCbis(imageStream *codebaseApi.CodebaseImageStream) []string {
var errs []string

if imageStream.Spec.Codebase == "" {
Expand All @@ -80,10 +86,6 @@ func validateCbis(imageStream *codebaseApi.CodebaseImageStream, envLabel string,
errs = append(errs, "tags are not defined in spec ")
}

if !labelValueRegexp.MatchString(envLabel) {
errs = append(errs, "label must be in format cd-pipeline-name/stage-name")
}

return errs
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ func TestPutCDStageDeploy_ServeRequest(t *testing.T) {
Spec: codebaseApi.CodebaseImageStreamSpec{
Codebase: "app",
ImageName: "latest",
Tags: []codebaseApi.Tag{{Name: "latest", Created: time.Now().Format(time.RFC3339)}},
},
},
client: func(t *testing.T) client.Client {
Expand All @@ -363,11 +364,8 @@ func TestPutCDStageDeploy_ServeRequest(t *testing.T) {
WithObjects().
Build()
},
wantErr: func(t require.TestingT, err error, i ...interface{}) {
require.Error(t, err)
require.Contains(t, err.Error(), "label must be in format cd-pipeline-name/stage-name")
},
want: func(t *testing.T, k8scl client.Client) {},
wantErr: require.NoError,
want: func(t *testing.T, k8scl client.Client) {},
},
{
name: "failed to create CDStageDeploy - no tags",
Expand Down

0 comments on commit 117bba5

Please sign in to comment.