Skip to content

Commit ff600d2

Browse files
authored
Merge pull request #1220 from nader-ziada/manager-namespace
[backport-release-0.4] dont use hard-coded value for manager namespace
2 parents 50376f2 + 4ce4313 commit ff600d2

File tree

5 files changed

+95
-7
lines changed

5 files changed

+95
-7
lines changed

api/v1alpha3/types.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ import (
2121
)
2222

2323
const (
24-
// ControllerNamespace is the namespace where controller manager will run
25-
ControllerNamespace = "capz-system"
2624
// ControlPlane machine label
2725
ControlPlane string = "control-plane"
2826
// Node machine label

cloud/scope/identity.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/pkg/errors"
2626
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1alpha3"
2727
"sigs.k8s.io/cluster-api-provider-azure/util/identity"
28+
"sigs.k8s.io/cluster-api-provider-azure/util/system"
2829
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
2930
clusterctl "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"
3031
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -83,7 +84,7 @@ func (p *AzureCredentialsProvider) GetAuthorizer(ctx context.Context, resourceMa
8384
},
8485
ObjectMeta: metav1.ObjectMeta{
8586
Name: identity.GetAzureIdentityName(p.AzureCluster.Name, p.AzureCluster.Namespace, p.Identity.Name),
86-
Namespace: infrav1.ControllerNamespace,
87+
Namespace: system.GetManagerNamespace(),
8788
Annotations: map[string]string{
8889
aadpodv1.BehaviorKey: "namespaced",
8990
},
@@ -104,7 +105,7 @@ func (p *AzureCredentialsProvider) GetAuthorizer(ctx context.Context, resourceMa
104105
}
105106
err = p.Client.Create(ctx, copiedIdentity)
106107
if err != nil && !apierrors.IsAlreadyExists(err) {
107-
return nil, errors.Errorf("failed to create copied AzureIdentity %s in %s: %v", copiedIdentity.Name, infrav1.ControllerNamespace, err)
108+
return nil, errors.Errorf("failed to create copied AzureIdentity %s in %s: %v", copiedIdentity.Name, system.GetManagerNamespace(), err)
108109
}
109110

110111
azureIdentityBinding := &aadpodv1.AzureIdentityBinding{
@@ -129,7 +130,7 @@ func (p *AzureCredentialsProvider) GetAuthorizer(ctx context.Context, resourceMa
129130
}
130131
err = p.Client.Create(ctx, azureIdentityBinding)
131132
if err != nil && !apierrors.IsAlreadyExists(err) {
132-
return nil, errors.Errorf("failed to create AzureIdentityBinding %s in %s: %v", copiedIdentity.Name, infrav1.ControllerNamespace, err)
133+
return nil, errors.Errorf("failed to create AzureIdentityBinding %s in %s: %v", copiedIdentity.Name, system.GetManagerNamespace(), err)
133134
}
134135

135136
var spt *adal.ServicePrincipalToken

controllers/azureidentity_controller.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1alpha3"
3232
"sigs.k8s.io/cluster-api-provider-azure/util/identity"
3333
"sigs.k8s.io/cluster-api-provider-azure/util/reconciler"
34+
"sigs.k8s.io/cluster-api-provider-azure/util/system"
3435
"sigs.k8s.io/cluster-api-provider-azure/util/tele"
3536
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
3637
"sigs.k8s.io/cluster-api/util"
@@ -112,7 +113,7 @@ func (r *AzureIdentityReconciler) Reconcile(req ctrl.Request) (_ ctrl.Result, re
112113

113114
// get all the bindings
114115
var bindings aadpodv1.AzureIdentityBindingList
115-
if err := r.List(ctx, &bindings, client.InNamespace(infrav1.ControllerNamespace)); err != nil {
116+
if err := r.List(ctx, &bindings, client.InNamespace(system.GetManagerNamespace())); err != nil {
116117
return ctrl.Result{}, err
117118
}
118119

@@ -151,7 +152,7 @@ func (r *AzureIdentityReconciler) Reconcile(req ctrl.Request) (_ ctrl.Result, re
151152
return ctrl.Result{}, err
152153
}
153154
azureIdentity := &aadpodv1.AzureIdentity{}
154-
if err := r.Client.Get(ctx, client.ObjectKey{Name: identityName, Namespace: infrav1.ControllerNamespace}, azureIdentity); err != nil {
155+
if err := r.Client.Get(ctx, client.ObjectKey{Name: identityName, Namespace: system.GetManagerNamespace()}, azureIdentity); err != nil {
155156
log.Error(err, "failed to fetch AzureIdentity")
156157
return ctrl.Result{}, err
157158
}

util/system/namespace.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
Copyright 2021 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package system
18+
19+
import "os"
20+
21+
const (
22+
// NamespaceEnvVarName is the env var coming from DownwardAPI in the manager manifest
23+
NamespaceEnvVarName = "POD_NAMESPACE"
24+
// DefaultNamespace is the default value from manifest
25+
DefaultNamespace = "capz-system"
26+
)
27+
28+
// GetManagerNamespace returns the namespace where the controller is running
29+
func GetManagerNamespace() string {
30+
managerNamespace := os.Getenv(NamespaceEnvVarName)
31+
if managerNamespace == "" {
32+
managerNamespace = DefaultNamespace
33+
}
34+
return managerNamespace
35+
}

util/system/namespace_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
Copyright 2021 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package system
18+
19+
import (
20+
"os"
21+
"testing"
22+
23+
"github.com/onsi/gomega"
24+
)
25+
26+
func TestGetNamespace(t *testing.T) {
27+
cases := []struct {
28+
Name string
29+
PodNamespace string
30+
Expected string
31+
}{
32+
{
33+
Name: "env var set to custom namespace",
34+
PodNamespace: "capz",
35+
Expected: "capz",
36+
},
37+
{
38+
Name: "env var empty",
39+
PodNamespace: "",
40+
Expected: "capz-system",
41+
},
42+
}
43+
44+
for _, c := range cases {
45+
c := c
46+
t.Run(c.Name, func(t *testing.T) {
47+
t.Parallel()
48+
g := gomega.NewWithT(t)
49+
os.Setenv(NamespaceEnvVarName, c.PodNamespace)
50+
g.Expect(GetManagerNamespace()).To(gomega.Equal(c.Expected))
51+
})
52+
}
53+
}

0 commit comments

Comments
 (0)