diff --git a/Makefile b/Makefile index 06a3d09a..0b5588f2 100644 --- a/Makefile +++ b/Makefile @@ -169,7 +169,7 @@ $(KUSTOMIZE): $(LOCALBIN) ########## Controller-Gen ########### CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen -CONTROLLER_TOOLS_VERSION ?= v0.14.0 +CONTROLLER_TOOLS_VERSION ?= v0.20.3 .PHONY: controller-gen controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary. diff --git a/api/v1alpha1/zz_generated.deepcopy.go b/api/v1alpha1/zz_generated.deepcopy.go index de4574b2..13df996b 100644 --- a/api/v1alpha1/zz_generated.deepcopy.go +++ b/api/v1alpha1/zz_generated.deepcopy.go @@ -1,4 +1,5 @@ //go:build !ignore_autogenerated +// +build !ignore_autogenerated /* Copyright 2022. diff --git a/components/central-application-gateway/pkg/apis/applicationconnector/v1alpha1/zz_generated.deepcopy.go b/components/central-application-gateway/pkg/apis/applicationconnector/v1alpha1/zz_generated.deepcopy.go index d9a6e12b..2da61339 100644 --- a/components/central-application-gateway/pkg/apis/applicationconnector/v1alpha1/zz_generated.deepcopy.go +++ b/components/central-application-gateway/pkg/apis/applicationconnector/v1alpha1/zz_generated.deepcopy.go @@ -1,4 +1,5 @@ //go:build !ignore_autogenerated +// +build !ignore_autogenerated /* Copyright 2022. diff --git a/components/compass-runtime-agent/pkg/apis/compass/v1alpha1/zz_generated.deepcopy.go b/components/compass-runtime-agent/pkg/apis/compass/v1alpha1/zz_generated.deepcopy.go index 76695692..23c923af 100644 --- a/components/compass-runtime-agent/pkg/apis/compass/v1alpha1/zz_generated.deepcopy.go +++ b/components/compass-runtime-agent/pkg/apis/compass/v1alpha1/zz_generated.deepcopy.go @@ -1,4 +1,5 @@ //go:build !ignore_autogenerated +// +build !ignore_autogenerated /* Copyright 2022. diff --git a/controllers/applicationconnector_controller.go b/controllers/applicationconnector_controller.go index 20dd8f20..b1cf89e3 100644 --- a/controllers/applicationconnector_controller.go +++ b/controllers/applicationconnector_controller.go @@ -18,6 +18,7 @@ package controllers import ( "context" + "k8s.io/apimachinery/pkg/labels" "reflect" "github.com/kyma-project/application-connector-manager/api/v1alpha1" @@ -70,7 +71,7 @@ type ApplicationConnetorReconciler interface { SetupWithManager(mgr ctrl.Manager) error } -type Watch = func(src source.Source, eventhandler handler.EventHandler, predicates ...predicate.Predicate) error +type Watch = func(src source.TypedSource[reconcile.Request]) error type applicationConnectorReconciler struct { log *zap.SugaredLogger @@ -135,23 +136,32 @@ var ommitStatusChanged = predicate.Or( predicate.GenerationChangedPredicate{}, ) +var hpaResourceVersionChangedPredicate2 = predicate.TypedResourceVersionChangedPredicate[*unstructured.Unstructured]{ + TypedFuncs: predicate.TypedFuncs[*unstructured.Unstructured]{ + CreateFunc: nil, + DeleteFunc: nil, + UpdateFunc: nil, + GenericFunc: nil, + }, +} + type hpaResourceVersionChangedPredicate struct { - predicate.ResourceVersionChangedPredicate + predicate.TypedResourceVersionChangedPredicate[*unstructured.Unstructured] log *zap.SugaredLogger } -func (h hpaResourceVersionChangedPredicate) Update(e event.UpdateEvent) bool { - if update := h.ResourceVersionChangedPredicate.Update(e); !update { +func (h hpaResourceVersionChangedPredicate) Update(e event.TypedUpdateEvent[*unstructured.Unstructured]) bool { + if update := h.TypedResourceVersionChangedPredicate.Update(e); !update { return false } var newObj v2.HorizontalPodAutoscaler - if err := runtime.DefaultUnstructuredConverter.FromUnstructured(e.ObjectNew.(*unstructured.Unstructured).Object, &newObj); err != nil { + if err := runtime.DefaultUnstructuredConverter.FromUnstructured(e.ObjectNew.Object, &newObj); err != nil { return true } var oldObj v2.HorizontalPodAutoscaler - if err := runtime.DefaultUnstructuredConverter.FromUnstructured(e.ObjectOld.(*unstructured.Unstructured).Object, &oldObj); err != nil { + if err := runtime.DefaultUnstructuredConverter.FromUnstructured(e.ObjectOld.Object, &oldObj); err != nil { return true } @@ -179,48 +189,66 @@ var ( } ) -// SetupWithManager sets up the controller with the Manager. -func (r *applicationConnectorReconciler) SetupWithManager(mgr ctrl.Manager) error { - labelSelectorPredicate, err := predicate.LabelSelectorPredicate( - metav1.LabelSelector{ - MatchLabels: map[string]string{ - "app.kubernetes.io/part-of": "application-connector-manager", - }, - }, - ) +func LabelSelectorPredicate(s metav1.LabelSelector) (predicate.TypedPredicate[*unstructured.Unstructured], error) { + selector, err := metav1.LabelSelectorAsSelector(&s) if err != nil { - return err + return predicate.TypedFuncs[*unstructured.Unstructured]{}, err } + // + return predicate.NewTypedPredicateFuncs[*unstructured.Unstructured](func(u *unstructured.Unstructured) bool { + return selector.Matches(labels.Set(u.GetLabels())) + }), nil +} + +// SetupWithManager sets up the controller with the Manager. +func (r *applicationConnectorReconciler) SetupWithManager(mgr ctrl.Manager) error { + //labelSelectorPredicate, err := predicate.LabelSelectorPredicate[*unstructured.Unstructured]( + // metav1.LabelSelector{ + // MatchLabels: map[string]string{ + // "app.kubernetes.io/part-of": "application-connector-manager", + // }, + // }, + //) + + //MatchLabels: map[string]string{ + // "app.kubernetes.io/part-of": "application-connector-manager", + //} + + var labelSelectorPredicate predicate.TypedLabelChangedPredicate[*unstructured.Unstructured] + b := ctrl.NewControllerManagedBy(mgr). For(&v1alpha1.ApplicationConnector{}, builder.WithPredicates(ommitStatusChanged)) // create functtion to register wached objects watchFn := func(u unstructured.Unstructured) { - var objPredicate predicate.Predicate = &predicate.ResourceVersionChangedPredicate{} + objPredicate := predicate.TypedResourceVersionChangedPredicate[*unstructured.Unstructured]{} if u.GroupVersionKind() == hpaGroupVersionKind { - objPredicate = hpaResourceVersionChangedPredicate{ - log: r.log, - } + objPredicate = hpaResourceVersionChangedPredicate2 } if u.GroupVersionKind() == deploymentVersionKind { - objPredicate = acm_predicate.NewDeploymentPredicate(r.log) + objPredicate = acm_predicate.NewDeploymentPredicate2 } r.log.With("gvk", u.GroupVersionKind().String()).Infoln("adding watcher") + allLabelSelectors := predicate.And(labelSelectorPredicate, objPredicate) + + kind := source.Kind(mgr.GetCache(), &unstructured.Unstructured{}, &handler.TypedEnqueueRequestForObject[*unstructured.Unstructured]{}, allLabelSelectors) + b = b.WatchesRawSource(kind) + + //b = b.WatchesRawSource( + // source.Kind(mgr.GetCache(), &u), + // handler.EnqueueRequestsFromMapFunc(r.mapFunction), + // builder.WithPredicates( + // predicate.And( + // labelSelectorPredicate, + // objPredicate, + // ), + // ), + //) - b = b.WatchesRawSource( - source.Kind(mgr.GetCache(), &u), - handler.EnqueueRequestsFromMapFunc(r.mapFunction), - builder.WithPredicates( - predicate.And( - labelSelectorPredicate, - objPredicate, - ), - ), - ) } // register watch for each managed type of object if err := registerWatchDistinct(r.Objs, watchFn); err != nil { diff --git a/pkg/common/controller-runtime/predicate/deployment.go b/pkg/common/controller-runtime/predicate/deployment.go index 3560cfac..2b92aa5a 100644 --- a/pkg/common/controller-runtime/predicate/deployment.go +++ b/pkg/common/controller-runtime/predicate/deployment.go @@ -16,6 +16,15 @@ type deploymentPredicate struct { log *zap.SugaredLogger } +var NewDeploymentPredicate2 = predicate.TypedResourceVersionChangedPredicate[*unstructured.Unstructured]{ + TypedFuncs: predicate.TypedFuncs[*unstructured.Unstructured]{ + CreateFunc: nil, + DeleteFunc: nil, + UpdateFunc: nil, + GenericFunc: nil, + }, +} + func NewDeploymentPredicate(log *zap.SugaredLogger) predicate.Predicate { return &deploymentPredicate{ log: log, diff --git a/pkg/reconciler/check_dependencies.go b/pkg/reconciler/check_dependencies.go index e644ac7c..59f1bc52 100644 --- a/pkg/reconciler/check_dependencies.go +++ b/pkg/reconciler/check_dependencies.go @@ -3,6 +3,7 @@ package reconciler import ( "context" "errors" + "github.com/kyma-project/application-connector-manager/pkg/unstructured" "time" "github.com/kyma-project/application-connector-manager/api/v1alpha1" @@ -99,7 +100,9 @@ func sFnRegisterDependencyWatch(_ context.Context, r *fsm, s *systemState) (stat objPredicate = acm_predicate.NewGatewayPredicate(r.log) } - err := r.Watch(source.Kind(r.Cache, &u), handler.EnqueueRequestsFromMapFunc(r.MapFunc), predicate.And(labelSelectorPredicate, objPredicate)) + kind := source.Kind(r.Cache, &unstructured.Unstructured{}, &handler.TypedEnqueueRequestForObject[*unstructured.Unstructured]{}, predicate.And(labelSelectorPredicate, objPredicate)) + //kind, handler.EnqueueRequestsFromMapFunc(r.MapFunc), predicate.And(labelSelectorPredicate, objPredicate) + err := r.Watch(kind) if err != nil { s.instance.UpdateStateFromErr(v1alpha1.ConditionTypeInstalled, v1alpha1.ConditionReasonApplyObjError, err) diff --git a/pkg/reconciler/fsm.go b/pkg/reconciler/fsm.go index 27f4064a..7be0044d 100644 --- a/pkg/reconciler/fsm.go +++ b/pkg/reconciler/fsm.go @@ -5,6 +5,7 @@ import ( "fmt" "reflect" "runtime" + "sigs.k8s.io/controller-runtime/pkg/reconcile" "sync" "github.com/kyma-project/application-connector-manager/api/v1alpha1" @@ -14,7 +15,6 @@ import ( "sigs.k8s.io/controller-runtime/pkg/cache" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/handler" - "sigs.k8s.io/controller-runtime/pkg/predicate" "sigs.k8s.io/controller-runtime/pkg/source" ) @@ -28,7 +28,7 @@ func (f stateFn) name() string { return runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name() } -type Watch = func(src source.Source, eventhandler handler.EventHandler, predicates ...predicate.Predicate) error +type Watch = func(src source.TypedSource[reconcile.Request]) error type K8s struct { client.Client