Skip to content

Commit

Permalink
fix: Failed auto-deploy for the release branch (#52)
Browse files Browse the repository at this point in the history
Change-Id: I0dfed7af590c21022268c08fa064cfa937dfc063
  • Loading branch information
zmotso committed Apr 1, 2024
1 parent a70f283 commit 19ef8b0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 81 deletions.
43 changes: 11 additions & 32 deletions controllers/cdstagedeploy/chain/process_trigger_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/json"
"errors"
"fmt"
"strings"

tektonTriggersApi "github.com/tektoncd/triggers/pkg/apis/triggers/v1beta1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand All @@ -16,6 +15,7 @@ import (
pipelineAPi "github.com/epam/edp-cd-pipeline-operator/v2/api/v1"

codebaseApi "github.com/epam/edp-codebase-operator/v2/api/v1"
"github.com/epam/edp-codebase-operator/v2/controllers/codebasebranch/chain/put_codebase_image_stream"
"github.com/epam/edp-codebase-operator/v2/pkg/codebaseimagestream"
)

Expand Down Expand Up @@ -97,31 +97,27 @@ func (h *ProcessTriggerTemplate) ServeRequest(ctx context.Context, stageDeploy *
func (h *ProcessTriggerTemplate) getAppPayload(ctx context.Context, pipeline *pipelineAPi.CDPipeline) (json.RawMessage, error) {
log := ctrl.LoggerFrom(ctx)

imageStreams, err := getCodebaseImageStreamMap(pipeline)
if err != nil {
return nil, err
}
appPayload := make(map[string]TriggerTemplateApplicationPayload, len(pipeline.Spec.InputDockerStreams))

appPayload := make(map[string]TriggerTemplateApplicationPayload, len(imageStreams))
for _, stream := range pipeline.Spec.InputDockerStreams {
imageStreamName := put_codebase_image_stream.ProcessNameToK8sConvention(stream)

for codebase, stream := range imageStreams {
imageStream := &codebaseApi.CodebaseImageStream{}
if err = h.k8sClient.Get(ctx, client.ObjectKey{
if err := h.k8sClient.Get(ctx, client.ObjectKey{
Namespace: pipeline.Namespace,
Name: stream,
Name: put_codebase_image_stream.ProcessNameToK8sConvention(imageStreamName),
}, imageStream); err != nil {
return nil, fmt.Errorf("failed to get %s CodebaseImageStream: %w", stream, err)
return nil, fmt.Errorf("failed to get %s CodebaseImageStream: %w", imageStreamName, err)
}

var tag codebaseApi.Tag

if tag, err = codebaseimagestream.GetLastTag(imageStream.Spec.Tags, log); err != nil {
log.Info("Codebase doesn't have tags in the CodebaseImageStream. Skip auto-deploy.", "codebase", codebase, "imagestream", stream)
tag, err := codebaseimagestream.GetLastTag(imageStream.Spec.Tags, log)
if err != nil {
log.Info("Codebase doesn't have tags in the CodebaseImageStream. Skip auto-deploy.", "codebase", imageStream.Spec.Codebase, "imagestream", imageStreamName)

return nil, errLasTagNotFound
}

appPayload[codebase] = TriggerTemplateApplicationPayload{
appPayload[imageStream.Spec.Codebase] = TriggerTemplateApplicationPayload{
ImageTag: tag.Name,
}
}
Expand Down Expand Up @@ -180,20 +176,3 @@ func (h *ProcessTriggerTemplate) createTriggerTemplateResource(

return nil
}

// getCodebaseImageStreamMap returns map of codebase name and image stream name.
func getCodebaseImageStreamMap(pipeline *pipelineAPi.CDPipeline) (map[string]string, error) {
m := make(map[string]string, len(pipeline.Spec.InputDockerStreams))

for _, stream := range pipeline.Spec.InputDockerStreams {
i := strings.LastIndex(stream, "-")

if i == -1 {
return nil, fmt.Errorf("invalid image stream name %s", stream)
}

m[stream[:i]] = stream
}

return m, nil
}
47 changes: 0 additions & 47 deletions controllers/cdstagedeploy/chain/process_trigger_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,50 +449,3 @@ func TestProcessTriggerTemplate_ServeRequest(t *testing.T) {
})
}
}

func Test_getCodebaseImageStreamMap(t *testing.T) {
t.Parallel()

tests := []struct {
name string
pipeline *pipelineAPi.CDPipeline
want map[string]string
wantErr assert.ErrorAssertionFunc
}{
{
name: "should return map of codebase name and image stream name",
pipeline: &pipelineAPi.CDPipeline{
Spec: pipelineAPi.CDPipelineSpec{
InputDockerStreams: []string{"app1-main", "app-2-master"},
},
},
want: map[string]string{
"app1": "app1-main",
"app-2": "app-2-master",
},
wantErr: assert.NoError,
},
{
name: "should return error if input docker streams are invalid",
pipeline: &pipelineAPi.CDPipeline{
Spec: pipelineAPi.CDPipelineSpec{
InputDockerStreams: []string{"app1main"},
},
},
want: nil,
wantErr: assert.Error,
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

got, err := getCodebaseImageStreamMap(tt.pipeline)

assert.Equal(t, tt.want, got)
tt.wantErr(t, err)
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (h PutCodebaseImageStream) ServeRequest(ctx context.Context, cb *codebaseAp
return err
}

cisName := fmt.Sprintf("%v-%v", c.Name, processNameToK8sConvention(cb.Spec.BranchName))
cisName := fmt.Sprintf("%v-%v", c.Name, ProcessNameToK8sConvention(cb.Spec.BranchName))
imageName := fmt.Sprintf("%v/%v", registryUrl, cb.Spec.CodebaseName)

if err = h.createCodebaseImageStreamIfNotExists(
Expand All @@ -80,7 +80,7 @@ func (h PutCodebaseImageStream) ServeRequest(ctx context.Context, cb *codebaseAp
return nil
}

func processNameToK8sConvention(name string) string {
func ProcessNameToK8sConvention(name string) string {
r := strings.NewReplacer("/", "-", ".", "-")
return r.Replace(name)
}
Expand Down

0 comments on commit 19ef8b0

Please sign in to comment.