Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tejal29 committed Mar 8, 2019
1 parent b7ae09b commit 4557fa8
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 128 deletions.
3 changes: 2 additions & 1 deletion cmd/skaffold/app/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ func NewCmdInit(out io.Writer) *cobra.Command {
CliArtifacts: cliArtifacts,
SkipBuild: skipBuild,
Force: force,
Analyze: analyze
SkaffoldOpts: opts,
}
return initializer.doInit(out, c)
return initializer.DoInit(out, c)
},
}
cmd.Flags().StringVarP(&opts.ConfigurationFile, "filename", "f", "skaffold.yaml", "Filename or URL to the pipeline file")
Expand Down
100 changes: 0 additions & 100 deletions cmd/skaffold/app/cmd/init_test.go

This file was deleted.

45 changes: 36 additions & 9 deletions pkg/skaffold/initializer/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package initializer

import (
"bufio"
"encoding/json"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -49,6 +50,8 @@ const NoDockerfile = "None (image not built from these sources)"
type Initializer interface {
// GenerateDeployConfig generates Deploy Config for skaffold configuration.
GenerateDeployConfig() latest.DeployConfig
// GetImages fetches all the images defined in the manifest files.
GetImages() []string
}

// Config defines the Initializer Config for Init API of skaffold.
Expand All @@ -58,9 +61,11 @@ type Config struct {
SkipBuild bool
Force bool
Opts config.SkaffoldOptions
Analyze bool
}

func doInit(out io.Writer, c Config) error {
// DoInit executes the `skaffold init` flow.
func DoInit(out io.Writer, c Config) error {
rootDir := "."

if c.ComposeFile != "" {
Expand All @@ -81,12 +86,15 @@ func doInit(out io.Writer, c Config) error {
if strings.HasPrefix(path, ".") {
return nil
}

if IsSupportedKubernetesFormat(path) {
if IsSkaffoldConfig(path) {
if !c.Force {
return fmt.Errorf("pre-existing %s found", path)
}
logrus.Debugf("%s is a valid skaffold configuration: continuing since --force=true", path)
} else if IsSupportedKubernetesFormat(path) {
potentialConfigs = append(potentialConfigs, path)
}
// try and parse dockerfile
if docker.ValidateDockerfile(path) {
} else if docker.ValidateDockerfile(path) {
// try and parse dockerfile
logrus.Infof("existing dockerfile found: %s", path)
dockerfiles = append(dockerfiles, path)
}
Expand All @@ -97,11 +105,14 @@ func doInit(out io.Writer, c Config) error {
return err
}

k, err := kubectl.New(potentialConfigs, c.Force)
k, err := kubectl.New(potentialConfigs)
if err != nil {
return err
}

images := k.GetImages()
if c.Analyze {
return printAnalyzeJSON(out, dockerfiles, images)
}
var pairs []dockerfilePair
// conditionally generate build artifacts
if !c.SkipBuild {
Expand All @@ -115,7 +126,7 @@ func doInit(out io.Writer, c Config) error {
return errors.Wrap(err, "processing cli artifacts")
}
} else {
pairs = resolveDockerfileImages(dockerfiles, k.Images())
pairs = resolveDockerfileImages(dockerfiles, images)
}
}

Expand Down Expand Up @@ -273,6 +284,22 @@ func generateSkaffoldPipeline(k Initializer, dockerfilePairs []dockerfilePair) (
return pipelineStr, nil
}

func printAnalyzeJSON(out io.Writer, dockerfiles, images []string) error {
a := struct {
Dockerfiles []string `json:"dockerfiles,omitempty"`
Images []string `json:"images,omitempty"`
}{
Dockerfiles: dockerfiles,
Images: images,
}
contents, err := json.Marshal(a)
if err != nil {
return errors.Wrap(err, "marshalling contents")
}
_, err = out.Write(contents)
return err
}

type dockerfilePair struct {
Dockerfile string
ImageName string
Expand Down
Empty file.
33 changes: 15 additions & 18 deletions pkg/skaffold/initializer/kubectl/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ package kubectl

import (
"bufio"
"fmt"
"io"
"os"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
Expand All @@ -37,22 +35,15 @@ var ValidSuffixes = []string{".yml", ".yaml", ".json"}

// Kubectl holds parameters to run kubectl.
type Kubectl struct {
Configs []string
Images []string
configs []string
images []string
}

//New returns a Kubectl skaffold generator.
func New(potentialConfigs []string, force bool) (*Kubectl, error) {
func New(potentialConfigs []string) (*Kubectl, error) {
var k8sConfigs, images []string
for _, file := range potentialConfigs {
if !force {
config, err := schema.ParseConfig(file, false)
if err == nil && config != nil {
return nil, fmt.Errorf("pre-existing %s found", file)
}
}
logrus.Debugf("%s is not a valid skaffold configuration: continuing", file)
imgs, err := parseKubernetesYaml(file)
imgs, err := parseImagesFromKubernetesYaml(file)
if err == nil {
logrus.Infof("found valid k8s yaml: %s", file)
k8sConfigs = append(k8sConfigs, file)
Expand All @@ -65,8 +56,8 @@ func New(potentialConfigs []string, force bool) (*Kubectl, error) {
return nil, errors.New("one or more valid kubernetes manifests is required to run skaffold")
}
return &Kubectl{
Configs: k8sConfigs,
Images: images,
configs: k8sConfigs,
images: images,
}, nil
}

Expand All @@ -76,16 +67,22 @@ func (k *Kubectl) GenerateDeployConfig() latest.DeployConfig {
return latest.DeployConfig{
DeployType: latest.DeployType{
KubectlDeploy: &latest.KubectlDeploy{
Manifests: k.Configs,
Manifests: k.configs,
},
},
}
}

// parseKubernetesYaml attempts to parse k8s objects from a yaml file
// GetImages implements the Initializer interface and lists all the
// images present in the k8 manifest files.
func (k *Kubectl) GetImages() []string {
return k.images
}

// parseImagesFromKubernetesYaml attempts to parse k8s objects from a yaml file
// if successful, it will return the images referenced in the k8s config
// so they can be built by the generated skaffold yaml
func parseKubernetesYaml(filepath string) ([]string, error) {
func parseImagesFromKubernetesYaml(filepath string) ([]string, error) {
f, err := os.Open(filepath)
if err != nil {
return nil, errors.Wrap(err, "opening config file")
Expand Down
Loading

0 comments on commit 4557fa8

Please sign in to comment.