-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add configurable serviceSelector instead of hardcoded labels (#57)
Allow a component to configure the label selectors used to select the services that should be exposable. This allows Platform Routing to operator on an unchanged target. ServiceSelectors also allow templating of the key and value in the configuration to extract data from the Target CR resource. Example: "routing.opendatahub.io/{{.kind}}": "{{.metadata.name}}", // > "routing.opendatahub.io/Service": "MyService" > [!CAUTION] > the target CR is represented as a map[string]any with lowercase > so in contrast to the Object representation {{ .Name }} it's required to > write the full object path as {{ .metadata.name }} --------- Co-authored-by: Bartosz Majsak <[email protected]>
- Loading branch information
1 parent
53cdac1
commit 1df2c4a
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