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

Fix diff for CRD .spec.preserveUnknownFields #2506

Merged
merged 1 commit into from
Jul 20, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Unreleased

- Gracefully handle undefined resource schemes (https://github.com/pulumi/pulumi-kubernetes/pull/2504)
- Fix diff for CRD .spec.preserveUnknownFields (https://github.com/pulumi/pulumi-kubernetes/pull/2506)

## 4.0.0 (July 19, 2023)

Expand Down
19 changes: 19 additions & 0 deletions provider/pkg/clients/unstructured.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ func ToUnstructured(object metav1.Object) (*unstructured.Unstructured, error) {
// 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) {
if IsCRD(uns) {
return normalizeCRD(uns), nil
}

obj, err := FromUnstructured(uns)
// Return the input resource rather than an error if this operation fails.
if err != nil {
Expand All @@ -73,6 +77,21 @@ func Normalize(uns *unstructured.Unstructured) (*unstructured.Unstructured, erro
return ToUnstructured(obj)
}

// normalizeCRD manually normalizes CRD resources, which require special handling due to the lack of defined conversion
// scheme for CRDs.
func normalizeCRD(uns *unstructured.Unstructured) *unstructured.Unstructured {
contract.Assertf(IsCRD(uns), "normalizeCRD called on a non-CRD resource: %s", uns.GetAPIVersion())

// .spec.preserveUnknownFields is deprecated, and will be removed by the apiserver on the created resource if the
// value is false. Normalize for diffing by removing this field if present and set to "false".
// See https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#field-pruning
preserve, found, err := unstructured.NestedBool(uns.Object, "spec", "preserveUnknownFields")
if err == nil && found && !preserve {
unstructured.RemoveNestedField(uns.Object, "spec", "preserveUnknownFields")
}
return uns
}

func PodFromUnstructured(uns *unstructured.Unstructured) (*corev1.Pod, error) {
const expectedAPIVersion = "v1"

Expand Down
90 changes: 90 additions & 0 deletions provider/pkg/clients/unstructured_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,95 @@ var (
},
},
}

crdPreserveUnknownFieldsUnstructured = &unstructured.Unstructured{
Object: map[string]any{
"apiVersion": "apiextensions.k8s.io/v1",
"kind": "CustomResourceDefinition",
"metadata": map[string]any{
"name": "foobars.stable.example.com",
},
"spec": map[string]any{
"group": "stable.example.com",
"names": map[string]any{
"kind": "FooBar",
"plural": "foobars",
"shortNames": []string{
"fb",
},
"singular": "foobar",
},
"preserveUnknownFields": false,
"scope": "Namespaced",
"versions": []map[string]any{
{
"name": "v1",
"schema": map[string]any{
"openAPIV3Schema": map[string]any{
"properties": map[string]any{
"spec": map[string]any{
"properties": map[string]any{
"foo": map[string]any{
"type": "string",
},
},
"type": "object",
},
},
"type": "object",
},
},
"served": true,
"storage": true,
},
},
},
},
}

crdUnstructured = &unstructured.Unstructured{
Object: map[string]any{
"apiVersion": "apiextensions.k8s.io/v1",
"kind": "CustomResourceDefinition",
"metadata": map[string]any{
"name": "foobars.stable.example.com",
},
"spec": map[string]any{
"group": "stable.example.com",
"names": map[string]any{
"kind": "FooBar",
"plural": "foobars",
"shortNames": []string{
"fb",
},
"singular": "foobar",
},
"scope": "Namespaced",
"versions": []map[string]any{
{
"name": "v1",
"schema": map[string]any{
"openAPIV3Schema": map[string]any{
"properties": map[string]any{
"spec": map[string]any{
"properties": map[string]any{
"foo": map[string]any{
"type": "string",
},
},
"type": "object",
},
},
"type": "object",
},
},
"served": true,
"storage": true,
},
},
},
},
}
)

func TestFromUnstructured(t *testing.T) {
Expand Down Expand Up @@ -159,6 +248,7 @@ func TestNormalize(t *testing.T) {
wantErr bool
}{
{"unregistered GVK", args{uns: unregisteredGVK}, unregisteredGVK, false},
{"CRD with preserveUnknownFields", args{uns: crdPreserveUnknownFieldsUnstructured}, crdUnstructured, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions provider/pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2684,9 +2684,9 @@ func pruneLiveState(live, oldInputs *unstructured.Unstructured) *unstructured.Un
return oldLivePruned
}

// shouldNormalize returns false for CRDs and CustomResources, and true otherwise.
// shouldNormalize returns false for CustomResources, and true otherwise.
func shouldNormalize(uns *unstructured.Unstructured) bool {
return !clients.IsCRD(uns) && kinds.KnownGroupVersions.Has(uns.GetAPIVersion())
return kinds.KnownGroupVersions.Has(uns.GetAPIVersion())
}

// normalize converts an Unstructured resource into a normalized form so that semantically equivalent representations
Expand Down
Loading