forked from maistra/odh-platform
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into handle-export-chg
- Loading branch information
Showing
9 changed files
with
145 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package config | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"strings" | ||
"text/template" | ||
|
||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
) | ||
|
||
// ResolveSelectors uses golang template engine to resolve the expressions in the `selectorExpressions` map using | ||
// `source` as a data input. Both the keys and values are resolved against the source data. | ||
// | ||
// Note: expressions are resolved against the source using lowercase keys | ||
// | ||
// Example source: | ||
// | ||
// kind: Service | ||
// metadata: | ||
// name: MyService | ||
// | ||
// Example selectorExpressions: | ||
// | ||
// map[string]{string} { | ||
// "routing.opendatahub.io/{{.kind}}": "{{.metadata.name}}", // > "routing.opendatahub.io/Service": "MyService" | ||
// } | ||
func ResolveSelectors(selectorExpressions map[string]string, source *unstructured.Unstructured) (map[string]string, error) { | ||
resolved := make(map[string]string, len(selectorExpressions)) | ||
mainTemplate := template.New("unused_name").Option("missingkey=error") | ||
|
||
for key, val := range selectorExpressions { | ||
var err error | ||
|
||
resolvedKey := key | ||
if strings.Contains(key, "{{") { | ||
resolvedKey, err = resolve(mainTemplate, key, source) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not resolve key %s: %w", key, err) | ||
} | ||
} | ||
|
||
resolvedVal := val | ||
if strings.Contains(val, "{{") { | ||
resolvedVal, err = resolve(mainTemplate, val, source) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not resolve value %s: %w", val, err) | ||
} | ||
} | ||
|
||
resolved[resolvedKey] = resolvedVal | ||
} | ||
|
||
return resolved, nil | ||
} | ||
|
||
func resolve(templ *template.Template, textTemplate string, source *unstructured.Unstructured) (string, error) { | ||
tmpl, err := templ.Parse(textTemplate) | ||
if err != nil { | ||
return "", fmt.Errorf("could not parse template: %w", err) | ||
} | ||
|
||
var buff bytes.Buffer | ||
|
||
if err := tmpl.Execute(&buff, source.Object); err != nil { | ||
return "", fmt.Errorf("could not execute template: %w", err) | ||
} | ||
|
||
return buff.String(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package config_test | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
"github.com/opendatahub-io/odh-platform/pkg/config" | ||
"github.com/opendatahub-io/odh-platform/test" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
) | ||
|
||
var _ = Describe("Templated selectors", test.Unit(), func() { | ||
|
||
Context("simple expressions", func() { | ||
|
||
It("should resolve simple expressions for both key and value", func() { | ||
labels := map[string]string{ | ||
"A.{{.kind}}": "{{.metadata.name}}", | ||
"B": "{{.kind}}", | ||
} | ||
|
||
target := unstructured.Unstructured{ | ||
Object: map[string]any{}, | ||
} | ||
target.SetName("X") | ||
target.SetKind("Y") | ||
|
||
renderedLabels, err := config.ResolveSelectors(labels, &target) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
Expect(renderedLabels["A.Y"]).To(Equal("X")) | ||
Expect(renderedLabels["B"]).To(Equal("Y")) | ||
}) | ||
|
||
It("should fail on missing expression", func() { | ||
labels := map[string]string{ | ||
"A": "{{.metadata.name}}", | ||
} | ||
|
||
target := unstructured.Unstructured{ | ||
Object: map[string]any{}, | ||
} | ||
target.SetKind("Y") | ||
|
||
_, err := config.ResolveSelectors(labels, &target) | ||
Expect(err).To(HaveOccurred()) | ||
Expect(err.Error()).To(ContainSubstring("could not execute template")) | ||
Expect(err.Error()).To(ContainSubstring("could not resolve value")) | ||
}) | ||
}) | ||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters