github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/initializer/deploy/kubectl.go (about)

     1  /*
     2  Copyright 2019 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 deploy
    18  
    19  import (
    20  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/initializer/errors"
    21  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes"
    22  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
    23  )
    24  
    25  // kubectl implements deploymentInitializer for the kubectl deployer.
    26  type kubectl struct {
    27  	configs []string // the k8s manifest files present in the project
    28  	images  []string // the images parsed from the k8s manifest files
    29  }
    30  
    31  // newKubectlInitializer returns a kubectl skaffold generator.
    32  func newKubectlInitializer(potentialConfigs []string) *kubectl {
    33  	var k8sConfigs, images []string
    34  	for _, file := range potentialConfigs {
    35  		imgs, err := kubernetes.ParseImagesFromKubernetesYaml(file)
    36  		if err == nil {
    37  			k8sConfigs = append(k8sConfigs, file)
    38  			images = append(images, imgs...)
    39  		}
    40  	}
    41  	return &kubectl{
    42  		configs: k8sConfigs,
    43  		images:  images,
    44  	}
    45  }
    46  
    47  // deployConfig implements the Initializer interface and generates
    48  // skaffold kubectl deployment config.
    49  func (k *kubectl) DeployConfig() (latest.DeployConfig, []latest.Profile) {
    50  	return latest.DeployConfig{
    51  		DeployType: latest.DeployType{
    52  			KubectlDeploy: &latest.KubectlDeploy{
    53  				Manifests: k.configs,
    54  			},
    55  		},
    56  	}, nil
    57  }
    58  
    59  // GetImages implements the Initializer interface and lists all the
    60  // images present in the k8s manifest files.
    61  func (k *kubectl) GetImages() []string {
    62  	return k.images
    63  }
    64  
    65  // Validate implements the Initializer interface and ensures
    66  // we have at least one manifest before generating a config
    67  func (k *kubectl) Validate() error {
    68  	if len(k.images) == 0 {
    69  		return errors.NoManifestErr{}
    70  	}
    71  	return nil
    72  }
    73  
    74  func (k *kubectl) AddManifestForImage(path, image string) {
    75  	k.configs = append(k.configs, path)
    76  	k.images = append(k.images, image)
    77  }