Skip to content

Commit

Permalink
[refactor] move DoInit phases into functions (#5037)
Browse files Browse the repository at this point in the history
* move init phases into functions

* fix return case and move manifest generation logic into function

* add comments to clarify code paths
  • Loading branch information
MarlonGamez authored Nov 25, 2020
1 parent d4b2a1c commit bbcfdb0
Showing 1 changed file with 52 additions and 13 deletions.
65 changes: 52 additions & 13 deletions pkg/skaffold/initializer/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/initializer/config"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/initializer/deploy"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/initializer/prompt"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/yaml"
)

Expand All @@ -40,64 +41,102 @@ func DoInit(ctx context.Context, out io.Writer, c config.Config) error {
}
}

a, err := AnalyzeProject(c)
if err != nil {
return err
}

newConfig, newManifests, err := Initialize(out, c, a)
// If the --analyze flag is used, we return early with the result of PrintAnalysis()
// TODO(marlongamez): Figure out a cleaner way to do this. Might have to change return values to include the different Initializers.
if err != nil || c.Analyze {
return err
}

return WriteData(out, c, newConfig, newManifests)
}

// AnalyzeProject scans the project directory for files and keeps track of what types of files it finds (builders, k8s manifests, etc.).
func AnalyzeProject(c config.Config) (*analyze.ProjectAnalysis, error) {
a := analyze.NewAnalyzer(c)
if err := a.Analyze("."); err != nil {
return err
return nil, err
}

// helm projects can't currently be bootstrapped automatically by skaffold, so we fail fast and link to our docs instead.
if len(a.ChartPaths()) > 0 {
//nolint
return errors.New(`Projects set up to deploy with helm must be manually configured.
return nil, errors.New(`Projects set up to deploy with helm must be manually configured.
See https://skaffold.dev/docs/pipeline-stages/deployers/helm/ for a detailed guide on setting your project up with skaffold.`)
}

return a, nil
}

// Initialize uses the information gathered by the analyzer to create a skaffold config and generate kubernetes manifests.
// The returned map[string][]byte represents a mapping from generated config name to its respective manifest data held in a []byte
func Initialize(out io.Writer, c config.Config, a *analyze.ProjectAnalysis) (*latest.SkaffoldConfig, map[string][]byte, error) {
deployInitializer := deploy.NewInitializer(a.Manifests(), a.KustomizeBases(), a.KustomizePaths(), c)
images := deployInitializer.GetImages()

buildInitializer := build.NewInitializer(a.Builders(), c)
if err := buildInitializer.ProcessImages(images); err != nil {
return err
return nil, nil, err
}

if c.Analyze {
return buildInitializer.PrintAnalysis(out)
return nil, nil, buildInitializer.PrintAnalysis(out)
}

newManifests, err := generateManifests(c, buildInitializer, deployInitializer)
if err != nil {
return nil, nil, err
}

if err := deployInitializer.Validate(); err != nil {
return nil, nil, err
}

return generateSkaffoldConfig(buildInitializer, deployInitializer), newManifests, nil
}

func generateManifests(c config.Config, bInitializer build.Initializer, dInitializer deploy.Initializer) (map[string][]byte, error) {
var generatedManifests map[string][]byte
if c.EnableManifestGeneration {
generatedManifestPairs, err := buildInitializer.GenerateManifests()
generatedManifestPairs, err := bInitializer.GenerateManifests()
if err != nil {
return err
return nil, err
}
generatedManifests = make(map[string][]byte, len(generatedManifestPairs))
for pair, manifest := range generatedManifestPairs {
deployInitializer.AddManifestForImage(pair.ManifestPath, pair.ImageName)
dInitializer.AddManifestForImage(pair.ManifestPath, pair.ImageName)
generatedManifests[pair.ManifestPath] = manifest
}
}

if err := deployInitializer.Validate(); err != nil {
return err
}
return generatedManifests, nil
}

pipeline, err := yaml.Marshal(generateSkaffoldConfig(buildInitializer, deployInitializer))
// WriteData takes the given skaffold config and k8s manifests and writes them out to a file or the given io.Writer
func WriteData(out io.Writer, c config.Config, newConfig *latest.SkaffoldConfig, newManifests map[string][]byte) error {
pipeline, err := yaml.Marshal(newConfig)
if err != nil {
return err
}

if c.Opts.ConfigurationFile == "-" {
out.Write(pipeline)
return nil
}

if !c.Force {
if done, err := prompt.WriteSkaffoldConfig(out, pipeline, generatedManifests, c.Opts.ConfigurationFile); done {
if done, err := prompt.WriteSkaffoldConfig(out, pipeline, newManifests, c.Opts.ConfigurationFile); done {
return err
}
}

for path, manifest := range generatedManifests {
for path, manifest := range newManifests {
if err = ioutil.WriteFile(path, manifest, 0644); err != nil {
return fmt.Errorf("writing k8s manifest to file: %w", err)
}
Expand Down

0 comments on commit bbcfdb0

Please sign in to comment.