|
| 1 | +/* |
| 2 | +Copyright 2018 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 cmd |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "io" |
| 22 | + "io/ioutil" |
| 23 | + "time" |
| 24 | + |
| 25 | + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker" |
| 26 | + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/runner" |
| 27 | + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest" |
| 28 | + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/version" |
| 29 | + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/watch" |
| 30 | + |
| 31 | + "github.com/pkg/errors" |
| 32 | + "github.com/spf13/cobra" |
| 33 | +) |
| 34 | + |
| 35 | +// NewCmdDiagnose describes the CLI command to diagnose skaffold. |
| 36 | +func NewCmdDiagnose(out io.Writer) *cobra.Command { |
| 37 | + cmd := &cobra.Command{ |
| 38 | + Use: "diagnose", |
| 39 | + Short: "Run a diagnostic on Skaffold", |
| 40 | + Args: cobra.NoArgs, |
| 41 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 42 | + return doDiagnose(out) |
| 43 | + }, |
| 44 | + } |
| 45 | + cmd.Flags().StringVarP(&opts.ConfigurationFile, "filename", "f", "skaffold.yaml", "Filename or URL to the pipeline file") |
| 46 | + return cmd |
| 47 | +} |
| 48 | + |
| 49 | +func doDiagnose(out io.Writer) error { |
| 50 | + _, config, err := newRunner(opts) |
| 51 | + if err != nil { |
| 52 | + return errors.Wrap(err, "creating runner") |
| 53 | + } |
| 54 | + |
| 55 | + fmt.Fprintln(out, "Skaffold version:", version.Get().GitCommit) |
| 56 | + fmt.Fprintln(out, "Configuration version:", config.APIVersion) |
| 57 | + fmt.Fprintln(out, "Number of artifacts:", len(config.Build.Artifacts)) |
| 58 | + |
| 59 | + fmt.Fprintln(out, "\nRunning a diagnostic on artifacts and their dependencies:") |
| 60 | + |
| 61 | + if err := diagnoseArtifacts(out, config.Build.Artifacts); err != nil { |
| 62 | + return errors.Wrap(err, "running diagnostic on artifacts") |
| 63 | + } |
| 64 | + |
| 65 | + fmt.Fprintln(out, "\nSecond run (that benefits from cache):") |
| 66 | + |
| 67 | + if err := diagnoseArtifacts(out, config.Build.Artifacts); err != nil { |
| 68 | + return errors.Wrap(err, "running second diagnostic on artifacts") |
| 69 | + } |
| 70 | + |
| 71 | + return nil |
| 72 | +} |
| 73 | + |
| 74 | +func diagnoseArtifacts(out io.Writer, artifacts []*latest.Artifact) error { |
| 75 | + for _, artifact := range artifacts { |
| 76 | + start := time.Now() |
| 77 | + deps, err := runner.DependenciesForArtifact(artifact) |
| 78 | + if err != nil { |
| 79 | + return errors.Wrap(err, "listing artifact dependencies") |
| 80 | + } |
| 81 | + |
| 82 | + fmt.Fprintln(out, "Type:", typeOfArtifact(artifact)) |
| 83 | + fmt.Fprintln(out, " - Dependencies to watch:", len(deps)) |
| 84 | + fmt.Fprintln(out, " - Time to list dependencies:", time.Since(start)) |
| 85 | + |
| 86 | + if artifact.DockerArtifact != nil { |
| 87 | + size, err := sizeOfTheDockerContext(artifact) |
| 88 | + if err != nil { |
| 89 | + return errors.Wrap(err, "computing the size of the Docker context") |
| 90 | + } |
| 91 | + |
| 92 | + fmt.Fprintln(out, " - Size of the Docker context:", size) |
| 93 | + } |
| 94 | + |
| 95 | + start = time.Now() |
| 96 | + _, err = watch.Stat(func() ([]string, error) { return deps, nil }) |
| 97 | + if err != nil { |
| 98 | + return errors.Wrap(err, "computing modTimes") |
| 99 | + } |
| 100 | + |
| 101 | + fmt.Fprintln(out, " - Time to compute mTimes for all dependencies:", time.Since(start)) |
| 102 | + } |
| 103 | + |
| 104 | + return nil |
| 105 | +} |
| 106 | + |
| 107 | +func sizeOfTheDockerContext(a *latest.Artifact) (int64, error) { |
| 108 | + buildCtx, buildCtxWriter := io.Pipe() |
| 109 | + go func() { |
| 110 | + err := docker.CreateDockerTarContext(buildCtxWriter, a.Workspace, a.DockerArtifact) |
| 111 | + if err != nil { |
| 112 | + buildCtxWriter.CloseWithError(errors.Wrap(err, "creating docker context")) |
| 113 | + return |
| 114 | + } |
| 115 | + buildCtxWriter.Close() |
| 116 | + }() |
| 117 | + |
| 118 | + return io.Copy(ioutil.Discard, buildCtx) |
| 119 | +} |
| 120 | + |
| 121 | +func typeOfArtifact(a *latest.Artifact) string { |
| 122 | + switch { |
| 123 | + case a.DockerArtifact != nil: |
| 124 | + return "Docker" |
| 125 | + case a.BazelArtifact != nil: |
| 126 | + return "Bazel" |
| 127 | + default: |
| 128 | + return "Unknown" |
| 129 | + } |
| 130 | +} |
0 commit comments