Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update metadata event emission to happen every devloop and update build metadata #5918

Merged
merged 4 commits into from
Jun 1, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 34 additions & 34 deletions pkg/skaffold/event/v2/util.go → pkg/skaffold/event/v2/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,31 @@ limitations under the License.
package v2

import (
"fmt"
"strings"

latestV1 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest/v1"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/version"
proto "github.com/GoogleContainerTools/skaffold/proto/v2"
)

func LogMetaEvent() {
metadata := handler.state.Metadata
handler.handle(
&proto.Event{
EventType: &proto.Event_MetaEvent{
MetaEvent: &proto.MetaEvent{
Entry: fmt.Sprintf("Starting Skaffold: %+v", version.Get()),
Metadata: metadata,
},
},
},
)
}

func initializeMetadata(pipelines []latestV1.Pipeline, kubeContext string) *proto.Metadata {
artifactCount := 0
for _, p := range pipelines {
artifactCount += len(p.Build.Artifacts)
}
m := &proto.Metadata{
Build: &proto.BuildMetadata{
NumberOfArtifacts: int32(artifactCount),
},
Build: &proto.BuildMetadata{},
Deploy: &proto.DeployMetadata{},
}

Expand All @@ -48,13 +58,13 @@ func initializeMetadata(pipelines []latestV1.Pipeline, kubeContext string) *prot
m.Build.Type = proto.BuildType_UNKNOWN_BUILD_TYPE
}

var builders []*proto.BuildMetadata_ImageBuilder
var artifacts []*proto.BuildMetadata_Artifact
var deployers []*proto.DeployMetadata_Deployer
for _, p := range pipelines {
builders = append(builders, getBuilders(p.Build)...)
artifacts = append(artifacts, getArtifacts(p.Build)...)
deployers = append(deployers, getDeploy(p.Deploy)...)
}
m.Build.Builders = builders
m.Build.Artifacts = artifacts

if len(deployers) == 0 {
m.Deploy = &proto.DeployMetadata{}
Expand All @@ -67,33 +77,31 @@ func initializeMetadata(pipelines []latestV1.Pipeline, kubeContext string) *prot
return m
}

func getBuilders(b latestV1.BuildConfig) []*proto.BuildMetadata_ImageBuilder {
m := map[proto.BuilderType]int{}
func getArtifacts(b latestV1.BuildConfig) []*proto.BuildMetadata_Artifact {
result := []*proto.BuildMetadata_Artifact{}
for _, a := range b.Artifacts {
artifact := &proto.BuildMetadata_Artifact{
Name: a.ImageName,
}
switch {
case a.BazelArtifact != nil:
updateOrAddKey(m, proto.BuilderType_BAZEL)
artifact.Type = proto.BuilderType_BAZEL
case a.BuildpackArtifact != nil:
updateOrAddKey(m, proto.BuilderType_BUILDPACKS)
artifact.Type = proto.BuilderType_BUILDPACKS
case a.CustomArtifact != nil:
updateOrAddKey(m, proto.BuilderType_CUSTOM)
artifact.Type = proto.BuilderType_CUSTOM
case a.DockerArtifact != nil:
updateOrAddKey(m, proto.BuilderType_DOCKER)
artifact.Type = proto.BuilderType_DOCKER
case a.JibArtifact != nil:
updateOrAddKey(m, proto.BuilderType_JIB)
artifact.Type = proto.BuilderType_JIB
case a.KanikoArtifact != nil:
updateOrAddKey(m, proto.BuilderType_KANIKO)
artifact.Type = proto.BuilderType_KANIKO
default:
updateOrAddKey(m, proto.BuilderType_UNKNOWN_BUILDER_TYPE)
artifact.Type = proto.BuilderType_UNKNOWN_BUILDER_TYPE
}
result = append(result, artifact)
}
builders := make([]*proto.BuildMetadata_ImageBuilder, len(m))
i := 0
for k, v := range m {
builders[i] = &proto.BuildMetadata_ImageBuilder{Type: k, Count: int32(v)}
i++
}
return builders
return result
}

func getDeploy(d latestV1.DeployConfig) []*proto.DeployMetadata_Deployer {
Expand All @@ -111,14 +119,6 @@ func getDeploy(d latestV1.DeployConfig) []*proto.DeployMetadata_Deployer {
return deployers
}

func updateOrAddKey(m map[proto.BuilderType]int, k proto.BuilderType) {
if _, ok := m[k]; ok {
m[k]++
return
}
m[k] = 1
}

func getClusterType(c string) proto.ClusterType {
switch {
case strings.Contains(c, "minikube"):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestEmptyState(t *testing.T) {
cfg: latestV1.Pipeline{
Build: latestV1.BuildConfig{
BuildType: latestV1.BuildType{LocalBuild: &latestV1.LocalBuild{}},
Artifacts: []*latestV1.Artifact{{ImageName: "img", ArtifactType: latestV1.ArtifactType{DockerArtifact: &latestV1.DockerArtifact{}}}},
Artifacts: []*latestV1.Artifact{{ImageName: "docker-artifact-1", ArtifactType: latestV1.ArtifactType{DockerArtifact: &latestV1.DockerArtifact{}}}},
},
Deploy: latestV1.DeployConfig{
DeployType: latestV1.DeployType{
Expand All @@ -49,9 +49,8 @@ func TestEmptyState(t *testing.T) {
cluster: "minikube",
expected: &proto.Metadata{
Build: &proto.BuildMetadata{
NumberOfArtifacts: 1,
Type: proto.BuildType_LOCAL,
Builders: []*proto.BuildMetadata_ImageBuilder{{Type: proto.BuilderType_DOCKER, Count: 1}},
Type: proto.BuildType_LOCAL,
Artifacts: []*proto.BuildMetadata_Artifact{{Type: proto.BuilderType_DOCKER, Name: "docker-artifact-1"}},
},
Deploy: &proto.DeployMetadata{
Cluster: proto.ClusterType_MINIKUBE,
Expand All @@ -67,9 +66,9 @@ func TestEmptyState(t *testing.T) {
Build: latestV1.BuildConfig{
BuildType: latestV1.BuildType{Cluster: &latestV1.ClusterDetails{}},
Artifacts: []*latestV1.Artifact{
{ImageName: "img1", ArtifactType: latestV1.ArtifactType{DockerArtifact: &latestV1.DockerArtifact{}}},
{ImageName: "img2", ArtifactType: latestV1.ArtifactType{DockerArtifact: &latestV1.DockerArtifact{}}},
{ImageName: "img3", ArtifactType: latestV1.ArtifactType{JibArtifact: &latestV1.JibArtifact{}}},
{ImageName: "docker-artifact-1", ArtifactType: latestV1.ArtifactType{DockerArtifact: &latestV1.DockerArtifact{}}},
{ImageName: "docker-artifact-2", ArtifactType: latestV1.ArtifactType{DockerArtifact: &latestV1.DockerArtifact{}}},
{ImageName: "jib-artifact-1", ArtifactType: latestV1.ArtifactType{JibArtifact: &latestV1.JibArtifact{}}},
},
},
Deploy: latestV1.DeployConfig{
Expand All @@ -81,11 +80,11 @@ func TestEmptyState(t *testing.T) {
cluster: "gke-tejal-test",
expected: &proto.Metadata{
Build: &proto.BuildMetadata{
NumberOfArtifacts: 3,
Type: proto.BuildType_CLUSTER,
Builders: []*proto.BuildMetadata_ImageBuilder{
{Type: proto.BuilderType_JIB, Count: 1},
{Type: proto.BuilderType_DOCKER, Count: 2},
Type: proto.BuildType_CLUSTER,
Artifacts: []*proto.BuildMetadata_Artifact{
{Type: proto.BuilderType_JIB, Name: "jib-artifact-1"},
{Type: proto.BuilderType_DOCKER, Name: "docker-artifact-1"},
{Type: proto.BuilderType_DOCKER, Name: "docker-artifact-2"},
},
},
Deploy: &proto.DeployMetadata{
Expand All @@ -99,16 +98,15 @@ func TestEmptyState(t *testing.T) {
Build: latestV1.BuildConfig{
BuildType: latestV1.BuildType{GoogleCloudBuild: &latestV1.GoogleCloudBuild{}},
Artifacts: []*latestV1.Artifact{
{ImageName: "img1", ArtifactType: latestV1.ArtifactType{KanikoArtifact: &latestV1.KanikoArtifact{}}},
{ImageName: "artifact-1", ArtifactType: latestV1.ArtifactType{KanikoArtifact: &latestV1.KanikoArtifact{}}},
},
},
},
cluster: "gke-tejal-test",
expected: &proto.Metadata{
Build: &proto.BuildMetadata{
NumberOfArtifacts: 1,
Type: proto.BuildType_GCB,
Builders: []*proto.BuildMetadata_ImageBuilder{{Type: proto.BuilderType_KANIKO, Count: 1}},
Type: proto.BuildType_GCB,
Artifacts: []*proto.BuildMetadata_Artifact{{Type: proto.BuilderType_KANIKO, Name: "artifact-1"}},
},
Deploy: &proto.DeployMetadata{},
},
Expand Down Expand Up @@ -138,10 +136,10 @@ func TestEmptyState(t *testing.T) {
state: emptyState(mockCfg([]latestV1.Pipeline{test.cfg}, test.cluster)),
}
metadata := handler.state.Metadata
builders := metadata.Build.Builders
artifacts := metadata.Build.Artifacts

// sort and compare
sort.Slice(builders, func(i, j int) bool { return builders[i].Type < builders[j].Type })
sort.Slice(artifacts, func(i, j int) bool { return artifacts[i].Type < artifacts[j].Type })
t.CheckDeepEqual(metadata, test.expected)
})
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/skaffold/runner/v1/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ func (r *SkaffoldRunner) doDev(ctx context.Context, out io.Writer, logger *logge
defer r.monitor.Reset()
defer r.listener.LogWatchToUser(out)
event.DevLoopInProgress(r.devIteration)
eventV2.InitializeState(r.runCtx)
eventV2.TaskInProgress(constants.DevLoop)
defer func() { r.devIteration++ }()
eventV2.LogMetaEvent()

meterUpdated := false
if needsSync {
Expand Down Expand Up @@ -177,8 +179,11 @@ func (r *SkaffoldRunner) doDev(ctx context.Context, out io.Writer, logger *logge
// config until interrupted by the user.
func (r *SkaffoldRunner) Dev(ctx context.Context, out io.Writer, artifacts []*latestV1.Artifact) error {
event.DevLoopInProgress(r.devIteration)
eventV2.InitializeState(r.runCtx)
eventV2.TaskInProgress(constants.DevLoop)
defer func() { r.devIteration++ }()
eventV2.LogMetaEvent()

g := getTransposeGraph(artifacts)
// Watch artifacts
start := time.Now()
Expand Down
1 change: 1 addition & 0 deletions pkg/skaffold/runner/v1/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func NewForConfig(runCtx *runcontext.RunContext) (*SkaffoldRunner, error) {
event.InitializeState(runCtx)
event.LogMetaEvent()
eventV2.InitializeState(runCtx)
eventV2.LogMetaEvent()
kubectlCLI := pkgkubectl.NewCLI(runCtx, "")

tagger, err := tag.NewTaggerMux(runCtx)
Expand Down
Loading