From bf79716519acdbf9dfa2e6600ae81b3b687aa5e4 Mon Sep 17 00:00:00 2001 From: Somtochi Onyekwere Date: Fri, 21 Aug 2020 12:36:15 +0100 Subject: [PATCH 1/2] Aggregate status looks in the correct namespace --- pkg/patterns/addon/pkg/status/aggregate.go | 18 ++++++++++++------ .../declarative/pkg/manifest/objects.go | 3 +++ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/pkg/patterns/addon/pkg/status/aggregate.go b/pkg/patterns/addon/pkg/status/aggregate.go index 85051008..5249fc66 100644 --- a/pkg/patterns/addon/pkg/status/aggregate.go +++ b/pkg/patterns/addon/pkg/status/aggregate.go @@ -54,12 +54,20 @@ func (a *aggregator) Reconciled(ctx context.Context, src declarative.Declarative for _, o := range objs.Items { gk := o.Group + "/" + o.Kind healthy := true + objKey := client.ObjectKey{ + Name: o.Name, + Namespace: o.Namespace, + } + // If the namespace isn't set on the object, we would want to use the namespace of src + if objKey.Namespace == "" { + objKey.Namespace = src.GetNamespace() + } var err error switch gk { case "/Service": - healthy, err = a.service(ctx, src, o.Name) + healthy, err = a.service(ctx, objKey) case "extensions/Deployment", "apps/Deployment": - healthy, err = a.deployment(ctx, src, o.Name) + healthy, err = a.deployment(ctx, objKey) default: log.WithValues("type", gk).V(2).Info("type not implemented for status aggregation, skipping") } @@ -129,8 +137,7 @@ func (a *aggregator) Reconciled(ctx context.Context, src declarative.Declarative return nil } -func (a *aggregator) deployment(ctx context.Context, src declarative.DeclarativeObject, name string) (bool, error) { - key := client.ObjectKey{Name: name} +func (a *aggregator) deployment(ctx context.Context, key client.ObjectKey) (bool, error) { dep := &appsv1.Deployment{} if err := a.client.Get(ctx, key, dep); err != nil { @@ -146,8 +153,7 @@ func (a *aggregator) deployment(ctx context.Context, src declarative.Declarative return false, fmt.Errorf("deployment (%s) does not meet condition: %s", key, successfulDeployment) } -func (a *aggregator) service(ctx context.Context, src declarative.DeclarativeObject, name string) (bool, error) { - key := client.ObjectKey{Namespace: src.GetNamespace(), Name: name} +func (a *aggregator) service(ctx context.Context, key client.ObjectKey) (bool, error) { svc := &corev1.Service{} err := a.client.Get(ctx, key, svc) if err != nil { diff --git a/pkg/patterns/declarative/pkg/manifest/objects.go b/pkg/patterns/declarative/pkg/manifest/objects.go index 6e54bf85..4d3206db 100644 --- a/pkg/patterns/declarative/pkg/manifest/objects.go +++ b/pkg/patterns/declarative/pkg/manifest/objects.go @@ -42,6 +42,7 @@ type Object struct { Group string Kind string Name string + Namespace string json []byte } @@ -62,6 +63,7 @@ func ParseJSONToObject(json []byte) (*Object, error) { Group: gvk.Group, Kind: gvk.Kind, Name: u.GetName(), + Namespace: u.GetNamespace(), json: json, }, nil } @@ -360,6 +362,7 @@ func newObject(u *unstructured.Unstructured, json []byte) (*Object, error) { o.Group = gvk.Group o.Kind = gvk.Kind o.Name = u.GetName() + o.Namespace = u.GetNamespace() return o, nil } From e778f94f2ea7052a94761d9d64acc4c325708f33 Mon Sep 17 00:00:00 2001 From: Somtochi Onyekwere Date: Fri, 21 Aug 2020 12:39:11 +0100 Subject: [PATCH 2/2] Runs go.mod --- pkg/patterns/addon/pkg/status/aggregate.go | 2 +- pkg/patterns/declarative/pkg/manifest/objects.go | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/pkg/patterns/addon/pkg/status/aggregate.go b/pkg/patterns/addon/pkg/status/aggregate.go index 5249fc66..d6807d1c 100644 --- a/pkg/patterns/addon/pkg/status/aggregate.go +++ b/pkg/patterns/addon/pkg/status/aggregate.go @@ -55,7 +55,7 @@ func (a *aggregator) Reconciled(ctx context.Context, src declarative.Declarative gk := o.Group + "/" + o.Kind healthy := true objKey := client.ObjectKey{ - Name: o.Name, + Name: o.Name, Namespace: o.Namespace, } // If the namespace isn't set on the object, we would want to use the namespace of src diff --git a/pkg/patterns/declarative/pkg/manifest/objects.go b/pkg/patterns/declarative/pkg/manifest/objects.go index 4d3206db..65e9746e 100644 --- a/pkg/patterns/declarative/pkg/manifest/objects.go +++ b/pkg/patterns/declarative/pkg/manifest/objects.go @@ -39,9 +39,9 @@ type Objects struct { type Object struct { object *unstructured.Unstructured - Group string - Kind string - Name string + Group string + Kind string + Name string Namespace string json []byte @@ -59,12 +59,12 @@ func ParseJSONToObject(json []byte) (*Object, error) { } return &Object{ - object: u, - Group: gvk.Group, - Kind: gvk.Kind, - Name: u.GetName(), + object: u, + Group: gvk.Group, + Kind: gvk.Kind, + Name: u.GetName(), Namespace: u.GetNamespace(), - json: json, + json: json, }, nil }