@@ -25,10 +25,17 @@ const (
25
25
AvailableReason = "PullSecretUpdated"
26
26
)
27
27
28
+ var (
29
+ disconnectedReason = "Disconnected"
30
+ noClusterTransfer = "NoClusterTransfer"
31
+ moreAcceptedClusterTransfers = "MoreAcceptedClusterTransfers"
32
+ dataCorrupted = "DataCorrupted"
33
+ unexpectedData = "UnexpectedData"
34
+ )
35
+
28
36
type Controller struct {
29
37
controllerstatus.StatusController
30
38
coreClient corev1client.CoreV1Interface
31
- ctx context.Context
32
39
configurator configobserver.Interface
33
40
client clusterTransferClient
34
41
pullSecret * v1.Secret
@@ -39,31 +46,29 @@ type clusterTransferClient interface {
39
46
}
40
47
41
48
// New creates new instance of the cluster transfer controller
42
- func New (ctx context.Context ,
43
- coreClient corev1client.CoreV1Interface ,
49
+ func New (coreClient corev1client.CoreV1Interface ,
44
50
configurator configobserver.Interface ,
45
51
insightsClient clusterTransferClient ) * Controller {
46
52
return & Controller {
47
53
StatusController : controllerstatus .New (ControllerName ),
48
54
coreClient : coreClient ,
49
- ctx : ctx ,
50
55
configurator : configurator ,
51
56
client : insightsClient ,
52
57
}
53
58
}
54
59
55
60
// Run periodically queries the OCM API and update pull-secret accordingly
56
- func (c * Controller ) Run () {
61
+ func (c * Controller ) Run (ctx context. Context ) {
57
62
cfg := c .configurator .Config ()
58
63
endpoint := cfg .ClusterTransfer .Endpoint
59
64
interval := cfg .ClusterTransfer .Interval
60
65
configCh , cancel := c .configurator .ConfigChanged ()
61
66
defer cancel ()
62
- c .requestDataAndUpdateSecret (endpoint )
67
+ c .requestDataAndUpdateSecret (ctx , endpoint )
63
68
for {
64
69
select {
65
70
case <- time .After (interval ):
66
- c .requestDataAndUpdateSecret (endpoint )
71
+ c .requestDataAndUpdateSecret (ctx , endpoint )
67
72
case <- configCh :
68
73
cfg := c .configurator .Config ()
69
74
interval = cfg .ClusterTransfer .Interval
@@ -74,7 +79,7 @@ func (c *Controller) Run() {
74
79
75
80
// requestDataAndUpdateSecret queries the provided endpoint. If there is any data
76
81
// in the response then check if a secret update is required, and if so, perform the update.
77
- func (c * Controller ) requestDataAndUpdateSecret (endpoint string ) {
82
+ func (c * Controller ) requestDataAndUpdateSecret (ctx context. Context , endpoint string ) {
78
83
klog .Infof ("checking the availability of cluster transfer. Next check is in %s" , c .configurator .Config ().ClusterTransfer .Interval )
79
84
data , err := c .requestClusterTransferWithExponentialBackoff (endpoint )
80
85
if err != nil {
@@ -88,60 +93,60 @@ func (c *Controller) requestDataAndUpdateSecret(endpoint string) {
88
93
}
89
94
// we are probably in disconnected environment
90
95
klog .Errorf (msg )
91
- c .updateStatus (true , msg , "Disconnected" , nil )
96
+ c .updateStatus (true , msg , disconnectedReason , nil )
92
97
return
93
98
}
94
99
95
100
// there's no cluster transfer for the cluster - HTTP 204
96
101
if len (data ) == 0 {
97
102
klog .Info ("no available accepted cluster transfer" )
98
- c .updateStatus (true , "no available cluster transfer" , "NoClusterTransfer" , nil )
103
+ c .updateStatus (true , "no available cluster transfer" , noClusterTransfer , nil )
99
104
return
100
105
}
101
106
// deserialize the data from the OCM API
102
107
ctList , err := unmarhallResponseData (data )
103
108
if err != nil {
104
109
msg := fmt .Sprintf ("unable to deserialize the cluster transfer API response: %v" , err )
105
110
klog .Error (msg )
106
- c .updateStatus (false , msg , "UnexpectedData" , nil )
111
+ c .updateStatus (false , msg , unexpectedData , nil )
107
112
return
108
113
}
109
- c .checkCTListAndOptionallyUpdatePS (ctList )
114
+ c .checkCTListAndOptionallyUpdatePS (ctx , ctList )
110
115
}
111
116
112
117
// checkCTListAndOptionallyUpdatePS checks the provided cluster transfer list length,
113
118
// If there is more than 1 accepted cluster transfer then log the message, update the controller status
114
119
// and do nothing. If there is only one accepted cluster transfer then
115
120
// check if the `pull-secret` needs to be updated. If the `pull-secret` needs to be updated then
116
121
// update it and update the controller status, otherwise just update the controller status.
117
- func (c * Controller ) checkCTListAndOptionallyUpdatePS (ctList * clusterTransferList ) {
122
+ func (c * Controller ) checkCTListAndOptionallyUpdatePS (ctx context. Context , ctList * clusterTransferList ) {
118
123
if ctList .Total > 1 {
119
124
msg := "there are more accepted cluster transfers. The pull-secret will not be updated!"
120
125
klog .Infof (msg )
121
- c .updateStatus (true , msg , "MoreAcceptedClusterTransfers" , nil )
126
+ c .updateStatus (true , msg , moreAcceptedClusterTransfers , nil )
122
127
return
123
128
}
124
129
// this should not happen. This is just safe check
125
130
if len (ctList .Transfers ) != 1 {
126
131
msg := "unexpected number of cluster transfers received from the API"
127
132
klog .Infof (msg )
128
- c .updateStatus (true , msg , "UnexpectedData" , nil )
133
+ c .updateStatus (true , msg , unexpectedData , nil )
129
134
return
130
135
}
131
136
132
137
newPullSecret := []byte (ctList .Transfers [0 ].Secret )
133
138
var statusMsg , reason string
134
139
// check if the pull-secret needs to be updated
135
- updating , err := c .isUpdateRequired (newPullSecret )
140
+ updating , err := c .isUpdateRequired (ctx , newPullSecret )
136
141
if err != nil {
137
142
statusMsg = fmt .Sprintf ("new pull-secret check failed: %v" , err )
138
143
klog .Errorf (statusMsg )
139
- c .updateStatus (false , statusMsg , "DataCorrupted" , nil )
144
+ c .updateStatus (false , statusMsg , dataCorrupted , nil )
140
145
return
141
146
}
142
147
if updating {
143
148
klog .Info ("updating the pull-secret content" )
144
- err = c .updatePullSecret (newPullSecret )
149
+ err = c .updatePullSecret (ctx , newPullSecret )
145
150
if err != nil {
146
151
statusMsg = fmt .Sprintf ("failed to update pull-secret: %v" , err )
147
152
klog .Errorf (statusMsg )
@@ -160,8 +165,8 @@ func (c *Controller) checkCTListAndOptionallyUpdatePS(ctList *clusterTransferLis
160
165
}
161
166
162
167
// isUpdateRequired checks if an update of the pull-secret is required or not.
163
- func (c * Controller ) isUpdateRequired (newData []byte ) (bool , error ) {
164
- pullSecret , err := c .getPullSecret ()
168
+ func (c * Controller ) isUpdateRequired (ctx context. Context , newData []byte ) (bool , error ) {
169
+ pullSecret , err := c .getPullSecret (ctx )
165
170
if err != nil {
166
171
return false , err
167
172
}
@@ -180,9 +185,9 @@ func (c *Controller) isUpdateRequired(newData []byte) (bool, error) {
180
185
181
186
// updatePullSecret creates a JSON merge patch of existing and new pull-secret data.
182
187
// The result of the patch is used for pull-secret data update.
183
- func (c * Controller ) updatePullSecret (newData []byte ) error {
188
+ func (c * Controller ) updatePullSecret (ctx context. Context , newData []byte ) error {
184
189
if c .pullSecret == nil {
185
- ps , err := c .getPullSecret ()
190
+ ps , err := c .getPullSecret (ctx )
186
191
if err != nil {
187
192
return err
188
193
}
@@ -196,7 +201,7 @@ func (c *Controller) updatePullSecret(newData []byte) error {
196
201
}
197
202
198
203
c .pullSecret .Data [v1 .DockerConfigJsonKey ] = updatedData
199
- _ , err = c .coreClient .Secrets ("openshift-config" ).Update (c . ctx , c .pullSecret , metav1.UpdateOptions {})
204
+ _ , err = c .coreClient .Secrets ("openshift-config" ).Update (ctx , c .pullSecret , metav1.UpdateOptions {})
200
205
if err != nil {
201
206
return err
202
207
}
@@ -260,8 +265,8 @@ func (c *Controller) updateStatus(healthy bool, msg, reason string, httpErr *ins
260
265
}
261
266
262
267
// getPullSecret gets pull-secret as *v1.Secret
263
- func (c * Controller ) getPullSecret () (* v1.Secret , error ) {
264
- return c .coreClient .Secrets ("openshift-config" ).Get (c . ctx , "pull-secret" , metav1.GetOptions {})
268
+ func (c * Controller ) getPullSecret (ctx context. Context ) (* v1.Secret , error ) {
269
+ return c .coreClient .Secrets ("openshift-config" ).Get (ctx , "pull-secret" , metav1.GetOptions {})
265
270
}
266
271
267
272
// isUpdatedPullSecretContentSame checks if the updatedPS content is different
0 commit comments