Skip to content

Commit

Permalink
Dynamic watches
Browse files Browse the repository at this point in the history
  • Loading branch information
roivaz committed Jan 11, 2024
1 parent fe121fd commit 6918a91
Show file tree
Hide file tree
Showing 14 changed files with 69 additions and 214 deletions.
21 changes: 4 additions & 17 deletions controllers/apicast_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,8 @@ import (

"github.com/3scale-ops/basereconciler/reconciler"
"github.com/3scale-ops/basereconciler/util"
marin3rv1alpha1 "github.com/3scale-ops/marin3r/apis/marin3r/v1alpha1"
saasv1alpha1 "github.com/3scale-ops/saas-operator/api/v1alpha1"
"github.com/3scale-ops/saas-operator/pkg/generators/apicast"
grafanav1alpha1 "github.com/grafana-operator/grafana-operator/v4/api/integreatly/v1alpha1"
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
appsv1 "k8s.io/api/apps/v1"
autoscalingv2 "k8s.io/api/autoscaling/v2"
corev1 "k8s.io/api/core/v1"
policyv1 "k8s.io/api/policy/v1"
ctrl "sigs.k8s.io/controller-runtime"
)

Expand Down Expand Up @@ -80,14 +73,8 @@ func (r *ApicastReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct

// SetupWithManager sets up the controller with the Manager.
func (r *ApicastReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&saasv1alpha1.Apicast{}).
Owns(&appsv1.Deployment{}).
Owns(&corev1.Service{}).
Owns(&policyv1.PodDisruptionBudget{}).
Owns(&autoscalingv2.HorizontalPodAutoscaler{}).
Owns(&monitoringv1.PodMonitor{}).
Owns(&grafanav1alpha1.GrafanaDashboard{}).
Owns(&marin3rv1alpha1.EnvoyConfig{}).
Complete(r)
return reconciler.SetupWithDynamicTypeWatches(r,
ctrl.NewControllerManagedBy(mgr).
For(&saasv1alpha1.Apicast{}),
)
}
19 changes: 4 additions & 15 deletions controllers/autossl_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@ import (
"github.com/3scale-ops/basereconciler/util"
saasv1alpha1 "github.com/3scale-ops/saas-operator/api/v1alpha1"
"github.com/3scale-ops/saas-operator/pkg/generators/autossl"
grafanav1alpha1 "github.com/grafana-operator/grafana-operator/v4/api/integreatly/v1alpha1"
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
appsv1 "k8s.io/api/apps/v1"
autoscalingv2 "k8s.io/api/autoscaling/v2"
corev1 "k8s.io/api/core/v1"
policyv1 "k8s.io/api/policy/v1"
ctrl "sigs.k8s.io/controller-runtime"
)

Expand Down Expand Up @@ -78,13 +72,8 @@ func (r *AutoSSLReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct

// SetupWithManager sets up the controller with the Manager.
func (r *AutoSSLReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&saasv1alpha1.AutoSSL{}).
Owns(&appsv1.Deployment{}).
Owns(&corev1.Service{}).
Owns(&policyv1.PodDisruptionBudget{}).
Owns(&autoscalingv2.HorizontalPodAutoscaler{}).
Owns(&monitoringv1.PodMonitor{}).
Owns(&grafanav1alpha1.GrafanaDashboard{}).
Complete(r)
return reconciler.SetupWithDynamicTypeWatches(r,
ctrl.NewControllerManagedBy(mgr).
For(&saasv1alpha1.AutoSSL{}),
)
}
26 changes: 6 additions & 20 deletions controllers/backend_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,9 @@ import (

"github.com/3scale-ops/basereconciler/reconciler"
"github.com/3scale-ops/basereconciler/util"
marin3rv1alpha1 "github.com/3scale-ops/marin3r/apis/marin3r/v1alpha1"
saasv1alpha1 "github.com/3scale-ops/saas-operator/api/v1alpha1"
"github.com/3scale-ops/saas-operator/pkg/generators/backend"
externalsecretsv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
grafanav1alpha1 "github.com/grafana-operator/grafana-operator/v4/api/integreatly/v1alpha1"
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
appsv1 "k8s.io/api/apps/v1"
autoscalingv2 "k8s.io/api/autoscaling/v2"
corev1 "k8s.io/api/core/v1"
policyv1 "k8s.io/api/policy/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/source"
Expand Down Expand Up @@ -86,17 +79,10 @@ func (r *BackendReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct

// SetupWithManager sets up the controller with the Manager.
func (r *BackendReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&saasv1alpha1.Backend{}).
Owns(&appsv1.Deployment{}).
Owns(&corev1.Service{}).
Owns(&policyv1.PodDisruptionBudget{}).
Owns(&autoscalingv2.HorizontalPodAutoscaler{}).
Owns(&monitoringv1.PodMonitor{}).
Owns(&externalsecretsv1beta1.ExternalSecret{}).
Owns(&grafanav1alpha1.GrafanaDashboard{}).
Owns(&marin3rv1alpha1.EnvoyConfig{}).
Watches(&source.Kind{Type: &corev1.Secret{TypeMeta: metav1.TypeMeta{Kind: "Secret"}}},
r.FilteredEventHandler(&saasv1alpha1.BackendList{}, nil, r.Log)).
Complete(r)
return reconciler.SetupWithDynamicTypeWatches(r,
ctrl.NewControllerManagedBy(mgr).
For(&saasv1alpha1.Backend{}).
Watches(&source.Kind{Type: &corev1.Secret{TypeMeta: metav1.TypeMeta{Kind: "Secret"}}},
r.FilteredEventHandler(&saasv1alpha1.BackendList{}, nil, r.Log)),
)
}
24 changes: 6 additions & 18 deletions controllers/corsproxy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,7 @@ import (
"github.com/3scale-ops/basereconciler/util"
saasv1alpha1 "github.com/3scale-ops/saas-operator/api/v1alpha1"
"github.com/3scale-ops/saas-operator/pkg/generators/corsproxy"
externalsecretsv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
grafanav1alpha1 "github.com/grafana-operator/grafana-operator/v4/api/integreatly/v1alpha1"
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
appsv1 "k8s.io/api/apps/v1"
autoscalingv2 "k8s.io/api/autoscaling/v2"
corev1 "k8s.io/api/core/v1"
policyv1 "k8s.io/api/policy/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/source"
Expand Down Expand Up @@ -80,16 +74,10 @@ func (r *CORSProxyReconciler) Reconcile(ctx context.Context, req ctrl.Request) (

// SetupWithManager sets up the controller with the Manager.
func (r *CORSProxyReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&saasv1alpha1.CORSProxy{}).
Owns(&appsv1.Deployment{}).
Owns(&corev1.Service{}).
Owns(&policyv1.PodDisruptionBudget{}).
Owns(&autoscalingv2.HorizontalPodAutoscaler{}).
Owns(&monitoringv1.PodMonitor{}).
Owns(&externalsecretsv1beta1.ExternalSecret{}).
Owns(&grafanav1alpha1.GrafanaDashboard{}).
Watches(&source.Kind{Type: &corev1.Secret{TypeMeta: metav1.TypeMeta{Kind: "Secret"}}},
r.FilteredEventHandler(&saasv1alpha1.CORSProxyList{}, nil, r.Log)).
Complete(r)
return reconciler.SetupWithDynamicTypeWatches(r,
ctrl.NewControllerManagedBy(mgr).
For(&saasv1alpha1.CORSProxy{}).
Watches(&source.Kind{Type: &corev1.Secret{TypeMeta: metav1.TypeMeta{Kind: "Secret"}}},
r.FilteredEventHandler(&saasv1alpha1.CORSProxyList{}, nil, r.Log)),
)
}
21 changes: 4 additions & 17 deletions controllers/echoapi_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,8 @@ import (

"github.com/3scale-ops/basereconciler/reconciler"
"github.com/3scale-ops/basereconciler/util"
marin3rv1alpha1 "github.com/3scale-ops/marin3r/apis/marin3r/v1alpha1"
saasv1alpha1 "github.com/3scale-ops/saas-operator/api/v1alpha1"
"github.com/3scale-ops/saas-operator/pkg/generators/echoapi"
grafanav1alpha1 "github.com/grafana-operator/grafana-operator/v4/api/integreatly/v1alpha1"
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
appsv1 "k8s.io/api/apps/v1"
autoscalingv2 "k8s.io/api/autoscaling/v2"
corev1 "k8s.io/api/core/v1"
policyv1 "k8s.io/api/policy/v1"
ctrl "sigs.k8s.io/controller-runtime"
)

Expand Down Expand Up @@ -76,14 +69,8 @@ func (r *EchoAPIReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct

// SetupWithManager sets up the controller with the Manager.
func (r *EchoAPIReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&saasv1alpha1.EchoAPI{}).
Owns(&appsv1.Deployment{}).
Owns(&corev1.Service{}).
Owns(&policyv1.PodDisruptionBudget{}).
Owns(&autoscalingv2.HorizontalPodAutoscaler{}).
Owns(&monitoringv1.PodMonitor{}).
Owns(&grafanav1alpha1.GrafanaDashboard{}).
Owns(&marin3rv1alpha1.EnvoyConfig{}).
Complete(r)
return reconciler.SetupWithDynamicTypeWatches(r,
ctrl.NewControllerManagedBy(mgr).
For(&saasv1alpha1.EchoAPI{}),
)
}
24 changes: 6 additions & 18 deletions controllers/mappingservice_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,7 @@ import (
"github.com/3scale-ops/basereconciler/util"
saasv1alpha1 "github.com/3scale-ops/saas-operator/api/v1alpha1"
"github.com/3scale-ops/saas-operator/pkg/generators/mappingservice"
externalsecretsv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
grafanav1alpha1 "github.com/grafana-operator/grafana-operator/v4/api/integreatly/v1alpha1"
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
appsv1 "k8s.io/api/apps/v1"
autoscalingv2 "k8s.io/api/autoscaling/v2"
corev1 "k8s.io/api/core/v1"
policyv1 "k8s.io/api/policy/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/source"
Expand Down Expand Up @@ -80,16 +74,10 @@ func (r *MappingServiceReconciler) Reconcile(ctx context.Context, req ctrl.Reque

// SetupWithManager sets up the controller with the Manager.
func (r *MappingServiceReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&saasv1alpha1.MappingService{}).
Owns(&appsv1.Deployment{}).
Owns(&corev1.Service{}).
Owns(&policyv1.PodDisruptionBudget{}).
Owns(&autoscalingv2.HorizontalPodAutoscaler{}).
Owns(&monitoringv1.PodMonitor{}).
Owns(&externalsecretsv1beta1.ExternalSecret{}).
Owns(&grafanav1alpha1.GrafanaDashboard{}).
Watches(&source.Kind{Type: &corev1.Secret{TypeMeta: metav1.TypeMeta{Kind: "Secret"}}},
r.FilteredEventHandler(&saasv1alpha1.MappingServiceList{}, nil, r.Log)).
Complete(r)
return reconciler.SetupWithDynamicTypeWatches(r,
ctrl.NewControllerManagedBy(mgr).
For(&saasv1alpha1.MappingService{}).
Watches(&source.Kind{Type: &corev1.Secret{TypeMeta: metav1.TypeMeta{Kind: "Secret"}}},
r.FilteredEventHandler(&saasv1alpha1.MappingServiceList{}, nil, r.Log)),
)
}
11 changes: 4 additions & 7 deletions controllers/redisshard_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
redis "github.com/3scale-ops/saas-operator/pkg/redis/server"
"github.com/3scale-ops/saas-operator/pkg/redis/sharded"
"github.com/go-logr/logr"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -83,12 +82,10 @@ func (r *RedisShardReconciler) Reconcile(ctx context.Context, req ctrl.Request)

// SetupWithManager sets up the controller with the Manager.
func (r *RedisShardReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&saasv1alpha1.RedisShard{}).
Owns(&appsv1.StatefulSet{}).
Owns(&corev1.Service{}).
Owns(&corev1.ConfigMap{}).
Complete(r)
return reconciler.SetupWithDynamicTypeWatches(r,
ctrl.NewControllerManagedBy(mgr).
For(&saasv1alpha1.RedisShard{}),
)
}

func (r *RedisShardReconciler) setRedisRoles(ctx context.Context, key types.NamespacedName,
Expand Down
24 changes: 7 additions & 17 deletions controllers/sentinel_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@ import (
redis "github.com/3scale-ops/saas-operator/pkg/redis/server"
"github.com/3scale-ops/saas-operator/pkg/redis/sharded"
"github.com/go-logr/logr"
grafanav1alpha1 "github.com/grafana-operator/grafana-operator/v4/api/integreatly/v1alpha1"
"golang.org/x/time/rate"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
policyv1 "k8s.io/api/policy/v1"
"k8s.io/apimachinery/pkg/api/equality"
"k8s.io/client-go/util/workqueue"
ctrl "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -207,21 +203,15 @@ func (r *SentinelReconciler) reconcileStatus(ctx context.Context, instance *saas

// SetupWithManager sets up the controller with the Manager.
func (r *SentinelReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&saasv1alpha1.Sentinel{}).
Owns(&appsv1.StatefulSet{}).
Owns(&corev1.Service{}).
Owns(&policyv1.PodDisruptionBudget{}).
Owns(&grafanav1alpha1.GrafanaDashboard{}).
Owns(&corev1.ConfigMap{}).
Watches(&source.Channel{Source: r.SentinelEvents.GetChannel()}, &handler.EnqueueRequestForObject{}).
WithOptions(controller.Options{
RateLimiter: AggressiveRateLimiter(),
}).
Complete(r)
return reconciler.SetupWithDynamicTypeWatches(r,
ctrl.NewControllerManagedBy(mgr).
For(&saasv1alpha1.Sentinel{}).
Watches(&source.Channel{Source: r.SentinelEvents.GetChannel()}, &handler.EnqueueRequestForObject{}).
WithOptions(controller.Options{RateLimiter: PermissiveRateLimiter()}),
)
}

func AggressiveRateLimiter() ratelimiter.RateLimiter {
func PermissiveRateLimiter() ratelimiter.RateLimiter {
// return workqueue.DefaultControllerRateLimiter()
return workqueue.NewMaxOfRateLimiter(
// First retries are more spaced that default
Expand Down
28 changes: 6 additions & 22 deletions controllers/system_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,7 @@ import (
"github.com/3scale-ops/basereconciler/util"
saasv1alpha1 "github.com/3scale-ops/saas-operator/api/v1alpha1"
"github.com/3scale-ops/saas-operator/pkg/generators/system"
externalsecretsv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
grafanav1alpha1 "github.com/grafana-operator/grafana-operator/v4/api/integreatly/v1alpha1"
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
pipelinev1beta1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
appsv1 "k8s.io/api/apps/v1"
autoscalingv2 "k8s.io/api/autoscaling/v2"
corev1 "k8s.io/api/core/v1"
policyv1 "k8s.io/api/policy/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/source"
Expand Down Expand Up @@ -86,19 +79,10 @@ func (r *SystemReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr

// SetupWithManager sets up the controller with the Manager.
func (r *SystemReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&saasv1alpha1.System{}).
Owns(&appsv1.Deployment{}).
Owns(&appsv1.StatefulSet{}).
Owns(&corev1.Service{}).
Owns(&policyv1.PodDisruptionBudget{}).
Owns(&autoscalingv2.HorizontalPodAutoscaler{}).
Owns(&monitoringv1.PodMonitor{}).
Owns(&externalsecretsv1beta1.ExternalSecret{}).
Owns(&grafanav1alpha1.GrafanaDashboard{}).
Owns(&pipelinev1beta1.Pipeline{}).
Owns(&pipelinev1beta1.Task{}).
Watches(&source.Kind{Type: &corev1.Secret{TypeMeta: metav1.TypeMeta{Kind: "Secret"}}},
r.FilteredEventHandler(&saasv1alpha1.SystemList{}, nil, r.Log)).
Complete(r)
return reconciler.SetupWithDynamicTypeWatches(r,
ctrl.NewControllerManagedBy(mgr).
For(&saasv1alpha1.System{}).
Watches(&source.Kind{Type: &corev1.Secret{TypeMeta: metav1.TypeMeta{Kind: "Secret"}}},
r.FilteredEventHandler(&saasv1alpha1.SystemList{}, nil, r.Log)),
)
}
20 changes: 12 additions & 8 deletions controllers/system_controller_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@ var _ = Describe("System controller", func() {
ReleaseStage: util.Pointer("staging"),
APIKey: saasv1alpha1.SecretReference{Override: util.Pointer("override")},
},
DatabaseSecret: saasv1alpha1.SecretReference{Override: util.Pointer("override")},
// DatabaseSecret: saasv1alpha1.SecretReference{Override: util.Pointer("override")},
DatabaseSecret: saasv1alpha1.SecretReference{FromVault: &saasv1alpha1.VaultSecretReference{
Path: "path",
Key: "key",
}},
MemcachedServers: "value",
Redis: saasv1alpha1.RedisSpec{
QueuesDSN: "value",
Expand Down Expand Up @@ -310,13 +314,13 @@ var _ = Describe("System controller", func() {

for _, esn := range []string{
"system-database",
"system-recaptcha",
"system-events-hook",
"system-smtp",
"system-master-apicast",
"system-zync",
"system-backend",
"system-multitenant-assets-s3",
// "system-recaptcha",
// "system-events-hook",
// "system-smtp",
// "system-master-apicast",
// "system-zync",
// "system-backend",
// "system-multitenant-assets-s3",
"system-app",
} {
es := &externalsecretsv1beta1.ExternalSecret{}
Expand Down
2 changes: 1 addition & 1 deletion controllers/twemproxyconfig_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ func (r *TwemproxyConfigReconciler) SetupWithManager(mgr ctrl.Manager) error {
Owns(&grafanav1alpha1.GrafanaDashboard{}).
Watches(&source.Channel{Source: r.SentinelEvents.GetChannel()}, &handler.EnqueueRequestForObject{}).
WithOptions(controller.Options{
RateLimiter: AggressiveRateLimiter(),
RateLimiter: PermissiveRateLimiter(),
// this allows for different resources to be reconciled in parallel
MaxConcurrentReconciles: 2,
}).
Expand Down
Loading

0 comments on commit 6918a91

Please sign in to comment.