From 01316612449960dbae42e04f80130934a2501c5f Mon Sep 17 00:00:00 2001 From: mlycore Date: Thu, 8 Jun 2023 13:30:23 +0800 Subject: [PATCH 1/3] refactor: adjust NetworkChaos type Signed-off-by: mlycore --- shardingsphere-operator/api/v1alpha1/chaos_types.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/shardingsphere-operator/api/v1alpha1/chaos_types.go b/shardingsphere-operator/api/v1alpha1/chaos_types.go index 6a327398..efc2d7fa 100644 --- a/shardingsphere-operator/api/v1alpha1/chaos_types.go +++ b/shardingsphere-operator/api/v1alpha1/chaos_types.go @@ -198,7 +198,7 @@ type PodKillParams struct { // NetworkChaosSpec Fields that need to be configured for network type chaos type NetworkChaosSpec struct { - Source PodSelector `json:",inline"` + Source PodSelector `json:"source,omitempty"` Target *PodSelector `json:"target,omitempty"` // +optional @@ -254,6 +254,7 @@ const ( Duplication NetworkChaosAction = "Duplication" Corruption NetworkChaosAction = "Corruption" Partition NetworkChaosAction = "Partition" + Bandwidth NetworkChaosAction = "Bandwidth" ) // Direction specifies the direction of action of network chaos From 83459c33ac9e76575b9389b3abf7e2efa68a7d65 Mon Sep 17 00:00:00 2001 From: mlycore Date: Thu, 8 Jun 2023 13:30:46 +0800 Subject: [PATCH 2/3] refactor: refactor NetworkChaos builder Signed-off-by: mlycore --- .../pkg/controllers/chaos_controller.go | 3 - .../pkg/kubernetes/chaosmesh/builder.go | 88 +++++++++---------- 2 files changed, 40 insertions(+), 51 deletions(-) diff --git a/shardingsphere-operator/pkg/controllers/chaos_controller.go b/shardingsphere-operator/pkg/controllers/chaos_controller.go index 14ad074e..76fd1649 100644 --- a/shardingsphere-operator/pkg/controllers/chaos_controller.go +++ b/shardingsphere-operator/pkg/controllers/chaos_controller.go @@ -107,9 +107,6 @@ func (r *ChaosReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl func (r *ChaosReconciler) reconcileChaos(ctx context.Context, chaos *v1alpha1.Chaos) error { logger := r.Log.WithValues("reconcile chaos", fmt.Sprintf("%s/%s", chaos.Namespace, chaos.Name)) - if chaos.Status.Phase == "" || chaos.Status.Phase == v1alpha1.BeforeSteady || chaos.Status.Phase == v1alpha1.AfterSteady { - return nil - } namespacedName := types.NamespacedName{ Namespace: chaos.Namespace, Name: chaos.Name, diff --git a/shardingsphere-operator/pkg/kubernetes/chaosmesh/builder.go b/shardingsphere-operator/pkg/kubernetes/chaosmesh/builder.go index fec531d0..38b2569b 100644 --- a/shardingsphere-operator/pkg/kubernetes/chaosmesh/builder.go +++ b/shardingsphere-operator/pkg/kubernetes/chaosmesh/builder.go @@ -69,8 +69,9 @@ func ConvertChaosStatus(ctx context.Context, ssChaos *v1alpha1.Chaos, chaos Gene } else if ssChaos.Spec.EmbedChaos.NetworkChaos != nil { if networkChaos, ok := chaos.(*chaosmeshv1alpha1.NetworkChaos); ok && networkChaos != nil { status = *networkChaos.GetStatus() + } else { + return v1alpha1.Unknown } - return v1alpha1.Unknown } var conditions = map[chaosmeshv1alpha1.ChaosConditionType]bool{} for i := range status.Conditions { @@ -224,30 +225,44 @@ func getAnnotation(anno map[string]string, k string) string { } func NewNetworkChaos(ssChao *v1alpha1.Chaos) (NetworkChaos, error) { + chao := ssChao.Spec.NetworkChaos + ncb := NewNetworkChaosBuilder() - ncb.SetName(ssChao.Name).SetNamespace(ssChao.Namespace).SetLabels(ssChao.Labels) + ncb.SetName(ssChao.Name).SetNamespace(ssChao.Namespace).SetLabels(ssChao.Labels). + SetAction(string(chao.Action)). + SetDuration(chao.Duration). + SetDirection(string(chao.Direction)) tcParams := &chaosmeshv1alpha1.TcParameter{} - - chao := ssChao.Spec.NetworkChaos - if act, ok := ssChao.Annotations[AnnoNetworkAction]; ok { - ncb.SetAction(act) - - if chaosmeshv1alpha1.NetworkChaosAction(act) == chaosmeshv1alpha1.BandwidthAction { - bwab := NewBandWidthActionBuilder() - bwab.SetRate(getAnnotation(ssChao.Annotations, AnnoBandwidthRate)) - bwab.SetLimit(getAnnotation(ssChao.Annotations, AnnoBandwidthLimit)) - bwab.SetBuffer(getAnnotation(ssChao.Annotations, AnnoBandwidthBuffer)) - bwab.SetPeakRate(getAnnotation(ssChao.Annotations, AnnoBandwidthPeakrate)) - bwab.SetMinBurst(getAnnotation(ssChao.Annotations, AnnoBandwidthMinBurst)) - tcParams.Bandwidth = bwab.Build() + switch chao.Action { + case v1alpha1.Delay: + tcParams.Delay = &chaosmeshv1alpha1.DelaySpec{ + Latency: chao.Params.Delay.Latency, + Jitter: chao.Params.Delay.Jitter, + } + case v1alpha1.Corruption: + tcParams.Corrupt = &chaosmeshv1alpha1.CorruptSpec{ + Corrupt: chao.Params.Corruption.Corruption, + } + case v1alpha1.Duplication: + tcParams.Duplicate = &chaosmeshv1alpha1.DuplicateSpec{ + Duplicate: chao.Params.Duplication.Duplication, + } + case v1alpha1.Loss: + tcParams.Loss = &chaosmeshv1alpha1.LossSpec{ + Loss: chao.Params.Loss.Loss, } - } else { - ncb.SetAction(string(chao.Action)) + // case v1alpha1.Partition: + case v1alpha1.Bandwidth: + bwab := NewBandWidthActionBuilder() + bwab.SetRate(getAnnotation(ssChao.Annotations, AnnoBandwidthRate)) + bwab.SetLimit(getAnnotation(ssChao.Annotations, AnnoBandwidthLimit)) + bwab.SetBuffer(getAnnotation(ssChao.Annotations, AnnoBandwidthBuffer)) + bwab.SetPeakRate(getAnnotation(ssChao.Annotations, AnnoBandwidthPeakrate)) + bwab.SetMinBurst(getAnnotation(ssChao.Annotations, AnnoBandwidthMinBurst)) + tcParams.Bandwidth = bwab.Build() } - ncb.SetDuration(chao.Duration).SetDirection(string(chao.Direction)) - psb := NewPodSelectorBuilder() psb.SetNamespaces(chao.Source.Namespaces). SetExpressionSelectors(chao.Source.ExpressionSelectors). @@ -259,7 +274,6 @@ func NewNetworkChaos(ssChao *v1alpha1.Chaos) (NetworkChaos, error) { psb.SetSelectMode(ssChao.Annotations[AnnoPodSelectorMode]). SetValue(ssChao.Annotations[AnnoPodSelectorValue]) - ncb.SetPodSelector(psb.Build()) tpsb := NewPodSelectorBuilder() @@ -269,37 +283,15 @@ func NewNetworkChaos(ssChao *v1alpha1.Chaos) (NetworkChaos, error) { SetNodeSelector(chao.Target.NodeSelectors). SetAnnotationSelectors(chao.Target.AnnotationSelectors). SetLabelSelector(chao.Target.LabelSelectors). - SetPods(chao.Target.Pods) - - tpsb.SetSelectMode(ssChao.Annotations[AnnoTargetPodSelectMode]). + SetPods(chao.Target.Pods). + SetSelectMode(ssChao.Annotations[AnnoTargetPodSelectMode]). SetValue(ssChao.Annotations[AnnoTargetPodSelectValue]) - ncb.SetTarget(tpsb.Build()) - ncb.SetDevice(ssChao.Annotations[AnnoDevice]). - SetTargetDevice(ssChao.Annotations[AnnoTargetDevice]) - - switch chao.Action { - case v1alpha1.Delay: - tcParams.Delay = &chaosmeshv1alpha1.DelaySpec{ - Latency: chao.Params.Delay.Latency, - Jitter: chao.Params.Delay.Jitter, - } - case v1alpha1.Corruption: - tcParams.Corrupt = &chaosmeshv1alpha1.CorruptSpec{ - Corrupt: chao.Params.Corruption.Corruption, - } - case v1alpha1.Duplication: - tcParams.Duplicate = &chaosmeshv1alpha1.DuplicateSpec{ - Duplicate: chao.Params.Duplication.Duplication, - } - case v1alpha1.Loss: - tcParams.Loss = &chaosmeshv1alpha1.LossSpec{ - Loss: chao.Params.Loss.Loss, - } - case v1alpha1.Partition: - } + ncb.SetTarget(tpsb.Build()). + SetDevice(ssChao.Annotations[AnnoDevice]). + SetTargetDevice(ssChao.Annotations[AnnoTargetDevice]). + SetTcParameter(*tcParams) - ncb.SetTcParameter(*tcParams) networkChao := ncb.Build() return networkChao, nil From 2b4e77e24212a9fc603df87497ee028717a20c54 Mon Sep 17 00:00:00 2001 From: mlycore Date: Thu, 8 Jun 2023 13:31:46 +0800 Subject: [PATCH 3/3] fix: fix switch Signed-off-by: mlycore --- shardingsphere-operator/pkg/kubernetes/chaosmesh/builder.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shardingsphere-operator/pkg/kubernetes/chaosmesh/builder.go b/shardingsphere-operator/pkg/kubernetes/chaosmesh/builder.go index 38b2569b..1bac55b0 100644 --- a/shardingsphere-operator/pkg/kubernetes/chaosmesh/builder.go +++ b/shardingsphere-operator/pkg/kubernetes/chaosmesh/builder.go @@ -252,7 +252,6 @@ func NewNetworkChaos(ssChao *v1alpha1.Chaos) (NetworkChaos, error) { tcParams.Loss = &chaosmeshv1alpha1.LossSpec{ Loss: chao.Params.Loss.Loss, } - // case v1alpha1.Partition: case v1alpha1.Bandwidth: bwab := NewBandWidthActionBuilder() bwab.SetRate(getAnnotation(ssChao.Annotations, AnnoBandwidthRate)) @@ -261,6 +260,7 @@ func NewNetworkChaos(ssChao *v1alpha1.Chaos) (NetworkChaos, error) { bwab.SetPeakRate(getAnnotation(ssChao.Annotations, AnnoBandwidthPeakrate)) bwab.SetMinBurst(getAnnotation(ssChao.Annotations, AnnoBandwidthMinBurst)) tcParams.Bandwidth = bwab.Build() + case v1alpha1.Partition: } psb := NewPodSelectorBuilder()