github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/initializer/build/builders.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 build
    18  
    19  import (
    20  	"io"
    21  
    22  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/initializer/config"
    23  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
    24  )
    25  
    26  // NoBuilder allows users to specify they don't want to build
    27  // an image we parse out from a Kubernetes manifest
    28  const NoBuilder = "None (image not built from these sources)"
    29  
    30  // InitBuilder represents a builder that can be chosen by skaffold init.
    31  type InitBuilder interface {
    32  	// Name returns the name of the builder.
    33  	Name() string
    34  
    35  	// Describe returns the initBuilder's string representation, used when prompting the user to choose a builder.
    36  	// Must be unique between artifacts.
    37  	Describe() string
    38  
    39  	// ArtifactType returns the type of the artifact to be built.  Paths should be relative to the workspace.
    40  	// To make skaffold.yaml more portable across OS-es we should always generate /-delimited filepaths.
    41  	ArtifactType(workspace string) latest.ArtifactType
    42  
    43  	// ConfiguredImage returns the target image configured by the builder, or an empty string if no image is configured.
    44  	// This should be a cheap operation.
    45  	ConfiguredImage() string
    46  
    47  	// Path returns the path to the build file
    48  	Path() string
    49  }
    50  
    51  type NoneBuilder struct{}
    52  
    53  const NoneBuilderName = "none"
    54  
    55  func (b NoneBuilder) Name() string {
    56  	return NoneBuilderName
    57  }
    58  
    59  func (b NoneBuilder) Describe() string {
    60  	return ""
    61  }
    62  
    63  func (b NoneBuilder) ArtifactType(string) latest.ArtifactType {
    64  	return latest.ArtifactType{}
    65  }
    66  
    67  func (b NoneBuilder) ConfiguredImage() string {
    68  	return ""
    69  }
    70  
    71  func (b NoneBuilder) Path() string {
    72  	return ""
    73  }
    74  
    75  // ArtifactInfo defines a builder and the image it builds
    76  type ArtifactInfo struct {
    77  	Builder   InitBuilder
    78  	ImageName string
    79  	Workspace string
    80  	Manifest  ManifestInfo
    81  }
    82  
    83  // GeneratedArtifactInfo pairs a discovered builder with a
    84  // generated image name, and the path to the manifest that should be generated
    85  type GeneratedArtifactInfo struct {
    86  	ArtifactInfo
    87  	ManifestPath string
    88  }
    89  
    90  type ManifestInfo struct {
    91  	Generate bool
    92  	Port     int
    93  }
    94  
    95  type Initializer interface {
    96  	// ProcessImages is the entrypoint call, and handles the pairing of all builders
    97  	// contained in the initializer with the provided images from the deploy initializer
    98  	ProcessImages([]string) error
    99  	// BuildConfig returns the processed build config to be written to the skaffold.yaml
   100  	BuildConfig() (latest.BuildConfig, []*latest.PortForwardResource)
   101  	// PrintAnalysis writes the project analysis to the provided out stream
   102  	PrintAnalysis(io.Writer) error
   103  	// GenerateManifests generates image names and manifests for all unresolved pairs
   104  	GenerateManifests(out io.Writer, force, enableManifestGeneration bool) (map[GeneratedArtifactInfo][]byte, error)
   105  }
   106  
   107  type emptyBuildInitializer struct {
   108  }
   109  
   110  func (e *emptyBuildInitializer) ProcessImages([]string) error {
   111  	return nil
   112  }
   113  
   114  func (e *emptyBuildInitializer) BuildConfig() (latest.BuildConfig, []*latest.PortForwardResource) {
   115  	return latest.BuildConfig{}, nil
   116  }
   117  
   118  func (e *emptyBuildInitializer) PrintAnalysis(io.Writer) error {
   119  	return nil
   120  }
   121  
   122  func (e *emptyBuildInitializer) GenerateManifests(io.Writer, bool, bool) (map[GeneratedArtifactInfo][]byte, error) {
   123  	return nil, nil
   124  }
   125  
   126  func NewInitializer(builders []InitBuilder, c config.Config) Initializer {
   127  	switch {
   128  	case c.SkipBuild:
   129  		return &emptyBuildInitializer{}
   130  	case len(c.CliArtifacts) > 0:
   131  		return &cliBuildInitializer{
   132  			cliArtifacts:    c.CliArtifacts,
   133  			builders:        builders,
   134  			skipBuild:       c.SkipBuild,
   135  			enableNewFormat: c.EnableNewInitFormat,
   136  		}
   137  	default:
   138  		return &defaultBuildInitializer{
   139  			builders:        builders,
   140  			skipBuild:       c.SkipBuild,
   141  			force:           c.Force,
   142  			enableNewFormat: c.EnableNewInitFormat,
   143  			resolveImages:   !c.Analyze,
   144  		}
   145  	}
   146  }