Skip to content

Commit

Permalink
Merge pull request #3533 from balopat/init_refactor/analyzers
Browse files Browse the repository at this point in the history
[init refactor] introducing init analyzers
  • Loading branch information
balopat committed Jan 20, 2020
2 parents 871be06 + b74d2f0 commit 99870a8
Show file tree
Hide file tree
Showing 7 changed files with 429 additions and 177 deletions.
170 changes: 170 additions & 0 deletions pkg/skaffold/initializer/analyze.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
/*
Copyright 2020 The Skaffold Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package initializer

import (
"fmt"
"path/filepath"
"sort"

"github.com/karrick/godirwalk"
"github.com/sirupsen/logrus"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/initializer/kubectl"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
)

type analysis struct {
kubectlAnalyzer *kubectlAnalyzer
skaffoldAnalyzer *skaffoldConfigAnalyzer
builderAnalyzer *builderAnalyzer
}

// analyzer is a generic Visitor that is called on every file in the directory
// It can manage state and react to walking events assuming a bread first search
type analyzer interface {
enterDir(dir string)
analyzeFile(file string) error
exitDir(dir string)
}

type directoryAnalyzer struct {
currentDir string
}

func (a *directoryAnalyzer) analyzeFile(filePath string) error {
return nil
}

func (a *directoryAnalyzer) enterDir(dir string) {
a.currentDir = dir
}

func (a *directoryAnalyzer) exitDir(dir string) {
//pass
}

type kubectlAnalyzer struct {
directoryAnalyzer
kubernetesManifests []string
}

func (a *kubectlAnalyzer) analyzeFile(filePath string) error {
if kubectl.IsKubernetesManifest(filePath) && !IsSkaffoldConfig(filePath) {
a.kubernetesManifests = append(a.kubernetesManifests, filePath)
}
return nil
}

type skaffoldConfigAnalyzer struct {
directoryAnalyzer
force bool
}

func (a *skaffoldConfigAnalyzer) analyzeFile(filePath string) error {
if !IsSkaffoldConfig(filePath) {
return nil
}
if !a.force {
return fmt.Errorf("pre-existing %s found (you may continue with --force)", filePath)
}
logrus.Debugf("%s is a valid skaffold configuration: continuing since --force=true", filePath)
return nil
}

type builderAnalyzer struct {
directoryAnalyzer
enableJibInit bool
enableBuildpackInit bool
findBuilders bool
foundBuilders []InitBuilder

parentDirToStopFindBuilders string
}

func (a *builderAnalyzer) analyzeFile(filePath string) error {
if a.findBuilders && (a.parentDirToStopFindBuilders == "" || a.parentDirToStopFindBuilders == a.currentDir) {
builderConfigs, continueSearchingBuilders := detectBuilders(a.enableJibInit, a.enableBuildpackInit, filePath)
a.foundBuilders = append(a.foundBuilders, builderConfigs...)
if !continueSearchingBuilders {
a.parentDirToStopFindBuilders = a.currentDir
}
}
return nil
}

func (a *builderAnalyzer) exitDir(dir string) {
if a.parentDirToStopFindBuilders == dir {
a.parentDirToStopFindBuilders = ""
}
}

// analyze recursively walks a directory and returns the k8s configs and builder configs that it finds
func (a *analysis) analyze(dir string) error {
for _, analyzer := range a.analyzers() {
analyzer.enterDir(dir)
}
dirents, err := godirwalk.ReadDirents(dir, nil)
if err != nil {
return err
}

var subdirectories []*godirwalk.Dirent
//this is for deterministic results - given the same directory structure
//init should have the same results
sort.Sort(dirents)

// Traverse files
for _, file := range dirents {
if util.IsHiddenFile(file.Name()) || util.IsHiddenDir(file.Name()) {
continue
}

// If we found a directory, keep track of it until we've gone through all the files first
if file.IsDir() {
subdirectories = append(subdirectories, file)
continue
}

filePath := filepath.Join(dir, file.Name())
for _, analyzer := range a.analyzers() {
if err := analyzer.analyzeFile(filePath); err != nil {
return err
}
}
}

// Recurse into subdirectories
for _, subdir := range subdirectories {
if err = a.analyze(filepath.Join(dir, subdir.Name())); err != nil {
return err
}
}

for _, analyzer := range a.analyzers() {
analyzer.exitDir(dir)
}
return nil
}

func (a *analysis) analyzers() []analyzer {
return []analyzer{
a.kubectlAnalyzer,
a.skaffoldAnalyzer,
a.builderAnalyzer,
}
}
17 changes: 0 additions & 17 deletions pkg/skaffold/initializer/builders.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"strings"

"github.com/sirupsen/logrus"
"gopkg.in/AlecAivazis/survey.v1"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/buildpacks"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/jib"
Expand Down Expand Up @@ -222,19 +221,3 @@ func resolveBuilderImages(builderConfigs []InitBuilder, images []string, force b
}
return pairs, nil
}

func promptUserForBuildConfig(image string, choices []string) (string, error) {
var selectedBuildConfig string
options := append(choices, NoBuilder)
prompt := &survey.Select{
Message: fmt.Sprintf("Choose the builder to build image %s", image),
Options: options,
PageSize: 15,
}
err := survey.AskOne(prompt, &selectedBuildConfig, nil)
if err != nil {
return "", err
}

return selectedBuildConfig, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@ import (
"strings"

"github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/warnings"
)

func generateSkaffoldConfig(k Initializer, buildConfigPairs []builderImagePair) ([]byte, error) {
var (
// for testing
getWd = os.Getwd
)

func generateSkaffoldConfig(k DeploymentInitializer, buildConfigPairs []builderImagePair) *latest.SkaffoldConfig {
// if we're here, the user has no skaffold yaml so we need to generate one
// if the user doesn't have any k8s yamls, generate one for each dockerfile
logrus.Info("generating skaffold config")
Expand All @@ -39,7 +43,7 @@ func generateSkaffoldConfig(k Initializer, buildConfigPairs []builderImagePair)
warnings.Printf("Couldn't generate default config name: %s", err.Error())
}

return yaml.Marshal(&latest.SkaffoldConfig{
return &latest.SkaffoldConfig{
APIVersion: latest.Version,
Kind: "Config",
Metadata: latest.Metadata{
Expand All @@ -51,11 +55,11 @@ func generateSkaffoldConfig(k Initializer, buildConfigPairs []builderImagePair)
},
Deploy: k.GenerateDeployConfig(),
},
})
}
}

func suggestConfigName() (string, error) {
cwd, err := os.Getwd()
cwd, err := getWd()
if err != nil {
return "", err
}
Expand Down

0 comments on commit 99870a8

Please sign in to comment.