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

fix: Properly represent multiple versions of the same custom resource #516

Closed
Closed
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
6 changes: 3 additions & 3 deletions pkg/rules/rego/custom.rego.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ deprecated_resource(r) = api {

deprecated_api(kind, api_version) = api {
deprecated_apis = {
{{- range . }}
"{{ .Kind }}": {
"old": ["{{ .Group }}/{{ .Version }}"],
{{- range $key, $value := . }}
"{{ $key }}": {
"old": [{{range $value}}"{{.}}",{{end}}],
},
{{- end }}
}
Expand Down
13 changes: 12 additions & 1 deletion pkg/rules/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type Rule struct {
}

func FetchRegoRules(additionalResources []schema.GroupVersionKind) ([]Rule, error) {

fis, err := local.ReadDir(RULES_DIR)
if err != nil {
return nil, err
Expand Down Expand Up @@ -63,7 +64,17 @@ func renderRule(inputData []byte, fileName string, additionalKinds []schema.Grou
}

var tpl bytes.Buffer
if err := t.Execute(&tpl, additionalKinds); err != nil {
customEntries := make(map[string][]string)
for _, gvk := range additionalKinds {
kind := gvk.Kind
if customEntries[kind] == nil {
customEntries[kind] = []string{fmt.Sprintf("%s/%s", gvk.Group, gvk.Version)}
} else {
customEntries[kind] = append(customEntries[kind], fmt.Sprintf("%s/%s", gvk.Group, gvk.Version))
}
}

if err := t.Execute(&tpl, customEntries); err != nil {
return nil, fmt.Errorf("failed to render template %s: %w", fileName, err)
}

Expand Down
66 changes: 50 additions & 16 deletions pkg/rules/rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"path/filepath"
"testing"
"strings"

"k8s.io/apimachinery/pkg/runtime/schema"
)
Expand All @@ -18,6 +19,9 @@ func TestFetchRules(t *testing.T) {
}
return nil
})
if err != nil {
t.Errorf("Failed to filenames with: %s", err)
}

rules, err := FetchRegoRules([]schema.GroupVersionKind{})
if err != nil {
Expand All @@ -38,14 +42,8 @@ func TestFetchRulesWithAdditionalResources(t *testing.T) {
}
return nil
})

additionalKindsStr := []string{
"ManagedCertificate.v1.networking.gke.io",
"Fake.v1beta.example.com"}
var additionalKinds []schema.GroupVersionKind
for _, ar := range additionalKindsStr {
gvr, _ := schema.ParseKindArg(ar)
additionalKinds = append(additionalKinds, *gvr)
if err != nil {
t.Errorf("Failed to filenames with: %s", err)
}

rules, err := FetchRegoRules([]schema.GroupVersionKind{})
Expand All @@ -68,39 +66,39 @@ func TestRenderRuleRego(t *testing.T) {
t.Errorf("Failed to render rule %s: %s", fileName, err)
}

if bytes.Compare(inputData, outputData) != 0 {
if !bytes.Equal(inputData, outputData) {
t.Errorf("expected the input to be same as output")
}
}

func TestRenderRuleTmpl(t *testing.T) {
additionalResources := []schema.GroupVersionKind{
schema.GroupVersionKind{
{
Group: "example.com",
Version: "v2",
Kind: "Test",
},
}
fileName := "test.tmpl"
inputData := []byte("{{- range . }}" +
"{{ .Kind }}.{{ .Version }}.{{ .Group }}" +
inputData := []byte("{{- range $key, $value := . }}" +
"{{ $key }}: {{range $value}}{{.}}{{end}}" +
"{{- end }}")
expectedData := []byte("Test.v2.example.com")
expectedData := []byte("Test: example.com/v2")

outputData, err := renderRule(inputData, fileName, additionalResources)
if err != nil {
t.Errorf("failed to render rule %s: %s", fileName, err)
}

if bytes.Compare(expectedData, outputData) != 0 {
if !bytes.Equal(expectedData, outputData) {
t.Errorf("result does not match expected output, expected: %s, got: %s", expectedData, outputData)
}
}

func TestRenderRuleTmplFail(t *testing.T) {
fileName := "test.tmpl"
inputData := []byte("{{- rangeasd . }}" +
"{{ .Kind }}.{{ .Version }}.{{ Group }}" +
inputData := []byte("{{- rangeasd $key, $value := . }}" +
"{{ $key }}: {{range $value}}{{.}}{{end}}" +
"{{- end }}")

_, err := renderRule(inputData, fileName, []schema.GroupVersionKind{})
Expand All @@ -109,6 +107,42 @@ func TestRenderRuleTmplFail(t *testing.T) {
}
}

func TestRenderMultipleResources(t *testing.T) {
additionalResources := []schema.GroupVersionKind{
{
Group: "example.com",
Version: "v2",
Kind: "Test",
},
{
Group: "example.com",
Version: "v3",
Kind: "Test",
},
{
Group: "example.com",
Version: "v2",
Kind: "Result",
},
}
fileName := "test.tmpl"
inputData := []byte("{{- range $key, $value := . }}" +
"{{ $key }}: {{range $value}}{{.}} {{end}}" +
"{{- end }}")
expectedData := []byte("Result: example.com/v2 Test: example.com/v2 example.com/v3")

outputData, err := renderRule(inputData, fileName, additionalResources)
if err != nil {
t.Errorf("failed to render rule %s: %s", fileName, err)
}

outputData = []byte(strings.TrimSpace(string(outputData)))

if !bytes.Equal(expectedData, outputData) {
t.Errorf("result does not match expected output, expected: %s, got: %s", expectedData, outputData)
}
}

func TestRenderRuleUnknownFail(t *testing.T) {
inputData := []byte("some input")
fileName := "test.txt"
Expand Down
Loading