From 117bba5cf10cabff94e5c6fc873eb863400a0a50 Mon Sep 17 00:00:00 2001 From: Zorian Motso Date: Thu, 24 Oct 2024 15:45:55 +0300 Subject: [PATCH] fix: Ignore extra labels in CodebaseImageStream for auto-deploy (#153) --- .../chain/put_cd_stage_deploy.go | 16 +++++++++------- .../chain/put_cd_stage_deploy_test.go | 8 +++----- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/controllers/codebaseimagestream/chain/put_cd_stage_deploy.go b/controllers/codebaseimagestream/chain/put_cd_stage_deploy.go index 980f74f..2549d5a 100644 --- a/controllers/codebaseimagestream/chain/put_cd_stage_deploy.go +++ b/controllers/codebaseimagestream/chain/put_cd_stage_deploy.go @@ -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 { @@ -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 == "" { @@ -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 } diff --git a/controllers/codebaseimagestream/chain/put_cd_stage_deploy_test.go b/controllers/codebaseimagestream/chain/put_cd_stage_deploy_test.go index a153758..1d1edb3 100644 --- a/controllers/codebaseimagestream/chain/put_cd_stage_deploy_test.go +++ b/controllers/codebaseimagestream/chain/put_cd_stage_deploy_test.go @@ -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 { @@ -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",