9
9
"strings"
10
10
"time"
11
11
12
- "gopkg.in/yaml.v3"
13
12
kerrors "k8s.io/apimachinery/pkg/api/errors"
14
13
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
15
14
"k8s.io/apimachinery/pkg/runtime"
@@ -87,8 +86,9 @@ func clusterOperatorEventFilterFunc(obj interface{}) bool {
87
86
}
88
87
89
88
const (
90
- clusterVersionKindName = "ClusterVersion"
91
- clusterOperatorKindName = "ClusterOperator"
89
+ clusterVersionKindName = "ClusterVersion"
90
+ clusterOperatorKindName = "ClusterOperator"
91
+ controlPlaneInformerName = "cpi"
92
92
)
93
93
94
94
// sync is called for any controller event. It will assess the state and health of the control plane, indicated by
@@ -118,9 +118,19 @@ func (c *controlPlaneInformerController) sync(ctx context.Context, syncCtx facto
118
118
119
119
now := c .now ()
120
120
cvInsight , healthInsights := assessClusterVersion (clusterVersion , now )
121
- msgs = append (msgs , makeInsightMsgForClusterVersion (cvInsight , now ))
121
+ msg , err := makeInsightMsgForClusterVersion (cvInsight , now )
122
+ if err != nil {
123
+ klog .Errorf ("BUG: Could not create insight message: %v" , err )
124
+ return nil
125
+ }
126
+ msgs = append (msgs , msg )
122
127
for item := range healthInsights {
123
- msgs = append (msgs , makeInsightMsgForHealthInsight (healthInsights [item ], now ))
128
+ msg , err := makeInsightMsgForHealthInsight (healthInsights [item ], now )
129
+ if err != nil {
130
+ klog .Errorf ("BUG: Could not create insight message: %v" , err )
131
+ return nil
132
+ }
133
+ msgs = append (msgs , msg )
124
134
}
125
135
126
136
case clusterOperatorKindName :
@@ -144,7 +154,12 @@ func (c *controlPlaneInformerController) sync(ctx context.Context, syncCtx facto
144
154
if err != nil {
145
155
return fmt .Errorf ("failed to assess cluster operator %s: %w" , name , err )
146
156
}
147
- msgs = append (msgs , makeInsightMsgForClusterOperator (insight , now ))
157
+ msg , err := makeInsightMsgForClusterOperator (insight , now )
158
+ if err != nil {
159
+ klog .Errorf ("BUG: Could not create insight message: %v" , err )
160
+ return nil
161
+ }
162
+ msgs = append (msgs , msg )
148
163
default :
149
164
return fmt .Errorf ("invalid queue key %s with unexpected type %s" , queueKey , t )
150
165
}
@@ -161,22 +176,16 @@ func (c *controlPlaneInformerController) sync(ctx context.Context, syncCtx facto
161
176
return nil
162
177
}
163
178
164
- func makeInsightMsgForClusterOperator (coInsight * ClusterOperatorStatusInsight , acquiredAt metav1.Time ) informerMsg {
165
- uid := fmt .Sprintf ("usc-co-%s" , coInsight .Name )
179
+ func makeInsightMsgForClusterOperator (coInsight * ClusterOperatorStatusInsight , acquiredAt metav1.Time ) (informerMsg , error ) {
166
180
insight := ControlPlaneInsight {
167
- UID : uid ,
181
+ UID : fmt . Sprintf ( "co-%s" , coInsight . Name ) ,
168
182
AcquiredAt : acquiredAt ,
169
183
ControlPlaneInsightUnion : ControlPlaneInsightUnion {
170
184
Type : ClusterOperatorStatusInsightType ,
171
185
ClusterOperatorStatusInsight : coInsight ,
172
186
},
173
187
}
174
- // Should handle errors, but ultimately we will have a proper API and won’t need to serialize ourselves
175
- rawInsight , _ := yaml .Marshal (insight )
176
- return informerMsg {
177
- uid : uid ,
178
- insight : rawInsight ,
179
- }
188
+ return makeControlPlaneInsightMsg (insight , controlPlaneInformerName )
180
189
}
181
190
182
191
func assessClusterOperator (ctx context.Context , operator * configv1.ClusterOperator , targetVersion string , appsClient appsv1client.AppsV1Interface , now metav1.Time ) (* ClusterOperatorStatusInsight , error ) {
@@ -300,22 +309,16 @@ func getImagePullSpec(ctx context.Context, name string, appsClient appsv1client.
300
309
// makeInsightMsgForClusterVersion creates an informerMsg for the given ClusterVersionStatusInsight. It defines an uid
301
310
// name and serializes the insight as YAML. Serialization is convenient because it prevents any data sharing issues
302
311
// between controllers.
303
- func makeInsightMsgForClusterVersion (cvInsight * ClusterVersionStatusInsight , acquiredAt metav1.Time ) informerMsg {
304
- uid := fmt .Sprintf ("usc-cv-%s" , cvInsight .Resource .Name )
312
+ func makeInsightMsgForClusterVersion (cvInsight * ClusterVersionStatusInsight , acquiredAt metav1.Time ) (informerMsg , error ) {
305
313
insight := ControlPlaneInsight {
306
- UID : uid ,
314
+ UID : fmt . Sprintf ( "cv-%s" , cvInsight . Resource . Name ) ,
307
315
AcquiredAt : acquiredAt ,
308
316
ControlPlaneInsightUnion : ControlPlaneInsightUnion {
309
317
Type : ClusterVersionStatusInsightType ,
310
318
ClusterVersionStatusInsight : cvInsight ,
311
319
},
312
320
}
313
- // Should handle errors, but ultimately we will have a proper API and won’t need to serialize ourselves
314
- rawInsight , _ := yaml .Marshal (insight )
315
- return informerMsg {
316
- uid : uid ,
317
- insight : rawInsight ,
318
- }
321
+ return makeControlPlaneInsightMsg (insight , controlPlaneInformerName )
319
322
}
320
323
321
324
func uidForHealthInsight (healthInsight * HealthInsight ) string {
@@ -332,26 +335,19 @@ func uidForHealthInsight(healthInsight *HealthInsight) string {
332
335
encoded := base64 .StdEncoding .EncodeToString (sum )
333
336
encoded = strings .TrimRight (encoded , "=" )
334
337
335
- return fmt . Sprintf ( "usc-%s" , encoded )
338
+ return encoded
336
339
}
337
340
338
- func makeInsightMsgForHealthInsight (healthInsight * HealthInsight , acquiredAt metav1.Time ) informerMsg {
339
- uid := uidForHealthInsight (healthInsight )
341
+ func makeInsightMsgForHealthInsight (healthInsight * HealthInsight , acquiredAt metav1.Time ) (informerMsg , error ) {
340
342
insight := ControlPlaneInsight {
341
- UID : uid ,
343
+ UID : uidForHealthInsight ( healthInsight ) ,
342
344
AcquiredAt : acquiredAt ,
343
345
ControlPlaneInsightUnion : ControlPlaneInsightUnion {
344
346
Type : HealthInsightType ,
345
347
HealthInsight : healthInsight ,
346
348
},
347
349
}
348
-
349
- // Should handle errors, but ultimately we will have a proper API and won’t need to serialize ourselves
350
- rawInsight , _ := yaml .Marshal (insight )
351
- return informerMsg {
352
- uid : uid ,
353
- insight : rawInsight ,
354
- }
350
+ return makeControlPlaneInsightMsg (insight , controlPlaneInformerName )
355
351
}
356
352
357
353
// assessClusterVersion produces a ClusterVersion status insight from the current state of the ClusterVersion resource.
0 commit comments