Skip to content

Commit

Permalink
Respect LintConfig for target/min kots versions (#186)
Browse files Browse the repository at this point in the history
  • Loading branch information
sgalsaleh authored Sep 17, 2024
1 parent 4a2ac03 commit 24bbca7
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 9 deletions.
43 changes: 41 additions & 2 deletions pkg/kots/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,23 @@ func lintTargetMinKotsVersions(specFiles domain.SpecFiles) ([]domain.LintExpress
return nil, errors.Wrap(err, "failed to separate multi docs")
}

lintConfig, err := findLintConfig(separatedSpecFiles)
if err != nil {
return nil, errors.Wrap(err, "failed to find lint config")
}

tvLintOff, mnLintOff := false, false
if lintConfig != nil {
for _, rule := range lintConfig.Spec.Rules {
if rule.Name == "non-existent-target-kots-version" {
tvLintOff = rule.Level == "off"
}
if rule.Name == "non-existent-min-kots-version" {
mnLintOff = rule.Level == "off"
}
}
}

for _, spec := range separatedSpecFiles {
var tv, mv string
var tvExists, mvExists bool
Expand All @@ -498,7 +515,7 @@ func lintTargetMinKotsVersions(specFiles domain.SpecFiles) ([]domain.LintExpress
if err != nil {
return nil, errors.Wrap(err, "failed to check if kots version exists")
}
if !exists {
if !exists && !tvLintOff {
targetVersionlintExpression := domain.LintExpression{
Rule: "non-existent-target-kots-version",
Type: "error",
Expand All @@ -514,7 +531,7 @@ func lintTargetMinKotsVersions(specFiles domain.SpecFiles) ([]domain.LintExpress
if err != nil {
return nil, errors.Wrap(err, "failed to check if kots version exists")
}
if !exists {
if !exists && !mnLintOff {
minVersionlintExpression := domain.LintExpression{
Rule: "non-existent-min-kots-version",
Type: "error",
Expand Down Expand Up @@ -963,3 +980,25 @@ func tryParsingAsHelmChartGVK(content []byte) helmchart.HelmChartInterface {

return nil
}

func findLintConfig(specFiles domain.SpecFiles) (*kotsv1beta1.LintConfig, error) {
var config *kotsv1beta1.LintConfig
for _, file := range specFiles {
document := &domain.GVKDoc{}
if err := yaml.Unmarshal([]byte(file.Content), document); err != nil {
continue
}
if document.APIVersion != "kots.io/v1beta1" || document.Kind != "LintConfig" {
continue
}
decode := scheme.Codecs.UniversalDeserializer().Decode
obj, gvk, err := decode([]byte(file.Content), nil, nil)
if err != nil {
return nil, errors.Wrap(err, "failed to decode lint config content")
}
if gvk.Group == "kots.io" && gvk.Version == "v1beta1" && gvk.Kind == "LintConfig" {
config = obj.(*kotsv1beta1.LintConfig)
}
}
return config, nil
}
49 changes: 42 additions & 7 deletions pkg/kots/lint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func Test_lintTargetMinKotsVersions(t *testing.T) {
name: "valid target and min version with 'v' prefix",
specFiles: domain.SpecFiles{
{
Path: "",
Path: "replicated-app.yaml",
Content: `apiVersion: kots.io/v1beta1
kind: Application
metadata:
Expand All @@ -201,7 +201,7 @@ spec:
name: "valid target and min version without 'v' prefix",
specFiles: domain.SpecFiles{
{
Path: "",
Path: "replicated-app.yaml",
Content: `apiVersion: kots.io/v1beta1
kind: Application
metadata:
Expand Down Expand Up @@ -240,7 +240,7 @@ applicationUrl: "http://example-nginx"`,
name: "invalid target version",
specFiles: domain.SpecFiles{
{
Path: "",
Path: "replicated-app.yaml",
Content: `apiVersion: kots.io/v1beta1
kind: Application
metadata:
Expand All @@ -256,18 +256,19 @@ spec:
Rule: "non-existent-target-kots-version",
Type: "error",
Message: "Target KOTS version not found",
Path: "replicated-app.yaml",
},
},
},
{
name: "invalid min version",
specFiles: domain.SpecFiles{
{
Path: "",
Path: "replicated-app.yaml",
Content: `apiVersion: kots.io/v1beta1
kind: Application
metadata:
name: invalidTargetVersion
name: invalidMinVersion
spec:
targetKotsVersion: "1.64.0"
minKotsVersion: "1000.0.0"
Expand All @@ -279,18 +280,19 @@ spec:
Rule: "non-existent-min-kots-version",
Type: "error",
Message: "Minimum KOTS version not found",
Path: "replicated-app.yaml",
},
},
},
{
name: "invalid target and min version",
specFiles: domain.SpecFiles{
{
Path: "",
Path: "replicated-app.yaml",
Content: `apiVersion: kots.io/v1beta1
kind: Application
metadata:
name: invalidTargetVersion
name: invalidTargetAndMinVersions
spec:
targetKotsVersion: "1000.0.0"
minKotsVersion: "1000.0.0"
Expand All @@ -302,12 +304,45 @@ spec:
Rule: "non-existent-target-kots-version",
Type: "error",
Message: "Target KOTS version not found",
Path: "replicated-app.yaml",
}, {
Rule: "non-existent-min-kots-version",
Type: "error",
Message: "Minimum KOTS version not found",
Path: "replicated-app.yaml",
},
},
},
{
name: "lint off for invalid target and min version",
specFiles: domain.SpecFiles{
{
Path: "replicated-app.yaml",
Content: `apiVersion: kots.io/v1beta1
kind: Application
metadata:
name: invalidTargetAndMinVersions
spec:
targetKotsVersion: "1000.0.0"
minKotsVersion: "1000.0.0"
`,
},
{
Path: "lint-config.yaml",
Content: `apiVersion: kots.io/v1beta1
kind: LintConfig
metadata:
name: lint-config
spec:
rules:
- name: non-existent-target-kots-version
level: "off"
- name: non-existent-min-kots-version
level: "off"
`,
},
},
expect: []domain.LintExpression{},
},
}

Expand Down

0 comments on commit 24bbca7

Please sign in to comment.