Skip to content

Commit ec4e37b

Browse files
flavianmissiopenshift-cherrypick-robot
authored andcommitted
azurepathfix: create Azure Stack Hub environment file accordingly
1 parent 0c555f7 commit ec4e37b

File tree

5 files changed

+109
-22
lines changed

5 files changed

+109
-22
lines changed

cmd/move-blobs/main.go

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ func main() {
3131
opts.environment = "AZUREPUBLICCLOUD"
3232
}
3333

34+
if err := createASHEnvironmentFile(opts); err != nil {
35+
panic(err)
36+
}
37+
3438
cloudConfig, err := getCloudConfig(opts.environment)
3539
if err != nil {
3640
panic(err)
@@ -218,6 +222,34 @@ type configOpts struct {
218222
federatedTokenFile string
219223
accountKey string
220224
environment string
225+
// environmentFilePath and environmentFileContents are specific
226+
// for Azure Stack Hub
227+
environmentFilePath string
228+
environmentFileContents string
229+
}
230+
231+
func createASHEnvironmentFile(opts *configOpts) error {
232+
if len(opts.environmentFilePath) == 0 || len(opts.environmentFileContents) == 0 {
233+
klog.Info("Azure Stack Hub environment variables not present in current environment, skipping setup...")
234+
return nil
235+
}
236+
f, err := os.Create(opts.environmentFilePath)
237+
if err != nil {
238+
return err
239+
}
240+
241+
_, err = f.WriteString(opts.environmentFileContents)
242+
if err != nil {
243+
f.Close()
244+
os.Remove(f.Name())
245+
return err
246+
}
247+
248+
err = f.Close()
249+
if err != nil {
250+
return err
251+
}
252+
return nil
221253
}
222254

223255
func getCloudConfig(environment string) (cloud.Configuration, error) {
@@ -238,14 +270,16 @@ func getCloudConfig(environment string) (cloud.Configuration, error) {
238270

239271
func getConfigOpts() *configOpts {
240272
return &configOpts{
241-
storageAccountName: strings.TrimSpace(os.Getenv("AZURE_STORAGE_ACCOUNT_NAME")),
242-
containerName: strings.TrimSpace(os.Getenv("AZURE_CONTAINER_NAME")),
243-
clientID: strings.TrimSpace(os.Getenv("AZURE_CLIENT_ID")),
244-
tenantID: strings.TrimSpace(os.Getenv("AZURE_TENANT_ID")),
245-
clientSecret: strings.TrimSpace(os.Getenv("AZURE_CLIENT_SECRET")),
246-
federatedTokenFile: strings.TrimSpace(os.Getenv("AZURE_FEDERATED_TOKEN_FILE")),
247-
accountKey: strings.TrimSpace(os.Getenv("AZURE_ACCOUNTKEY")),
248-
environment: strings.TrimSpace(os.Getenv("AZURE_ENVIRONMENT")),
273+
storageAccountName: strings.TrimSpace(os.Getenv("AZURE_STORAGE_ACCOUNT_NAME")),
274+
containerName: strings.TrimSpace(os.Getenv("AZURE_CONTAINER_NAME")),
275+
clientID: strings.TrimSpace(os.Getenv("AZURE_CLIENT_ID")),
276+
tenantID: strings.TrimSpace(os.Getenv("AZURE_TENANT_ID")),
277+
clientSecret: strings.TrimSpace(os.Getenv("AZURE_CLIENT_SECRET")),
278+
federatedTokenFile: strings.TrimSpace(os.Getenv("AZURE_FEDERATED_TOKEN_FILE")),
279+
accountKey: strings.TrimSpace(os.Getenv("AZURE_ACCOUNTKEY")),
280+
environment: strings.TrimSpace(os.Getenv("AZURE_ENVIRONMENT")),
281+
environmentFilePath: strings.TrimSpace(os.Getenv("AZURE_ENVIRONMENT_FILEPATH")),
282+
environmentFileContents: strings.TrimSpace(os.Getenv("AZURE_ENVIRONMENT_FILECONTENTS")),
249283
}
250284
}
251285

cmd/move-blobs/main_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"context"
55
"fmt"
6+
"io"
67
"math/rand"
78
"os"
89
"strings"
@@ -244,3 +245,30 @@ func TestValidation(t *testing.T) {
244245
})
245246
}
246247
}
248+
249+
func TestStackHubEnvironmentFile(t *testing.T) {
250+
path := strings.TrimSpace(os.Getenv("AZURE_ENVIRONMENT_FILEPATH"))
251+
contents := strings.TrimSpace(os.Getenv("AZURE_ENVIRONMENT_FILECONTENTS"))
252+
if len(path) == 0 || len(contents) == 0 {
253+
t.Fatal("both AZURE_ENVIRONMENT_FILEPATH and AZURE_ENVIRONMENT_FILECONTENTS must be set")
254+
}
255+
opts := getConfigOpts()
256+
err := createASHEnvironmentFile(opts)
257+
if err != nil {
258+
t.Fatal(err)
259+
}
260+
defer os.Remove(path)
261+
f, err := os.Open(path)
262+
if err != nil {
263+
t.Fatalf("error opening file %q: %v", path, err)
264+
}
265+
b, err := io.ReadAll(f)
266+
if err != nil {
267+
t.Fatalf("error reading file %q: %v", path, err)
268+
}
269+
if string(b) != contents {
270+
t.Logf("AZURE_ENVIRONMENT_FILECONTENTS: %s", contents)
271+
t.Logf("%s: %s", path, string(b))
272+
t.Fatalf("file contents differed from AZURE_ENVIRONMENT_FILECONTENTS")
273+
}
274+
}

pkg/operator/azurepathfixcontroller.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type AzurePathFixController struct {
4545
podLister corev1listers.PodNamespaceLister
4646
infrastructureLister configlisters.InfrastructureLister
4747
proxyLister configlisters.ProxyLister
48+
openshiftConfigLister corev1listers.ConfigMapNamespaceLister
4849
kubeconfig *restclient.Config
4950

5051
cachesToSync []cache.InformerSynced
@@ -60,6 +61,7 @@ func NewAzurePathFixController(
6061
infrastructureInformer configv1informers.InfrastructureInformer,
6162
secretInformer corev1informers.SecretInformer,
6263
proxyInformer configv1informers.ProxyInformer,
64+
openshiftConfigInformer corev1informers.ConfigMapInformer,
6365
podInformer corev1informers.PodInformer,
6466
) (*AzurePathFixController, error) {
6567
c := &AzurePathFixController{
@@ -71,6 +73,7 @@ func NewAzurePathFixController(
7173
secretLister: secretInformer.Lister().Secrets(defaults.ImageRegistryOperatorNamespace),
7274
podLister: podInformer.Lister().Pods(defaults.ImageRegistryOperatorNamespace),
7375
proxyLister: proxyInformer.Lister(),
76+
openshiftConfigLister: openshiftConfigInformer.Lister().ConfigMaps(defaults.OpenShiftConfigNamespace),
7477
kubeconfig: kubeconfig,
7578
queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "AzurePathFixController"),
7679
}
@@ -105,6 +108,11 @@ func NewAzurePathFixController(
105108
}
106109
c.cachesToSync = append(c.cachesToSync, proxyInformer.Informer().HasSynced)
107110

111+
if _, err := openshiftConfigInformer.Informer().AddEventHandler(c.eventHandler()); err != nil {
112+
return nil, err
113+
}
114+
c.cachesToSync = append(c.cachesToSync, openshiftConfigInformer.Informer().HasSynced)
115+
108116
// bootstrap the job if it doesn't exist
109117
c.queue.Add("instance")
110118

@@ -182,6 +190,7 @@ func (c *AzurePathFixController) sync() error {
182190
c.secretLister,
183191
c.infrastructureLister,
184192
c.proxyLister,
193+
c.openshiftConfigLister,
185194
imageRegistryConfig,
186195
c.kubeconfig,
187196
)

pkg/operator/starter.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ func RunOperator(ctx context.Context, kubeconfig *restclient.Config) error {
177177
configInformers.Config().V1().Infrastructures(),
178178
kubeInformers.Core().V1().Secrets(),
179179
configInformers.Config().V1().Proxies(),
180+
kubeInformersForOpenShiftConfig.Core().V1().ConfigMaps(),
180181
kubeInformers.Core().V1().Pods(),
181182
)
182183
if err != nil {

pkg/resource/azurepathfixjob.go

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@ import (
2727
var _ Mutator = &generatorAzurePathFixJob{}
2828

2929
type generatorAzurePathFixJob struct {
30-
lister batchlisters.JobNamespaceLister
31-
secretLister corev1listers.SecretNamespaceLister
32-
infrastructureLister configlisters.InfrastructureLister
33-
proxyLister configlisters.ProxyLister
34-
client batchset.BatchV1Interface
35-
cr *imageregistryv1.Config
36-
kubeconfig *restclient.Config
30+
lister batchlisters.JobNamespaceLister
31+
secretLister corev1listers.SecretNamespaceLister
32+
infrastructureLister configlisters.InfrastructureLister
33+
proxyLister configlisters.ProxyLister
34+
openshiftConfigLister corev1listers.ConfigMapNamespaceLister
35+
client batchset.BatchV1Interface
36+
cr *imageregistryv1.Config
37+
kubeconfig *restclient.Config
3738
}
3839

3940
func NewGeneratorAzurePathFixJob(
@@ -42,17 +43,19 @@ func NewGeneratorAzurePathFixJob(
4243
secretLister corev1listers.SecretNamespaceLister,
4344
infrastructureLister configlisters.InfrastructureLister,
4445
proxyLister configlisters.ProxyLister,
46+
openshiftConfigLister corev1listers.ConfigMapNamespaceLister,
4547
cr *imageregistryv1.Config,
4648
kubeconfig *restclient.Config,
4749
) *generatorAzurePathFixJob {
4850
return &generatorAzurePathFixJob{
49-
lister: lister,
50-
client: client,
51-
cr: cr,
52-
infrastructureLister: infrastructureLister,
53-
secretLister: secretLister,
54-
proxyLister: proxyLister,
55-
kubeconfig: kubeconfig,
51+
lister: lister,
52+
client: client,
53+
cr: cr,
54+
infrastructureLister: infrastructureLister,
55+
secretLister: secretLister,
56+
proxyLister: proxyLister,
57+
openshiftConfigLister: openshiftConfigLister,
58+
kubeconfig: kubeconfig,
5659
}
5760
}
5861

@@ -88,6 +91,7 @@ func (gapfj *generatorAzurePathFixJob) expected() (runtime.Object, error) {
8891

8992
optional := true
9093
envs := []corev1.EnvVar{
94+
{Name: "AZURE_ENVIRONMENT_FILEPATH", Value: os.Getenv("AZURE_ENVIRONMENT_FILEPATH")},
9195
{Name: "AZURE_STORAGE_ACCOUNT_NAME", Value: azureStorage.AccountName},
9296
{Name: "AZURE_CONTAINER_NAME", Value: azureStorage.Container},
9397
{Name: "AZURE_CLIENT_ID", Value: azureCfg.ClientID},
@@ -105,6 +109,17 @@ func (gapfj *generatorAzurePathFixJob) expected() (runtime.Object, error) {
105109
}},
106110
}
107111

112+
// for Azure Stack Hub, the move-blobs command needs to know the endpoints,
113+
// and those come from the cloud-provider-config in the openshift-config
114+
// namespace.
115+
cm, err := gapfj.openshiftConfigLister.Get("cloud-provider-config")
116+
if err != nil && !errors.IsNotFound(err) {
117+
return nil, err
118+
}
119+
if cm != nil {
120+
envs = append(envs, corev1.EnvVar{Name: "AZURE_ENVIRONMENT_FILECONTENTS", Value: cm.Data["endpoints"]})
121+
}
122+
108123
if len(azureStorage.CloudName) > 0 {
109124
envs = append(envs, corev1.EnvVar{Name: "AZURE_ENVIRONMENT", Value: azureStorage.CloudName})
110125
}

0 commit comments

Comments
 (0)