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

     1  /*
     2  Copyright 2020 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/config"
    21  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/initializer/errors"
    22  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
    23  )
    24  
    25  // Initializer detects a deployment type and is able to extract image names from it
    26  type Initializer interface {
    27  	// deployConfig generates Deploy Config for skaffold configuration.
    28  	DeployConfig() (latest.DeployConfig, []latest.Profile)
    29  	// GetImages fetches all the images defined in the manifest files.
    30  	GetImages() []string
    31  	// Validate ensures preconditions are met before generating a skaffold config
    32  	Validate() error
    33  	// AddManifestForImage adds a provided manifest for a given image to the initializer
    34  	AddManifestForImage(string, string)
    35  }
    36  
    37  type cliDeployInit struct {
    38  	cliKubernetesManifests []string
    39  }
    40  
    41  func (c *cliDeployInit) DeployConfig() (latest.DeployConfig, []latest.Profile) {
    42  	return latest.DeployConfig{
    43  		DeployType: latest.DeployType{
    44  			KubectlDeploy: &latest.KubectlDeploy{
    45  				Manifests: c.cliKubernetesManifests,
    46  			},
    47  		},
    48  	}, nil
    49  }
    50  
    51  func (c *cliDeployInit) GetImages() []string {
    52  	return nil
    53  }
    54  
    55  func (c *cliDeployInit) Validate() error {
    56  	if len(c.cliKubernetesManifests) == 0 {
    57  		return errors.NoManifestErr{}
    58  	}
    59  	return nil
    60  }
    61  
    62  func (c *cliDeployInit) AddManifestForImage(string, string) {}
    63  
    64  type emptyDeployInit struct {
    65  }
    66  
    67  func (e *emptyDeployInit) DeployConfig() (latest.DeployConfig, []latest.Profile) {
    68  	return latest.DeployConfig{}, nil
    69  }
    70  
    71  func (e *emptyDeployInit) GetImages() []string {
    72  	return nil
    73  }
    74  
    75  func (e *emptyDeployInit) Validate() error {
    76  	return nil
    77  }
    78  
    79  func (e *emptyDeployInit) AddManifestForImage(string, string) {}
    80  
    81  // if any CLI manifests are provided, we always use those as part of a kubectl deploy first
    82  // if not, then if a kustomization yaml is found, we use that next
    83  // otherwise, default to a kubectl deploy.
    84  func NewInitializer(manifests, bases, kustomizations []string, c config.Config) Initializer {
    85  	switch {
    86  	case c.SkipDeploy:
    87  		return &emptyDeployInit{}
    88  	case len(c.CliKubernetesManifests) > 0:
    89  		return &cliDeployInit{c.CliKubernetesManifests}
    90  	case len(kustomizations) > 0:
    91  		return newKustomizeInitializer(c.DefaultKustomization, bases, kustomizations, manifests)
    92  	default:
    93  		return newKubectlInitializer(manifests)
    94  	}
    95  }