Skip to content

Commit

Permalink
refactor: build reconcilers in a loop (#683)
Browse files Browse the repository at this point in the history
  • Loading branch information
byashimov authored Mar 26, 2024
1 parent 47f1c1a commit a5e5b06
Show file tree
Hide file tree
Showing 21 changed files with 116 additions and 117 deletions.
4 changes: 4 additions & 0 deletions controllers/cassandra_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ type CassandraReconciler struct {
Controller
}

func newCassandraReconciler(c Controller) reconcilerType {
return &CassandraReconciler{Controller: c}
}

// +kubebuilder:rbac:groups=aiven.io,resources=cassandras,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=aiven.io,resources=cassandras/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=aiven.io,resources=cassandras/finalizers,verbs=get;list;watch;create;update;patch;delete
Expand Down
4 changes: 4 additions & 0 deletions controllers/clickhouse_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ type ClickhouseReconciler struct {
Controller
}

func newClickhouseReconciler(c Controller) reconcilerType {
return &ClickhouseReconciler{Controller: c}
}

//+kubebuilder:rbac:groups=aiven.io,resources=clickhouses,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=aiven.io,resources=clickhouses/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=aiven.io,resources=clickhouses/finalizers,verbs=get;list;watch;create;update;patch;delete
Expand Down
4 changes: 4 additions & 0 deletions controllers/clickhouseuser_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ type ClickhouseUserReconciler struct {
Controller
}

func newClickhouseUserReconciler(c Controller) reconcilerType {
return &ClickhouseUserReconciler{Controller: c}
}

//+kubebuilder:rbac:groups=aiven.io,resources=clickhouseusers,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=aiven.io,resources=clickhouseusers/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=aiven.io,resources=clickhouseusers/finalizers,verbs=get;list;watch;create;update;patch;delete
Expand Down
4 changes: 4 additions & 0 deletions controllers/connectionpool_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ type ConnectionPoolReconciler struct {
Controller
}

func newConnectionPoolReconciler(c Controller) reconcilerType {
return &ConnectionPoolReconciler{Controller: c}
}

// ConnectionPoolHandler handles an Aiven ConnectionPool
type ConnectionPoolHandler struct{}

Expand Down
4 changes: 4 additions & 0 deletions controllers/database_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ type DatabaseReconciler struct {
Controller
}

func newDatabaseReconciler(c Controller) reconcilerType {
return &DatabaseReconciler{Controller: c}
}

// DatabaseHandler handles an Aiven Database
type DatabaseHandler struct{}

Expand Down
4 changes: 4 additions & 0 deletions controllers/grafana_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ type GrafanaReconciler struct {
Controller
}

func newGrafanaReconciler(c Controller) reconcilerType {
return &GrafanaReconciler{Controller: c}
}

// +kubebuilder:rbac:groups=aiven.io,resources=grafanas,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=aiven.io,resources=grafanas/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=aiven.io,resources=grafanas/finalizers,verbs=get;list;watch;create;update;patch;delete
Expand Down
4 changes: 4 additions & 0 deletions controllers/kafka_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ type KafkaReconciler struct {
Controller
}

func newKafkaReconciler(c Controller) reconcilerType {
return &KafkaReconciler{Controller: c}
}

//+kubebuilder:rbac:groups=aiven.io,resources=kafkas,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=aiven.io,resources=kafkas/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=aiven.io,resources=kafkas/finalizers,verbs=get;list;watch;create;update;patch;delete
Expand Down
4 changes: 4 additions & 0 deletions controllers/kafkaacl_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ type KafkaACLReconciler struct {
Controller
}

func newKafkaACLReconciler(c Controller) reconcilerType {
return &KafkaACLReconciler{Controller: c}
}

type KafkaACLHandler struct{}

// +kubebuilder:rbac:groups=aiven.io,resources=kafkaacls,verbs=get;list;watch;create;update;patch;delete
Expand Down
4 changes: 4 additions & 0 deletions controllers/kafkaconnect_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ type KafkaConnectReconciler struct {
Controller
}

func newKafkaConnectReconciler(c Controller) reconcilerType {
return &KafkaConnectReconciler{Controller: c}
}

// +kubebuilder:rbac:groups=aiven.io,resources=kafkaconnects,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=aiven.io,resources=kafkaconnects/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=aiven.io,resources=kafkaconnects/finalizers,verbs=get;list;watch;create;update;patch;delete
Expand Down
4 changes: 4 additions & 0 deletions controllers/kafkaconnector_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ type KafkaConnectorReconciler struct {
Controller
}

func newKafkaConnectorReconciler(c Controller) reconcilerType {
return &KafkaConnectorReconciler{Controller: c}
}

type KafkaConnectorHandler struct {
k8s client.Client
}
Expand Down
4 changes: 4 additions & 0 deletions controllers/kafkaschema_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ type KafkaSchemaReconciler struct {
Controller
}

func newKafkaSchemaReconciler(c Controller) reconcilerType {
return &KafkaSchemaReconciler{Controller: c}
}

type KafkaSchemaHandler struct{}

// +kubebuilder:rbac:groups=aiven.io,resources=kafkaschemas,verbs=get;list;watch;create;update;patch;delete
Expand Down
4 changes: 4 additions & 0 deletions controllers/kafkatopic_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ type KafkaTopicReconciler struct {
Controller
}

func newKafkaTopicReconciler(c Controller) reconcilerType {
return &KafkaTopicReconciler{Controller: c}
}

type KafkaTopicHandler struct{}

// +kubebuilder:rbac:groups=aiven.io,resources=kafkatopics,verbs=get;list;watch;create;update;patch;delete
Expand Down
4 changes: 4 additions & 0 deletions controllers/mysql_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ type MySQLReconciler struct {
Controller
}

func newMySQLReconciler(c Controller) reconcilerType {
return &MySQLReconciler{Controller: c}
}

//+kubebuilder:rbac:groups=aiven.io,resources=mysqls,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=aiven.io,resources=mysqls/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=aiven.io,resources=mysqls/finalizers,verbs=get;list;watch;create;update;patch;delete
Expand Down
4 changes: 4 additions & 0 deletions controllers/opensearch_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ type OpenSearchReconciler struct {
Controller
}

func newOpenSearchReconciler(c Controller) reconcilerType {
return &OpenSearchReconciler{Controller: c}
}

type OpenSearchHandler struct{}

//+kubebuilder:rbac:groups=aiven.io,resources=opensearches,verbs=get;list;watch;create;update;patch;delete
Expand Down
4 changes: 4 additions & 0 deletions controllers/postgresql_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ type PostgreSQLReconciler struct {
Controller
}

func newPostgreSQLReconciler(c Controller) reconcilerType {
return &PostgreSQLReconciler{Controller: c}
}

const waitForTaskToCompleteInterval = time.Second * 10

//+kubebuilder:rbac:groups=aiven.io,resources=postgresqls,verbs=get;list;watch;create;update;patch;delete
Expand Down
4 changes: 4 additions & 0 deletions controllers/project_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ type ProjectReconciler struct {
Controller
}

func newProjectReconciler(c Controller) reconcilerType {
return &ProjectReconciler{Controller: c}
}

// ProjectHandler handles an Aiven project
type ProjectHandler struct{}

Expand Down
4 changes: 4 additions & 0 deletions controllers/projectvpc_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ type ProjectVPCReconciler struct {
Controller
}

func newProjectVPCReconciler(c Controller) reconcilerType {
return &ProjectVPCReconciler{Controller: c}
}

type ProjectVPCHandler struct {
log logr.Logger
}
Expand Down
4 changes: 4 additions & 0 deletions controllers/redis_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ type RedisReconciler struct {
Controller
}

func newRedisReconciler(c Controller) reconcilerType {
return &RedisReconciler{Controller: c}
}

type RedisHandler struct{}

//+kubebuilder:rbac:groups=aiven.io,resources=redis,verbs=get;list;watch;create;update;patch;delete
Expand Down
4 changes: 4 additions & 0 deletions controllers/serviceintegration_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ type ServiceIntegrationReconciler struct {
Controller
}

func newServiceIntegrationReconciler(c Controller) reconcilerType {
return &ServiceIntegrationReconciler{Controller: c}
}

type ServiceIntegrationHandler struct{}

// +kubebuilder:rbac:groups=aiven.io,resources=serviceintegrations,verbs=get;list;watch;create;update;patch;delete
Expand Down
4 changes: 4 additions & 0 deletions controllers/serviceuser_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ type ServiceUserReconciler struct {
Controller
}

func newServiceUserReconciler(c Controller) reconcilerType {
return &ServiceUserReconciler{Controller: c}
}

type ServiceUserHandler struct{}

// +kubebuilder:rbac:groups=aiven.io,resources=serviceusers,verbs=update;get;list;watch;create;delete
Expand Down
153 changes: 36 additions & 117 deletions controllers/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,16 @@ import (
"strings"

ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

type reconcilerBuilder func(controller Controller) reconcilerType

type reconcilerType interface {
reconcile.Reconciler
SetupWithManager(mgr ctrl.Manager) error
}

func SetupControllers(mgr ctrl.Manager, defaultToken, kubeVersion, operatorVersion string) error {
if err := (&SecretFinalizerGCController{
Client: mgr.GetClient(),
Expand All @@ -15,123 +23,34 @@ func SetupControllers(mgr ctrl.Manager, defaultToken, kubeVersion, operatorVersi
return fmt.Errorf("controller SecretFinalizerGCController: %w", err)
}

if err := (&ProjectReconciler{
Controller: newController(mgr, "Project", defaultToken, kubeVersion, operatorVersion),
}).SetupWithManager(mgr); err != nil {
return fmt.Errorf("controller Project: %w", err)
}

if err := (&PostgreSQLReconciler{
Controller: newController(mgr, "PostgreSQL", defaultToken, kubeVersion, operatorVersion),
}).SetupWithManager(mgr); err != nil {
return fmt.Errorf("controller PostgreSQL: %w", err)
}

if err := (&ConnectionPoolReconciler{
Controller: newController(mgr, "ConnectionPool", defaultToken, kubeVersion, operatorVersion),
}).SetupWithManager(mgr); err != nil {
return fmt.Errorf("controller ConnectionPool: %w", err)
}

if err := (&DatabaseReconciler{
Controller: newController(mgr, "Database", defaultToken, kubeVersion, operatorVersion),
}).SetupWithManager(mgr); err != nil {
return fmt.Errorf("controller Database: %w", err)
}

if err := (&KafkaReconciler{
Controller: newController(mgr, "Kafka", defaultToken, kubeVersion, operatorVersion),
}).SetupWithManager(mgr); err != nil {
return fmt.Errorf("controller Kafka: %w", err)
}

if err := (&ProjectVPCReconciler{
Controller: newController(mgr, "ProjectVPC", defaultToken, kubeVersion, operatorVersion),
}).SetupWithManager(mgr); err != nil {
return fmt.Errorf("controller ProjectVPC: %w", err)
}

if err := (&KafkaTopicReconciler{
Controller: newController(mgr, "KafkaTopic", defaultToken, kubeVersion, operatorVersion),
}).SetupWithManager(mgr); err != nil {
return fmt.Errorf("controller KafkaTopic: %w", err)
}

if err := (&KafkaACLReconciler{
Controller: newController(mgr, "KafkaACL", defaultToken, kubeVersion, operatorVersion),
}).SetupWithManager(mgr); err != nil {
return fmt.Errorf("controller KafkaACL: %w", err)
}

if err := (&KafkaConnectReconciler{
Controller: newController(mgr, "KafkaConnect", defaultToken, kubeVersion, operatorVersion),
}).SetupWithManager(mgr); err != nil {
return fmt.Errorf("controller KafkaConnect: %w", err)
}

if err := (&ServiceUserReconciler{
Controller: newController(mgr, "ServiceUser", defaultToken, kubeVersion, operatorVersion),
}).SetupWithManager(mgr); err != nil {
return fmt.Errorf("controller ServiceUser: %w", err)
}

if err := (&KafkaSchemaReconciler{
Controller: newController(mgr, "KafkaSchema", defaultToken, kubeVersion, operatorVersion),
}).SetupWithManager(mgr); err != nil {
return fmt.Errorf("controller KafkaSchema: %w", err)
}

if err := (&ServiceIntegrationReconciler{
Controller: newController(mgr, "ServiceIntegration", defaultToken, kubeVersion, operatorVersion),
}).SetupWithManager(mgr); err != nil {
return fmt.Errorf("controller ServiceIntegration: %w", err)
}
if err := (&KafkaConnectorReconciler{
Controller: newController(mgr, "KafkaConnector", defaultToken, kubeVersion, operatorVersion),
}).SetupWithManager(mgr); err != nil {
return fmt.Errorf("controller KafkaConnector: %w", err)
}

if err := (&RedisReconciler{
Controller: newController(mgr, "Redis", defaultToken, kubeVersion, operatorVersion),
}).SetupWithManager(mgr); err != nil {
return fmt.Errorf("controller Redis: %w", err)
}

if err := (&OpenSearchReconciler{
Controller: newController(mgr, "OpenSearch", defaultToken, kubeVersion, operatorVersion),
}).SetupWithManager(mgr); err != nil {
return fmt.Errorf("controller OpenSearch: %w", err)
}

if err := (&ClickhouseReconciler{
Controller: newController(mgr, "Clickhouse", defaultToken, kubeVersion, operatorVersion),
}).SetupWithManager(mgr); err != nil {
return fmt.Errorf("controller Clickhouse: %w", err)
}

if err := (&ClickhouseUserReconciler{
Controller: newController(mgr, "ClickhouseUser", defaultToken, kubeVersion, operatorVersion),
}).SetupWithManager(mgr); err != nil {
return fmt.Errorf("controller ClickhouseUser: %w", err)
}

if err := (&MySQLReconciler{
Controller: newController(mgr, "MySQL", defaultToken, kubeVersion, operatorVersion),
}).SetupWithManager(mgr); err != nil {
return fmt.Errorf("controller MySQL: %w", err)
}

if err := (&CassandraReconciler{
Controller: newController(mgr, "Cassandra", defaultToken, kubeVersion, operatorVersion),
}).SetupWithManager(mgr); err != nil {
return fmt.Errorf("controller Cassandra: %w", err)
}

if err := (&GrafanaReconciler{
Controller: newController(mgr, "Grafana", defaultToken, kubeVersion, operatorVersion),
}).SetupWithManager(mgr); err != nil {
return fmt.Errorf("controller Grafana: %w", err)
builders := map[string]reconcilerBuilder{
"Cassandra": newCassandraReconciler,
"Clickhouse": newClickhouseReconciler,
"ClickhouseUser": newClickhouseUserReconciler,
"ConnectionPool": newConnectionPoolReconciler,
"Database": newDatabaseReconciler,
"Grafana": newGrafanaReconciler,
"Kafka": newKafkaReconciler,
"KafkaACL": newKafkaACLReconciler,
"KafkaConnect": newKafkaConnectReconciler,
"KafkaConnector": newKafkaConnectorReconciler,
"KafkaSchema": newKafkaSchemaReconciler,
"KafkaTopic": newKafkaTopicReconciler,
"MySQL": newMySQLReconciler,
"OpenSearch": newOpenSearchReconciler,
"PostgreSQL": newPostgreSQLReconciler,
"Project": newProjectReconciler,
"ProjectVPC": newProjectVPCReconciler,
"Redis": newRedisReconciler,
"ServiceIntegration": newServiceIntegrationReconciler,
"ServiceUser": newServiceUserReconciler,
}

for k, v := range builders {
err := v(newController(mgr, k, defaultToken, kubeVersion, operatorVersion)).SetupWithManager(mgr)
if err != nil {
return fmt.Errorf("controller %s setup error: %w", k, err)
}
}

//+kubebuilder:scaffold:builder
Expand Down

0 comments on commit a5e5b06

Please sign in to comment.