From c70d294c50d4d9ee64c2bb4fd17083a2670897d0 Mon Sep 17 00:00:00 2001 From: Aaron Prindle Date: Thu, 11 Nov 2021 10:09:25 -0800 Subject: [PATCH] fix: fix nil pointer issue for skaff lint when encountering skaffold.yaml with no k8s manifests (#6832) --- pkg/skaffold/lint/k8smanifests.go | 3 +++ pkg/skaffold/lint/k8smanifests_test.go | 36 +++++++++++++++++++------- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/pkg/skaffold/lint/k8smanifests.go b/pkg/skaffold/lint/k8smanifests.go index 69ee2651ac7..d6192f6abf7 100644 --- a/pkg/skaffold/lint/k8smanifests.go +++ b/pkg/skaffold/lint/k8smanifests.go @@ -67,6 +67,9 @@ func GetK8sManifestsLintResults(ctx context.Context, opts Options) (*[]Result, e } for _, c := range cfgs { + if c.Deploy.KubectlDeploy == nil { + continue + } for _, pattern := range c.Deploy.KubectlDeploy.Manifests { // NOTE: pattern is a pattern that can have wildcards, eg: leeroy-app/kubernetes/* if util.IsURL(pattern) { diff --git a/pkg/skaffold/lint/k8smanifests_test.go b/pkg/skaffold/lint/k8smanifests_test.go index a76586876db..bfff228ffd0 100644 --- a/pkg/skaffold/lint/k8smanifests_test.go +++ b/pkg/skaffold/lint/k8smanifests_test.go @@ -62,14 +62,15 @@ func TestGetK8sManifestsLintResults(t *testing.T) { ruleIDToK8sManifestRule[k8sManifestLintRules[i].RuleID] = &k8sManifestLintRules[i] } tests := []struct { + shouldErr bool + k8sManifestIsNil bool description string - rules []RuleID - moduleAndSkaffoldYamls map[string]string - profiles []string - modules []string k8sManifestText string - shouldErr bool err error + profiles []string + modules []string + rules []RuleID + moduleAndSkaffoldYamls map[string]string expected map[string]*[]Result }{ { @@ -97,6 +98,12 @@ func TestGetK8sManifestsLintResults(t *testing.T) { moduleAndSkaffoldYamls: map[string]string{"cfg0": testSkaffoldYaml}, shouldErr: true, }, + { + rules: []RuleID{}, + description: "no k8sManifest file for skaffold.yaml", + k8sManifestIsNil: true, + moduleAndSkaffoldYamls: map[string]string{"cfg0": testSkaffoldYaml}, + }, } for _, test := range tests { testutil.Run(t, test.description, func(t *testutil.T) { @@ -124,11 +131,20 @@ func TestGetK8sManifestsLintResults(t *testing.T) { if err != nil { t.Fatalf("error creating deployment.yaml %s: %v", mp, err) } - configSet = append(configSet, &parser.SkaffoldConfigEntry{SkaffoldConfig: &v1.SkaffoldConfig{ - Metadata: v1.Metadata{Name: module}, - Pipeline: v1.Pipeline{Deploy: v1.DeployConfig{DeployType: v1.DeployType{KubectlDeploy: &v1.KubectlDeploy{Manifests: []string{mp}}}}}, - }, - }) + if test.k8sManifestIsNil { + configSet = append(configSet, &parser.SkaffoldConfigEntry{SkaffoldConfig: &v1.SkaffoldConfig{ + Metadata: v1.Metadata{Name: module}, + Pipeline: v1.Pipeline{}, + }, + }) + } else { + configSet = append(configSet, &parser.SkaffoldConfigEntry{SkaffoldConfig: &v1.SkaffoldConfig{ + Metadata: v1.Metadata{Name: module}, + Pipeline: v1.Pipeline{Deploy: v1.DeployConfig{DeployType: v1.DeployType{KubectlDeploy: &v1.KubectlDeploy{Manifests: []string{mp}}}}}, + }, + }) + } + // test overwrites file paths for expected K8sManifestRules as they are made dynamically results := test.expected[module] if results == nil {