Skip to content

Commit

Permalink
add cluster finalizer (#409)
Browse files Browse the repository at this point in the history
  • Loading branch information
MegaByte875 authored Dec 12, 2023
1 parent 552e89d commit 202dac5
Show file tree
Hide file tree
Showing 13 changed files with 165 additions and 8 deletions.
21 changes: 20 additions & 1 deletion pkg/controller/component/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,21 @@ func (c *nebulaConsole) Reconcile(nc *v1alpha1.NebulaCluster) error {
return c.syncConsolePod(nc)
}

func (c *nebulaConsole) Delete(nc *v1alpha1.NebulaCluster) error {
if nc.Spec.Console == nil {
return nil
}
podName := getConsolePodName(nc.Name)
_, err := c.clientSet.Pod().GetPod(nc.Namespace, podName)
if err != nil {
if apierrors.IsNotFound(err) {
return nil
}
return err
}
return c.clientSet.Pod().DeletePod(nc.Namespace, podName, true)
}

func (c *nebulaConsole) syncConsolePod(nc *v1alpha1.NebulaCluster) error {
newPod := c.generatePod(nc)
oldPod, err := c.clientSet.Pod().GetPod(newPod.Namespace, newPod.Name)
Expand Down Expand Up @@ -140,7 +155,7 @@ func (c *nebulaConsole) generatePod(nc *v1alpha1.NebulaCluster) *corev1.Pod {
volumes = append(volumes, certVolumes...)
}

podName := nc.GetName() + "-console"
podName := getConsolePodName(nc.Name)
return &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: podName,
Expand Down Expand Up @@ -190,6 +205,10 @@ func getConsoleImage(console *v1alpha1.ConsoleSpec) string {
return image
}

func getConsolePodName(clusterName string) string {
return clusterName + "-console"
}

type FakeNebulaConsole struct {
err error
}
Expand Down
21 changes: 20 additions & 1 deletion pkg/controller/component/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,21 @@ func (e *nebulaExporter) Reconcile(nc *v1alpha1.NebulaCluster) error {
return e.syncExporterDeployment(nc)
}

func (e *nebulaExporter) Delete(nc *v1alpha1.NebulaCluster) error {
if nc.Spec.Exporter == nil {
return nil
}
deployName := getExporterDeploymentName(nc.Name)
deploy, err := e.clientSet.Deployment().GetDeployment(nc.Namespace, deployName)
if err != nil {
if apierrors.IsNotFound(err) {
return nil
}
return err
}
return e.clientSet.Deployment().DeleteDeployment(deploy)
}

func (e *nebulaExporter) syncExporterService(nc *v1alpha1.NebulaCluster) error {
newSvc := e.generateService(nc)

Expand Down Expand Up @@ -108,7 +123,7 @@ func (e *nebulaExporter) generateService(nc *v1alpha1.NebulaCluster) *corev1.Ser
func (e *nebulaExporter) generateDeployment(nc *v1alpha1.NebulaCluster) *appsv1.Deployment {
namespace := nc.GetNamespace()
ncName := nc.GetName()
deployName := fmt.Sprintf("%s-exporter", nc.GetName())
deployName := getExporterDeploymentName(nc.Name)
labels := e.getExporterLabels(nc)
livenessProbe := nc.ExporterComponent().ComponentSpec().LivenessProbe()
containers := make([]corev1.Container, 0)
Expand Down Expand Up @@ -215,6 +230,10 @@ func (e *nebulaExporter) getExporterLabels(nc *v1alpha1.NebulaCluster) map[strin
return maputil.MergeStringMaps(true, labels, podLabels)
}

func getExporterDeploymentName(clusterName string) string {
return clusterName + "-exporter"
}

type FakeNebulaExporter struct {
err error
}
Expand Down
21 changes: 21 additions & 0 deletions pkg/controller/component/graphd_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,27 @@ func (c *graphdCluster) Reconcile(nc *v1alpha1.NebulaCluster) error {
return c.syncGraphdWorkload(nc)
}

func (c *graphdCluster) Delete(nc *v1alpha1.NebulaCluster) error {
if nc.Spec.Graphd == nil {
return nil
}
namespace := nc.GetNamespace()
componentName := nc.GraphdComponent().GetName()
gvk, err := resource.GetGVKFromDefinition(c.dm, nc.Spec.Reference)
if err != nil {
return fmt.Errorf("get workload kind failed: %v", err)
}
workload, err := c.clientSet.Workload().GetWorkload(namespace, componentName, gvk)
if err != nil {
if apierrors.IsNotFound(err) {
return nil
}
klog.Errorf("get graphd cluster failed: %v", err)
return err
}
return c.clientSet.Workload().DeleteWorkload(workload)
}

func (c *graphdCluster) syncGraphdWorkload(nc *v1alpha1.NebulaCluster) error {
namespace := nc.GetNamespace()
componentName := nc.GraphdComponent().GetName()
Expand Down
3 changes: 3 additions & 0 deletions pkg/controller/component/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ import (
type ReconcileManager interface {
// Reconcile reconciles the cluster to desired state
Reconcile(cluster *v1alpha1.NebulaCluster) error

// Delete deletes the cluster
Delete(cluster *v1alpha1.NebulaCluster) error
}

type ScaleManager interface {
Expand Down
21 changes: 21 additions & 0 deletions pkg/controller/component/metad_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,27 @@ func (c *metadCluster) Reconcile(nc *v1alpha1.NebulaCluster) error {
return c.syncMetadWorkload(nc)
}

func (c *metadCluster) Delete(nc *v1alpha1.NebulaCluster) error {
if nc.Spec.Metad == nil {
return nil
}
namespace := nc.GetNamespace()
componentName := nc.MetadComponent().GetName()
gvk, err := resource.GetGVKFromDefinition(c.dm, nc.Spec.Reference)
if err != nil {
return fmt.Errorf("get workload kind failed: %v", err)
}
workload, err := c.clientSet.Workload().GetWorkload(namespace, componentName, gvk)
if err != nil {
if apierrors.IsNotFound(err) {
return nil
}
klog.Errorf("get metad cluster failed: %v", err)
return err
}
return c.clientSet.Workload().DeleteWorkload(workload)
}

func (c *metadCluster) syncMetadHeadlessService(nc *v1alpha1.NebulaCluster) error {
newSvc := nc.MetadComponent().GenerateHeadlessService()
if newSvc == nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
"github.com/vesoft-inc/nebula-operator/pkg/kube"
)

func PvcGc(cli client.Client, namespace, clusterName string) error {
func PVCDeleter(cli client.Client, namespace, clusterName string) error {
selector, err := label.New().Cluster(clusterName).Selector()
if err != nil {
return fmt.Errorf("get cluster [%s/%s] label selector failed: %v", namespace, clusterName, err)
Expand Down
5 changes: 5 additions & 0 deletions pkg/controller/component/reclaimer/meta_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ func (m *meta) resolvePVCFromPod(pod *corev1.Pod) ([]*corev1.PersistentVolumeCla
return pvcs, nil
}

func (m *meta) Delete(nc *v1alpha1.NebulaCluster) error {
//TODO implement me
return nil
}

type FakeMetaReconciler struct {
err error
}
Expand Down
21 changes: 21 additions & 0 deletions pkg/controller/component/storaged_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,27 @@ func (c *storagedCluster) Reconcile(nc *v1alpha1.NebulaCluster) error {
return c.syncStoragedWorkload(nc)
}

func (c *storagedCluster) Delete(nc *v1alpha1.NebulaCluster) error {
if nc.Spec.Storaged == nil {
return nil
}
namespace := nc.GetNamespace()
componentName := nc.StoragedComponent().GetName()
gvk, err := resource.GetGVKFromDefinition(c.dm, nc.Spec.Reference)
if err != nil {
return fmt.Errorf("get workload kind failed: %v", err)
}
workload, err := c.clientSet.Workload().GetWorkload(namespace, componentName, gvk)
if err != nil {
if apierrors.IsNotFound(err) {
return nil
}
klog.Errorf("get storaged cluster failed: %v", err)
return err
}
return c.clientSet.Workload().DeleteWorkload(workload)
}

func (c *storagedCluster) syncStoragedHeadlessService(nc *v1alpha1.NebulaCluster) error {
newSvc := nc.StoragedComponent().GenerateHeadlessService()
if newSvc == nil {
Expand Down
28 changes: 28 additions & 0 deletions pkg/controller/nebulacluster/nebula_cluster_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,14 @@ import (
utilerrors "github.com/vesoft-inc/nebula-operator/pkg/util/errors"
)

const (
finalizer = "apps.nebula-graph.io/cluster-cleanup"
)

type ControlInterface interface {
UpdateNebulaCluster(cluster *v1alpha1.NebulaCluster) error

DeleteCluster(cluster *v1alpha1.NebulaCluster) error
}

var _ ControlInterface = &defaultNebulaClusterControl{}
Expand Down Expand Up @@ -102,7 +108,29 @@ func (c *defaultNebulaClusterControl) UpdateNebulaCluster(nc *v1alpha1.NebulaClu
return errorutils.NewAggregate(errs)
}

func (c *defaultNebulaClusterControl) DeleteCluster(nc *v1alpha1.NebulaCluster) error {
if err := c.graphdCluster.Delete(nc); err != nil {
return err
}
if err := c.storagedCluster.Delete(nc); err != nil {
return err
}
if err := c.metaReconciler.Delete(nc); err != nil {
return err
}
if err := component.PVCDeleter(c.client, nc.Namespace, nc.Name); err != nil {
return err
}
return kube.UpdateFinalizer(context.TODO(), c.client, nc, kube.RemoveFinalizerOpType, finalizer)
}

func (c *defaultNebulaClusterControl) updateNebulaCluster(nc *v1alpha1.NebulaCluster) error {
if !kube.HasFinalizer(nc, finalizer) {
if err := kube.UpdateFinalizer(context.TODO(), c.client, nc, kube.AddFinalizerOpType, finalizer); err != nil {
return err
}
}

if err := kube.CheckRBAC(context.TODO(), c.client, nc.Namespace); err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/controller/nebulacluster/nebula_cluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,6 @@ func (r *ClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (re
if err := r.client.Get(subCtx, req.NamespacedName, &nebulaCluster); err != nil {
if apierrors.IsNotFound(err) {
klog.Infof("Skipping because NebulaCluster [%s] has been deleted", key)
if err := component.PvcGc(r.client, req.Namespace, req.Name); err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
}
}
return ctrl.Result{}, client.IgnoreNotFound(err)
}
Expand Down Expand Up @@ -187,6 +184,9 @@ func (r *ClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (re
}

func (r *ClusterReconciler) syncNebulaCluster(nc *v1alpha1.NebulaCluster) error {
if nc.DeletionTimestamp != nil {
return r.control.DeleteCluster(nc)
}
return r.control.UpdateNebulaCluster(nc)
}

Expand Down
14 changes: 13 additions & 1 deletion pkg/kube/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

appsv1 "k8s.io/api/apps/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/util/retry"
Expand All @@ -16,7 +17,8 @@ import (
type Deployment interface {
CreateDeployment(deploy *appsv1.Deployment) error
GetDeployment(namespace string, name string) (*appsv1.Deployment, error)
UpdateDeployment(*appsv1.Deployment) error
UpdateDeployment(deploy *appsv1.Deployment) error
DeleteDeployment(deploy *appsv1.Deployment) error
}

type deployClient struct {
Expand Down Expand Up @@ -77,3 +79,13 @@ func (d *deployClient) UpdateDeployment(deploy *appsv1.Deployment) error {
return updateErr
})
}

func (d *deployClient) DeleteDeployment(deploy *appsv1.Deployment) error {
preconditions := metav1.Preconditions{UID: &deploy.UID, ResourceVersion: &deploy.ResourceVersion}
policy := metav1.DeletePropagationForeground
options := &client.DeleteOptions{
PropagationPolicy: &policy,
Preconditions: &preconditions,
}
return d.kubecli.Delete(context.TODO(), deploy, options)
}
4 changes: 4 additions & 0 deletions pkg/kube/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ func UpdateFinalizer(ctx context.Context, c client.Client, object client.Object,
})
}

func HasFinalizer(obj client.Object, finalizer string) bool {
return controllerutil.ContainsFinalizer(obj, finalizer)
}

func ValidVersion(ver *version.Info) (bool, error) {
major, err := strconv.Atoi(ver.Major)
if err != nil {
Expand Down
6 changes: 5 additions & 1 deletion pkg/kube/workload.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,13 @@ func (w *workloadClient) UpdateWorkload(obj *unstructured.Unstructured) error {
}

func (w *workloadClient) DeleteWorkload(obj *unstructured.Unstructured) error {
policy := metav1.DeletePropagationBackground
uid := obj.GetUID()
resourceVersion := obj.GetResourceVersion()
preconditions := metav1.Preconditions{UID: &uid, ResourceVersion: &resourceVersion}
policy := metav1.DeletePropagationForeground
options := &client.DeleteOptions{
PropagationPolicy: &policy,
Preconditions: &preconditions,
}
return w.kubecli.Delete(context.TODO(), obj, options)
}
Expand Down

0 comments on commit 202dac5

Please sign in to comment.