Skip to content

Commit

Permalink
Upgrade controller-runtime to v0.15.0 (#293)
Browse files Browse the repository at this point in the history
  • Loading branch information
orishoshan authored Nov 18, 2023
1 parent 80b7a6b commit 3841ace
Show file tree
Hide file tree
Showing 17 changed files with 216 additions and 117 deletions.
29 changes: 14 additions & 15 deletions src/go.mod

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

82 changes: 43 additions & 39 deletions src/go.sum

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions src/operator/controllers/intents_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
)

var intentsLegacyFinalizers = []string{
Expand Down Expand Up @@ -191,7 +190,7 @@ func (r *IntentsReconciler) SetupWithManager(mgr ctrl.Manager) error {
err := ctrl.NewControllerManagedBy(mgr).
For(&otterizev1alpha3.ClientIntents{}).
WithOptions(controller.Options{RecoverPanic: lo.ToPtr(true)}).
Watches(&source.Kind{Type: &otterizev1alpha3.ProtectedService{}}, handler.EnqueueRequestsFromMapFunc(r.mapProtectedServiceToClientIntents)).
Watches(&otterizev1alpha3.ProtectedService{}, handler.EnqueueRequestsFromMapFunc(r.mapProtectedServiceToClientIntents)).
Complete(r)
if err != nil {
return err
Expand All @@ -202,7 +201,7 @@ func (r *IntentsReconciler) SetupWithManager(mgr ctrl.Manager) error {
return nil
}

func (r *IntentsReconciler) mapProtectedServiceToClientIntents(obj client.Object) []reconcile.Request {
func (r *IntentsReconciler) mapProtectedServiceToClientIntents(_ context.Context, obj client.Object) []reconcile.Request {
protectedService := obj.(*otterizev1alpha3.ProtectedService)
logrus.Infof("Enqueueing client intents for protected services %s", protectedService.Name)

Expand Down
4 changes: 2 additions & 2 deletions src/operator/controllers/intents_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (s *IntentsControllerTestSuite) TestMappingProtectedServicesToIntent() {
},
},
}
res := s.intentsReconciler.mapProtectedServiceToClientIntents(&protectedService)
res := s.intentsReconciler.mapProtectedServiceToClientIntents(context.Background(), &protectedService)
s.Require().Equal(expected, res)
}

Expand All @@ -138,7 +138,7 @@ func (s *IntentsControllerTestSuite) TestMappingProtectedServicesToIntentNoInten
).Return(nil)

expected := make([]reconcile.Request, 0)
res := s.intentsReconciler.mapProtectedServiceToClientIntents(&protectedService)
res := s.intentsReconciler.mapProtectedServiceToClientIntents(context.Background(), &protectedService)
s.Require().Equal(expected, res)
}

Expand Down

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

31 changes: 31 additions & 0 deletions src/operator/controllers/istiopolicy/mocks/mock_client.go

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

11 changes: 5 additions & 6 deletions src/operator/controllers/kafkaserverconfig_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
)

const (
Expand Down Expand Up @@ -100,7 +99,7 @@ func (r *KafkaServerConfigReconciler) SetupWithManager(mgr ctrl.Manager) error {
// Uncomment the following line adding a pointer to an instance of the controlled resource as an argument
For(&otterizev1alpha3.KafkaServerConfig{}).
WithOptions(controller.Options{RecoverPanic: lo.ToPtr(true)}).
Watches(&source.Kind{Type: &otterizev1alpha3.ProtectedService{}}, handler.EnqueueRequestsFromMapFunc(r.mapProtectedServiceToKafkaServerConfig)).
Watches(&otterizev1alpha3.ProtectedService{}, handler.EnqueueRequestsFromMapFunc(r.mapProtectedServiceToKafkaServerConfig)).
Complete(r)
if err != nil {
return err
Expand All @@ -122,11 +121,11 @@ func (r *KafkaServerConfigReconciler) InitKafkaServerConfigIndices(mgr ctrl.Mana
})
}

func (r *KafkaServerConfigReconciler) mapProtectedServiceToKafkaServerConfig(obj client.Object) []reconcile.Request {
func (r *KafkaServerConfigReconciler) mapProtectedServiceToKafkaServerConfig(ctx context.Context, obj client.Object) []reconcile.Request {
protectedService := obj.(*otterizev1alpha3.ProtectedService)
logrus.Infof("Enqueueing KafkaServerConfigs for protected service %s", protectedService.Name)

kscsToReconcile := r.getKSCsForProtectedService(protectedService)
kscsToReconcile := r.getKSCsForProtectedService(ctx, protectedService)
return lo.Map(kscsToReconcile, func(ksc otterizev1alpha3.KafkaServerConfig, _ int) reconcile.Request {
return reconcile.Request{
NamespacedName: types.NamespacedName{
Expand All @@ -137,10 +136,10 @@ func (r *KafkaServerConfigReconciler) mapProtectedServiceToKafkaServerConfig(obj
})
}

func (r *KafkaServerConfigReconciler) getKSCsForProtectedService(protectedService *otterizev1alpha3.ProtectedService) []otterizev1alpha3.KafkaServerConfig {
func (r *KafkaServerConfigReconciler) getKSCsForProtectedService(ctx context.Context, protectedService *otterizev1alpha3.ProtectedService) []otterizev1alpha3.KafkaServerConfig {
kscsToReconcile := make([]otterizev1alpha3.KafkaServerConfig, 0)
var kafkaServerConfigs otterizev1alpha3.KafkaServerConfigList
err := r.Client.List(context.Background(),
err := r.Client.List(ctx,
&kafkaServerConfigs,
&client.MatchingFields{otterizev1alpha3.OtterizeKafkaServerConfigServiceNameField: protectedService.Spec.Name},
&client.ListOptions{Namespace: protectedService.Namespace},
Expand Down
2 changes: 1 addition & 1 deletion src/operator/controllers/pod_reconcilers/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (ns *NamespaceWatcher) Register(mgr manager.Manager) error {
return fmt.Errorf("unable to set up namespace controller: %w", err)
}

if err = watcher.Watch(&source.Kind{Type: &v1.Namespace{}}, &handler.EnqueueRequestForObject{}); err != nil {
if err = watcher.Watch(source.Kind(mgr.GetCache(), &v1.Namespace{}), &handler.EnqueueRequestForObject{}); err != nil {
return fmt.Errorf("unable to watch Namespaces: %w", err)
}

Expand Down
2 changes: 1 addition & 1 deletion src/operator/controllers/pod_reconcilers/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ func (p *PodWatcher) Register(mgr manager.Manager) error {
return fmt.Errorf("unable to set up pods controller: %p", err)
}

if err = watcher.Watch(&source.Kind{Type: &v1.Pod{}}, &handler.EnqueueRequestForObject{}); err != nil {
if err = watcher.Watch(source.Kind(mgr.GetCache(), &v1.Pod{}), &handler.EnqueueRequestForObject{}); err != nil {
return fmt.Errorf("unable to watch Pods: %p", err)
}

Expand Down
3 changes: 1 addition & 2 deletions src/operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/metadata"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

Expand Down Expand Up @@ -148,7 +147,7 @@ func main() {
}

if len(watchedNamespaces) != 0 {
options.NewCache = cache.MultiNamespacedCacheBuilder(watchedNamespaces)
options.Cache.Namespaces = watchedNamespaces
logrus.Infof("Will only watch the following namespaces: %v", watchedNamespaces)
}

Expand Down
21 changes: 11 additions & 10 deletions src/operator/webhooks/clientintents_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
"strings"
)

Expand All @@ -52,12 +53,12 @@ func NewIntentsValidatorV1alpha2(c client.Client) *IntentsValidator {
var _ webhook.CustomValidator = &IntentsValidator{}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (v *IntentsValidator) ValidateCreate(ctx context.Context, obj runtime.Object) error {
func (v *IntentsValidator) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
var allErrs field.ErrorList
intentsObj := obj.(*otterizev1alpha2.ClientIntents)
intentsList := &otterizev1alpha2.ClientIntentsList{}
if err := v.List(ctx, intentsList, &client.ListOptions{Namespace: intentsObj.Namespace}); err != nil {
return err
return nil, err
}
if err := v.validateNoDuplicateClients(intentsObj, intentsList); err != nil {
allErrs = append(allErrs, err)
Expand All @@ -68,22 +69,22 @@ func (v *IntentsValidator) ValidateCreate(ctx context.Context, obj runtime.Objec
}

if len(allErrs) == 0 {
return nil
return nil, nil
}

gvk := intentsObj.GroupVersionKind()
return errors.NewInvalid(
return nil, errors.NewInvalid(
schema.GroupKind{Group: gvk.Group, Kind: gvk.Kind},
intentsObj.Name, allErrs)
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (v *IntentsValidator) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) error {
func (v *IntentsValidator) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (admission.Warnings, error) {
var allErrs field.ErrorList
intentsObj := newObj.(*otterizev1alpha2.ClientIntents)
intentsList := &otterizev1alpha2.ClientIntentsList{}
if err := v.List(ctx, intentsList, &client.ListOptions{Namespace: intentsObj.Namespace}); err != nil {
return err
return nil, err
}
if err := v.validateNoDuplicateClients(intentsObj, intentsList); err != nil {
allErrs = append(allErrs, err)
Expand All @@ -94,18 +95,18 @@ func (v *IntentsValidator) ValidateUpdate(ctx context.Context, oldObj, newObj ru
}

if len(allErrs) == 0 {
return nil
return nil, nil
}

gvk := intentsObj.GroupVersionKind()
return errors.NewInvalid(
return nil, errors.NewInvalid(
schema.GroupKind{Group: gvk.Group, Kind: gvk.Kind},
intentsObj.Name, allErrs)
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (v *IntentsValidator) ValidateDelete(ctx context.Context, obj runtime.Object) error {
return nil
func (v *IntentsValidator) ValidateDelete(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
return nil, nil
}

func (v *IntentsValidator) validateNoDuplicateClients(
Expand Down
Loading

0 comments on commit 3841ace

Please sign in to comment.