Skip to content

Commit 5cef425

Browse files
authored
don't create rbac in master namespace (#34)
1 parent 76c0fdc commit 5cef425

File tree

3 files changed

+66
-3
lines changed

3 files changed

+66
-3
lines changed

controllers/common/util.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,16 @@
1717
package common
1818

1919
import (
20+
"fmt"
21+
"io/ioutil"
22+
"os"
2023
"sort"
24+
"strings"
2125

2226
gset "github.com/deckarep/golang-set"
2327
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2428
"k8s.io/apimachinery/pkg/types"
29+
"k8s.io/klog"
2530
)
2631

2732
func MakeSet(strs []string) gset.Set {
@@ -97,3 +102,23 @@ func Reverse(original []string) []string {
97102
}
98103
return reversed
99104
}
105+
106+
// GetOperatorNamespace returns the namespace the operator should be running in.
107+
func GetOperatorNamespace() (string, error) {
108+
ns, found := os.LookupEnv("OPERATOR_NAMESPACE")
109+
if !found {
110+
nsBytes, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace")
111+
if err != nil {
112+
if os.IsNotExist(err) {
113+
return "", fmt.Errorf("namespace not found for current environment")
114+
}
115+
return "", err
116+
}
117+
ns = strings.TrimSpace(string(nsBytes))
118+
}
119+
if len(ns) == 0 {
120+
return "", fmt.Errorf("operator namespace is empty")
121+
}
122+
klog.V(1).Info("Found namespace", "Namespace", ns)
123+
return ns, nil
124+
}

controllers/namespacescope_controller.go

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,17 @@ func (r *NamespaceScopeReconciler) PushRbacToNamespace(instance *operatorv1.Name
211211
"namespace-scope-configmap": instance.Namespace + "-" + instance.Spec.ConfigmapName,
212212
}
213213

214+
operatorNs, err := util.GetOperatorNamespace()
215+
if err != nil {
216+
klog.Error("get operator namespace failed: ", err)
217+
return err
218+
}
219+
214220
for _, toNs := range instance.Spec.NamespaceMembers {
221+
if toNs == operatorNs {
222+
continue
223+
}
224+
215225
if err := r.CreateRole(labels, toNs); err != nil {
216226
if errors.IsForbidden(err) {
217227
r.Recorder.Eventf(instance, corev1.EventTypeWarning, "Forbidden", "cannot create resource roles in API group rbac.authorization.k8s.io in the namespace %s", toNs)
@@ -248,7 +258,18 @@ func (r *NamespaceScopeReconciler) DeleteRbacFromUnmanagedNamespace(instance *op
248258
labels := map[string]string{
249259
"namespace-scope-configmap": instance.Namespace + "-" + instance.Spec.ConfigmapName,
250260
}
261+
262+
operatorNs, err := util.GetOperatorNamespace()
263+
if err != nil {
264+
klog.Error("get operator namespace failed: ", err)
265+
return err
266+
}
267+
251268
for _, toNs := range unmanagedNss {
269+
if toNs == operatorNs {
270+
continue
271+
}
272+
252273
if err := r.DeleteRoleBinding(labels, toNs); err != nil {
253274
if errors.IsForbidden(err) {
254275
r.Recorder.Eventf(instance, corev1.EventTypeWarning, "Forbidden", "cannot delete resource rolebindings in API group rbac.authorization.k8s.io in the namespace %s", toNs)
@@ -273,7 +294,17 @@ func (r *NamespaceScopeReconciler) DeleteAllRbac(instance *operatorv1.NamespaceS
273294
labels := map[string]string{
274295
"namespace-scope-configmap": instance.Namespace + "-" + instance.Spec.ConfigmapName,
275296
}
297+
298+
operatorNs, err := util.GetOperatorNamespace()
299+
if err != nil {
300+
klog.Error("get operator namespace failed: ", err)
301+
return err
302+
}
303+
276304
for _, toNs := range instance.Spec.NamespaceMembers {
305+
if toNs == operatorNs {
306+
continue
307+
}
277308
if err := r.DeleteRoleBinding(labels, toNs); err != nil {
278309
if errors.IsForbidden(err) {
279310
r.Recorder.Eventf(instance, corev1.EventTypeWarning, "Forbidden", "cannot delete resource rolebindings in API group rbac.authorization.k8s.io in the namespace %s", toNs)
@@ -463,8 +494,8 @@ func (r *NamespaceScopeReconciler) getNamespaceList(instance *operatorv1.Namespa
463494
klog.Errorf("Cannot list namespacescope with in namespace %s: %v", instance.Namespace, err)
464495
return nil, err
465496
}
466-
for _, cr := range crList.Items {
467-
cr := setDefaults(&cr)
497+
for i := range crList.Items {
498+
cr := setDefaults(&crList.Items[i])
468499
if !cr.GetDeletionTimestamp().IsZero() {
469500
continue
470501
}

main.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434

3535
operatorv1 "github.com/IBM/ibm-namespace-scope-operator/api/v1"
3636
"github.com/IBM/ibm-namespace-scope-operator/controllers"
37+
util "github.com/IBM/ibm-namespace-scope-operator/controllers/common"
3738
// +kubebuilder:scaffold:imports
3839
)
3940

@@ -65,10 +66,16 @@ func main() {
6566
rbacv1.SchemeGroupVersion.WithKind("RoleBinding"): {LabelSelector: "namespace-scope-configmap"},
6667
}
6768

69+
operatorNs, err := util.GetOperatorNamespace()
70+
if err != nil {
71+
klog.Error("Failed to get operator namespace: ", err)
72+
os.Exit(1)
73+
}
74+
6875
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
6976
Scheme: scheme,
7077
MetricsBindAddress: metricsAddr,
71-
Namespace: os.Getenv("OPERATOR_NAMESPACE"),
78+
Namespace: operatorNs,
7279
Port: 9443,
7380
LeaderElection: enableLeaderElection,
7481
LeaderElectionID: "6a4a72f9.ibm.com",

0 commit comments

Comments
 (0)