Skip to content

Commit

Permalink
fixup! fix: NAC install validation
Browse files Browse the repository at this point in the history
working example

Signed-off-by: Mateus Oliveira <[email protected]>
  • Loading branch information
mateusoliveira43 committed Nov 7, 2024
1 parent 38f7068 commit e7d61d6
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 19 deletions.
3 changes: 2 additions & 1 deletion controllers/dpa_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type DPAReconciler struct {
NamespacedName types.NamespacedName
EventRecorder record.EventRecorder
dpa *oadpv1alpha1.DataProtectionApplication
OADPNamespace string
}

var debugMode = os.Getenv("DEBUG") == "true"
Expand Down Expand Up @@ -148,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)).
WithEventFilter(veleroPredicate(r.Scheme, r.OADPNamespace)).
Complete(r)
}

Expand Down
11 changes: 4 additions & 7 deletions controllers/nonadmin_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ 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 @@ -85,17 +84,15 @@ 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, &client.ListOptions{FieldSelector: selector})
err := r.List(r.Context, dpaList)
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 (&DPAReconciler{dpa: &dpa}).checkNonAdminEnabled() {
if dpa.Namespace != r.NamespacedName.Namespace && (&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
13 changes: 8 additions & 5 deletions controllers/predicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,33 @@ import (
oadpv1alpha1 "github.com/openshift/oadp-operator/api/v1alpha1"
)

func veleroPredicate(scheme *runtime.Scheme) predicate.Predicate {
func veleroPredicate(scheme *runtime.Scheme, namespace string) 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)
return isObjectOurs(scheme, e.ObjectOld, namespace)
},
// Create returns true if the Create event should be processed
CreateFunc: func(e event.CreateEvent) bool {
return isObjectOurs(scheme, e.Object)
return isObjectOurs(scheme, e.Object, namespace)
},
// Delete returns true if the Delete event should be processed
DeleteFunc: func(e event.DeleteEvent) bool {
return !e.DeleteStateUnknown && isObjectOurs(scheme, e.Object)
return !e.DeleteStateUnknown && isObjectOurs(scheme, e.Object, namespace)
},
}
}

// 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) bool {
func isObjectOurs(scheme *runtime.Scheme, object client.Object, namespace string) bool {
if object.GetNamespace() != namespace {
return false
}
objGVKs, _, err := scheme.ObjectKinds(object)
if err != nil {
return false
Expand Down
13 changes: 7 additions & 6 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 @@ -222,6 +222,7 @@ func main() {
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
EventRecorder: mgr.GetEventRecorderFor("DPA-controller"),
OADPNamespace: watchNamespace,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "DataProtectionApplication")
os.Exit(1)
Expand Down

0 comments on commit e7d61d6

Please sign in to comment.