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

refactor(operator): change chaos deletion to finalizer rather than ownreference #337

Merged
merged 1 commit into from
May 5, 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
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"strings"
"time"

"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

"github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/api/v1alpha1"
sschaos "github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/pkg/kubernetes/chaosmesh"
"github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/pkg/kubernetes/configmap"
Expand All @@ -48,6 +50,7 @@ import (
const (
ShardingSphereChaosControllerName = "shardingsphere-chaos-controller"
VerifyJobCheck = "Verify"
SSChaosFinalizeName = "shardingsphere.apache.org/finalizer"
)

type JobCondition string
Expand Down Expand Up @@ -80,6 +83,7 @@ func (r *ShardingSphereChaosReconciler) Reconcile(ctx context.Context, req ctrl.
logger := r.Log.WithValues(ShardingSphereChaosControllerName, req.NamespacedName)

ssChaos, err := r.getRuntimeChaos(ctx, req.NamespacedName)

if err != nil {
if apierrors.IsNotFound(err) {
return ctrl.Result{RequeueAfter: defaultRequeueTime}, nil
Expand All @@ -89,8 +93,8 @@ func (r *ShardingSphereChaosReconciler) Reconcile(ctx context.Context, req ctrl.
return ctrl.Result{Requeue: true}, err
}

if !ssChaos.ObjectMeta.DeletionTimestamp.IsZero() {
return ctrl.Result{}, nil
if err := r.finalize(ctx, ssChaos); err != nil {
return ctrl.Result{Requeue: true}, err
}

logger.Info("start reconcile chaos")
Expand Down Expand Up @@ -125,6 +129,58 @@ func (r *ShardingSphereChaosReconciler) getRuntimeChaos(ctx context.Context, nam
return rt, err
}

// nolint:nestif
func (r *ShardingSphereChaosReconciler) finalize(ctx context.Context, chao *v1alpha1.ShardingSphereChaos) error {
if chao.ObjectMeta.DeletionTimestamp.IsZero() {
if !controllerutil.ContainsFinalizer(chao, SSChaosFinalizeName) {
controllerutil.AddFinalizer(chao, SSChaosFinalizeName)
if err := r.Update(ctx, chao); err != nil {
return err
}
}
} else if controllerutil.ContainsFinalizer(chao, SSChaosFinalizeName) {
if err := r.deleteExternalResources(ctx, chao); err != nil {
return err
}

controllerutil.RemoveFinalizer(chao, SSChaosFinalizeName)
if err := r.Update(ctx, chao); err != nil {
return err
}
}

return nil
}

func (r *ShardingSphereChaosReconciler) deleteExternalResources(ctx context.Context, chao *v1alpha1.ShardingSphereChaos) error {
nameSpacedName := types.NamespacedName{Namespace: chao.Namespace, Name: chao.Name}
if chao.Spec.EmbedChaos.PodChaos != nil {
podchao, err := r.getPodChaosByNamespacedName(ctx, nameSpacedName)
if err != nil {
return err
}
if podchao != nil {
if err := r.Chaos.DeletePodChaos(ctx, podchao); err != nil {
return err
}
}
}

if chao.Spec.EmbedChaos.NetworkChaos != nil {
networkchao, err := r.getNetworkChaosByNamespacedName(ctx, nameSpacedName)
if err != nil {
return err
}
if networkchao != nil {
if err := r.Chaos.DeleteNetworkChaos(ctx, networkchao); err != nil {
return err
}
}
}

return nil
}

func (r *ShardingSphereChaosReconciler) reconcileChaos(ctx context.Context, chaos *v1alpha1.ShardingSphereChaos) error {
logger := r.Log.WithValues("reconcile shardingspherechaos", fmt.Sprintf("%s/%s", chaos.Namespace, chaos.Name))

Expand Down Expand Up @@ -189,7 +245,6 @@ func (r *ShardingSphereChaosReconciler) updatePodChaos(ctx context.Context, chao
return err
}

r.Events.Event(chaos, "Normal", "applied", fmt.Sprintf("podChaos %s", "new changes updated"))
return nil
}

Expand All @@ -210,7 +265,7 @@ func (r *ShardingSphereChaosReconciler) updateNetWorkChaos(ctx context.Context,
if err != nil {
return err
}
r.Events.Event(chaos, "Normal", "applied", fmt.Sprintf("networkChaos %s", "new changes updated"))

return nil
}

Expand Down Expand Up @@ -283,6 +338,17 @@ func (r *ShardingSphereChaosReconciler) reconcileJob(ctx context.Context, chaos
}

func (r *ShardingSphereChaosReconciler) reconcileStatus(ctx context.Context, chaos *v1alpha1.ShardingSphereChaos) error {
namespacedName := types.NamespacedName{
Name: chaos.Name,
Namespace: chaos.Namespace,
}
chaos, err := r.getRuntimeChaos(ctx, namespacedName)
if err != nil {
if apierrors.IsNotFound(err) {
return nil
}
return err
}
r.setDefaultStatus(chaos)

req := getInjectRequirement(chaos)
Expand Down Expand Up @@ -312,10 +378,7 @@ func (r *ShardingSphereChaosReconciler) reconcileStatus(ctx context.Context, cha
}

// sts := setRtStatus(chaos)
rt, err := r.getRuntimeChaos(ctx, types.NamespacedName{
Name: chaos.Name,
Namespace: chaos.Namespace,
})
rt, err := r.getRuntimeChaos(ctx, namespacedName)
if err != nil {
return err
}
Expand Down Expand Up @@ -589,7 +652,6 @@ func (r *ShardingSphereChaosReconciler) updateJob(ctx context.Context, requireme
if err := r.Delete(ctx, cur); err != nil && !apierrors.IsNotFound(err) {
return err
}
// r.Events.Event(chao, "Normal", "Updated", "job Updated")
}
return nil
}
Expand Down Expand Up @@ -665,6 +727,8 @@ func (r *ShardingSphereChaosReconciler) createJob(ctx context.Context, requireme
return err
}
}

r.Events.Event(chao, "Normal", "Created", fmt.Sprintf("%s job created", requirement))
return nil
}

Expand Down
13 changes: 1 addition & 12 deletions shardingsphere-operator/pkg/kubernetes/chaosmesh/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,6 @@ func NewPodChaos(ssChao *v1alpha1.ShardingSphereChaos) (PodChaos, error) {
pcb.SetContainerSelector(containerSelector)
podChao := pcb.Build()

// FIXME
/*
if err := ctrl.SetControllerReference(ssChao, podChao, c.r.Scheme()); err != nil {
return nil, err
}
*/
return podChao, nil
}

Expand Down Expand Up @@ -250,12 +244,7 @@ func NewNetworkChaos(ssChao *v1alpha1.ShardingSphereChaos) (NetworkChaos, error)
ncb.SetTcParameter(*tcParams)

networkChao := ncb.Build()
// FIXME
/*
if err := ctrl.SetControllerReference(ssChao, networkChao, c.r.Scheme()); err != nil {
return nil, err
}
*/

return networkChao, nil
}

Expand Down
34 changes: 34 additions & 0 deletions shardingsphere-operator/pkg/kubernetes/chaosmesh/chaosmesh.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package chaosmesh

import (
"context"
"reflect"

"github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/api/v1alpha1"
chaosmeshapi "github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
Expand Down Expand Up @@ -69,9 +70,11 @@ type Getter interface {
type Setter interface {
CreatePodChaos(context.Context, *v1alpha1.ShardingSphereChaos) error
UpdatePodChaos(context.Context, PodChaos, *v1alpha1.ShardingSphereChaos) error
DeletePodChaos(context.Context, PodChaos) error

CreateNetworkChaos(context.Context, *v1alpha1.ShardingSphereChaos) error
UpdateNetworkChaos(context.Context, NetworkChaos, *v1alpha1.ShardingSphereChaos) error
DeleteNetworkChaos(context.Context, NetworkChaos) error
}

type getter struct {
Expand Down Expand Up @@ -145,11 +148,27 @@ func (cs setter) UpdatePodChaos(ctx context.Context, podChaos PodChaos, sschaos
if !ok {
return ErrConvert
}
if reflect.DeepEqual(s.Spec, t.Spec) {
return nil
}
t.Spec = s.Spec

return cs.Client.Update(ctx, t)
}

// DeletePodChaos deletes a pod chaos
func (cs setter) DeletePodChaos(ctx context.Context, chao PodChaos) error {
podChao, ok := chao.(*chaosmeshapi.PodChaos)
if !ok {
return ErrConvert
}
if err := cs.Client.Delete(ctx, podChao); err != nil {
return err
}

return nil
}

// CreateNetworkChaos creates a new network chaos
func (cs setter) CreateNetworkChaos(ctx context.Context, sschaos *v1alpha1.ShardingSphereChaos) error {
nc, err := NewNetworkChaos(sschaos)
Expand All @@ -173,7 +192,22 @@ func (cs setter) UpdateNetworkChaos(ctx context.Context, networkChaos NetworkCha
if !ok {
return ErrConvert
}
if reflect.DeepEqual(s.Spec, t.Spec) {
return nil
}
t.Spec = s.Spec

return cs.Client.Update(ctx, t)
}

func (cs setter) DeleteNetworkChaos(ctx context.Context, chao NetworkChaos) error {
networkChaos, ok := chao.(*chaosmeshapi.NetworkChaos)
if !ok {
return ErrConvert
}
if err := cs.Client.Delete(ctx, networkChaos); err != nil {
return err
}

return nil
}