generated from SAP/repository-template
-
Couldn't load subscription status.
- Fork 2
fix(projection): nested value retrieval with jsonpath selectors #81
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
Merged
christophrj
merged 3 commits into
openmcp-project:main
from
christophrj:fix/projection-for-non-scalar-fields
Sep 24, 2025
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,40 @@ | ||
| package orchestrator | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
|
|
||
| "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
| "k8s.io/client-go/util/jsonpath" | ||
| ) | ||
|
|
||
| // nestedPrimitiveValue returns a string value based on the result of the client-go JSONPath parser. | ||
| // Returns false if the value is not found. | ||
| // Returns an error if the value is ambiguous or a collection type. | ||
| // Returns an error if the given path can't be parsed. | ||
| // | ||
| // String conversion of non-string primitives relies on the default format when printing the value. | ||
| // The input path is expected to be passed in dot-notation without brackets or a leading dot. | ||
| // The implementation is based on similar internal client-go jsonpath usages, like kubectl | ||
| func nestedPrimitiveValue(obj unstructured.Unstructured, path string) (string, bool, error) { | ||
| jp := jsonpath.New("projection").AllowMissingKeys(true) | ||
| if err := jp.Parse(fmt.Sprintf("{.%s}", path)); err != nil { | ||
| return "", false, fmt.Errorf("failed to parse path: %v", err) | ||
| } | ||
| results, err := jp.FindResults(obj.UnstructuredContent()) | ||
| if err != nil { | ||
| return "", false, fmt.Errorf("failed to find results: %v", err) | ||
| } | ||
| if len(results) == 0 || len(results[0]) == 0 { | ||
| return "", false, nil | ||
| } | ||
| if len(results) > 1 || len(results[0]) > 1 { | ||
| return "", true, errors.New("fieldPath matches more than one value which is not supported") | ||
| } | ||
| value := results[0][0] | ||
| switch value.Interface().(type) { | ||
| case map[string]interface{}, []interface{}: | ||
| return "", true, errors.New("fieldPath results in collection type which is not supported") | ||
| } | ||
| return fmt.Sprintf("%v", value.Interface()), true, nil | ||
| } |
This file contains hidden or 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,156 @@ | ||
| package orchestrator | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
| "sigs.k8s.io/yaml" | ||
| ) | ||
|
|
||
| const subaccountCR = ` | ||
| apiVersion: account.btp.sap.crossplane.io/v1alpha1 | ||
| kind: Subaccount | ||
| metadata: | ||
| annotations: | ||
| crossplane.io/external-name: test-subaccount | ||
| name: test-subaccount | ||
| spec: | ||
| deletionPolicy: Delete | ||
| status: | ||
| conditions: | ||
| - lastTransitionTime: "2025-09-12T15:57:41Z" | ||
| observedGeneration: 1 | ||
| reason: ReconcileSuccess | ||
| status: "True" | ||
| type: Synced | ||
| - lastTransitionTime: "2025-09-09T14:33:38Z" | ||
| reason: Available | ||
| status: "True" | ||
| type: Ready | ||
| ` | ||
|
|
||
| func TestNestedPrimitiveValue(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| resourceYaml string | ||
| path string | ||
| wantValue string | ||
| wantFound bool | ||
| wantError bool | ||
| }{ | ||
| { | ||
| name: "top level value retrieval", | ||
| resourceYaml: subaccountCR, | ||
| path: "kind", | ||
| wantValue: "Subaccount", | ||
| wantFound: true, | ||
| wantError: false, | ||
| }, | ||
| { | ||
| name: "nested value retrieval with name selector", | ||
| resourceYaml: subaccountCR, | ||
| path: "spec.deletionPolicy", | ||
| wantValue: "Delete", | ||
| wantFound: true, | ||
| wantError: false, | ||
| }, | ||
| { | ||
| name: "nested value retrieval with escaped name selector", | ||
| resourceYaml: subaccountCR, | ||
| path: "metadata.annotations.crossplane\\.io/external-name", | ||
| wantValue: "test-subaccount", | ||
| wantFound: true, | ||
| wantError: false, | ||
| }, | ||
| { | ||
| name: "nested value retrieval with index selector", | ||
| resourceYaml: subaccountCR, | ||
| path: "status.conditions[1].status", | ||
| wantValue: "True", | ||
| wantFound: true, | ||
| wantError: false, | ||
| }, | ||
| { | ||
| name: "nested value retrieval with filter selector", | ||
| resourceYaml: subaccountCR, | ||
| path: "status.conditions[?(@.type=='Ready')].status", | ||
| wantValue: "True", | ||
| wantFound: true, | ||
| wantError: false, | ||
| }, | ||
| { | ||
| name: "nested value retrieval with array slice selector", | ||
| resourceYaml: subaccountCR, | ||
| path: "status.conditions[0:1].status", | ||
| wantValue: "True", | ||
| wantFound: true, | ||
| wantError: false, | ||
| }, | ||
| { | ||
| name: "non-existent value", | ||
| resourceYaml: subaccountCR, | ||
| path: "metadata.labels.app", | ||
| wantValue: "", | ||
| wantFound: false, | ||
| wantError: false, | ||
| }, | ||
| { | ||
| name: "nested non-string value retrieval with default print format", | ||
| resourceYaml: subaccountCR, | ||
| path: "status.conditions[0].observedGeneration", | ||
| wantValue: "1", | ||
| wantFound: true, | ||
| wantError: false, | ||
| }, | ||
| { | ||
| name: "retrieval of collection types is not supported", | ||
| resourceYaml: subaccountCR, | ||
| path: "status.conditions[0]", | ||
| wantValue: "", | ||
| wantFound: true, | ||
| wantError: true, | ||
| }, | ||
| { | ||
| name: "invalid array index returns an error", | ||
| resourceYaml: subaccountCR, | ||
| path: "status.conditions[abc].status", | ||
| wantValue: "", | ||
| wantFound: false, | ||
| wantError: true, | ||
| }, | ||
| { | ||
| name: "invalid path syntax returns an error", | ||
| resourceYaml: subaccountCR, | ||
| path: "$.[status.conditions[0].status]", | ||
| wantValue: "", | ||
| wantFound: false, | ||
| wantError: true, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| obj := toUnstructured(t, tt.resourceYaml) | ||
| value, ok, err := nestedPrimitiveValue(obj, tt.path) | ||
|
|
||
| if (err != nil) != tt.wantError { | ||
| t.Errorf("unexpected error: got %v, wantErr %v", err, tt.wantError) | ||
| } | ||
| if ok != tt.wantFound { | ||
| t.Errorf("unexpected ok result: got %v, want %v", ok, tt.wantFound) | ||
| } | ||
| if value != tt.wantValue { | ||
| t.Errorf("unexpected value: got %v, want %v", value, tt.wantValue) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func toUnstructured(t *testing.T, resourceYaml string) unstructured.Unstructured { | ||
| t.Helper() | ||
| var object map[string]interface{} | ||
| if err := yaml.Unmarshal([]byte(resourceYaml), &object); err != nil { | ||
| t.Fatalf("failed to unmarshal YAML: %v", err) | ||
| } | ||
| return unstructured.Unstructured{Object: object} | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.