/
azurepathfixjob.go
262 lines (236 loc) · 8.99 KB
/
azurepathfixjob.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package resource
import (
"context"
"fmt"
"os"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
kcorev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
batchset "k8s.io/client-go/kubernetes/typed/batch/v1"
batchlisters "k8s.io/client-go/listers/batch/v1"
corev1listers "k8s.io/client-go/listers/core/v1"
restclient "k8s.io/client-go/rest"
configapiv1 "github.com/openshift/api/config/v1"
imageregistryv1 "github.com/openshift/api/imageregistry/v1"
configlisters "github.com/openshift/client-go/config/listers/config/v1"
"github.com/openshift/cluster-image-registry-operator/pkg/defaults"
"github.com/openshift/cluster-image-registry-operator/pkg/storage/azure"
)
var _ Mutator = &generatorAzurePathFixJob{}
type generatorAzurePathFixJob struct {
lister batchlisters.JobNamespaceLister
secretLister corev1listers.SecretNamespaceLister
infrastructureLister configlisters.InfrastructureLister
proxyLister configlisters.ProxyLister
openshiftConfigLister corev1listers.ConfigMapNamespaceLister
client batchset.BatchV1Interface
cr *imageregistryv1.Config
kubeconfig *restclient.Config
}
func NewGeneratorAzurePathFixJob(
lister batchlisters.JobNamespaceLister,
client batchset.BatchV1Interface,
secretLister corev1listers.SecretNamespaceLister,
infrastructureLister configlisters.InfrastructureLister,
proxyLister configlisters.ProxyLister,
openshiftConfigLister corev1listers.ConfigMapNamespaceLister,
cr *imageregistryv1.Config,
kubeconfig *restclient.Config,
) *generatorAzurePathFixJob {
return &generatorAzurePathFixJob{
lister: lister,
client: client,
cr: cr,
infrastructureLister: infrastructureLister,
secretLister: secretLister,
proxyLister: proxyLister,
openshiftConfigLister: openshiftConfigLister,
kubeconfig: kubeconfig,
}
}
func (gapfj *generatorAzurePathFixJob) Type() runtime.Object {
return &batchv1.Job{}
}
func (gapfj *generatorAzurePathFixJob) GetNamespace() string {
return defaults.ImageRegistryOperatorNamespace
}
func (gapfj *generatorAzurePathFixJob) GetName() string {
return "azure-path-fix"
}
func (gapfj *generatorAzurePathFixJob) expected() (runtime.Object, error) {
azureCfg, err := azure.GetConfig(gapfj.secretLister, gapfj.infrastructureLister)
if err != nil {
return nil, err
}
clusterProxy, err := gapfj.proxyLister.Get(defaults.ClusterProxyResourceName)
if errors.IsNotFound(err) {
clusterProxy = &configapiv1.Proxy{}
} else if err != nil {
// TODO: should we report Degraded?
return nil, fmt.Errorf("unable to get cluster proxy configuration: %v", err)
}
azureStorage := gapfj.cr.Status.Storage.Azure
if azureStorage == nil {
return nil, fmt.Errorf("storage not yet provisioned")
}
optional := true
envs := []corev1.EnvVar{
{Name: "AZURE_ENVIRONMENT_FILEPATH", Value: os.Getenv("AZURE_ENVIRONMENT_FILEPATH")},
{Name: "AZURE_STORAGE_ACCOUNT_NAME", Value: azureStorage.AccountName},
{Name: "AZURE_CONTAINER_NAME", Value: azureStorage.Container},
{Name: "AZURE_CLIENT_ID", Value: azureCfg.ClientID},
{Name: "AZURE_TENANT_ID", Value: azureCfg.TenantID},
{Name: "AZURE_CLIENT_SECRET", Value: azureCfg.ClientSecret},
{Name: "AZURE_FEDERATED_TOKEN_FILE", Value: azureCfg.FederatedTokenFile},
{Name: "AZURE_ACCOUNTKEY", ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
Optional: &optional,
LocalObjectReference: corev1.LocalObjectReference{
Name: defaults.ImageRegistryPrivateConfiguration,
},
Key: "REGISTRY_STORAGE_AZURE_ACCOUNTKEY",
},
}},
}
// for Azure Stack Hub, the move-blobs command needs to know the endpoints,
// and those come from the cloud-provider-config in the openshift-config
// namespace.
// get configmap contents
// assign cm.Data["endpoints"] into a var
// export that var into the container
cm, err := gapfj.openshiftConfigLister.Get("cloud-provider-config")
if err != nil && !errors.IsNotFound(err) {
return nil, err
}
if cm != nil {
envs = append(envs, corev1.EnvVar{Name: "AZURE_ENVIRONMENT_FILECONTENTS", Value: cm.Data["endpoints"]})
}
if len(azureStorage.CloudName) > 0 {
envs = append(envs, corev1.EnvVar{Name: "AZURE_ENVIRONMENT", Value: azureStorage.CloudName})
}
if gapfj.cr.Spec.Proxy.HTTP != "" {
envs = append(envs, corev1.EnvVar{Name: "HTTP_PROXY", Value: gapfj.cr.Spec.Proxy.HTTP})
} else if clusterProxy.Status.HTTPProxy != "" {
envs = append(envs, corev1.EnvVar{Name: "HTTP_PROXY", Value: clusterProxy.Status.HTTPProxy})
}
if gapfj.cr.Spec.Proxy.HTTPS != "" {
envs = append(envs, corev1.EnvVar{Name: "HTTPS_PROXY", Value: gapfj.cr.Spec.Proxy.HTTPS})
} else if clusterProxy.Status.HTTPSProxy != "" {
envs = append(envs, corev1.EnvVar{Name: "HTTPS_PROXY", Value: clusterProxy.Status.HTTPSProxy})
}
if gapfj.cr.Spec.Proxy.NoProxy != "" {
envs = append(envs, corev1.EnvVar{Name: "NO_PROXY", Value: gapfj.cr.Spec.Proxy.NoProxy})
} else if clusterProxy.Status.NoProxy != "" {
envs = append(envs, corev1.EnvVar{Name: "NO_PROXY", Value: clusterProxy.Status.NoProxy})
}
// Cluster trusted certificate authorities - mount to /usr/share/pki/ca-trust-source/ to add
// CAs as low-priority trust sources. Registry runs update-ca-trust extract on startup, which
// merges the registry CAs with the cluster's trusted CAs into a single CA bundle.
//
// See man update-ca-trust for more information.
optional = true
trustedCAVolume := corev1.Volume{
Name: "trusted-ca",
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: defaults.TrustedCAName,
},
// Trust bundle is in PEM format - needs to be mounted to /anchors so that
// update-ca-trust extract knows that these CAs should always be trusted.
// This also ensures that no other low-priority trust is present in the container.
//
// See man update-ca-trust for more information.
Items: []corev1.KeyToPath{
{
Key: "ca-bundle.crt",
Path: "anchors/ca-bundle.crt",
},
},
Optional: &optional,
},
},
}
trustedCAMount := corev1.VolumeMount{
Name: trustedCAVolume.Name,
MountPath: "/usr/share/pki/ca-trust-source",
}
caTrustExtractedVolume := corev1.Volume{
Name: "ca-trust-extracted",
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{},
},
}
caTrustExtractedMount := corev1.VolumeMount{
Name: "ca-trust-extracted",
MountPath: "/etc/pki/ca-trust/extracted",
}
backoffLimit := int32(0)
cj := &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Name: gapfj.GetName(),
Namespace: gapfj.GetNamespace(),
},
Spec: batchv1.JobSpec{
BackoffLimit: &backoffLimit,
Template: kcorev1.PodTemplateSpec{
Spec: kcorev1.PodSpec{
RestartPolicy: kcorev1.RestartPolicyNever,
ServiceAccountName: defaults.ServiceAccountName,
PriorityClassName: "system-cluster-critical",
Containers: []kcorev1.Container{
{
Image: os.Getenv("OPERATOR_IMAGE"),
Resources: kcorev1.ResourceRequirements{
Requests: kcorev1.ResourceList{
kcorev1.ResourceCPU: resource.MustParse("100m"),
kcorev1.ResourceMemory: resource.MustParse("256Mi"),
},
},
TerminationMessagePolicy: kcorev1.TerminationMessageFallbackToLogsOnError,
Env: envs,
VolumeMounts: []corev1.VolumeMount{trustedCAMount, caTrustExtractedMount},
Name: gapfj.GetName(),
Command: []string{"/bin/sh"},
Args: []string{
"-c",
"mkdir -p /etc/pki/ca-trust/extracted/edk2 /etc/pki/ca-trust/extracted/java /etc/pki/ca-trust/extracted/openssl /etc/pki/ca-trust/extracted/pem && update-ca-trust extract && /usr/bin/move-blobs",
},
},
},
Volumes: []corev1.Volume{trustedCAVolume, caTrustExtractedVolume},
},
},
},
}
return cj, nil
}
func (gapfj *generatorAzurePathFixJob) Get() (runtime.Object, error) {
return gapfj.lister.Get(gapfj.GetName())
}
func (gapfj *generatorAzurePathFixJob) Create() (runtime.Object, error) {
return commonCreate(gapfj, func(obj runtime.Object) (runtime.Object, error) {
return gapfj.client.Jobs(gapfj.GetNamespace()).Create(
context.TODO(), obj.(*batchv1.Job), metav1.CreateOptions{},
)
})
}
func (gapfj *generatorAzurePathFixJob) Update(o runtime.Object) (runtime.Object, bool, error) {
return commonUpdate(gapfj, o, func(obj runtime.Object) (runtime.Object, error) {
return gapfj.client.Jobs(gapfj.GetNamespace()).Update(
context.TODO(), obj.(*batchv1.Job), metav1.UpdateOptions{},
)
})
}
func (gapfj *generatorAzurePathFixJob) Delete(opts metav1.DeleteOptions) error {
return gapfj.client.Jobs(gapfj.GetNamespace()).Delete(
context.TODO(), gapfj.GetName(), opts,
)
}
func (gapfj *generatorAzurePathFixJob) Owned() bool {
return true
}