Skip to content

Commit 24073d9

Browse files
committed
Handles both unstructured and commonObject
1 parent a32c56c commit 24073d9

File tree

3 files changed

+88
-24
lines changed

3 files changed

+88
-24
lines changed

pkg/patterns/addon/pkg/loaders/fs.go

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"flag"
2222
"fmt"
23+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2324
"strings"
2425

2526
"k8s.io/apimachinery/pkg/runtime"
@@ -53,16 +54,36 @@ func NewManifestLoader(channel string) (*ManifestLoader, error) {
5354
func (c *ManifestLoader) ResolveManifest(ctx context.Context, object runtime.Object) (map[string]string, error) {
5455
log := log.Log
5556

56-
addonObject, ok := object.(addonsv1alpha1.CommonObject)
57-
if !ok {
58-
return nil, fmt.Errorf("object %T was not an addonsv1alpha1.CommonObject", object)
59-
}
57+
var (
58+
channelName string
59+
version string
60+
componentName string
61+
)
6062

61-
componentName := addonObject.ComponentName()
63+
unstruct, ok := object.(*unstructured.Unstructured)
64+
if ok {
65+
v, _, err := unstructured.NestedString(unstruct.Object, "spec", "version")
66+
if err != nil {
67+
return nil, fmt.Errorf("unable to get spec.version: %v", err)
68+
}
69+
version = v
6270

63-
spec := addonObject.CommonSpec()
64-
version := spec.Version
65-
channelName := spec.Channel
71+
c, _, err := unstructured.NestedString(unstruct.Object, "spec", "channel")
72+
if err != nil {
73+
return nil, fmt.Errorf("unable to get spec.version: %v", err)
74+
}
75+
channelName = c
76+
77+
componentName = strings.ToLower(unstruct.GetKind())
78+
} else if addonObject, ok := object.(addonsv1alpha1.CommonObject); ok {
79+
componentName = addonObject.ComponentName()
80+
81+
spec := addonObject.CommonSpec()
82+
version = spec.Version
83+
channelName = spec.Channel
84+
} else {
85+
return nil, fmt.Errorf("object %T was not an addonsv1alpha1.CommonObject", object)
86+
}
6687

6788
// TODO: We should actually do id (1.1.2-aws or 1.1.1-nginx). But maybe YAGNI
6889
id := version

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

Lines changed: 57 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package status
1919
import (
2020
"context"
2121
"fmt"
22+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2223
"reflect"
2324

2425
appsv1 "k8s.io/api/apps/v1"
@@ -46,50 +47,90 @@ type aggregator struct {
4647
func (a *aggregator) Reconciled(ctx context.Context, src declarative.DeclarativeObject, objs *manifest.Objects) error {
4748
log := log.Log
4849

49-
instance, ok := src.(addonv1alpha1.CommonObject)
50-
if !ok {
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+
56+
statusHealthy := true
57+
statusErrors := []string{}
58+
59+
if ok{
60+
unstructStatus["Healthy"] = true
61+
} else if commonOkay {
62+
status = addonv1alpha1.CommonStatus{Healthy: true}
63+
} else {
5164
return fmt.Errorf("object %T was not an addonv1alpha1.CommonObject", src)
5265
}
5366

54-
status := addonv1alpha1.CommonStatus{Healthy: true}
55-
5667
for _, o := range objs.Items {
5768
gk := o.Group + "/" + o.Kind
5869
healthy := true
5970
var err error
6071
switch gk {
6172
case "/Service":
62-
healthy, err = a.service(ctx, instance, o.Name)
73+
healthy, err = a.service(ctx, src, o.Name)
6374
case "extensions/Deployment", "apps/Deployment":
64-
healthy, err = a.deployment(ctx, instance, o.Name)
75+
healthy, err = a.deployment(ctx, src, o.Name)
6576
default:
6677
log.WithValues("type", gk).V(2).Info("type not implemented for status aggregation, skipping")
6778
}
6879

69-
status.Healthy = status.Healthy && healthy
80+
81+
statusHealthy = statusHealthy && healthy
7082
if err != nil {
71-
status.Errors = append(status.Errors, fmt.Sprintf("%v", err))
83+
statusErrors = append(statusErrors, fmt.Sprintf("%v", err))
7284
}
7385
}
7486

7587
log.WithValues("object", src).WithValues("status", status).V(2).Info("built status")
7688

77-
if !reflect.DeepEqual(status, instance.GetCommonStatus()) {
78-
instance.SetCommonStatus(status)
89+
if commonOkay {
90+
status.Errors = statusErrors
91+
status.Healthy = statusHealthy
92+
93+
if !reflect.DeepEqual(status, instance.GetCommonStatus()) {
94+
instance.SetCommonStatus(status)
7995

80-
log.WithValues("name", instance.GetName()).WithValues("status", status).Info("updating status")
96+
log.WithValues("name", instance.GetName()).WithValues("status", status).Info("updating status")
8197

82-
err := a.client.Update(ctx, instance)
98+
err := a.client.Update(ctx, instance)
99+
if err != nil {
100+
log.Error(err, "updating status")
101+
return err
102+
}
103+
}
104+
} else {
105+
unstructStatus["Healthy"] = true
106+
unstructStatus["Errors"] = statusErrors
107+
s, _, err := unstructured.NestedMap(unstruct.Object, "status")
83108
if err != nil {
84-
log.Error(err, "updating status")
85-
return err
109+
log.Error(err, "getting status")
110+
return fmt.Errorf("unable to get status from unstructured: %v", err)
111+
}
112+
if !reflect.DeepEqual(status, s) {
113+
err = unstructured.SetNestedField(unstruct.Object, statusHealthy, "status", "healthy")
114+
err = unstructured.SetNestedStringSlice(unstruct.Object, statusErrors, "status", "errors")
115+
if err != nil {
116+
log.Error(err, "updating status")
117+
return fmt.Errorf("unable to set status in unstructured", err)
118+
}
119+
120+
log.WithValues("name", unstruct.GetName()).WithValues("status", status).Info("updating status")
121+
122+
err = a.client.Update(ctx, unstruct)
123+
if err != nil {
124+
log.Error(err, "updating status")
125+
return err
126+
}
86127
}
87128
}
88129

89130
return nil
90131
}
91132

92-
func (a *aggregator) deployment(ctx context.Context, src addonv1alpha1.CommonObject, name string) (bool, error) {
133+
func (a *aggregator) deployment(ctx context.Context, src declarative.DeclarativeObject, name string) (bool, error) {
93134
key := client.ObjectKey{Namespace: src.GetNamespace(), Name: name}
94135
dep := &appsv1.Deployment{}
95136

@@ -106,7 +147,7 @@ func (a *aggregator) deployment(ctx context.Context, src addonv1alpha1.CommonObj
106147
return false, fmt.Errorf("deployment (%s) does not meet condition: %s", key, successfulDeployment)
107148
}
108149

109-
func (a *aggregator) service(ctx context.Context, src addonv1alpha1.CommonObject, name string) (bool, error) {
150+
func (a *aggregator) service(ctx context.Context, src declarative.DeclarativeObject, name string) (bool, error) {
110151
key := client.ObjectKey{Namespace: src.GetNamespace(), Name: name}
111152
svc := &corev1.Service{}
112153
err := a.client.Get(ctx, key, svc)

pkg/patterns/declarative/reconciler.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121
"errors"
2222
"fmt"
2323
"path/filepath"
24+
"sigs.k8s.io/cli-utils/pkg/kstatus/status"
25+
addonsv1alpha1 "sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/addon/pkg/apis/v1alpha1"
2426
"strings"
2527

2628
apierrors "k8s.io/apimachinery/pkg/api/errors"

0 commit comments

Comments
 (0)