diff --git a/pkg/app/pipedv1/controller/planner.go b/pkg/app/pipedv1/controller/planner.go index 70ef0e20db..14f4fc0645 100644 --- a/pkg/app/pipedv1/controller/planner.go +++ b/pkg/app/pipedv1/controller/planner.go @@ -105,7 +105,7 @@ func newPlanner( zap.String("deployment-id", d.Id), zap.String("app-id", d.ApplicationId), zap.String("project-id", d.ProjectId), - zap.String("app-kind", d.Kind.String()), + zap.String("labels", d.GetLabelsString()), zap.String("working-dir", workingDir), ) @@ -173,8 +173,8 @@ func (p *planner) Run(ctx context.Context) error { "Plan", trace.WithAttributes( attribute.String("application-id", p.deployment.ApplicationId), - attribute.String("kind", p.deployment.Kind.String()), attribute.String("deployment-id", p.deployment.Id), + attribute.String("labels", p.deployment.GetLabelsString()), )) defer span.End() diff --git a/pkg/app/pipedv1/controller/scheduler.go b/pkg/app/pipedv1/controller/scheduler.go index d8b5384081..9ec3a74de6 100644 --- a/pkg/app/pipedv1/controller/scheduler.go +++ b/pkg/app/pipedv1/controller/scheduler.go @@ -91,7 +91,7 @@ func newScheduler( zap.String("deployment-id", d.Id), zap.String("app-id", d.ApplicationId), zap.String("project-id", d.ProjectId), - zap.String("app-kind", d.Kind.String()), + zap.String("labels", d.GetLabelsString()), zap.String("working-dir", workingDir), ) @@ -265,8 +265,8 @@ func (s *scheduler) Run(ctx context.Context) error { "Deploy", trace.WithAttributes( attribute.String("application-id", s.deployment.ApplicationId), - attribute.String("kind", s.deployment.Kind.String()), attribute.String("deployment-id", s.deployment.Id), + attribute.String("labels", s.deployment.GetLabelsString()), )) defer span.End() @@ -325,8 +325,8 @@ func (s *scheduler) Run(ctx context.Context) error { go func() { _, span := s.tracer.Start(ctx, ps.Name, trace.WithAttributes( attribute.String("application-id", s.deployment.ApplicationId), - attribute.String("kind", s.deployment.Kind.String()), attribute.String("deployment-id", s.deployment.Id), + attribute.String("labels", s.deployment.GetLabelsString()), attribute.String("stage-id", ps.Id), )) defer span.End() @@ -435,8 +435,8 @@ func (s *scheduler) Run(ctx context.Context) error { _, span := s.tracer.Start(ctx, rbs.Name, trace.WithAttributes( attribute.String("application-id", s.deployment.ApplicationId), - attribute.String("kind", s.deployment.Kind.String()), attribute.String("deployment-id", s.deployment.Id), + attribute.String("labels", s.deployment.GetLabelsString()), attribute.String("stage-id", rbs.Id), )) defer span.End() diff --git a/pkg/app/pipedv1/notifier/slack.go b/pkg/app/pipedv1/notifier/slack.go index bc04cdb56d..40eabc88c2 100644 --- a/pkg/app/pipedv1/notifier/slack.go +++ b/pkg/app/pipedv1/notifier/slack.go @@ -212,7 +212,7 @@ func (s *slack) buildSlackMessage(event model.NotificationEvent, webURL string) fields = []slackField{ {"Project", truncateText(d.ProjectId, 8), true}, {"Application", makeSlackLink(d.ApplicationName, fmt.Sprintf("%s/applications/%s?project=%s", webURL, d.ApplicationId, d.ProjectId)), true}, - {"Kind", strings.ToLower(d.Kind.String()), true}, + {"Labels", d.GetLabelsString(), true}, {"Deployment", makeSlackLink(truncateText(d.Id, 8), link), true}, {"Triggered By", d.TriggeredBy(), true}, {"Mention To Users", accountsStr, true}, @@ -262,7 +262,7 @@ func (s *slack) buildSlackMessage(event model.NotificationEvent, webURL string) fields = []slackField{ {"Project", truncateText(d.ProjectId, 8), true}, {"Application", makeSlackLink(d.ApplicationName, fmt.Sprintf("%s/applications/%s?project=%s", webURL, d.ApplicationId, d.ProjectId)), true}, - {"Kind", strings.ToLower(d.Kind.String()), true}, + {"Labels", d.GetLabelsString(), true}, {"Deployment", makeSlackLink(truncateText(d.Id, 8), link), true}, {"Stage", s.Name, true}, {"Triggered By", d.TriggeredBy(), true}, diff --git a/pkg/app/pipedv1/trigger/deployment.go b/pkg/app/pipedv1/trigger/deployment.go index ac464c4c3c..cfe68609da 100644 --- a/pkg/app/pipedv1/trigger/deployment.go +++ b/pkg/app/pipedv1/trigger/deployment.go @@ -77,7 +77,6 @@ func buildDeployment( ApplicationName: app.Name, PipedId: app.PipedId, ProjectId: app.ProjectId, - Kind: app.Kind, Trigger: &model.DeploymentTrigger{ Commit: &model.Commit{ Hash: commit.Hash, diff --git a/pkg/model/deployment.go b/pkg/model/deployment.go index d3f2b7a973..6ca077dd38 100644 --- a/pkg/model/deployment.go +++ b/pkg/model/deployment.go @@ -16,6 +16,7 @@ package model import ( "fmt" + "strings" "google.golang.org/protobuf/proto" ) @@ -92,6 +93,28 @@ func CanUpdateStageStatus(cur, next StageStatus) bool { return false } +// DeploymentStatusesFromStrings converts a list of strings to list of DeploymentStatus. +func DeploymentStatusesFromStrings(statuses []string) ([]DeploymentStatus, error) { + out := make([]DeploymentStatus, 0, len(statuses)) + for _, s := range statuses { + status, ok := DeploymentStatus_value[s] + if !ok { + return nil, fmt.Errorf("invalid status %s", s) + } + out = append(out, DeploymentStatus(status)) + } + return out, nil +} + +// DeploymentStatusStrings returns a list of available deployment statuses in string. +func DeploymentStatusStrings() []string { + out := make([]string, 0, len(DeploymentStatus_value)) + for s := range DeploymentStatus_value { + out = append(out, s) + } + return out +} + // StageMap returns the map of id and the stage. func (d *Deployment) StageMap() map[string]*PipelineStage { stage := make(map[string]*PipelineStage, len(d.Stages)) @@ -111,11 +134,6 @@ func (d *Deployment) Stage(id string) (*PipelineStage, bool) { return nil, false } -// IsSkippable checks whether skippable or not. -func (p *PipelineStage) IsSkippable() bool { - return p.Name == StageAnalysis.String() -} - // CommitHash returns the hash value of trigger commit. func (d *Deployment) CommitHash() string { return d.Trigger.Commit.Hash @@ -171,28 +189,6 @@ func (d *Deployment) FindRollbackStages() ([]*PipelineStage, bool) { return rollbackStages, len(rollbackStages) > 0 } -// DeploymentStatusesFromStrings converts a list of strings to list of DeploymentStatus. -func DeploymentStatusesFromStrings(statuses []string) ([]DeploymentStatus, error) { - out := make([]DeploymentStatus, 0, len(statuses)) - for _, s := range statuses { - status, ok := DeploymentStatus_value[s] - if !ok { - return nil, fmt.Errorf("invalid status %s", s) - } - out = append(out, DeploymentStatus(status)) - } - return out, nil -} - -// DeploymentStatusStrings returns a list of available deployment statuses in string. -func DeploymentStatusStrings() []string { - out := make([]string, 0, len(DeploymentStatus_value)) - for s := range DeploymentStatus_value { - out = append(out, s) - } - return out -} - // ContainLabels checks if it has all the given labels. func (d *Deployment) ContainLabels(labels map[string]string) bool { if len(d.Labels) < len(labels) { @@ -230,6 +226,19 @@ func (d *Deployment) GetDeployTargets(pluginName string) []string { return dps.GetDeployTargets() } +func (d *Deployment) GetLabelsString() string { + labels := make([]string, 0, len(d.Labels)) + for k, v := range d.Labels { + labels = append(labels, fmt.Sprintf("%s=%s", k, v)) + } + return strings.Join(labels, ",") +} + +// IsSkippable checks whether skippable or not. +func (p *PipelineStage) IsSkippable() bool { + return p.Name == StageAnalysis.String() +} + // Implement sort.Interface for PipelineStages. type PipelineStages []*PipelineStage diff --git a/pkg/model/deployment_test.go b/pkg/model/deployment_test.go index 008b4ca9cd..e39f0d5d22 100644 --- a/pkg/model/deployment_test.go +++ b/pkg/model/deployment_test.go @@ -16,6 +16,7 @@ package model import ( "sort" + "strings" "testing" "time" @@ -725,3 +726,58 @@ func TestDeployment_GetDeployTargets(t *testing.T) { }) } } + +func TestDeployment_GetLabelsString(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + deployment *Deployment + expected string + }{ + { + name: "nil labels", + deployment: &Deployment{Labels: nil}, + expected: "", + }, + { + name: "empty labels", + deployment: &Deployment{Labels: map[string]string{}}, + expected: "", + }, + { + name: "single label", + deployment: &Deployment{ + Labels: map[string]string{ + "env": "prod", + }, + }, + expected: "env=prod", + }, + { + name: "multiple labels", + deployment: &Deployment{ + Labels: map[string]string{ + "env": "prod", + "version": "v1.0.0", + "region": "us-west-1", + }, + }, + expected: "env=prod,version=v1.0.0,region=us-west-1", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + result := tt.deployment.GetLabelsString() + assert.Equal(t, len(tt.expected), len(result)) + expectedParts := strings.Split(tt.expected, ",") + resultParts := strings.Split(result, ",") + sort.Strings(expectedParts) + sort.Strings(resultParts) + assert.Equal(t, expectedParts, resultParts) + }) + } +}