|
| 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 | + "context" |
| 21 | + "fmt" |
| 22 | + "io" |
| 23 | + "io/ioutil" |
| 24 | + "time" |
| 25 | + |
| 26 | + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/color" |
| 27 | + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker" |
| 28 | + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/runner" |
| 29 | + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest" |
| 30 | + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/version" |
| 31 | + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/watch" |
| 32 | + |
| 33 | + "github.com/pkg/errors" |
| 34 | + "github.com/spf13/cobra" |
| 35 | +) |
| 36 | + |
| 37 | +// NewCmdDiagnose describes the CLI command to diagnose skaffold. |
| 38 | +func NewCmdDiagnose(out io.Writer) *cobra.Command { |
| 39 | + cmd := &cobra.Command{ |
| 40 | + Use: "diagnose", |
| 41 | + Short: "Run a diagnostic on Skaffold", |
| 42 | + Args: cobra.NoArgs, |
| 43 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 44 | + return doDiagnose(out) |
| 45 | + }, |
| 46 | + } |
| 47 | + cmd.Flags().StringVarP(&opts.ConfigurationFile, "filename", "f", "skaffold.yaml", "Filename or URL to the pipeline file") |
| 48 | + return cmd |
| 49 | +} |
| 50 | + |
| 51 | +func doDiagnose(out io.Writer) error { |
| 52 | + _, config, err := newRunner(opts) |
| 53 | + if err != nil { |
| 54 | + return errors.Wrap(err, "creating runner") |
| 55 | + } |
| 56 | + |
| 57 | + fmt.Fprintln(out, "Skaffold version:", version.Get().GitCommit) |
| 58 | + fmt.Fprintln(out, "Configuration version:", config.APIVersion) |
| 59 | + fmt.Fprintln(out, "Number of artifacts:", len(config.Build.Artifacts)) |
| 60 | + |
| 61 | + if err := diagnoseArtifacts(out, config.Build.Artifacts); err != nil { |
| 62 | + return errors.Wrap(err, "running diagnostic on artifacts") |
| 63 | + } |
| 64 | + |
| 65 | + return nil |
| 66 | +} |
| 67 | + |
| 68 | +func diagnoseArtifacts(out io.Writer, artifacts []*latest.Artifact) error { |
| 69 | + ctx := context.Background() |
| 70 | + |
| 71 | + for _, artifact := range artifacts { |
| 72 | + color.Default.Fprintf(out, "\n%s: %s\n", typeOfArtifact(artifact), artifact.ImageName) |
| 73 | + |
| 74 | + if artifact.DockerArtifact != nil { |
| 75 | + size, err := sizeOfDockerContext(ctx, artifact) |
| 76 | + if err != nil { |
| 77 | + return errors.Wrap(err, "computing the size of the Docker context") |
| 78 | + } |
| 79 | + |
| 80 | + fmt.Fprintf(out, " - Size of the context: %vbytes\n", size) |
| 81 | + } |
| 82 | + |
| 83 | + timeDeps1, deps, err := timeToListDependencies(ctx, artifact) |
| 84 | + if err != nil { |
| 85 | + return errors.Wrap(err, "listing artifact dependencies") |
| 86 | + } |
| 87 | + timeDeps2, _, err := timeToListDependencies(ctx, artifact) |
| 88 | + if err != nil { |
| 89 | + return errors.Wrap(err, "listing artifact dependencies") |
| 90 | + } |
| 91 | + |
| 92 | + fmt.Fprintln(out, " - Dependencies:", len(deps), "files") |
| 93 | + fmt.Fprintf(out, " - Time to list dependencies: %v (2nd time: %v)\n", timeDeps1, timeDeps2) |
| 94 | + |
| 95 | + timeMTimes1, err := timeToComputeMTimes(deps) |
| 96 | + if err != nil { |
| 97 | + return errors.Wrap(err, "computing modTimes") |
| 98 | + } |
| 99 | + timeMTimes2, err := timeToComputeMTimes(deps) |
| 100 | + if err != nil { |
| 101 | + return errors.Wrap(err, "computing modTimes") |
| 102 | + } |
| 103 | + |
| 104 | + fmt.Fprintf(out, " - Time to compute mTimes on dependencies: %v (2nd time: %v)\n", timeMTimes1, timeMTimes2) |
| 105 | + } |
| 106 | + |
| 107 | + return nil |
| 108 | +} |
| 109 | + |
| 110 | +func timeToListDependencies(ctx context.Context, a *latest.Artifact) (time.Duration, []string, error) { |
| 111 | + start := time.Now() |
| 112 | + |
| 113 | + deps, err := runner.DependenciesForArtifact(ctx, a) |
| 114 | + if err != nil { |
| 115 | + return 0, nil, errors.Wrap(err, "listing artifact dependencies") |
| 116 | + } |
| 117 | + |
| 118 | + return time.Since(start), deps, nil |
| 119 | +} |
| 120 | + |
| 121 | +func timeToComputeMTimes(deps []string) (time.Duration, error) { |
| 122 | + start := time.Now() |
| 123 | + |
| 124 | + if _, err := watch.Stat(func() ([]string, error) { return deps, nil }); err != nil { |
| 125 | + return 0, errors.Wrap(err, "computing modTimes") |
| 126 | + } |
| 127 | + |
| 128 | + return time.Since(start), nil |
| 129 | +} |
| 130 | + |
| 131 | +func sizeOfDockerContext(ctx context.Context, a *latest.Artifact) (int64, error) { |
| 132 | + buildCtx, buildCtxWriter := io.Pipe() |
| 133 | + go func() { |
| 134 | + err := docker.CreateDockerTarContext(ctx, buildCtxWriter, a.Workspace, a.DockerArtifact) |
| 135 | + if err != nil { |
| 136 | + buildCtxWriter.CloseWithError(errors.Wrap(err, "creating docker context")) |
| 137 | + return |
| 138 | + } |
| 139 | + buildCtxWriter.Close() |
| 140 | + }() |
| 141 | + |
| 142 | + return io.Copy(ioutil.Discard, buildCtx) |
| 143 | +} |
| 144 | + |
| 145 | +func typeOfArtifact(a *latest.Artifact) string { |
| 146 | + switch { |
| 147 | + case a.DockerArtifact != nil: |
| 148 | + return "Docker artifact" |
| 149 | + case a.BazelArtifact != nil: |
| 150 | + return "Bazel artifact" |
| 151 | + default: |
| 152 | + return "Unknown artifact" |
| 153 | + } |
| 154 | +} |
0 commit comments