Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

skaffold diagnose to not initialize runconfig for yaml only flag #5762

Merged
merged 5 commits into from
Apr 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 29 additions & 13 deletions cmd/skaffold/app/cmd/diagnose.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,18 @@ import (

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/color"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/diagnose"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/parser"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/runner/runcontext"
latest_v1 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest/v1"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/version"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/yaml"
)

var (
yamlOnly bool
// for testing
getRunContext = runcontext.GetRunContext
getCfgs = parser.GetAllConfigs
)

// NewCmdDiagnose describes the CLI command to diagnose skaffold.
Expand All @@ -46,22 +52,13 @@ func NewCmdDiagnose() *cobra.Command {
}

func doDiagnose(ctx context.Context, out io.Writer) error {
runCtx, configs, err := runContext(out, opts)
configs, err := getCfgs(opts)
if err != nil {
return err
}

for _, config := range configs {
if !yamlOnly {
fmt.Fprintln(out, "Skaffold version:", version.Get().GitCommit)
fmt.Fprintln(out, "Configuration version:", config.APIVersion)
fmt.Fprintln(out, "Number of artifacts:", len(config.Build.Artifacts))

if err := diagnose.CheckArtifacts(ctx, runCtx, out); err != nil {
return fmt.Errorf("running diagnostic on artifacts: %w", err)
}

color.Blue.Fprintln(out, "\nConfiguration")
if !yamlOnly {
if err := printArtifactDiagnostics(ctx, out, configs); err != nil {
return err
}
}
// remove the dependency config references since they have already been imported and will be marshalled together.
Expand All @@ -76,3 +73,22 @@ func doDiagnose(ctx context.Context, out io.Writer) error {

return nil
}

func printArtifactDiagnostics(ctx context.Context, out io.Writer, configs []*latest_v1.SkaffoldConfig) error {
runCtx, err := getRunContext(opts, configs)
if err != nil {
return fmt.Errorf("getting run context: %w", err)
}
for _, config := range configs {
fmt.Fprintln(out, "Skaffold version:", version.Get().GitCommit)
fmt.Fprintln(out, "Configuration version:", config.APIVersion)
fmt.Fprintln(out, "Number of artifacts:", len(config.Build.Artifacts))

if err := diagnose.CheckArtifacts(ctx, runCtx, out); err != nil {
return fmt.Errorf("running diagnostic on artifacts: %w", err)
}

color.Blue.Fprintln(out, "\nConfiguration")
}
return nil
}
87 changes: 87 additions & 0 deletions cmd/skaffold/app/cmd/diagnose_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
Copyright 2021 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 cmd

import (
"bytes"
"context"
"fmt"
"testing"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/runner/runcontext"
latest_v1 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest/v1"
"github.com/GoogleContainerTools/skaffold/testutil"
)

func TestDoDiagnose(t *testing.T) {
tests := []struct {
description string
yamlOnly bool
shouldErr bool
expected string
}{
{
description: "yaml only set to true",
yamlOnly: true,
expected: `apiVersion: testVersion
kind: Config
metadata:
name: config1
---
apiVersion: testVersion
kind: Config
metadata:
name: config2
`,
},
{
description: "yaml only set to false",
shouldErr: true,
},
}

for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
t.Override(&getRunContext, func(config.SkaffoldOptions, []*latest_v1.SkaffoldConfig) (*runcontext.RunContext, error) {
return nil, fmt.Errorf("cannot get the runtime context")
})
t.Override(&yamlOnly, test.yamlOnly)
t.Override(&getCfgs, func(opts config.SkaffoldOptions) ([]*latest_v1.SkaffoldConfig, error) {
return []*latest_v1.SkaffoldConfig{
{
APIVersion: "testVersion",
Kind: "Config",
Metadata: latest_v1.Metadata{
Name: "config1",
},
},
{
APIVersion: "testVersion",
Kind: "Config",
Metadata: latest_v1.Metadata{
Name: "config2",
},
},
}, nil
})
var b bytes.Buffer
err := doDiagnose(context.Background(), &b)
t.CheckErrorAndDeepEqual(test.shouldErr, err, test.expected, b.String())
})
}
}
6 changes: 1 addition & 5 deletions cmd/skaffold/app/cmd/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,6 @@ func runContext(out io.Writer, opts config.SkaffoldOptions) (*runcontext.RunCont
return nil, nil, err
}
setDefaultDeployer(configs)
var pipelines []latest_v1.Pipeline
for _, cfg := range configs {
pipelines = append(pipelines, cfg.Pipeline)
}

// TODO: Should support per-config kubecontext. Right now we constrain all configs to define the same kubecontext.
kubectx.ConfigureKubeConfig(opts.KubeConfig, opts.KubeContext, configs[0].Deploy.KubeContext)
Expand All @@ -91,7 +87,7 @@ func runContext(out io.Writer, opts config.SkaffoldOptions) (*runcontext.RunCont
return nil, nil, fmt.Errorf("invalid skaffold config: %w", err)
}

runCtx, err := runcontext.GetRunContext(opts, pipelines)
runCtx, err := runcontext.GetRunContext(opts, configs)
if err != nil {
return nil, nil, fmt.Errorf("getting run context: %w", err)
}
Expand Down
8 changes: 7 additions & 1 deletion pkg/skaffold/runner/runcontext/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,13 @@ func (rc *RunContext) WatchPollInterval() int { return rc.Opt
func (rc *RunContext) BuildConcurrency() int { return rc.Opts.BuildConcurrency }
func (rc *RunContext) IsMultiConfig() bool { return rc.Pipelines.IsMultiPipeline() }

func GetRunContext(opts config.SkaffoldOptions, pipelines []latest_v1.Pipeline) (*RunContext, error) {
func GetRunContext(opts config.SkaffoldOptions, configs []*latest_v1.SkaffoldConfig) (*RunContext, error) {
var pipelines []latest_v1.Pipeline
for _, cfg := range configs {
if cfg != nil {
pipelines = append(pipelines, cfg.Pipeline)
}
}
kubeConfig, err := kubectx.CurrentConfig()
if err != nil {
return nil, fmt.Errorf("getting current cluster context: %w", err)
Expand Down