Skip to content

Commit

Permalink
Refactor runID usage, populate runID field in metadata event (GoogleC…
Browse files Browse the repository at this point in the history
…ontainerTools#6033)

* add runID to metadata

* update tests

* make RunID available through RunContext rather than package variable
  • Loading branch information
MarlonGamez authored Jun 18, 2021
1 parent 329a9c7 commit e01d089
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 13 deletions.
6 changes: 1 addition & 5 deletions pkg/skaffold/deploy/label/labeller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,21 @@ package label
import (
"fmt"
"strings"

"github.com/google/uuid"
)

const (
K8sManagedByLabelKey = "app.kubernetes.io/managed-by"
RunIDLabel = "skaffold.dev/run-id"
)

var runID = uuid.New().String()

// DefaultLabeller adds K8s style managed-by label and a run-specific UUID label
type DefaultLabeller struct {
addSkaffoldLabels bool
customLabels []string
runID string
}

func NewLabeller(addSkaffoldLabels bool, customLabels []string) *DefaultLabeller {
func NewLabeller(addSkaffoldLabels bool, customLabels []string, runID string) *DefaultLabeller {
return &DefaultLabeller{
addSkaffoldLabels: addSkaffoldLabels,
customLabels: customLabels,
Expand Down
6 changes: 3 additions & 3 deletions pkg/skaffold/deploy/status/status_check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import (
)

func TestGetDeployments(t *testing.T) {
labeller := label.NewLabeller(true, nil)
labeller := label.NewLabeller(true, nil, "run-id")
tests := []struct {
description string
deps []*appsv1.Deployment
Expand Down Expand Up @@ -325,7 +325,7 @@ func TestGetDeployStatus(t *testing.T) {
}

func TestPrintSummaryStatus(t *testing.T) {
labeller := label.NewLabeller(true, nil)
labeller := label.NewLabeller(true, nil, "run-id")
tests := []struct {
description string
namespace string
Expand Down Expand Up @@ -402,7 +402,7 @@ func TestPrintSummaryStatus(t *testing.T) {
}

func TestPrintStatus(t *testing.T) {
labeller := label.NewLabeller(true, nil)
labeller := label.NewLabeller(true, nil, "run-id")
tests := []struct {
description string
rs []*resource.Deployment
Expand Down
1 change: 1 addition & 0 deletions pkg/skaffold/event/v2/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ type Config interface {
AutoDeploy() bool
AutoSync() bool
GetPipelines() []latestV1.Pipeline
GetRunID() string
}
2 changes: 1 addition & 1 deletion pkg/skaffold/event/v2/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func emptyState(cfg Config) proto.State {
builds[a.ImageName] = NotStarted
}
}
metadata := initializeMetadata(cfg.GetPipelines(), cfg.GetKubeContext())
metadata := initializeMetadata(cfg.GetPipelines(), cfg.GetKubeContext(), cfg.GetRunID())
return emptyStateWithArtifacts(builds, metadata, cfg.AutoBuild(), cfg.AutoDeploy(), cfg.AutoSync())
}

Expand Down
1 change: 1 addition & 0 deletions pkg/skaffold/event/v2/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ func (c config) AutoBuild() bool { return true }
func (c config) AutoDeploy() bool { return true }
func (c config) AutoSync() bool { return true }
func (c config) GetPipelines() []latestV1.Pipeline { return c.pipes }
func (c config) GetRunID() string { return "run-id" }

func mockCfg(pipes []latestV1.Pipeline, kubectx string) config {
return config{
Expand Down
3 changes: 2 additions & 1 deletion pkg/skaffold/event/v2/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ func LogMetaEvent() {
)
}

func initializeMetadata(pipelines []latestV1.Pipeline, kubeContext string) *proto.Metadata {
func initializeMetadata(pipelines []latestV1.Pipeline, kubeContext string, runID string) *proto.Metadata {
m := &proto.Metadata{
Build: &proto.BuildMetadata{},
Deploy: &proto.DeployMetadata{},
RunID: runID,
}

// TODO: Event metadata should support multiple build types.
Expand Down
10 changes: 8 additions & 2 deletions pkg/skaffold/event/v2/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ func TestEmptyState(t *testing.T) {
Deployers: []*proto.DeployMetadata_Deployer{
{Type: proto.DeployerType_HELM, Count: 2},
{Type: proto.DeployerType_KUBECTL, Count: 1},
}},
},
},
RunID: "run-id",
},
},
{
Expand Down Expand Up @@ -89,7 +91,9 @@ func TestEmptyState(t *testing.T) {
},
Deploy: &proto.DeployMetadata{
Cluster: proto.ClusterType_GKE,
Deployers: []*proto.DeployMetadata_Deployer{{Type: proto.DeployerType_KUSTOMIZE, Count: 1}}},
Deployers: []*proto.DeployMetadata_Deployer{{Type: proto.DeployerType_KUSTOMIZE, Count: 1}},
},
RunID: "run-id",
},
},
{
Expand All @@ -109,6 +113,7 @@ func TestEmptyState(t *testing.T) {
Artifacts: []*proto.BuildMetadata_Artifact{{Type: proto.BuilderType_KANIKO, Name: "artifact-1"}},
},
Deploy: &proto.DeployMetadata{},
RunID: "run-id",
},
},
{
Expand All @@ -127,6 +132,7 @@ func TestEmptyState(t *testing.T) {
Cluster: proto.ClusterType_OTHER,
Deployers: []*proto.DeployMetadata_Deployer{{Type: proto.DeployerType_KUSTOMIZE, Count: 1}},
},
RunID: "run-id",
},
},
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/skaffold/runner/runcontext/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"os"

"github.com/google/uuid"
"github.com/sirupsen/logrus"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
Expand All @@ -41,6 +42,7 @@ type RunContext struct {
WorkingDir string
InsecureRegistries map[string]bool
Cluster config.Cluster
RunID string
}

// Pipelines encapsulates multiple config pipelines
Expand Down Expand Up @@ -229,6 +231,7 @@ func (rc *RunContext) WaitForDeletions() config.WaitForDeletions { return rc.Opt
func (rc *RunContext) WatchPollInterval() int { return rc.Opts.WatchPollInterval }
func (rc *RunContext) BuildConcurrency() int { return rc.Opts.BuildConcurrency }
func (rc *RunContext) IsMultiConfig() bool { return rc.Pipelines.IsMultiPipeline() }
func (rc *RunContext) GetRunID() string { return rc.RunID }

func GetRunContext(opts config.SkaffoldOptions, configs []*latestV1.SkaffoldConfig) (*RunContext, error) {
var pipelines []latestV1.Pipeline
Expand Down Expand Up @@ -280,6 +283,8 @@ func GetRunContext(opts config.SkaffoldOptions, configs []*latestV1.SkaffoldConf
return nil, fmt.Errorf("getting cluster: %w", err)
}

runID := uuid.New().String()

return &RunContext{
Opts: opts,
Pipelines: ps,
Expand All @@ -288,6 +293,7 @@ func GetRunContext(opts config.SkaffoldOptions, configs []*latestV1.SkaffoldConf
Namespaces: namespaces,
InsecureRegistries: insecureRegistries,
Cluster: cluster,
RunID: runID,
}, nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/skaffold/runner/v1/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func NewForConfig(runCtx *runcontext.RunContext) (*SkaffoldRunner, error) {
isLocalImage := func(imageName string) (bool, error) {
return isImageLocal(runCtx, imageName)
}
labeller := label.NewLabeller(runCtx.AddSkaffoldLabels(), runCtx.CustomLabels())
labeller := label.NewLabeller(runCtx.AddSkaffoldLabels(), runCtx.CustomLabels(), runCtx.GetRunID())
tester, err := getTester(runCtx, isLocalImage)
if err != nil {
endTrace(instrumentation.TraceEndError(err))
Expand Down
1 change: 1 addition & 0 deletions testutil/event/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ func (c config) AutoDeploy() bool { return true }
func (c config) AutoSync() bool { return true }
func (c config) GetPipelines() []latestV1.Pipeline { return c.pipes }
func (c config) GetKubeContext() string { return "temp" }
func (c config) GetRunID() string { return "run-id" }

0 comments on commit e01d089

Please sign in to comment.