Skip to content

Commit

Permalink
fixup! fix: NAC install validation
Browse files Browse the repository at this point in the history
use another client

Signed-off-by: Mateus Oliveira <[email protected]>
  • Loading branch information
mateusoliveira43 committed Nov 13, 2024
1 parent 96d4056 commit acab81f
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 32 deletions.
16 changes: 8 additions & 8 deletions controllers/dpa_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ import (
// DPAReconciler reconciles a Velero object
type DPAReconciler struct {
client.Client
Scheme *runtime.Scheme
Log logr.Logger
Context context.Context
NamespacedName types.NamespacedName
EventRecorder record.EventRecorder
dpa *oadpv1alpha1.DataProtectionApplication
OADPNamespace string
Scheme *runtime.Scheme
Log logr.Logger
Context context.Context
NamespacedName types.NamespacedName
EventRecorder record.EventRecorder
dpa *oadpv1alpha1.DataProtectionApplication
ClusterWideClient client.Client
}

var debugMode = os.Getenv("DEBUG") == "true"
Expand Down Expand Up @@ -149,7 +149,7 @@ func (r *DPAReconciler) SetupWithManager(mgr ctrl.Manager) error {
Owns(&routev1.Route{}).
Owns(&corev1.ConfigMap{}).
Watches(&corev1.Secret{}, &labelHandler{}).
WithEventFilter(veleroPredicate(r.Scheme, r.OADPNamespace)).
WithEventFilter(veleroPredicate(r.Scheme)).
Complete(r)
}

Expand Down
11 changes: 7 additions & 4 deletions controllers/nonadmin_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
corev1 "k8s.io/api/core/v1"
k8serror "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -84,15 +85,17 @@ func (r *DPAReconciler) ReconcileNonAdminController(log logr.Logger) (bool, erro
return true, nil
}

selector, err := fields.ParseSelector(fmt.Sprintf("metadata.namespace!=%s", r.NamespacedName.Namespace))
if err != nil {
return false, err
}
dpaList := &oadpv1alpha1.DataProtectionApplicationList{}
err := r.List(r.Context, dpaList)
err = r.ClusterWideClient.List(r.Context, dpaList, &client.ListOptions{FieldSelector: selector})
if err != nil {
return false, err
}
r.Log.Info("number of DPAs fetched: ", "number of DPAs", len(dpaList.Items))
r.Log.Info("DPA list fetched:\n", "DPAs", dpaList.Items)
for _, dpa := range dpaList.Items {
if dpa.Namespace != r.NamespacedName.Namespace && (&DPAReconciler{dpa: &dpa}).checkNonAdminEnabled() {
if (&DPAReconciler{dpa: &dpa}).checkNonAdminEnabled() {
return false, fmt.Errorf("only a single instance of Non-Admin Controller can be installed across the entire cluster. Non-Admin controller is also configured to be installed in %s namespace", dpa.Namespace)
}
}
Expand Down
5 changes: 3 additions & 2 deletions controllers/nonadmin_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,9 @@ func runReconcileNonAdminControllerTest(
Name: scenario.dpa,
Namespace: scenario.namespace,
},
EventRecorder: event,
dpa: dpa,
EventRecorder: event,
dpa: dpa,
ClusterWideClient: k8sClient,
}
result, err := r.ReconcileNonAdminController(logr.Discard())

Expand Down
13 changes: 5 additions & 8 deletions controllers/predicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,30 @@ import (
oadpv1alpha1 "github.com/openshift/oadp-operator/api/v1alpha1"
)

func veleroPredicate(scheme *runtime.Scheme, namespace string) predicate.Predicate {
func veleroPredicate(scheme *runtime.Scheme) predicate.Predicate {
return predicate.Funcs{
// Update returns true if the Update event should be processed
UpdateFunc: func(e event.UpdateEvent) bool {
if e.ObjectOld.GetGeneration() == e.ObjectNew.GetGeneration() {
return false
}
return isObjectOurs(scheme, e.ObjectOld, namespace)
return isObjectOurs(scheme, e.ObjectOld)
},
// Create returns true if the Create event should be processed
CreateFunc: func(e event.CreateEvent) bool {
return isObjectOurs(scheme, e.Object, namespace)
return isObjectOurs(scheme, e.Object)
},
// Delete returns true if the Delete event should be processed
DeleteFunc: func(e event.DeleteEvent) bool {
return !e.DeleteStateUnknown && isObjectOurs(scheme, e.Object, namespace)
return !e.DeleteStateUnknown && isObjectOurs(scheme, e.Object)
},
}
}

// isObjectOurs returns true if the object is ours.
// it first checks if the object has our group, version, and kind
// else it will check for non empty OadpOperatorlabel labels
func isObjectOurs(scheme *runtime.Scheme, object client.Object, namespace string) bool {
if object.GetNamespace() != namespace {
return false
}
func isObjectOurs(scheme *runtime.Scheme, object client.Object) bool {
objGVKs, _, err := scheme.ObjectKinds(object)
if err != nil {
return false
Expand Down
30 changes: 20 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import (
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/client-go/rest"
ctrl "sigs.k8s.io/controller-runtime"
// "sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
Expand Down Expand Up @@ -171,11 +171,11 @@ func main() {
RenewDeadline: &leConfig.RenewDeadline.Duration,
RetryPeriod: &leConfig.RetryPeriod.Duration,
LeaderElectionID: "oadp.openshift.io",
// Cache: cache.Options{
// DefaultNamespaces: map[string]cache.Config{
// watchNamespace: {},
// },
// },
Cache: cache.Options{
DefaultNamespaces: map[string]cache.Config{
watchNamespace: {},
},
},
})
if err != nil {
setupLog.Error(err, "unable to start manager")
Expand Down Expand Up @@ -218,11 +218,21 @@ func main() {
os.Exit(1)
}

dpaClientScheme := runtime.NewScheme()
utilruntime.Must(oadpv1alpha1.AddToScheme(dpaClientScheme))
dpaClient, err := client.New(kubeconf, client.Options{
Scheme: dpaClientScheme,
})
if err != nil {
setupLog.Error(err, "unable to create Kubernetes client")
os.Exit(1)
}

if err = (&controllers.DPAReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
EventRecorder: mgr.GetEventRecorderFor("DPA-controller"),
OADPNamespace: watchNamespace,
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
EventRecorder: mgr.GetEventRecorderFor("DPA-controller"),
ClusterWideClient: dpaClient,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "DataProtectionApplication")
os.Exit(1)
Expand Down

0 comments on commit acab81f

Please sign in to comment.