Skip to content

Commit

Permalink
Gracefully handle undefined resource schemes (#2504)
Browse files Browse the repository at this point in the history
The provider includes normalization logic that converts Unstructured resource definitions to their typed equivalents and back. This conversion process requires a defined scheme, and will not work for extension resource types (CRDs, CustomResources, apiregistration, etc). If the scheme is not registered, skip this process rather than returning an error.
  • Loading branch information
lblackstone committed Jul 19, 2023
1 parent 7b7e29b commit ed16a55
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
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)
}
})
}
}

0 comments on commit ed16a55

Please sign in to comment.