Skip to content

Commit

Permalink
Allow duplicate kots kinds in secrets and configmaps (#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
divolgin authored Mar 26, 2024
1 parent 41f6376 commit 34f0033
Show file tree
Hide file tree
Showing 14 changed files with 85 additions and 50 deletions.
58 changes: 42 additions & 16 deletions pkg/handlers/lint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,9 @@ func Test_LintRelease(t *testing.T) {
{
name: "one valid chart without kotskinds",
chartReader: func(t *testing.T) io.ReadCloser {
yamlFiles, err := testdata.ReadDir("test-data/kots/without-preflight")
assert.NoError(t, err)

files := []string{
"test-data/builders/testchart-with-labels-16.2.2.tgz",
}
for _, f := range yamlFiles {
files = append(files, filepath.Join("test-data/kots/without-preflight", f.Name()))
"test-data/kots/chart-crds/testchart-with-labels-16.2.2.yaml",
}

return io.NopCloser(getTarReader(files))
Expand Down Expand Up @@ -112,16 +107,11 @@ func Test_LintRelease(t *testing.T) {
},
},
{
name: "one valid chart without kotskinds but with preflights",
name: "one valid chart with preflights but without kotskinds in release",
chartReader: func(t *testing.T) io.ReadCloser {
yamlFiles, err := testdata.ReadDir("test-data/kots/with-preflight")
assert.NoError(t, err)

files := []string{
"test-data/builders/testchart-with-labels-with-preflightspec-in-secret-16.2.2.tgz",
}
for _, f := range yamlFiles {
files = append(files, filepath.Join("test-data/kots/with-preflight", f.Name()))
"test-data/kots/chart-crds/testchart-with-labels-with-preflightspec-in-secret-16.2.2.yaml",
}

return io.NopCloser(getTarReader(files))
Expand Down Expand Up @@ -154,16 +144,52 @@ func Test_LintRelease(t *testing.T) {
},
},
{
name: "one valid chart with kotskinds but without preflights",
name: "one valid chart without preflights but with kotskinds in release",
chartReader: func(t *testing.T) io.ReadCloser {
yamlFiles, err := testdata.ReadDir("test-data/kots/with-kots-kinds")
yamlFiles, err := testdata.ReadDir("test-data/kots/kots-kinds")
assert.NoError(t, err)

files := []string{
"test-data/builders/testchart-with-labels-16.2.2.tgz",
"test-data/kots/chart-crds/testchart-with-labels-16.2.2.yaml",
}
for _, f := range yamlFiles {
files = append(files, filepath.Join("test-data/kots/kots-kinds", f.Name()))
}

return io.NopCloser(getTarReader(files))
},
contentType: "application/tar",
want: resultType{
LintExpressions: []kots.LintExpression{
{
Rule: "application-statusInformers",
Type: "warn",
Message: "Missing application statusInformers",
Path: "kots-app.yaml",
Positions: []kots.LintExpressionItemPosition{
{
Start: kots.LintExpressionItemLinePosition{
Line: 5,
},
},
},
},
},
},
},
{
name: "one valid chart with preflights and with kotskinds in release",
chartReader: func(t *testing.T) io.ReadCloser {
yamlFiles, err := testdata.ReadDir("test-data/kots/kots-kinds")
assert.NoError(t, err)

files := []string{
"test-data/builders/testchart-with-labels-with-preflightspec-in-secret-16.2.2.tgz",
"test-data/kots/chart-crds/testchart-with-labels-with-preflightspec-in-secret-16.2.2.yaml",
}
for _, f := range yamlFiles {
files = append(files, filepath.Join("test-data/kots/with-kots-kinds", f.Name()))
files = append(files, filepath.Join("test-data/kots/kots-kinds", f.Name()))
}

return io.NopCloser(getTarReader(files))
Expand Down
8 changes: 0 additions & 8 deletions pkg/handlers/test-data/kots/without-preflight/chart.yaml

This file was deleted.

18 changes: 10 additions & 8 deletions pkg/kots/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,11 @@ func LintSpecFiles(ctx context.Context, specFiles SpecFiles) ([]LintExpression,
troubleshootSpecs := GetEmbeddedTroubleshootSpecs(ctx, yamlFiles)
for _, tsSpec := range troubleshootSpecs {
yamlFiles = append(yamlFiles, SpecFile{
Name: tsSpec.Name,
Path: tsSpec.Path,
Content: tsSpec.Content,
DocIndex: len(yamlFiles),
Name: tsSpec.Name,
Path: tsSpec.Path,
Content: tsSpec.Content,
DocIndex: len(yamlFiles),
AllowDuplicates: tsSpec.AllowDuplicates,
})
}

Expand All @@ -176,10 +177,11 @@ func LintSpecFiles(ctx context.Context, specFiles SpecFiles) ([]LintExpression,
troubleshootSpecs := GetEmbeddedTroubleshootSpecs(ctx, files)
for _, tsSpec := range troubleshootSpecs {
yamlFiles = append(yamlFiles, SpecFile{
Name: tsSpec.Name,
Path: tsSpec.Path,
Content: tsSpec.Content,
DocIndex: len(yamlFiles),
Name: tsSpec.Name,
Path: tsSpec.Path,
Content: tsSpec.Content,
DocIndex: len(yamlFiles),
AllowDuplicates: tsSpec.AllowDuplicates,
})
}
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/kots/rego/builders-opa.rego
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ files[output] {
"name": file.name,
"path": file.path,
"content": yaml.unmarshal(file.content),
"docIndex": object.get(file, "docIndex", 0)
"docIndex": object.get(file, "docIndex", 0),
"allowDuplicates": object.get(file, "allowDuplicates", false)
}
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/kots/rego/enterprise-opa-prepend.rego
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ files[output] {
"name": file.name,
"path": file.path,
"content": yaml.unmarshal(file.content),
"docIndex": object.get(file, "docIndex", 0)
"docIndex": object.get(file, "docIndex", 0),
"allowDuplicates": object.get(file, "allowDuplicates", false)
}
}

Expand Down
8 changes: 6 additions & 2 deletions pkg/kots/rego/kots-spec-opa-nonrendered.rego
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ files[output] {
"name": file.name,
"path": file.path,
"content": yaml.unmarshal(file.content),
"docIndex": object.get(file, "docIndex", 0)
"docIndex": object.get(file, "docIndex", 0),
"allowDuplicates": object.get(file, "allowDuplicates", false)
}
}

Expand Down Expand Up @@ -146,7 +147,8 @@ kots_kinds[output] {
"apiVersion": file.content.apiVersion,
"kind": file.content.kind,
"filePath": file.path,
"docIndex": file.docIndex
"docIndex": file.docIndex,
"allowDuplicates": file.allowDuplicates
}
}

Expand Down Expand Up @@ -487,6 +489,8 @@ lint[output] {
kki := kots_kinds[i]
kkj := kots_kinds[j]
i != j
not kki.allowDuplicates
not kkj.allowDuplicates
is_same_kots_kind(kki, kkj)
output := {
"rule": rule_name,
Expand Down
15 changes: 10 additions & 5 deletions pkg/kots/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ import (
type SpecFiles []SpecFile

type SpecFile struct {
Name string `json:"name"`
Path string `json:"path"`
Content string `json:"content"`
DocIndex int `json:"docIndex,omitempty"`
Children SpecFiles `json:"children"`
Name string `json:"name"`
Path string `json:"path"`
Content string `json:"content"`
DocIndex int `json:"docIndex,omitempty"`
AllowDuplicates bool `json:"allowDuplicates"` // kotskinds can be duplicated if they are coming from secrets or configmaps
Children SpecFiles `json:"children"`
}

type GVKDoc struct {
Expand Down Expand Up @@ -94,6 +95,10 @@ func (fs SpecFiles) separate() (SpecFiles, error) {
Path: file.Path, // keep original path to be able to link it back
Content: doc,
DocIndex: index,
// Split files inherit the original file's AllowDuplicates.
// This will only be set for KotsKinds extracted from Secrets and ConfigMaps, so this works.
// But also there is no good way to allow split docs to have their own flag.
AllowDuplicates: file.AllowDuplicates,
}

separatedSpecFiles = append(separatedSpecFiles, separatedSpecFile)
Expand Down
22 changes: 13 additions & 9 deletions pkg/kots/troubleshoot.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ func GetEmbeddedTroubleshootSpecs(ctx context.Context, specsFiles SpecFiles) Spe
troubleshootSpecs := findTroubleshootSpecs(ctx, specFile.Content)
for _, tsSpec := range troubleshootSpecs {
tsSpecs = append(tsSpecs, SpecFile{
Name: path.Join(specFile.Name, tsSpec.Name),
Path: specFile.Name,
Content: tsSpec.Content,
Name: path.Join(specFile.Name, tsSpec.Name),
Path: specFile.Name,
Content: tsSpec.Content,
AllowDuplicates: tsSpec.AllowDuplicates,
})
}
}
Expand Down Expand Up @@ -76,8 +77,9 @@ func getSpecFromConfigMap(cm *v1.ConfigMap, namePrefix string) SpecFiles {
str, ok := cm.Data[key]
if ok {
specs = append(specs, SpecFile{
Name: namePrefix + key,
Content: str,
Name: namePrefix + key,
Content: str,
AllowDuplicates: true,
})
}
}
Expand All @@ -98,16 +100,18 @@ func getSpecFromSecret(secret *v1.Secret, namePrefix string) SpecFiles {
data, ok := secret.Data[key]
if ok {
specs = append(specs, SpecFile{
Name: namePrefix + key,
Content: string(data),
Name: namePrefix + key,
Content: string(data),
AllowDuplicates: true,
})
}

str, ok := secret.StringData[key]
if ok {
specs = append(specs, SpecFile{
Name: namePrefix + key,
Content: str,
Name: namePrefix + key,
Content: str,
AllowDuplicates: true,
})
}
}
Expand Down

0 comments on commit 34f0033

Please sign in to comment.