Skip to content

Commit 9f56540

Browse files
committed
Fixes unstructured for patches and application transform
1 parent 24073d9 commit 9f56540

File tree

2 files changed

+46
-18
lines changed

2 files changed

+46
-18
lines changed

pkg/patterns/addon/application.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"errors"
2222
"fmt"
2323

24+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2425
addonsv1alpha1 "sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/addon/pkg/apis/v1alpha1"
2526
"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/declarative"
2627
"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/declarative/pkg/manifest"
@@ -42,8 +43,22 @@ const (
4243

4344
// TransformApplicationFromStatus modifies the Application in the deployment based off the CommonStatus
4445
func TransformApplicationFromStatus(ctx context.Context, instance declarative.DeclarativeObject, objects *manifest.Objects) error {
45-
addonObject, ok := instance.(addonsv1alpha1.CommonObject)
46-
if !ok {
46+
var version string
47+
var healthy bool
48+
var addonObject addonsv1alpha1.CommonObject
49+
50+
if unstruct, ok := instance.(*unstructured.Unstructured); ok {
51+
v, _, err := unstructured.NestedString(unstruct.Object, "spec", "version")
52+
if err != nil {
53+
return fmt.Errorf("unable to get version from unstuctured: %v", err)
54+
}
55+
version = v
56+
57+
healthy, _, err = unstructured.NestedBool(unstruct.Object, "spec", "healthy")
58+
} else if addonObject, ok = instance.(addonsv1alpha1.CommonObject); ok {
59+
version = addonObject.CommonSpec().Version
60+
healthy = addonObject.GetCommonStatus().Healthy
61+
} else {
4762
return fmt.Errorf("instance %T was not an addonsv1alpha1.CommonObject", instance)
4863
}
4964

@@ -56,12 +71,12 @@ func TransformApplicationFromStatus(ctx context.Context, instance declarative.De
5671
}
5772

5873
assemblyPhase := Pending
59-
if addonObject.GetCommonStatus().Healthy {
74+
if healthy {
6075
assemblyPhase = Succeeded
6176
}
6277

6378
// TODO: Version should be on CommonStatus as well
64-
app.SetNestedField(addonObject.CommonSpec().Version, "spec", "descriptor", "version")
79+
app.SetNestedField(version, "spec", "descriptor", "version")
6580
app.SetNestedField(assemblyPhase, "spec", "assemblyPhase")
6681

6782
return nil

pkg/patterns/addon/patch.go

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,25 +35,38 @@ import (
3535
func ApplyPatches(ctx context.Context, object declarative.DeclarativeObject, objects *manifest.Objects) error {
3636
log := log.Log
3737

38-
p, ok := object.(addonsv1alpha1.Patchable)
39-
if !ok {
40-
return fmt.Errorf("provided object (%T) does not implement Patchable type", object)
41-
}
42-
38+
var p addonsv1alpha1.Patchable
4339
var patches []*unstructured.Unstructured
4440

45-
for _, p := range p.PatchSpec().Patches {
46-
// Object is nil, Raw is populated (with json, even when input was yaml)
47-
r := bytes.NewReader(p.Raw)
48-
decoder := yaml.NewYAMLOrJSONDecoder(r, 1024)
49-
patch := &unstructured.Unstructured{}
41+
unstruct, ok := object.(*unstructured.Unstructured)
42+
if ok {
43+
patch, _, err := unstructured.NestedSlice(unstruct.Object, "spec", "patches")
44+
if err != nil {
45+
return fmt.Errorf("unable to get patches from unstruct: %v", err)
46+
}
5047

51-
if err := decoder.Decode(patch); err != nil {
52-
return fmt.Errorf("error parsing json into unstructured object: %v", err)
48+
for _, p := range patch {
49+
m := p.(map[string]interface{})
50+
patches = append(patches, &unstructured.Unstructured{
51+
Object: m,
52+
})
5353
}
54-
log.WithValues("patch", patch).V(1).Info("parsed patch")
54+
} else if p, ok = object.(addonsv1alpha1.Patchable); ok{
55+
for _, p := range p.PatchSpec().Patches {
56+
// Object is nil, Raw is populated (with json, even when input was yaml)
57+
r := bytes.NewReader(p.Raw)
58+
decoder := yaml.NewYAMLOrJSONDecoder(r, 1024)
59+
patch := &unstructured.Unstructured{}
5560

56-
patches = append(patches, patch)
61+
if err := decoder.Decode(patch); err != nil {
62+
return fmt.Errorf("error parsing json into unstructured object: %v", err)
63+
}
64+
log.WithValues("patch", patch).V(1).Info("parsed patch")
65+
66+
patches = append(patches, patch)
67+
}
68+
} else {
69+
return fmt.Errorf("provided object (%T) does not implement Patchable type", object)
5770
}
5871

5972
return objects.Patch(patches)

0 commit comments

Comments
 (0)