github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/event/v2/metadata.go (about)

     1  /*
     2  Copyright 2021 The Skaffold Authors
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package v2
    18  
    19  import (
    20  	"fmt"
    21  	"strings"
    22  
    23  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
    24  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/version"
    25  	proto "github.com/GoogleContainerTools/skaffold/proto/v2"
    26  )
    27  
    28  func LogMetaEvent() {
    29  	metadata := handler.state.Metadata
    30  	handler.handle(
    31  		&proto.Event{
    32  			EventType: &proto.Event_MetaEvent{
    33  				MetaEvent: &proto.MetaEvent{
    34  					Entry:    fmt.Sprintf("Starting Skaffold: %+v", version.Get()),
    35  					Metadata: metadata,
    36  				},
    37  			},
    38  		},
    39  	)
    40  }
    41  
    42  func initializeMetadata(pipelines []latest.Pipeline, kubeContext string, runID string) *proto.Metadata {
    43  	m := &proto.Metadata{
    44  		Build:  &proto.BuildMetadata{},
    45  		Render: &proto.RenderMetadata{},
    46  		Deploy: &proto.DeployMetadata{},
    47  		RunID:  runID,
    48  	}
    49  
    50  	// TODO: Event metadata should support multiple build types.
    51  	// All pipelines are currently constrained to have the same build type.
    52  	switch {
    53  	case pipelines[0].Build.LocalBuild != nil:
    54  		m.Build.Type = proto.BuildType_LOCAL
    55  	case pipelines[0].Build.GoogleCloudBuild != nil:
    56  		m.Build.Type = proto.BuildType_GCB
    57  	case pipelines[0].Build.Cluster != nil:
    58  		m.Build.Type = proto.BuildType_CLUSTER
    59  	default:
    60  		m.Build.Type = proto.BuildType_UNKNOWN_BUILD_TYPE
    61  	}
    62  
    63  	var artifacts []*proto.BuildMetadata_Artifact
    64  	var deployers []*proto.DeployMetadata_Deployer
    65  	for _, p := range pipelines {
    66  		artifacts = append(artifacts, getArtifacts(p.Build)...)
    67  		deployers = append(deployers, getDeploy(p.Deploy)...)
    68  	}
    69  	m.Build.Artifacts = artifacts
    70  
    71  	if len(deployers) == 0 {
    72  		m.Deploy = &proto.DeployMetadata{}
    73  	} else {
    74  		m.Deploy = &proto.DeployMetadata{
    75  			Deployers: deployers,
    76  			Cluster:   getClusterType(kubeContext),
    77  		}
    78  	}
    79  	// TODO(v2 render): Add the renderMetadata initialization once the pipeline is switched to latestV2.Pipeline
    80  	return m
    81  }
    82  
    83  func getArtifacts(b latest.BuildConfig) []*proto.BuildMetadata_Artifact {
    84  	result := []*proto.BuildMetadata_Artifact{}
    85  	for _, a := range b.Artifacts {
    86  		artifact := &proto.BuildMetadata_Artifact{
    87  			Name:    a.ImageName,
    88  			Context: a.Workspace,
    89  		}
    90  		switch {
    91  		case a.BazelArtifact != nil:
    92  			artifact.Type = proto.BuilderType_BAZEL
    93  		case a.BuildpackArtifact != nil:
    94  			artifact.Type = proto.BuilderType_BUILDPACKS
    95  		case a.CustomArtifact != nil:
    96  			artifact.Type = proto.BuilderType_CUSTOM
    97  		case a.DockerArtifact != nil:
    98  			artifact.Type = proto.BuilderType_DOCKER
    99  			artifact.Dockerfile = a.DockerArtifact.DockerfilePath
   100  		case a.JibArtifact != nil:
   101  			artifact.Type = proto.BuilderType_JIB
   102  		case a.KanikoArtifact != nil:
   103  			artifact.Type = proto.BuilderType_KANIKO
   104  			artifact.Dockerfile = a.KanikoArtifact.DockerfilePath
   105  		case a.KoArtifact != nil:
   106  			artifact.Type = proto.BuilderType_KO
   107  		default:
   108  			artifact.Type = proto.BuilderType_UNKNOWN_BUILDER_TYPE
   109  		}
   110  		result = append(result, artifact)
   111  	}
   112  	return result
   113  }
   114  
   115  func getDeploy(d latest.DeployConfig) []*proto.DeployMetadata_Deployer {
   116  	var deployers []*proto.DeployMetadata_Deployer
   117  
   118  	if d.HelmDeploy != nil {
   119  		deployers = append(deployers, &proto.DeployMetadata_Deployer{Type: proto.DeployerType_HELM, Count: int32(len(d.HelmDeploy.Releases))})
   120  	}
   121  	if d.KubectlDeploy != nil {
   122  		deployers = append(deployers, &proto.DeployMetadata_Deployer{Type: proto.DeployerType_KUBECTL, Count: 1})
   123  	}
   124  	if d.KustomizeDeploy != nil {
   125  		deployers = append(deployers, &proto.DeployMetadata_Deployer{Type: proto.DeployerType_KUSTOMIZE, Count: 1})
   126  	}
   127  	return deployers
   128  }
   129  
   130  func getClusterType(c string) proto.ClusterType {
   131  	switch {
   132  	case strings.Contains(c, "minikube"):
   133  		return proto.ClusterType_MINIKUBE
   134  	case strings.HasPrefix(c, "gke"):
   135  		return proto.ClusterType_GKE
   136  	default:
   137  		return proto.ClusterType_OTHER
   138  	}
   139  }