Skip to content

Commit

Permalink
rebased
Browse files Browse the repository at this point in the history
  • Loading branch information
Priya Wadhwa committed Oct 8, 2018
2 parents 8143f6c + b809bd5 commit a87a459
Show file tree
Hide file tree
Showing 67 changed files with 4,038 additions and 270 deletions.
10 changes: 10 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions cmd/skaffold/app/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func NewSkaffoldCommand(out, err io.Writer) *cobra.Command {
rootCmd.AddCommand(NewCmdConfig(out))
rootCmd.AddCommand(NewCmdInit(out))

rootCmd.PersistentFlags().StringVarP(&v, "verbosity", "v", constants.DefaultLogLevel.String(), "Log level (debug, info, warn, error, fatal, panic")
rootCmd.PersistentFlags().StringVarP(&v, "verbosity", "v", constants.DefaultLogLevel.String(), "Log level (debug, info, warn, error, fatal, panic)")

setFlagsFromEnvVariables(rootCmd.Commands())

Expand Down Expand Up @@ -136,17 +136,19 @@ func AddDevFlags(cmd *cobra.Command) {
cmd.Flags().StringArrayVarP(&opts.Watch, "watch-image", "w", nil, "Choose which artifacts to watch. Artifacts with image names that contain the expression will be watched only. Default is to watch sources for all artifacts.")
cmd.Flags().IntVarP(&opts.WatchPollInterval, "watch-poll-interval", "i", 1000, "Interval (in ms) between two checks for file changes.")
cmd.Flags().BoolVar(&opts.PortForward, "port-forward", true, "Port-forward exposed container ports within pods")
cmd.Flags().StringArrayVarP(&opts.CustomLabels, "label", "l", nil, "Add custom labels to deployed objects. Set multiple times for multiple labels.")
}

func AddRunDeployFlags(cmd *cobra.Command) {
cmd.Flags().BoolVar(&opts.Tail, "tail", false, "Stream logs from deployed objects")
cmd.Flags().StringArrayVarP(&opts.CustomLabels, "label", "l", nil, "Add custom labels to deployed objects. Set multiple times for multiple labels.")
}

func AddRunDevFlags(cmd *cobra.Command) {
cmd.Flags().StringVarP(&opts.ConfigurationFile, "filename", "f", "skaffold.yaml", "Filename or URL to the pipeline file")
cmd.Flags().BoolVar(&opts.Notification, "toot", false, "Emit a terminal beep after the deploy is complete")
cmd.Flags().StringArrayVarP(&opts.Profiles, "profile", "p", nil, "Activate profiles by name")
cmd.Flags().StringVarP(&opts.Namespace, "namespace", "n", "", "Run Helm deployments in the specified namespace")
cmd.Flags().StringVarP(&opts.Namespace, "namespace", "n", "", "Run deployments in the specified namespace")
}

func AddFixFlags(cmd *cobra.Command) {
Expand Down
1 change: 1 addition & 0 deletions cmd/skaffold/app/cmd/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func NewCmdDev(out io.Writer) *cobra.Command {
AddRunDevFlags(cmd)
AddDevFlags(cmd)
cmd.Flags().BoolVar(&opts.TailDev, "tail", true, "Stream logs from deployed objects")
cmd.Flags().StringVar(&opts.Trigger, "trigger", "polling", "How are changes detected? (polling or manual)")
return cmd
}

Expand Down
3 changes: 2 additions & 1 deletion cmd/skaffold/app/cmd/fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ import (

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/color"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
schemautil "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/util"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
yaml "gopkg.in/yaml.v2"
"k8s.io/client-go/tools/clientcmd/api/latest"
)

func NewCmdFix(out io.Writer) *cobra.Command {
Expand Down
87 changes: 58 additions & 29 deletions cmd/skaffold/app/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,13 @@ import (
// an image we parse out from a kubernetes manifest
const NoDockerfile = "None (image not built from these sources)"

var outfile string
var skipBuild bool
var cliArtifacts []string
var (
cliArtifacts []string
skipBuild bool
force bool
)

// NewCmdInit describes the CLI command to generate a skaffold configuration.
func NewCmdInit(out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "init",
Expand All @@ -57,18 +60,16 @@ func NewCmdInit(out io.Writer) *cobra.Command {
return doInit(out)
},
}
AddInitFlags(cmd)
return cmd
}

func AddInitFlags(cmd *cobra.Command) {
cmd.Flags().StringVarP(&outfile, "file", "f", "", "File to write generated skaffold config")
cmd.Flags().StringVarP(&opts.ConfigurationFile, "filename", "f", "skaffold.yaml", "Filename or URL to the pipeline file")
cmd.Flags().BoolVar(&skipBuild, "skip-build", false, "Skip generating build artifacts in skaffold config")
cmd.Flags().BoolVar(&force, "force", false, "Force the generation of the skaffold config")
cmd.Flags().StringArrayVarP(&cliArtifacts, "artifact", "a", nil, "'='-delimited dockerfile/image pair to generate build artifact\n(example: --artifact=/web/Dockerfile.web=gcr.io/web-project/image)")
return cmd
}

func doInit(out io.Writer) error {
rootDir := "."

var potentialConfigs, k8sConfigs, dockerfiles, images []string
err := filepath.Walk(rootDir, func(path string, f os.FileInfo, e error) error {
if f.IsDir() {
Expand All @@ -90,11 +91,13 @@ func doInit(out io.Writer) error {
if err != nil {
return err
}

for _, file := range potentialConfigs {
config, err := schema.ParseConfig(file, true)
if err == nil && config != nil {
out.Write([]byte(fmt.Sprintf("pre-existing skaffold yaml %s found: exiting\n", file)))
return nil
if !force {
config, err := schema.ParseConfig(file, true)
if err == nil && config != nil {
return fmt.Errorf("pre-existing %s found", file)
}
}

logrus.Debugf("%s is not a valid skaffold configuration: continuing", file)
Expand Down Expand Up @@ -129,18 +132,45 @@ func doInit(out io.Writer) error {
}
}

cfg, err := generateSkaffoldConfig(k8sConfigs, pairs)
pipeline, err := generateSkaffoldPipeline(k8sConfigs, pairs)
if err != nil {
return err
}
if outfile != "" {
if err := ioutil.WriteFile(outfile, cfg, 0644); err != nil {
return errors.Wrap(err, "writing config to file")

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

if !force {
fmt.Fprintln(out, string(pipeline))

reader := bufio.NewReader(os.Stdin)
confirmLoop:
for {
fmt.Fprintf(out, "Do you want to write this configuration to %s? [y/n]: ", opts.ConfigurationFile)

response, err := reader.ReadString('\n')
if err != nil {
return errors.Wrap(err, "reading user confirmation")
}

response = strings.ToLower(strings.TrimSpace(response))
switch response {
case "y", "yes":
break confirmLoop
case "n", "no":
return nil
}
}
} else {
out.Write(cfg)
}

if err := ioutil.WriteFile(opts.ConfigurationFile, pipeline, 0644); err != nil {
return errors.Wrap(err, "writing config to file")
}

fmt.Fprintf(out, "Configuration %s was written\n", opts.ConfigurationFile)

return nil
}

Expand Down Expand Up @@ -231,34 +261,33 @@ func processBuildArtifacts(pairs []dockerfilePair) latest.BuildConfig {
return config
}

func generateSkaffoldConfig(k8sConfigs []string, dockerfilePairs []dockerfilePair) ([]byte, error) {
func generateSkaffoldPipeline(k8sConfigs []string, dockerfilePairs []dockerfilePair) ([]byte, error) {
// 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")

config := &latest.SkaffoldConfig{
pipeline := &latest.SkaffoldPipeline{
APIVersion: latest.Version,
Kind: "Config",
}
if err := config.SetDefaultValues(); err != nil {
return nil, errors.Wrap(err, "generating default config")
if err := pipeline.SetDefaultValues(); err != nil {
return nil, errors.Wrap(err, "generating default pipeline")
}

config.Build = processBuildArtifacts(dockerfilePairs)
config.Deploy = latest.DeployConfig{
pipeline.Build = processBuildArtifacts(dockerfilePairs)
pipeline.Deploy = latest.DeployConfig{
DeployType: latest.DeployType{
KubectlDeploy: &latest.KubectlDeploy{
Manifests: k8sConfigs,
},
},
}

cfgStr, err := yaml.Marshal(config)
pipelineStr, err := yaml.Marshal(pipeline)
if err != nil {
return nil, errors.Wrap(err, "marshaling generated config")
return nil, errors.Wrap(err, "marshaling generated pipeline")
}

return cfgStr, nil
return pipelineStr, nil
}

// parseKubernetesYaml attempts to parse k8s objects from a yaml file
Expand Down
6 changes: 3 additions & 3 deletions cmd/skaffold/app/cmd/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import (
"github.com/pkg/errors"
)

// newRunner creates a SkaffoldRunner and returns the SkaffoldConfig associated with it.
func newRunner(opts *config.SkaffoldOptions) (*runner.SkaffoldRunner, *latest.SkaffoldConfig, error) {
// newRunner creates a SkaffoldRunner and returns the SkaffoldPipeline associated with it.
func newRunner(opts *config.SkaffoldOptions) (*runner.SkaffoldRunner, *latest.SkaffoldPipeline, error) {
parsed, err := schema.ParseConfig(opts.ConfigurationFile, true)
if err != nil {
return nil, nil, errors.Wrap(err, "parsing skaffold config")
Expand All @@ -35,7 +35,7 @@ func newRunner(opts *config.SkaffoldOptions) (*runner.SkaffoldRunner, *latest.Sk
return nil, nil, errors.Wrap(err, "invalid config")
}

config := parsed.(*latest.SkaffoldConfig)
config := parsed.(*latest.SkaffoldPipeline)
err = schema.ApplyProfiles(config, opts.Profiles)
if err != nil {
return nil, nil, errors.Wrap(err, "applying profiles")
Expand Down
6 changes: 5 additions & 1 deletion deploy/skaffold/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ RUN echo "deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8
RUN apt-get update \
&& apt-get install -y bazel

ENV CONTAINER_STRUCTURE_TEST_VERSION=1.5.0
RUN curl -LO https://storage.googleapis.com/container-structure-test/v${CONTAINER_STRUCTURE_TEST_VERSION}/container-structure-test-linux-amd64 \
&& chmod +x container-structure-test-linux-amd64 \
&& mv container-structure-test-linux-amd64 /usr/local/bin/container-structure-test

ENV PATH /usr/local/go/bin:/go/bin:/google-cloud-sdk/bin:$PATH

FROM runtime_deps as builder
Expand Down Expand Up @@ -94,4 +99,3 @@ CMD ["make", "integration"]
FROM runtime_deps as distribution

COPY --from=integration /usr/bin/skaffold /usr/bin/skaffold

9 changes: 9 additions & 0 deletions integration/examples/getting-started/skaffold.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ kind: Config
build:
artifacts:
- image: gcr.io/k8s-skaffold/skaffold-example
test:
- image: gcr.io/k8s-skaffold/skaffold-example
structureTests:
- ./test/*
deploy:
kubectl:
manifests:
Expand All @@ -12,3 +16,8 @@ profiles:
build:
googleCloudBuild:
projectId: k8s-skaffold
- name: test
test:
- image: gcr.io/k8s-skaffold/skaffold-example
structureTests:
- ./test/profile_structure_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
schemaVersion: 2.0.0

fileExistenceTests:
- name: 'no go binary'
path: '/usr/bin/go'
shouldExist: false
6 changes: 6 additions & 0 deletions integration/examples/getting-started/test/structure_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
schemaVersion: 2.0.0

fileExistenceTests:
- name: 'no local go binary'
path: /usr/local/bin/go'
shouldExist: false
3 changes: 3 additions & 0 deletions integration/examples/hot-reload/node/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM gcr.io/k8s-skaffold/nodemon
CMD ["nodemon","--legacy-watch", "server.js"]
COPY *.js .
10 changes: 10 additions & 0 deletions integration/examples/hot-reload/node/k8s/pod.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: v1
kind: Pod
metadata:
name: node
spec:
containers:
- name: node
image: gcr.io/k8s-skaffold/node-example
ports:
- containerPort: 3000
Loading

0 comments on commit a87a459

Please sign in to comment.