Skip to content

Commit

Permalink
issue-118, k8s resource events for Cluster network firewall rule reso…
Browse files Browse the repository at this point in the history
…urce is implemented
  • Loading branch information
RostislavPorohnya authored and testisnullus committed Apr 5, 2023
1 parent 7d54034 commit ae44642
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ metadata:
name: clusternetworkfirewallrule-sample
spec:
network: 62.212.64.19/32
clusterId: bb7166e4-cffa-48e7-8f0a-5ee32873b750
type: CASSANDRA_CQL
clusterId: 944cfe6b-441f-4c5a-865b-42fd40c7d816
type: KAFKA
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 @@ -45,21 +46,19 @@ const (
// ClusterNetworkFirewallRuleReconciler reconciles a ClusterNetworkFirewallRule object
type ClusterNetworkFirewallRuleReconciler 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=clusterresources.instaclustr.com,resources=clusternetworkfirewallrules,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=clusterresources.instaclustr.com,resources=clusternetworkfirewallrules/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=clusterresources.instaclustr.com,resources=clusternetworkfirewallrules/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 ClusterNetworkFirewallRule 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.13.0/pkg/reconcile
Expand Down Expand Up @@ -121,14 +120,29 @@ func (r *ClusterNetworkFirewallRuleReconciler) HandleCreateFirewallRule(
err, "Cannot create cluster network firewall rule",
"spec", firewallRule.Spec,
)
r.EventRecorder.Eventf(
firewallRule, models.Warning, models.CreationFailed,
"Resource creation on the Instaclustr is failed. Reason: %v",
err,
)
return models.ReconcileRequeue
}

r.EventRecorder.Eventf(
firewallRule, models.Normal, models.Created,
"Resource creation request is sent",
)

firewallRule.Status.FirewallRuleStatus = *firewallRuleStatus

err = r.Status().Patch(ctx, firewallRule, patch)
if err != nil {
l.Error(err, "Cannot patch cluster network firewall rule status ", "ID", firewallRule.Status.ID)
r.EventRecorder.Eventf(
firewallRule, models.Warning, models.PatchFailed,
"Resource status patch is failed. Reason: %v",
err,
)
return models.ReconcileRequeue
}

Expand All @@ -141,6 +155,11 @@ func (r *ClusterNetworkFirewallRuleReconciler) HandleCreateFirewallRule(
"cluster ID", firewallRule.Spec.ClusterID,
"type", firewallRule.Spec.Type,
)
r.EventRecorder.Eventf(
firewallRule, models.Warning, models.PatchFailed,
"Resource patch is failed. Reason: %v",
err,
)
return models.ReconcileRequeue
}

Expand All @@ -155,9 +174,19 @@ func (r *ClusterNetworkFirewallRuleReconciler) HandleCreateFirewallRule(
if err != nil {
l.Error(err, "Cannot start cluster network firewall rule status checker job",
"firewall rule ID", firewallRule.Status.ID)
r.EventRecorder.Eventf(
firewallRule, models.Warning, models.CreationFailed,
"Resource status job creation is failed. Reason: %v",
err,
)
return models.ReconcileRequeue
}

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

return models.ExitReconcile
}

Expand Down Expand Up @@ -186,6 +215,11 @@ func (r *ClusterNetworkFirewallRuleReconciler) HandleDeleteFirewallRule(
"cluster ID", firewallRule.Spec.ClusterID,
"type", firewallRule.Spec.Type,
)
r.EventRecorder.Eventf(
firewallRule, models.Warning, models.PatchFailed,
"Resource patch is failed. Reason: %v",
err,
)
return models.ReconcileRequeue
}

Expand All @@ -196,6 +230,11 @@ func (r *ClusterNetworkFirewallRuleReconciler) HandleDeleteFirewallRule(
"cluster ID", firewallRule.Spec.ClusterID,
"type", firewallRule.Spec.Type,
)
r.EventRecorder.Eventf(
firewallRule, models.Warning, models.FetchFailed,
"Fetch resource from the Instaclustr API is failed. Reason: %v",
err,
)
return models.ReconcileRequeue
}

Expand All @@ -207,23 +246,35 @@ func (r *ClusterNetworkFirewallRuleReconciler) HandleDeleteFirewallRule(
"cluster ID", firewallRule.Spec.ClusterID,
"type", firewallRule.Spec.Type,
)
r.EventRecorder.Eventf(
firewallRule, models.Warning, models.DeletionFailed,
"Resource deletion on the Instaclustr is failed. Reason: %v",
err,
)
return models.ReconcileRequeue
}

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

r.Scheduler.RemoveJob(firewallRule.GetJobID(scheduler.StatusChecker))
controllerutil.RemoveFinalizer(firewallRule, models.DeletionFinalizer)
firewallRule.Annotations[models.ResourceStateAnnotation] = models.DeletedEvent

err = r.Patch(ctx, firewallRule, patch)
if err != nil {
l.Error(err, "Cannot patch cluster network firewall rule metadata",
"cluster ID", firewallRule.Spec.ClusterID,
"type", firewallRule.Spec.Type,
"status", firewallRule.Status,
)
r.EventRecorder.Eventf(
firewallRule, models.Warning, models.PatchFailed,
"Resource patch is failed. Reason: %v",
err,
)
return models.ReconcileRequeue
}

Expand All @@ -233,6 +284,11 @@ func (r *ClusterNetworkFirewallRuleReconciler) HandleDeleteFirewallRule(
"status", firewallRule.Status,
)

r.EventRecorder.Eventf(
firewallRule, models.Normal, models.Deleted,
"Resource is deleted",
)

return models.ExitReconcile
}

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

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

Expand Down
9 changes: 5 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,11 @@ func main() {
os.Exit(1)
}
if err = (&clusterresourcescontrollers.ClusterNetworkFirewallRuleReconciler{
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", "ClusterNetworkFirewallRule")
os.Exit(1)
Expand Down

0 comments on commit ae44642

Please sign in to comment.