5
5
"encoding/json"
6
6
"fmt"
7
7
"net/http"
8
+ "runtime"
8
9
"strings"
9
10
"time"
10
11
@@ -34,7 +35,6 @@ const (
34
35
type Controller struct {
35
36
controllerstatus.StatusController
36
37
coreClient corev1client.CoreV1Interface
37
- ctx context.Context
38
38
configurator configobserver.Interface
39
39
client * insightsclient.Client
40
40
}
@@ -48,33 +48,32 @@ type Response struct {
48
48
}
49
49
50
50
// New creates new instance
51
- func New (ctx context. Context , coreClient corev1client.CoreV1Interface , configurator configobserver.Interface ,
51
+ func New (coreClient corev1client.CoreV1Interface , configurator configobserver.Interface ,
52
52
insightsClient * insightsclient.Client ) * Controller {
53
53
return & Controller {
54
54
StatusController : controllerstatus .New (ControllerName ),
55
55
coreClient : coreClient ,
56
- ctx : ctx ,
57
56
configurator : configurator ,
58
57
client : insightsClient ,
59
58
}
60
59
}
61
60
62
61
// Run periodically queries the OCM API and update corresponding secret accordingly
63
- func (c * Controller ) Run () {
62
+ func (c * Controller ) Run (ctx context. Context ) {
64
63
cfg := c .configurator .Config ()
65
64
endpoint := cfg .SCA .Endpoint
66
65
interval := cfg .SCA .Interval
67
66
disabled := cfg .SCA .Disabled
68
67
configCh , cancel := c .configurator .ConfigChanged ()
69
68
defer cancel ()
70
69
if ! disabled {
71
- c .requestDataAndCheckSecret (endpoint )
70
+ c .requestDataAndCheckSecret (ctx , endpoint )
72
71
}
73
72
for {
74
73
select {
75
74
case <- time .After (interval ):
76
75
if ! disabled {
77
- c .requestDataAndCheckSecret (endpoint )
76
+ c .requestDataAndCheckSecret (ctx , endpoint )
78
77
} else {
79
78
msg := "Pulling of the SCA certs from the OCM API is disabled"
80
79
klog .Warning (msg )
@@ -95,10 +94,11 @@ func (c *Controller) Run() {
95
94
}
96
95
}
97
96
98
- func (c * Controller ) requestDataAndCheckSecret (endpoint string ) {
97
+ func (c * Controller ) requestDataAndCheckSecret (ctx context. Context , endpoint string ) {
99
98
klog .Infof ("Pulling SCA certificates from %s. Next check is in %s" , c .configurator .Config ().SCA .Endpoint ,
100
99
c .configurator .Config ().SCA .Interval )
101
- data , err := c .requestSCAWithExpBackoff (endpoint )
100
+
101
+ data , err := c .requestSCAWithExpBackoff (ctx , endpoint )
102
102
if err != nil {
103
103
httpErr , ok := err .(insightsclient.HttpError )
104
104
errMsg := fmt .Sprintf ("Failed to pull SCA certs from %s: %v" , endpoint , err )
@@ -131,13 +131,13 @@ func (c *Controller) requestDataAndCheckSecret(endpoint string) {
131
131
klog .Errorf ("Unable to decode response: %v" , err )
132
132
return
133
133
}
134
-
135
134
// check & update the secret here
136
- err = c .checkSecret (& ocmRes )
135
+ err = c .checkSecret (ctx , & ocmRes )
137
136
if err != nil {
138
137
klog .Errorf ("Error when checking the %s secret: %v" , secretName , err )
139
138
return
140
139
}
140
+
141
141
klog .Infof ("%s secret successfully updated" , secretName )
142
142
c .StatusController .UpdateStatus (controllerstatus.Summary {
143
143
Operation : controllerstatus .PullingSCACerts ,
@@ -151,12 +151,12 @@ func (c *Controller) requestDataAndCheckSecret(endpoint string) {
151
151
// checkSecret checks "etc-pki-entitlement" secret in the "openshift-config-managed" namespace.
152
152
// If the secret doesn't exist then it will create a new one.
153
153
// If the secret already exist then it will update the data.
154
- func (c * Controller ) checkSecret (ocmData * Response ) error {
155
- scaSec , err := c .coreClient .Secrets (targetNamespaceName ).Get (c . ctx , secretName , metav1.GetOptions {})
154
+ func (c * Controller ) checkSecret (ctx context. Context , ocmData * Response ) error {
155
+ scaSec , err := c .coreClient .Secrets (targetNamespaceName ).Get (ctx , secretName , metav1.GetOptions {})
156
156
157
157
// if the secret doesn't exist then create one
158
158
if errors .IsNotFound (err ) {
159
- _ , err = c .createSecret (ocmData )
159
+ _ , err = c .createSecret (ctx , ocmData )
160
160
if err != nil {
161
161
return err
162
162
}
@@ -166,14 +166,14 @@ func (c *Controller) checkSecret(ocmData *Response) error {
166
166
return err
167
167
}
168
168
169
- _ , err = c .updateSecret (scaSec , ocmData )
169
+ _ , err = c .updateSecret (ctx , scaSec , ocmData )
170
170
if err != nil {
171
171
return err
172
172
}
173
173
return nil
174
174
}
175
175
176
- func (c * Controller ) createSecret (ocmData * Response ) (* v1.Secret , error ) {
176
+ func (c * Controller ) createSecret (ctx context. Context , ocmData * Response ) (* v1.Secret , error ) {
177
177
newSCA := & v1.Secret {
178
178
ObjectMeta : metav1.ObjectMeta {
179
179
Name : secretName ,
@@ -185,41 +185,56 @@ func (c *Controller) createSecret(ocmData *Response) (*v1.Secret, error) {
185
185
},
186
186
Type : v1 .SecretTypeOpaque ,
187
187
}
188
- cm , err := c .coreClient .Secrets (targetNamespaceName ).Create (c . ctx , newSCA , metav1.CreateOptions {})
188
+ cm , err := c .coreClient .Secrets (targetNamespaceName ).Create (ctx , newSCA , metav1.CreateOptions {})
189
189
if err != nil {
190
190
return nil , err
191
191
}
192
192
return cm , nil
193
193
}
194
194
195
195
// updateSecret updates provided secret with given data
196
- func (c * Controller ) updateSecret (s * v1.Secret , ocmData * Response ) (* v1.Secret , error ) {
196
+ func (c * Controller ) updateSecret (ctx context. Context , s * v1.Secret , ocmData * Response ) (* v1.Secret , error ) {
197
197
s .Data = map [string ][]byte {
198
198
entitlementAttrName : []byte (ocmData .Cert ),
199
199
entitlementKeyAttrName : []byte (ocmData .Key ),
200
200
}
201
- s , err := c .coreClient .Secrets (s .Namespace ).Update (c . ctx , s , metav1.UpdateOptions {})
201
+ s , err := c .coreClient .Secrets (s .Namespace ).Update (ctx , s , metav1.UpdateOptions {})
202
202
if err != nil {
203
203
return nil , err
204
204
}
205
205
return s , nil
206
206
}
207
207
208
+ // getArch check the value of GOARCH and return a valid representation for
209
+ // OCM certificates API
210
+ func getArch () string {
211
+ validArchs := map [string ]string {
212
+ "amd64" : "x86_64" ,
213
+ "i386" : "x86" ,
214
+ }
215
+
216
+ if translation , ok := validArchs [runtime .GOARCH ]; ok {
217
+ return translation
218
+ }
219
+ return runtime .GOARCH
220
+ }
221
+
208
222
// requestSCAWithExpBackoff queries OCM API with exponential backoff.
209
223
// Returns HttpError (see insightsclient.go) in case of any HTTP error response from OCM API.
210
224
// The exponential backoff is applied only for HTTP errors >= 500.
211
- func (c * Controller ) requestSCAWithExpBackoff (endpoint string ) ([]byte , error ) {
225
+ func (c * Controller ) requestSCAWithExpBackoff (ctx context. Context , endpoint string ) ([]byte , error ) {
212
226
bo := wait.Backoff {
213
227
Duration : c .configurator .Config ().SCA .Interval / 32 , // 15 min by default
214
228
Factor : 2 ,
215
229
Jitter : 0 ,
216
230
Steps : ocm .FailureCountThreshold ,
217
231
Cap : c .configurator .Config ().SCA .Interval ,
218
232
}
233
+
219
234
var data []byte
220
235
err := wait .ExponentialBackoff (bo , func () (bool , error ) {
221
236
var err error
222
- data , err = c .client .RecvSCACerts (c . ctx , endpoint )
237
+ data , err = c .client .RecvSCACerts (ctx , endpoint , getArch () )
223
238
if err != nil {
224
239
// don't try again in case it's not an HTTP error - it could mean we're in disconnected env
225
240
if ! insightsclient .IsHttpError (err ) {
0 commit comments