@@ -19,6 +19,7 @@ package status
19
19
import (
20
20
"context"
21
21
"fmt"
22
+ "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
22
23
"reflect"
23
24
24
25
appsv1 "k8s.io/api/apps/v1"
@@ -46,50 +47,90 @@ type aggregator struct {
46
47
func (a * aggregator ) Reconciled (ctx context.Context , src declarative.DeclarativeObject , objs * manifest.Objects ) error {
47
48
log := log .Log
48
49
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 {
51
64
return fmt .Errorf ("object %T was not an addonv1alpha1.CommonObject" , src )
52
65
}
53
66
54
- status := addonv1alpha1.CommonStatus {Healthy : true }
55
-
56
67
for _ , o := range objs .Items {
57
68
gk := o .Group + "/" + o .Kind
58
69
healthy := true
59
70
var err error
60
71
switch gk {
61
72
case "/Service" :
62
- healthy , err = a .service (ctx , instance , o .Name )
73
+ healthy , err = a .service (ctx , src , o .Name )
63
74
case "extensions/Deployment" , "apps/Deployment" :
64
- healthy , err = a .deployment (ctx , instance , o .Name )
75
+ healthy , err = a .deployment (ctx , src , o .Name )
65
76
default :
66
77
log .WithValues ("type" , gk ).V (2 ).Info ("type not implemented for status aggregation, skipping" )
67
78
}
68
79
69
- status .Healthy = status .Healthy && healthy
80
+
81
+ statusHealthy = statusHealthy && healthy
70
82
if err != nil {
71
- status . Errors = append (status . Errors , fmt .Sprintf ("%v" , err ))
83
+ statusErrors = append (statusErrors , fmt .Sprintf ("%v" , err ))
72
84
}
73
85
}
74
86
75
87
log .WithValues ("object" , src ).WithValues ("status" , status ).V (2 ).Info ("built status" )
76
88
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 )
79
95
80
- log .WithValues ("name" , instance .GetName ()).WithValues ("status" , status ).Info ("updating status" )
96
+ log .WithValues ("name" , instance .GetName ()).WithValues ("status" , status ).Info ("updating status" )
81
97
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" )
83
108
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
+ }
86
127
}
87
128
}
88
129
89
130
return nil
90
131
}
91
132
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 ) {
93
134
key := client.ObjectKey {Namespace : src .GetNamespace (), Name : name }
94
135
dep := & appsv1.Deployment {}
95
136
@@ -106,7 +147,7 @@ func (a *aggregator) deployment(ctx context.Context, src addonv1alpha1.CommonObj
106
147
return false , fmt .Errorf ("deployment (%s) does not meet condition: %s" , key , successfulDeployment )
107
148
}
108
149
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 ) {
110
151
key := client.ObjectKey {Namespace : src .GetNamespace (), Name : name }
111
152
svc := & corev1.Service {}
112
153
err := a .client .Get (ctx , key , svc )
0 commit comments