Skip to content

Commit 2fea28b

Browse files
authored
Merge pull request #116 from SomtochiAma/helper-functions
Adds helper functions
2 parents 9aa700e + da25769 commit 2fea28b

File tree

4 files changed

+88
-46
lines changed

4 files changed

+88
-46
lines changed

pkg/patterns/addon/application.go

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

24-
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
25-
addonsv1alpha1 "sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/addon/pkg/apis/v1alpha1"
24+
"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/addon/pkg/utils"
2625
"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/declarative"
2726
"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/declarative/pkg/manifest"
2827
)
@@ -43,26 +42,14 @@ const (
4342

4443
// TransformApplicationFromStatus modifies the Application in the deployment based off the CommonStatus
4544
func TransformApplicationFromStatus(ctx context.Context, instance declarative.DeclarativeObject, objects *manifest.Objects) error {
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
45+
version, err := utils.GetCommonVersion(instance)
46+
if err != nil {
47+
return fmt.Errorf("unable to get version from object: %v", err)
48+
}
5649

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-
}
61-
} else if addonObject, ok = instance.(addonsv1alpha1.CommonObject); ok {
62-
version = addonObject.CommonSpec().Version
63-
healthy = addonObject.GetCommonStatus().Healthy
64-
} else {
65-
return fmt.Errorf("instance %T was not an addonsv1alpha1.CommonObject", instance)
50+
healthy, err := utils.GetCommonHealth(instance)
51+
if err != nil {
52+
return fmt.Errorf("unable to get health from object: %v", err)
6653
}
6754

6855
app, err := declarative.ExtractApplication(objects)

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

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,10 @@ import (
2222
"fmt"
2323
"strings"
2424

25-
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
25+
"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/addon/pkg/utils"
2626

2727
"k8s.io/apimachinery/pkg/runtime"
2828
"sigs.k8s.io/controller-runtime/pkg/log"
29-
addonsv1alpha1 "sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/addon/pkg/apis/v1alpha1"
3029
)
3130

3231
var FlagChannel = "./channels"
@@ -66,29 +65,19 @@ func (c *ManifestLoader) ResolveManifest(ctx context.Context, object runtime.Obj
6665
componentName string
6766
)
6867

69-
unstruct, ok := object.(*unstructured.Unstructured)
70-
if ok {
71-
v, _, err := unstructured.NestedString(unstruct.Object, "spec", "version")
72-
if err != nil {
73-
return nil, fmt.Errorf("unable to get spec.version: %v", err)
74-
}
75-
version = v
76-
77-
c, _, err := unstructured.NestedString(unstruct.Object, "spec", "channel")
78-
if err != nil {
79-
return nil, fmt.Errorf("unable to get spec.version: %v", err)
80-
}
81-
channelName = c
68+
version, err := utils.GetCommonVersion(object)
69+
if err != nil {
70+
return nil, err
71+
}
8272

83-
componentName = strings.ToLower(unstruct.GetKind())
84-
} else if addonObject, ok := object.(addonsv1alpha1.CommonObject); ok {
85-
componentName = addonObject.ComponentName()
73+
componentName, err = utils.GetCommonName(object)
74+
if err != nil {
75+
return nil, err
76+
}
8677

87-
spec := addonObject.CommonSpec()
88-
version = spec.Version
89-
channelName = spec.Channel
90-
} else {
91-
return nil, fmt.Errorf("object %T was not an addonsv1alpha1.CommonObject", object)
78+
channelName, err = utils.GetCommonChannel(object)
79+
if err != nil {
80+
return nil, err
9281
}
9382

9483
// TODO: We should actually do id (1.1.2-aws or 1.1.1-nginx). But maybe YAGNI
@@ -122,7 +111,7 @@ func (c *ManifestLoader) ResolveManifest(ctx context.Context, object runtime.Obj
122111
log.WithValues("version", version).Info("using specified version")
123112
}
124113
s := make(map[string]string)
125-
s, err := c.repo.LoadManifest(ctx, componentName, id)
114+
s, err = c.repo.LoadManifest(ctx, componentName, id)
126115
if err != nil {
127116
return nil, fmt.Errorf("error loading manifest: %v", err)
128117
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func (a *aggregator) Reconciled(ctx context.Context, src declarative.Declarative
124124

125125
log.WithValues("name", unstruct.GetName()).WithValues("status", status).Info("updating status")
126126

127-
a.client.Status().Update(ctx, instance)
127+
err = a.client.Status().Update(ctx, unstruct)
128128
if err != nil {
129129
log.Error(err, "updating status")
130130
return err
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package utils
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
8+
"k8s.io/apimachinery/pkg/runtime"
9+
addonsv1alpha1 "sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/addon/pkg/apis/v1alpha1"
10+
)
11+
12+
func GetCommonVersion(instance runtime.Object) (string, error) {
13+
switch v := instance.(type) {
14+
case addonsv1alpha1.CommonObject:
15+
return v.CommonSpec().Version, nil
16+
case *unstructured.Unstructured:
17+
version, _, err := unstructured.NestedString(v.Object, "spec", "version")
18+
if err != nil {
19+
return "", fmt.Errorf("unable to get version from unstuctured: %v", err)
20+
}
21+
return version, nil
22+
default:
23+
return "", fmt.Errorf("instance %T is not addonsv1alpha1.CommonObject or unstructured", v)
24+
}
25+
}
26+
27+
func GetCommonHealth(instance runtime.Object) (bool, error) {
28+
switch v := instance.(type) {
29+
case addonsv1alpha1.CommonObject:
30+
return v.GetCommonStatus().Healthy, nil
31+
case *unstructured.Unstructured:
32+
version, _, err := unstructured.NestedBool(v.Object, "status", "healthy")
33+
if err != nil {
34+
return false, fmt.Errorf("unable to get version from unstuctured: %v", err)
35+
}
36+
return version, nil
37+
default:
38+
return false, fmt.Errorf("instance %T is not addonsv1alpha1.CommonObject or unstructured", v)
39+
}
40+
}
41+
42+
func GetCommonChannel(instance runtime.Object) (string, error) {
43+
switch v := instance.(type) {
44+
case addonsv1alpha1.CommonObject:
45+
return v.CommonSpec().Channel, nil
46+
case *unstructured.Unstructured:
47+
channel, _, err := unstructured.NestedString(v.Object, "spec", "channel")
48+
if err != nil {
49+
return "", fmt.Errorf("unable to get version from unstuctured: %v", err)
50+
}
51+
return channel, nil
52+
default:
53+
return "", fmt.Errorf("instance %T is not addonsv1alpha1.CommonObject or unstructured", v)
54+
}
55+
}
56+
57+
func GetCommonName(instance runtime.Object) (string, error) {
58+
switch v := instance.(type) {
59+
case addonsv1alpha1.CommonObject:
60+
return v.ComponentName(), nil
61+
case *unstructured.Unstructured:
62+
return strings.ToLower(v.GetKind()), nil
63+
default:
64+
return "", fmt.Errorf("instance %T is not addonsv1alpha1.CommonObject or unstructured", v)
65+
}
66+
}

0 commit comments

Comments
 (0)