Skip to content

Commit 5ae7327

Browse files
committed
Moved the type-check around
1 parent c3a2e08 commit 5ae7327

File tree

3 files changed

+28
-19
lines changed

3 files changed

+28
-19
lines changed

pkg/patterns/addon/application.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ func TransformApplicationFromStatus(ctx context.Context, instance declarative.De
5454
}
5555
version = v
5656

57-
healthy, _, err = unstructured.NestedBool(unstruct.Object, "spec", "healthy")
57+
healthy, _, err = unstructured.NestedBool(unstruct.Object, "status", "healthy")
58+
if err != nil {
59+
return fmt.Errorf("unable to get status from unstuctured: %v", err)
60+
}
5861
} else if addonObject, ok = instance.(addonsv1alpha1.CommonObject); ok {
5962
version = addonObject.CommonSpec().Version
6063
healthy = addonObject.GetCommonStatus().Healthy

pkg/patterns/addon/patch.go

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

38-
var p addonsv1alpha1.Patchable
3938
var patches []*unstructured.Unstructured
4039

4140
unstruct, ok := object.(*unstructured.Unstructured)
4241
if ok {
4342
patch, _, err := unstructured.NestedSlice(unstruct.Object, "spec", "patches")
4443
if err != nil {
45-
return fmt.Errorf("unable to get patches from unstruct: %v", err)
44+
return fmt.Errorf("unable to get patches from unstructured: %v", err)
4645
}
4746

4847
for _, p := range patch {
@@ -51,7 +50,7 @@ func ApplyPatches(ctx context.Context, object declarative.DeclarativeObject, obj
5150
Object: m,
5251
})
5352
}
54-
} else if p, ok = object.(addonsv1alpha1.Patchable); ok{
53+
} else if p, ok := object.(addonsv1alpha1.Patchable); ok{
5554
for _, p := range p.PatchSpec().Patches {
5655
// Object is nil, Raw is populated (with json, even when input was yaml)
5756
r := bytes.NewReader(p.Raw)

pkg/patterns/addon/pkg/status/aggregate.go

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,23 +47,9 @@ type aggregator struct {
4747
func (a *aggregator) Reconciled(ctx context.Context, src declarative.DeclarativeObject, objs *manifest.Objects) error {
4848
log := log.Log
4949

50-
unstruct, ok := src.(*unstructured.Unstructured)
51-
instance, commonOkay := src.(addonv1alpha1.CommonObject)
52-
53-
unstructStatus := make(map[string]interface{})
54-
var status addonv1alpha1.CommonStatus
55-
5650
statusHealthy := true
5751
statusErrors := []string{}
5852

59-
if ok{
60-
unstructStatus["Healthy"] = true
61-
} else if commonOkay {
62-
status = addonv1alpha1.CommonStatus{Healthy: true}
63-
} else {
64-
return fmt.Errorf("object %T was not an addonv1alpha1.CommonObject", src)
65-
}
66-
6753
for _, o := range objs.Items {
6854
gk := o.Group + "/" + o.Kind
6955
healthy := true
@@ -84,7 +70,21 @@ func (a *aggregator) Reconciled(ctx context.Context, src declarative.Declarative
8470
}
8571
}
8672

87-
log.WithValues("object", src).WithValues("status", status).V(2).Info("built status")
73+
log.WithValues("object", src).WithValues("status", statusHealthy).V(2).Info("built status")
74+
75+
unstruct, ok := src.(*unstructured.Unstructured)
76+
instance, commonOkay := src.(addonv1alpha1.CommonObject)
77+
78+
unstructStatus := make(map[string]interface{})
79+
var status addonv1alpha1.CommonStatus
80+
81+
if ok{
82+
unstructStatus["Healthy"] = true
83+
} else if commonOkay {
84+
status = addonv1alpha1.CommonStatus{Healthy: true}
85+
} else {
86+
return fmt.Errorf("object %T was not an addonv1alpha1.CommonObject", src)
87+
}
8888

8989
if commonOkay {
9090
status.Errors = statusErrors
@@ -111,6 +111,11 @@ func (a *aggregator) Reconciled(ctx context.Context, src declarative.Declarative
111111
}
112112
if !reflect.DeepEqual(status, s) {
113113
err = unstructured.SetNestedField(unstruct.Object, statusHealthy, "status", "healthy")
114+
if err != nil {
115+
log.Error(err, "updating status")
116+
return fmt.Errorf("unable to set status in unstructured", err)
117+
}
118+
114119
err = unstructured.SetNestedStringSlice(unstruct.Object, statusErrors, "status", "errors")
115120
if err != nil {
116121
log.Error(err, "updating status")
@@ -127,6 +132,8 @@ func (a *aggregator) Reconciled(ctx context.Context, src declarative.Declarative
127132
}
128133
}
129134

135+
136+
130137
return nil
131138
}
132139

0 commit comments

Comments
 (0)