Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

90 changes: 59 additions & 31 deletions controllers/applicationconnector_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@

import (
"context"
"k8s.io/apimachinery/pkg/labels"
"reflect"

"github.com/kyma-project/application-connector-manager/api/v1alpha1"
acm_predicate "github.com/kyma-project/application-connector-manager/pkg/common/controller-runtime/predicate"

Check failure on line 25 in controllers/applicationconnector_controller.go

View workflow job for this annotation

GitHub Actions / golangci-lint

could not import github.com/kyma-project/application-connector-manager/pkg/common/controller-runtime/predicate (-: # github.com/kyma-project/application-connector-manager/pkg/common/controller-runtime/predicate
"github.com/kyma-project/application-connector-manager/pkg/reconciler"
"go.uber.org/zap"
appsv1 "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -70,7 +71,7 @@
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

Check failure on line 74 in controllers/applicationconnector_controller.go

View workflow job for this annotation

GitHub Actions / govuln

undefined: source.TypedSource

Check failure on line 74 in controllers/applicationconnector_controller.go

View workflow job for this annotation

GitHub Actions / golangci-lint

undefined: source.TypedSource (typecheck)

type applicationConnectorReconciler struct {
log *zap.SugaredLogger
Expand Down Expand Up @@ -135,23 +136,32 @@
predicate.GenerationChangedPredicate{},
)

var hpaResourceVersionChangedPredicate2 = predicate.TypedResourceVersionChangedPredicate[*unstructured.Unstructured]{

Check failure on line 139 in controllers/applicationconnector_controller.go

View workflow job for this annotation

GitHub Actions / govuln

undefined: predicate.TypedResourceVersionChangedPredicate

Check failure on line 139 in controllers/applicationconnector_controller.go

View workflow job for this annotation

GitHub Actions / golangci-lint

undefined: predicate.TypedResourceVersionChangedPredicate (typecheck)
TypedFuncs: predicate.TypedFuncs[*unstructured.Unstructured]{

Check failure on line 140 in controllers/applicationconnector_controller.go

View workflow job for this annotation

GitHub Actions / govuln

undefined: predicate.TypedFuncs

Check failure on line 140 in controllers/applicationconnector_controller.go

View workflow job for this annotation

GitHub Actions / golangci-lint

undefined: predicate.TypedFuncs (typecheck)
CreateFunc: nil,
DeleteFunc: nil,
UpdateFunc: nil,
GenericFunc: nil,
},
}

type hpaResourceVersionChangedPredicate struct {
predicate.ResourceVersionChangedPredicate
predicate.TypedResourceVersionChangedPredicate[*unstructured.Unstructured]

Check failure on line 149 in controllers/applicationconnector_controller.go

View workflow job for this annotation

GitHub Actions / govuln

undefined: predicate.TypedResourceVersionChangedPredicate

Check failure on line 149 in controllers/applicationconnector_controller.go

View workflow job for this annotation

GitHub Actions / golangci-lint

undefined: predicate.TypedResourceVersionChangedPredicate (typecheck)
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 {

Check failure on line 153 in controllers/applicationconnector_controller.go

View workflow job for this annotation

GitHub Actions / golangci-lint

undefined: event.TypedUpdateEvent (typecheck)
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
}

Expand Down Expand Up @@ -179,48 +189,66 @@
}
)

// 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 {
Expand Down
9 changes: 9 additions & 0 deletions pkg/common/controller-runtime/predicate/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@
log *zap.SugaredLogger
}

var NewDeploymentPredicate2 = predicate.TypedResourceVersionChangedPredicate[*unstructured.Unstructured]{

Check failure on line 19 in pkg/common/controller-runtime/predicate/deployment.go

View workflow job for this annotation

GitHub Actions / govuln

undefined: predicate.TypedResourceVersionChangedPredicate

Check failure on line 19 in pkg/common/controller-runtime/predicate/deployment.go

View workflow job for this annotation

GitHub Actions / golangci-lint

undefined: predicate.TypedResourceVersionChangedPredicate

Check failure on line 19 in pkg/common/controller-runtime/predicate/deployment.go

View workflow job for this annotation

GitHub Actions / golangci-lint

undefined: predicate.TypedResourceVersionChangedPredicate
TypedFuncs: predicate.TypedFuncs[*unstructured.Unstructured]{

Check failure on line 20 in pkg/common/controller-runtime/predicate/deployment.go

View workflow job for this annotation

GitHub Actions / govuln

undefined: predicate.TypedFuncs

Check failure on line 20 in pkg/common/controller-runtime/predicate/deployment.go

View workflow job for this annotation

GitHub Actions / golangci-lint

undefined: predicate.TypedFuncs (typecheck)

Check failure on line 20 in pkg/common/controller-runtime/predicate/deployment.go

View workflow job for this annotation

GitHub Actions / golangci-lint

undefined: predicate.TypedFuncs) (typecheck)
CreateFunc: nil,
DeleteFunc: nil,
UpdateFunc: nil,
GenericFunc: nil,
},
}

func NewDeploymentPredicate(log *zap.SugaredLogger) predicate.Predicate {
return &deploymentPredicate{
log: log,
Expand Down
5 changes: 4 additions & 1 deletion pkg/reconciler/check_dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import (
"context"
"errors"
"github.com/kyma-project/application-connector-manager/pkg/unstructured"
"time"

"github.com/kyma-project/application-connector-manager/api/v1alpha1"
Expand Down Expand Up @@ -99,7 +100,9 @@
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))

Check failure on line 103 in pkg/reconciler/check_dependencies.go

View workflow job for this annotation

GitHub Actions / govuln

undefined: handler.TypedEnqueueRequestForObject

Check failure on line 103 in pkg/reconciler/check_dependencies.go

View workflow job for this annotation

GitHub Actions / govuln

too many arguments in call to source.Kind
//kind, handler.EnqueueRequestsFromMapFunc(r.MapFunc), predicate.And(labelSelectorPredicate, objPredicate)
err := r.Watch(kind)

if err != nil {
s.instance.UpdateStateFromErr(v1alpha1.ConditionTypeInstalled, v1alpha1.ConditionReasonApplyObjError, err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/reconciler/fsm.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"fmt"
"reflect"
"runtime"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

Check failure on line 8 in pkg/reconciler/fsm.go

View workflow job for this annotation

GitHub Actions / govuln

"sigs.k8s.io/controller-runtime/pkg/reconcile" imported and not used
"sync"

"github.com/kyma-project/application-connector-manager/api/v1alpha1"
Expand All @@ -14,7 +15,6 @@
"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"
)

Expand All @@ -28,7 +28,7 @@
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

Check failure on line 31 in pkg/reconciler/fsm.go

View workflow job for this annotation

GitHub Actions / govuln

undefined: source.TypedSource

type K8s struct {
client.Client
Expand Down
Loading