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

Gracefully handle undefined resource schemes #2504

Merged
merged 2 commits into from
Jul 19, 2023
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Unreleased

- Gracefully handle undefined resource schemes (https://github.com/pulumi/pulumi-kubernetes/pull/2504)

## 4.0.0 (July 19, 2023)

Breaking changes:
Expand Down
4 changes: 3 additions & 1 deletion provider/pkg/clients/unstructured.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,12 @@ func ToUnstructured(object metav1.Object) (*unstructured.Unstructured, error) {

// Normalize converts an Unstructured Kubernetes resource into the typed equivalent and then back to Unstructured.
// This process normalizes semantically-equivalent resources into an identical output, which is important for diffing.
// If the scheme is not defined, then return the original resource.
func Normalize(uns *unstructured.Unstructured) (*unstructured.Unstructured, error) {
obj, err := FromUnstructured(uns)
// Return the input resource rather than an error if this operation fails.
if err != nil {
return nil, err
return uns, nil
}
return ToUnstructured(obj)
}
Expand Down
26 changes: 26 additions & 0 deletions provider/pkg/clients/unstructured_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,29 @@ func TestFromUnstructured(t *testing.T) {
})
}
}

func TestNormalize(t *testing.T) {
type args struct {
uns *unstructured.Unstructured
}
tests := []struct {
name string
args args
want *unstructured.Unstructured
wantErr bool
}{
{"unregistered GVK", args{uns: unregisteredGVK}, unregisteredGVK, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := Normalize(tt.args.uns)
if (err != nil) != tt.wantErr {
t.Errorf("Normalize() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Normalize() got = %v, want %v", got, tt.want)
}
})
}
}