Skip to content

Commit 6c44803

Browse files
authored
Merge pull request #4 from ashimagarg27/cleanup
Code Cleanup
2 parents 0c07caa + 3f0fccf commit 6c44803

File tree

8 files changed

+11
-100
lines changed

8 files changed

+11
-100
lines changed

config/manager/kustomization.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ kind: Kustomization
55
images:
66
- name: controller
77
newName: ashimagarg/csi
8-
newTag: "39"
8+
newTag: "41"

config/manager/manager.yaml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,6 @@ spec:
7373
image: controller:latest
7474
imagePullPolicy: Always
7575
name: manager
76-
env:
77-
- name: WATCH_NAMESPACE
78-
#value: default
79-
valueFrom:
80-
fieldRef:
81-
fieldPath: metadata.namespace
8276
securityContext:
8377
allowPrivilegeEscalation: false
8478
capabilities:

controllers/ibmobjectcsi_controller.go

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,7 @@ var csiLog = logf.Log.WithName("ibmobjectcsi_controller")
5656
type IBMObjectCSIReconciler struct {
5757
client.Client
5858
Scheme *runtime.Scheme
59-
Namespace string
6059
Recorder record.EventRecorder
61-
ServerVersion string
6260
ControllerHelper *common.ControllerHelper
6361
}
6462

@@ -102,7 +100,7 @@ func (r *IBMObjectCSIReconciler) Reconcile(ctx context.Context, req ctrl.Request
102100
r.ControllerHelper.Log = csiLog
103101

104102
// Fetch the CSIDriver instance
105-
instance := crutils.New(&csiv1alpha1.IBMObjectCSI{}, r.ServerVersion)
103+
instance := crutils.New(&csiv1alpha1.IBMObjectCSI{})
106104
err := r.Get(context.TODO(), req.NamespacedName, instance.Unwrap())
107105
if err != nil {
108106
if errors.IsNotFound(err) {
@@ -130,17 +128,11 @@ func (r *IBMObjectCSIReconciler) Reconcile(ctx context.Context, req ctrl.Request
130128
}
131129
// If the deletion timestamp is set, perform cleanup operations and remove a finalizer before returning from the reconciliation process.
132130
if !instance.GetDeletionTimestamp().IsZero() {
133-
isFinalizerExists, err := r.ControllerHelper.HasFinalizer(instance)
134-
if err != nil {
131+
if err := r.deleteClusterRoleBindings(instance); err != nil {
135132
return reconcile.Result{}, err
136133
}
137-
// If the finalizer doesn't exist, return early, indicating that no further action is needed.
138-
139-
if !isFinalizerExists {
140-
return reconcile.Result{}, nil
141-
}
142134

143-
if err := r.deleteClusterRolesAndBindings(instance); err != nil {
135+
if err := r.deleteClusterRoles(instance); err != nil {
144136
return reconcile.Result{}, err
145137
}
146138

@@ -462,17 +454,6 @@ func (r *IBMObjectCSIReconciler) deleteCSIDriver(instance *crutils.IBMObjectCSI)
462454
return nil
463455
}
464456

465-
func (r *IBMObjectCSIReconciler) deleteClusterRolesAndBindings(instance *crutils.IBMObjectCSI) error {
466-
if err := r.deleteClusterRoleBindings(instance); err != nil {
467-
return err
468-
}
469-
470-
if err := r.deleteClusterRoles(instance); err != nil {
471-
return err
472-
}
473-
return nil
474-
}
475-
476457
func (r *IBMObjectCSIReconciler) deleteClusterRoleBindings(instance *crutils.IBMObjectCSI) error {
477458
clusterRoleBindings := r.getClusterRoleBindings(instance)
478459
return r.ControllerHelper.DeleteClusterRoleBindings(clusterRoleBindings)

controllers/internal/crutils/default_setter.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ import (
99
// Replace it with kubernetes native default setter when it is available.
1010
// https://kubernetes.io/docs/tasks/access-kubernetes-api/custom-resources/custom-resource-definitions/#defaulting
1111
func (c *IBMObjectCSI) SetDefaults() bool {
12-
1312
c.setDefaultForNilSliceFields()
14-
1513
return c.setDefaults()
1614
}
1715

@@ -35,7 +33,6 @@ func (c *IBMObjectCSI) setDefaults() bool {
3533
}
3634

3735
changed = c.setDefaultSidecars() || changed
38-
3936
return changed
4037
}
4138

@@ -57,11 +54,8 @@ func (c *IBMObjectCSI) setDefaultSidecars() bool {
5754

5855
if len(defaultSidecars) == len(c.Spec.Sidecars) {
5956
for _, sidecar := range c.Spec.Sidecars {
60-
if defaultSidecar, found := config.DefaultSidecarsByName[sidecar.Name]; found {
61-
if sidecar != defaultSidecar {
62-
change = true
63-
}
64-
} else {
57+
defaultSidecar, found := config.DefaultSidecarsByName[sidecar.Name]
58+
if (found && sidecar != defaultSidecar) || !found {
6559
change = true
6660
}
6761
}

controllers/internal/crutils/ibmobjectcsi.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,12 @@ import (
1313
// IBMObjectCSI is the wrapper for csiv1alpha1.IBMObjectCSI type
1414
type IBMObjectCSI struct {
1515
*csiv1alpha1.IBMObjectCSI
16-
ServerVersion string
1716
}
1817

1918
// New returns a wrapper for csiv1.IBMObjectCSI
20-
func New(c *csiv1alpha1.IBMObjectCSI, serverVersion string) *IBMObjectCSI {
19+
func New(c *csiv1alpha1.IBMObjectCSI) *IBMObjectCSI {
2120
return &IBMObjectCSI{
22-
IBMObjectCSI: c,
23-
ServerVersion: serverVersion,
21+
IBMObjectCSI: c,
2422
}
2523
}
2624

controllers/util/common/common.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -178,15 +178,6 @@ func (ch *ControllerHelper) getClusterRole(cr *rbacv1.ClusterRole) (*rbacv1.Clus
178178
return found, err
179179
}
180180

181-
func (ch *ControllerHelper) HasFinalizer(instance crutils.Instance) (bool, error) {
182-
accessor, finalizerName, err := ch.getAccessorAndFinalizerName(instance)
183-
if err != nil {
184-
return false, err
185-
}
186-
187-
return util.Contains(accessor.GetFinalizers(), finalizerName), nil
188-
}
189-
190181
func (ch *ControllerHelper) AddFinalizerIfNotPresent(instance crutils.Instance,
191182
unwrappedInstance client.Object) error {
192183
logger := ch.Log.WithName("AddFinalizerIfNotPresent")

main.go

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package main
1818

1919
import (
2020
"flag"
21-
"fmt"
2221
"os"
2322

2423
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
@@ -41,10 +40,9 @@ import (
4140
)
4241

4342
var (
44-
scheme = runtime.NewScheme()
45-
setupLog = ctrl.Log.WithName("setup")
46-
watchNamespaceEnvVar = "WATCH_NAMESPACE"
47-
log = logf.Log.WithName("cmd")
43+
scheme = runtime.NewScheme()
44+
setupLog = ctrl.Log.WithName("setup")
45+
log = logf.Log.WithName("cmd")
4846
)
4947

5048
func init() {
@@ -78,12 +76,6 @@ func main() {
7876
os.Exit(1)
7977
}
8078

81-
namespace, err := getWatchNamespace()
82-
if err != nil {
83-
log.Error(err, "Failed to get watch namespace")
84-
os.Exit(1)
85-
}
86-
8779
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
8880
Scheme: scheme,
8981
Metrics: metricsserver.Options{BindAddress: metricsAddr},
@@ -112,7 +104,6 @@ func main() {
112104
if err = (&controllers.IBMObjectCSIReconciler{
113105
Client: mgr.GetClient(),
114106
Scheme: mgr.GetScheme(),
115-
Namespace: namespace,
116107
ControllerHelper: controllerHelper,
117108
}).SetupWithManager(mgr); err != nil {
118109
setupLog.Error(err, "unable to create controller", "controller", "IBMObjectCSI")
@@ -142,11 +133,3 @@ func main() {
142133
os.Exit(1)
143134
}
144135
}
145-
146-
func getWatchNamespace() (string, error) {
147-
ns, found := os.LookupEnv(watchNamespaceEnvVar)
148-
if !found {
149-
return "", fmt.Errorf("%s must be set", watchNamespaceEnvVar)
150-
}
151-
return ns, nil
152-
}

pkg/util/boolptr/boolptr.go

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,5 @@
11
package boolptr
22

3-
// IsTrue returns true if and only if the bool pointer is non-nil and set to true.
4-
func IsTrue(b *bool) bool {
5-
return b != nil && *b
6-
}
7-
8-
// IsFalse returns true if and only if the bool pointer is non-nil and set to false.
9-
func IsFalse(b *bool) bool {
10-
return b != nil && !*b
11-
}
12-
133
// True returns a *bool whose underlying value is true.
144
func True() *bool {
155
t := true
@@ -21,23 +11,3 @@ func False() *bool {
2111
t := false
2212
return &t
2313
}
24-
25-
// Equal returns true if and only if both values are set and equal.
26-
func Equal(a, b *bool) bool {
27-
if a == nil || b == nil {
28-
return false
29-
} else {
30-
return *a == *b
31-
}
32-
}
33-
34-
// Equal returns true if both values are set and equal or both are nil.
35-
func NilOrEqual(a, b *bool) bool {
36-
if a == nil && b == nil {
37-
return true
38-
} else if a == nil || b == nil {
39-
return false
40-
} else {
41-
return *a == *b
42-
}
43-
}

0 commit comments

Comments
 (0)