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): refactor network chaos in Chaos reconciliation #404

Merged
merged 3 commits into from
Jun 8, 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
3 changes: 2 additions & 1 deletion shardingsphere-operator/api/v1alpha1/chaos_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions shardingsphere-operator/pkg/controllers/chaos_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
88 changes: 40 additions & 48 deletions shardingsphere-operator/pkg/kubernetes/chaosmesh/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.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()
case v1alpha1.Partition:
}

ncb.SetDuration(chao.Duration).SetDirection(string(chao.Direction))

psb := NewPodSelectorBuilder()
psb.SetNamespaces(chao.Source.Namespaces).
SetExpressionSelectors(chao.Source.ExpressionSelectors).
Expand All @@ -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()
Expand All @@ -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
Expand Down