Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue-112, k8s resource events for Zookeeper resource is implemented #354

Merged
merged 1 commit into from
Mar 14, 2023
Merged
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 config/manager/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ kind: Kustomization
images:
- name: controller
newName: rostislavporohnya/instaclustr-operator
newTag: icop370
newTag: icop372
1 change: 1 addition & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ rules:
- events
verbs:
- create
- patch
- apiGroups:
- ""
resources:
Expand Down
8 changes: 4 additions & 4 deletions config/samples/clusters_v1alpha1_postgresql.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ spec:
- region: "US_WEST_2"
network: "10.1.0.0/16"
cloudProvider: "AWS_VPC"
# nodeSize: "PGS-DEV-t4g.medium-30"
nodeSize: "PGS-DEV-t4g.small-5"
nodeSize: "PGS-DEV-t4g.medium-30"
# nodeSize: "PGS-DEV-t4g.small-5"
nodesNumber: 2
clientEncryption: false
name: "testDC1"
Expand All @@ -37,8 +37,8 @@ spec:
# clusterConfigurations:
# idle_in_transaction_session_timeout: "2"
# statement_timeout: "1"
# twoFactorDelete:
# - email: "rostyslp@netapp.com"
# twoFactorDelete:
# - email: "rostyslp@netapp.com"
# description: "test 222"
slaTier: "NON_PRODUCTION"
privateNetworkCluster: false
Expand Down
2 changes: 1 addition & 1 deletion controllers/clusters/cassandra_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ type CassandraReconciler struct {
//+kubebuilder:rbac:groups=clusters.instaclustr.com,resources=cassandras,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=clusters.instaclustr.com,resources=cassandras/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=clusters.instaclustr.com,resources=cassandras/finalizers,verbs=update
//+kubebuilder:rbac:groups="",resources=events,verbs=create
//+kubebuilder:rbac:groups="",resources=events,verbs=create;patch

// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
Expand Down
4 changes: 2 additions & 2 deletions controllers/clusters/kafka_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type KafkaReconciler struct {
//+kubebuilder:rbac:groups=clusters.instaclustr.com,resources=kafkas,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=clusters.instaclustr.com,resources=kafkas/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=clusters.instaclustr.com,resources=kafkas/finalizers,verbs=update
//+kubebuilder:rbac:groups="",resources=events,verbs=create
//+kubebuilder:rbac:groups="",resources=events,verbs=create;patch

// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
Expand Down Expand Up @@ -109,7 +109,7 @@ func (r *KafkaReconciler) handleCreateCluster(ctx context.Context, kafka *cluste
)
r.EventRecorder.Eventf(
kafka, models.Warning, models.CreationFailed,
"Cluster creation on the Instaclustr cloud is failed. Reason: %v",
"Cluster creation on the Instaclustr is failed. Reason: %v",
err,
)
return models.ReconcileRequeue
Expand Down
2 changes: 1 addition & 1 deletion controllers/clusters/kafkaconnect_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type KafkaConnectReconciler struct {
//+kubebuilder:rbac:groups=clusters.instaclustr.com,resources=kafkaconnects,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=clusters.instaclustr.com,resources=kafkaconnects/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=clusters.instaclustr.com,resources=kafkaconnects/finalizers,verbs=update
//+kubebuilder:rbac:groups="",resources=events,verbs=create
//+kubebuilder:rbac:groups="",resources=events,verbs=create;patch

// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
Expand Down
8 changes: 4 additions & 4 deletions controllers/clusters/postgresql_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ type PostgreSQLReconciler struct {
//+kubebuilder:rbac:groups=clusters.instaclustr.com,resources=postgresqls/finalizers,verbs=update
//+kubebuilder:rbac:groups=clusterresources.instaclustr.com,resources=clusterbackups,verbs=get;list;create;update;patch;deletecollection;delete
//+kubebuilder:rbac:groups="",resources=secrets,verbs=get;watch;create;delete;update
//+kubebuilder:rbac:groups="",resources=events,verbs=create
//+kubebuilder:rbac:groups="",resources=events,verbs=create;patch

// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
Expand Down Expand Up @@ -198,7 +198,7 @@ func (r *PostgreSQLReconciler) HandleCreateCluster(

r.EventRecorder.Eventf(
pgCluster, models.Normal, models.Created,
"Cluster is restored from backup. Original cluster ID: %s, new cluster ID: %s",
"Cluster restore request is sent. Original cluster ID: %s, new cluster ID: %s",
pgCluster.Spec.PgRestoreFrom.ClusterID,
pgCluster.Status.ID,
)
Expand All @@ -220,7 +220,7 @@ func (r *PostgreSQLReconciler) HandleCreateCluster(

r.EventRecorder.Eventf(
pgCluster, models.Warning, models.CreationFailed,
"Cluster creation on the Instaclustr cloud is failed. Reason: %v",
"Cluster creation on the Instaclustr is failed. Reason: %v",
err,
)

Expand All @@ -232,7 +232,7 @@ func (r *PostgreSQLReconciler) HandleCreateCluster(

r.EventRecorder.Eventf(
pgCluster, models.Normal, models.Created,
"Cluster is created. Cluster ID: %s",
"Cluster creation request is sent. Cluster ID: %s",
pgCluster.Status.ID,
)
}
Expand Down
7 changes: 4 additions & 3 deletions controllers/clusters/redis_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type RedisReconciler struct {
//+kubebuilder:rbac:groups=clusters.instaclustr.com,resources=redis,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=clusters.instaclustr.com,resources=redis/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=clusters.instaclustr.com,resources=redis/finalizers,verbs=update
//+kubebuilder:rbac:groups="",resources=events,verbs=create
//+kubebuilder:rbac:groups="",resources=events,verbs=create;patch

// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
Expand Down Expand Up @@ -137,7 +137,7 @@ func (r *RedisReconciler) handleCreateCluster(

r.EventRecorder.Eventf(
redis, models.Normal, models.Created,
"Cluster is restored from backup. Original cluster ID: %s, new cluster ID: %s",
"Cluster restore request is sent. Original cluster ID: %s, new cluster ID: %s",
redis.Spec.RestoreFrom.ClusterID,
id,
)
Expand Down Expand Up @@ -169,7 +169,8 @@ func (r *RedisReconciler) handleCreateCluster(
)
r.EventRecorder.Eventf(
redis, models.Normal, models.Created,
"Cluster is created on the Instaclustr.",
"Cluster creation request is sent. Cluster ID: %s",
id,
)
}

Expand Down
9 changes: 5 additions & 4 deletions controllers/clusters/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,11 @@ var _ = BeforeSuite(func() {
Expect(err).ToNot(HaveOccurred())

err = (&ZookeeperReconciler{
Client: k8sManager.GetClient(),
Scheme: k8sManager.GetScheme(),
API: instaClient,
Scheduler: scheduler.NewScheduler(logf.Log),
Client: k8sManager.GetClient(),
Scheme: k8sManager.GetScheme(),
API: instaClient,
Scheduler: scheduler.NewScheduler(logf.Log),
EventRecorder: eRecorder,
}).SetupWithManager(k8sManager)
Expect(err).ToNot(HaveOccurred())

Expand Down
73 changes: 66 additions & 7 deletions controllers/clusters/zookeeper_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/go-logr/logr"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -41,21 +42,19 @@ import (
// ZookeeperReconciler reconciles a Zookeeper object
type ZookeeperReconciler struct {
client.Client
Scheme *runtime.Scheme
API instaclustr.API
Scheduler scheduler.Interface
Scheme *runtime.Scheme
API instaclustr.API
Scheduler scheduler.Interface
EventRecorder record.EventRecorder
}

//+kubebuilder:rbac:groups=clusters.instaclustr.com,resources=zookeepers,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=clusters.instaclustr.com,resources=zookeepers/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=clusters.instaclustr.com,resources=zookeepers/finalizers,verbs=update
//+kubebuilder:rbac:groups="",resources=events,verbs=create;patch

// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
// TODO(user): Modify the Reconcile function to compare the state specified by
// the Zookeeper object against the actual cluster state, and then
// perform operations to make the cluster state reflect the state specified by
// the user.
//
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.12.2/pkg/reconcile
Expand Down Expand Up @@ -115,13 +114,29 @@ func (r *ZookeeperReconciler) handleCreateCluster(
zook.Status.ID, err = r.API.CreateCluster(instaclustr.ZookeeperEndpoint, zook.Spec.ToInstAPI())
if err != nil {
l.Error(err, "Cannot create zookeeper cluster", "spec", zook.Spec)
r.EventRecorder.Eventf(
zook, models.Warning, models.CreationFailed,
"Cluster creation on the Instaclustr is failed. Reason: %v",
err,
)
return models.ReconcileRequeue
}

r.EventRecorder.Eventf(
zook, models.Normal, models.Created,
"Cluster creation request is sent. Cluster ID: %s",
zook.Status.ID,
)

err = r.Status().Patch(ctx, zook, patch)
if err != nil {
l.Error(err, "Cannot patch zookeeper cluster status from the Instaclustr API",
"spec", zook.Spec)
r.EventRecorder.Eventf(
zook, models.Warning, models.PatchFailed,
"Cluster resource status patch is failed. Reason: %v",
err,
)
return models.ReconcileRequeue
}

Expand All @@ -130,6 +145,11 @@ func (r *ZookeeperReconciler) handleCreateCluster(
err = r.Patch(ctx, zook, patch)
if err != nil {
l.Error(err, "Cannot patch zookeeper", "zookeeper name", zook.Spec.Name)
r.EventRecorder.Eventf(
zook, models.Warning, models.PatchFailed,
"Cluster resource patch is failed. Reason: %v",
err,
)
return models.ReconcileRequeue
}
}
Expand All @@ -138,9 +158,19 @@ func (r *ZookeeperReconciler) handleCreateCluster(
if err != nil {
l.Error(err, "Cannot start cluster status job",
"zookeeper cluster ID", zook.Status.ID)
r.EventRecorder.Eventf(
zook, models.Warning, models.CreationFailed,
"Cluster status check job creation is failed. Reason: %v",
err,
)
return models.ReconcileRequeue
}

r.EventRecorder.Eventf(
zook, models.Normal, models.Created,
"Cluster status check job is started",
)

l.Info("Zookeeper cluster has been created", "cluster ID", zook.Status.ID)

return models.ExitReconcile
Expand Down Expand Up @@ -169,6 +199,11 @@ func (r *ZookeeperReconciler) handleDeleteCluster(
if err != nil {
l.Error(err, "Cannot patch Zookeeper cluster",
"cluster name", zook.Spec.Name, "status", zook.Status.State)
r.EventRecorder.Eventf(
zook, models.Warning, models.PatchFailed,
"Cluster resource patch is failed. Reason: %v",
err,
)
return models.ReconcileRequeue
}

Expand All @@ -177,6 +212,11 @@ func (r *ZookeeperReconciler) handleDeleteCluster(
l.Error(err, "Cannot get zookeeper cluster",
"cluster name", zook.Spec.Name,
"status", zook.Status.ClusterStatus.State)
r.EventRecorder.Eventf(
zook, models.Warning, models.FetchFailed,
"Cluster resource fetch from the Instaclustr API is failed. Reason: %v",
err,
)
return models.ReconcileRequeue
}

Expand All @@ -186,9 +226,18 @@ func (r *ZookeeperReconciler) handleDeleteCluster(
l.Error(err, "Cannot delete zookeeper cluster",
"cluster name", zook.Spec.Name,
"cluster status", zook.Status.State)
r.EventRecorder.Eventf(
zook, models.Warning, models.DeletionFailed,
"Cluster deletion is failed on the Instaclustr. Reason: %v",
err,
)
return models.ReconcileRequeue
}

r.EventRecorder.Eventf(
zook, models.Normal, models.DeletionStarted,
"Cluster deletion request is sent to the Instaclustr API.",
)
return models.ReconcileRequeue
}

Expand All @@ -199,13 +248,23 @@ func (r *ZookeeperReconciler) handleDeleteCluster(
if err != nil {
l.Error(err, "Cannot patch remove finalizer from zookeeper",
"cluster name", zook.Spec.Name)
r.EventRecorder.Eventf(
zook, models.Warning, models.PatchFailed,
"Cluster resource patch is failed. Reason: %v",
err,
)
return models.ReconcileRequeue
}

l.Info("Zookeeper cluster was deleted",
"cluster ID", zook.Status.ID,
)

r.EventRecorder.Eventf(
zook, models.Normal, models.Deleted,
"Cluster resource is deleted",
)

return models.ExitReconcile
}

Expand Down
9 changes: 5 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,11 @@ func main() {
os.Exit(1)
}
if err = (&clusterscontrollers.ZookeeperReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
API: instaClient,
Scheduler: s,
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
API: instaClient,
Scheduler: s,
EventRecorder: eventRecorder,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Zookeeper")
os.Exit(1)
Expand Down