Skip to content

Commit b89c20b

Browse files
bumped to operator utils v1.1.2 (#91)
improved context propagation fixed #90 Signed-off-by: raffaelespazzoli <[email protected]>
1 parent 616ec5d commit b89c20b

File tree

7 files changed

+21
-21
lines changed

7 files changed

+21
-21
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ spec:
141141

142142
Although not enforced by the operator the general expectation is that the NamespaceConfig CR will be used to create objects inside the selected namespace.
143143

144+
The `default` namespace and all namespaces starting with either `kube-` or `openshift-` are never considered by this operator. This is a safety feature to ensure that this operator does not interfere with the core of the system.
145+
144146
Examples of NamespaceConfig usages can be found [here](./examples/namespace-config/readme.md)
145147

146148
## GroupConfig

controllers/groupconfig_controller.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func (r *GroupConfigReconciler) Reconcile(context context.Context, req ctrl.Requ
106106
}
107107

108108
//get selected users
109-
selectedGroups, err := r.getSelectedGroups(instance)
109+
selectedGroups, err := r.getSelectedGroups(context, instance)
110110
if err != nil {
111111
log.Error(err, "unable to get groups selected by", "GroupConfig", instance)
112112
return r.ManageError(context, instance, err)
@@ -140,7 +140,7 @@ func (r *GroupConfigReconciler) getResourceList(instance *redhatcopv1alpha1.Grou
140140
return lockedresources, nil
141141
}
142142

143-
func (r *GroupConfigReconciler) getSelectedGroups(instance *redhatcopv1alpha1.GroupConfig) ([]userv1.Group, error) {
143+
func (r *GroupConfigReconciler) getSelectedGroups(context context.Context, instance *redhatcopv1alpha1.GroupConfig) ([]userv1.Group, error) {
144144
groupList := &userv1.GroupList{}
145145

146146
labelSelector, err := metav1.LabelSelectorAsSelector(&instance.Spec.LabelSelector)
@@ -155,7 +155,7 @@ func (r *GroupConfigReconciler) getSelectedGroups(instance *redhatcopv1alpha1.Gr
155155
return []userv1.Group{}, err
156156
}
157157

158-
err = r.GetClient().List(context.TODO(), groupList, &client.ListOptions{
158+
err = r.GetClient().List(context, groupList, &client.ListOptions{
159159
LabelSelector: labelSelector,
160160
})
161161
if err != nil {

controllers/namespaceconfig_controller.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ type NamespaceConfigReconciler struct {
6464
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile
6565
func (r *NamespaceConfigReconciler) Reconcile(context context.Context, req ctrl.Request) (ctrl.Result, error) {
6666
log := r.Log.WithValues("namespaceconfig", req.NamespacedName)
67-
67+
log.Info("reconciling started")
6868
// Fetch the NamespaceConfig instance
6969
instance := &redhatcopv1alpha1.NamespaceConfig{}
7070
err := r.GetClient().Get(context, req.NamespacedName, instance)
@@ -78,7 +78,6 @@ func (r *NamespaceConfigReconciler) Reconcile(context context.Context, req ctrl.
7878
// Error reading the object - requeue the request.
7979
return reconcile.Result{}, err
8080
}
81-
8281
if !r.IsInitialized(instance) {
8382
err := r.GetClient().Update(context, instance)
8483
if err != nil {
@@ -105,9 +104,8 @@ func (r *NamespaceConfigReconciler) Reconcile(context context.Context, req ctrl.
105104
}
106105
return reconcile.Result{}, nil
107106
}
108-
109-
//get selected users
110-
selectedNamespaces, err := r.getSelectedNamespaces(instance)
107+
//get selected namespaces
108+
selectedNamespaces, err := r.getSelectedNamespaces(context, instance)
111109
if err != nil {
112110
log.Error(err, "unable to get namespaces selected by", "NamespaceConfig", instance)
113111
return r.ManageError(context, instance, err)
@@ -172,7 +170,7 @@ func (r *NamespaceConfigReconciler) getResourceList(instance *redhatcopv1alpha1.
172170
return lockedresources, nil
173171
}
174172

175-
func (r *NamespaceConfigReconciler) getSelectedNamespaces(namespaceconfig *redhatcopv1alpha1.NamespaceConfig) ([]corev1.Namespace, error) {
173+
func (r *NamespaceConfigReconciler) getSelectedNamespaces(context context.Context, namespaceconfig *redhatcopv1alpha1.NamespaceConfig) ([]corev1.Namespace, error) {
176174
nl := corev1.NamespaceList{}
177175
selector, err := metav1.LabelSelectorAsSelector(&namespaceconfig.Spec.LabelSelector)
178176
if err != nil {
@@ -186,7 +184,7 @@ func (r *NamespaceConfigReconciler) getSelectedNamespaces(namespaceconfig *redha
186184
return []corev1.Namespace{}, err
187185
}
188186

189-
err = r.GetClient().List(context.TODO(), &nl, &client.ListOptions{LabelSelector: selector})
187+
err = r.GetClient().List(context, &nl, &client.ListOptions{LabelSelector: selector})
190188
if err != nil {
191189
r.Log.Error(err, "unable to list namespaces with selector", "selector", selector)
192190
return []corev1.Namespace{}, err
@@ -239,7 +237,7 @@ func (r *NamespaceConfigReconciler) findApplicableNameSpaceConfigs(namespace cor
239237
}
240238

241239
func isProhibitedNamespaceName(name string) bool {
242-
return name == "default" || strings.HasPrefix(name, "openshift") || strings.HasPrefix(name, "kube")
240+
return name == "default" || strings.HasPrefix(name, "openshift-") || strings.HasPrefix(name, "kube-")
243241
}
244242

245243
// SetupWithManager sets up the controller with the Manager.

controllers/userconfig_controller.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func (r *UserConfigReconciler) Reconcile(context context.Context, req ctrl.Reque
107107
}
108108

109109
//get selected users
110-
selectedUsers, err := r.getSelectedUsers(instance)
110+
selectedUsers, err := r.getSelectedUsers(context, instance)
111111
if err != nil {
112112
log.Error(err, "unable to get users selected by", "UserConfig", instance)
113113
return r.ManageError(context, instance, err)
@@ -141,17 +141,17 @@ func (r *UserConfigReconciler) getResourceList(instance *redhatcopv1alpha1.UserC
141141
return lockedresources, nil
142142
}
143143

144-
func (r *UserConfigReconciler) getSelectedUsers(instance *redhatcopv1alpha1.UserConfig) ([]userv1.User, error) {
144+
func (r *UserConfigReconciler) getSelectedUsers(context context.Context, instance *redhatcopv1alpha1.UserConfig) ([]userv1.User, error) {
145145
userList := &userv1.UserList{}
146146
identitiesList := &userv1.IdentityList{}
147147

148-
err := r.GetClient().List(context.TODO(), userList, &client.ListOptions{})
148+
err := r.GetClient().List(context, userList, &client.ListOptions{})
149149
if err != nil {
150150
r.Log.Error(err, "unable to get all users")
151151
return []userv1.User{}, err
152152
}
153153

154-
err = r.GetClient().List(context.TODO(), identitiesList, &client.ListOptions{})
154+
err = r.GetClient().List(context, identitiesList, &client.ListOptions{})
155155
if err != nil {
156156
r.Log.Error(err, "unable to get all identities")
157157
return []userv1.User{}, err

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require (
77
github.com/onsi/ginkgo v1.14.1
88
github.com/onsi/gomega v1.10.2
99
github.com/openshift/api v3.9.0+incompatible
10-
github.com/redhat-cop/operator-utils v1.0.0
10+
github.com/redhat-cop/operator-utils v1.1.2
1111
github.com/scylladb/go-set v1.0.2
1212
k8s.io/api v0.20.0
1313
k8s.io/apimachinery v0.20.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -451,8 +451,8 @@ github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4O
451451
github.com/prometheus/procfs v0.2.0 h1:wH4vA7pcjKuZzjF7lM8awk4fnuJO6idemZXoKnULUx4=
452452
github.com/prometheus/procfs v0.2.0/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU=
453453
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
454-
github.com/redhat-cop/operator-utils v1.0.0 h1:h6/0vvWny3Ac6RtR8p6ds4x7ZIz6L7JF6czxV9T/tpE=
455-
github.com/redhat-cop/operator-utils v1.0.0/go.mod h1:8YX6iLy5xOOY9VF6PT5xjv7Tul249ALwnQFXS206Sss=
454+
github.com/redhat-cop/operator-utils v1.1.2 h1:jQjQs3U5ayzHPNFfiadEFmH/z/oE3OFJLJ9Kv++KH8U=
455+
github.com/redhat-cop/operator-utils v1.1.2/go.mod h1:d/7g1ZoiKNDjRjgUsqn7jzrWHKNCX2RNDbPmgXUgJN0=
456456
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
457457
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
458458
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=

main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,15 @@ func main() {
8383
}
8484

8585
if err = (&controllers.NamespaceConfigReconciler{
86-
EnforcingReconciler: lockedresourcecontroller.NewEnforcingReconciler(mgr.GetClient(), mgr.GetScheme(), mgr.GetConfig(), mgr.GetEventRecorderFor("NamespaceConfig_controller"), true),
86+
EnforcingReconciler: lockedresourcecontroller.NewEnforcingReconciler(mgr.GetClient(), mgr.GetScheme(), mgr.GetConfig(), mgr.GetAPIReader(), mgr.GetEventRecorderFor("NamespaceConfig_controller"), true),
8787
Log: ctrl.Log.WithName("controllers").WithName("NamespaceConfig"),
8888
}).SetupWithManager(mgr); err != nil {
8989
setupLog.Error(err, "unable to create controller", "controller", "NamespaceConfig")
9090
os.Exit(1)
9191
}
9292

9393
userConfigController := &controllers.UserConfigReconciler{
94-
EnforcingReconciler: lockedresourcecontroller.NewEnforcingReconciler(mgr.GetClient(), mgr.GetScheme(), mgr.GetConfig(), mgr.GetEventRecorderFor("UserConfig_controller"), true),
94+
EnforcingReconciler: lockedresourcecontroller.NewEnforcingReconciler(mgr.GetClient(), mgr.GetScheme(), mgr.GetConfig(), mgr.GetAPIReader(), mgr.GetEventRecorderFor("UserConfig_controller"), true),
9595
Log: ctrl.Log.WithName("controllers").WithName("UserConfig"),
9696
}
9797

@@ -112,7 +112,7 @@ func main() {
112112
}
113113

114114
groupConfigController := &controllers.GroupConfigReconciler{
115-
EnforcingReconciler: lockedresourcecontroller.NewEnforcingReconciler(mgr.GetClient(), mgr.GetScheme(), mgr.GetConfig(), mgr.GetEventRecorderFor("GroupConfig_controller"), true),
115+
EnforcingReconciler: lockedresourcecontroller.NewEnforcingReconciler(mgr.GetClient(), mgr.GetScheme(), mgr.GetConfig(), mgr.GetAPIReader(), mgr.GetEventRecorderFor("GroupConfig_controller"), true),
116116
Log: ctrl.Log.WithName("controllers").WithName("GroupConfig"),
117117
}
118118

0 commit comments

Comments
 (0)