Skip to content

Commit

Permalink
ssa: deprecate SetNativeKindsDefaults
Browse files Browse the repository at this point in the history
In favor of the newly added `NormalizeUnstructuredList`.

Signed-off-by: Hidde Beydals <[email protected]>
  • Loading branch information
hiddeco committed Oct 6, 2023
1 parent bcecc4b commit 9cb0840
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 17 deletions.
8 changes: 4 additions & 4 deletions ssa/manager_apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ func TestApply_SetNativeKindsDefaults(t *testing.T) {

manager.SetOwnerLabels(objects, "app1", "default")

if err := SetNativeKindsDefaults(objects); err != nil {
if err := NormalizeUnstructuredList(objects); err != nil {
t.Fatal(err)
}

Expand Down Expand Up @@ -417,7 +417,7 @@ func TestApply_NoOp(t *testing.T) {

manager.SetOwnerLabels(objects, "app1", "default")

if err := SetNativeKindsDefaults(objects); err != nil {
if err := NormalizeUnstructuredList(objects); err != nil {
t.Fatal(err)
}

Expand Down Expand Up @@ -639,7 +639,7 @@ func TestApply_Cleanup(t *testing.T) {

_, deployObject := getFirstObject(objects, "Deployment", id)

if err := SetNativeKindsDefaults(objects); err != nil {
if err = NormalizeUnstructuredList(objects); err != nil {
t.Fatal(err)
}

Expand Down Expand Up @@ -897,7 +897,7 @@ func TestApply_Cleanup_Exclusions(t *testing.T) {

_, deployObject := getFirstObject(objects, "Deployment", id)

if err := SetNativeKindsDefaults(objects); err != nil {
if err = NormalizeUnstructuredList(objects); err != nil {
t.Fatal(err)
}

Expand Down
18 changes: 12 additions & 6 deletions ssa/manager_diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,17 @@ func TestDiff(t *testing.T) {
t.Run("generates diff for replaced key in stringData secret", func(t *testing.T) {
// create a new stringData secret
sec := secret.DeepCopy()
if err := unstructured.SetNestedField(sec.Object, generateName("diff"), "metadata", "name"); err != nil {
if err = unstructured.SetNestedField(sec.Object, generateName("diff"), "metadata", "name"); err != nil {
t.Fatal(err)
}

// copy the secret to simulate a replace of key
diffSecret := sec.DeepCopy()

// apply stringData conversion
SetNativeKindsDefaults([]*unstructured.Unstructured{sec})
if err = NormalizeUnstructured(sec); err != nil {
t.Fatal(err)
}

if _, err = manager.Apply(ctx, sec, DefaultApplyOptions()); err != nil {
t.Fatal(err)
Expand All @@ -108,13 +110,14 @@ func TestDiff(t *testing.T) {
unstructured.RemoveNestedField(diffSecret.Object, "stringData", "key")

newKey := "key.new"
err = unstructured.SetNestedField(diffSecret.Object, newVal, "stringData", newKey)
if err != nil {
if err = unstructured.SetNestedField(diffSecret.Object, newVal, "stringData", newKey); err != nil {
t.Fatal(err)
}

// apply stringData conversion
SetNativeKindsDefaults([]*unstructured.Unstructured{diffSecret})
if err = NormalizeUnstructured(diffSecret); err != nil {
t.Fatal(err)
}

_, liveObj, mergedObj, err := manager.Diff(ctx, diffSecret, DefaultDiffOptions())
if err != nil {
Expand Down Expand Up @@ -234,7 +237,10 @@ func TestDiff_Removals(t *testing.T) {
if err != nil {
t.Fatal(err)
}
SetNativeKindsDefaults(objects)

if err = NormalizeUnstructuredList(objects); err != nil {
t.Fatal(err)
}

configMapName, configMap := getFirstObject(objects, "ConfigMap", id)

Expand Down
25 changes: 25 additions & 0 deletions ssa/normalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package ssa

import (
"fmt"

appsv1 "k8s.io/api/apps/v1"
hpav2 "k8s.io/api/autoscaling/v2"
hpav2beta2 "k8s.io/api/autoscaling/v2beta2"
Expand Down Expand Up @@ -68,6 +70,29 @@ func ToUnstructured(object metav1.Object) (*unstructured.Unstructured, error) {
return &unstructured.Unstructured{Object: u}, nil
}

// NormalizeUnstructuredList normalizes a list of Unstructured objects by
// converting them to typed Kubernetes resources, normalizing them, and then
// converting them back to Unstructured objects. It only works for API types
// registered with the default client-go scheme. If the conversion fails, only
// certain standard fields are removed.
func NormalizeUnstructuredList(objects []*unstructured.Unstructured) error {
return NormalizeUnstructuredListWithScheme(objects, defaultScheme)
}

// NormalizeUnstructuredListWithScheme normalizes a list of Unstructured
// objects by converting them to typed Kubernetes resources, normalizing them,
// and then converting them back to Unstructured objects. It only works for API
// types registered with the given scheme. If the conversion fails, only
// certain standard fields are removed.
func NormalizeUnstructuredListWithScheme(objects []*unstructured.Unstructured, scheme *runtime.Scheme) error {
for _, o := range objects {
if err := NormalizeUnstructuredWithScheme(o, scheme); err != nil {
return fmt.Errorf("%s normalization error: %w", FmtUnstructured(o), err)
}
}
return nil
}

// NormalizeUnstructured normalizes an Unstructured object by converting it to
// a typed Kubernetes resource, normalizing it, and then converting it back to
// an Unstructured object. It only works for API types registered with the
Expand Down
10 changes: 3 additions & 7 deletions ssa/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package ssa

import (
"encoding/json"
"fmt"
"io"
"regexp"
"strings"
Expand Down Expand Up @@ -271,13 +270,10 @@ func AnyInMetadata(object *unstructured.Unstructured, metadata map[string]string

// SetNativeKindsDefaults sets default values for native Kubernetes objects,
// working around various upstream Kubernetes API bugs.
//
// Deprecated: use NormalizeUnstructuredList or NormalizeUnstructured instead.
func SetNativeKindsDefaults(objects []*unstructured.Unstructured) error {
for _, u := range objects {
if err := NormalizeUnstructured(u); err != nil {
return fmt.Errorf("%s validation error: %w", FmtUnstructured(u), err)
}
}
return nil
return NormalizeUnstructuredList(objects)
}

func containsItemString(s []string, e string) bool {
Expand Down

0 comments on commit 9cb0840

Please sign in to comment.