You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by su...@apache.org on 2023/04/28 05:20:03 UTC
[shardingsphere-on-cloud] branch main updated: refactor(operator): merge builder and setter and refactor chaos mgmt (#332)
This is an automated email from the ASF dual-hosted git repository.
sunnianjun pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/shardingsphere-on-cloud.git
The following commit(s) were added to refs/heads/main by this push:
new a491745 refactor(operator): merge builder and setter and refactor chaos mgmt (#332)
a491745 is described below
commit a491745216e6e63fce68f172abca7884dfdf8824
Author: liyao <ma...@126.com>
AuthorDate: Fri Apr 28 13:19:56 2023 +0800
refactor(operator): merge builder and setter and refactor chaos mgmt (#332)
* chore(ci): merge codecov
Signed-off-by: mlycore <ma...@126.com>
* refactor: update duration type to string pointer
Signed-off-by: mlycore <ma...@126.com>
* refactor: update duration type to string pointer
Signed-off-by: mlycore <ma...@126.com>
* refactor: rename functions
Signed-off-by: mlycore <ma...@126.com>
* refactor: merge builder and setter
Signed-off-by: mlycore <ma...@126.com>
* refactor: remove ErrChangedSpec
Signed-off-by: mlycore <ma...@126.com>
* fix: fix typo
Signed-off-by: mlycore <ma...@126.com>
* refactor: refactor podchaos and networkchaos creation and update
Signed-off-by: mlycore <ma...@126.com>
* fix: fix shardingspherechaos test
Signed-off-by: mlycore <ma...@126.com>
* fix: add type assertion check
Signed-off-by: mlycore <ma...@126.com>
---------
Signed-off-by: mlycore <ma...@126.com>
---
.github/workflows/operator-unit-test.yml | 16 +
.../api/v1alpha1/shardingsphere_chaos_types.go | 5 +-
.../api/v1alpha1/zz_generated.deepcopy.go | 12 +-
.../cmd/shardingsphere-operator/manager/option.go | 4 +-
.../controllers/shardingsphere_chaos_controller.go | 411 +++++++++++----------
.../pkg/kubernetes/chaos/chaos.go | 55 ---
.../pkg/kubernetes/chaos/chaos_mesh.go | 60 ---
.../chaosmesh/builder.go} | 136 ++-----
.../pkg/kubernetes/chaosmesh/chaosmesh.go | 179 +++++++++
.../pkg/reconcile/shardingspherechaos/chaos.go | 45 ---
.../pkg/reconcile/shardingspherechaos/job.go | 5 +-
..._test.go => shardingsphere_chaos_suite_test.go} | 0
...echaos_test.go => shardingsphere_chaos_test.go} | 8 +-
13 files changed, 452 insertions(+), 484 deletions(-)
diff --git a/.github/workflows/operator-unit-test.yml b/.github/workflows/operator-unit-test.yml
index f9baa0c..564706e 100644
--- a/.github/workflows/operator-unit-test.yml
+++ b/.github/workflows/operator-unit-test.yml
@@ -41,3 +41,19 @@ jobs:
uses: codecov/codecov-action@v3
with:
files: ./shardingsphere-operator/cover.out
+ - name: "unit test"
+ run: |
+ cd pitr/agent
+ make test
+ - name: "Code coverage"
+ uses: codecov/codecov-action@v3
+ with:
+ files: ./pitr/agent/cover.out
+ - name: "unit test"
+ run: |
+ cd pitr/cli
+ make test
+ - name: "Code coverage"
+ uses: codecov/codecov-action@v3
+ with:
+ files: ./pitr/cli/cover.out
diff --git a/shardingsphere-operator/api/v1alpha1/shardingsphere_chaos_types.go b/shardingsphere-operator/api/v1alpha1/shardingsphere_chaos_types.go
index d2d71af..1f95f6c 100644
--- a/shardingsphere-operator/api/v1alpha1/shardingsphere_chaos_types.go
+++ b/shardingsphere-operator/api/v1alpha1/shardingsphere_chaos_types.go
@@ -137,7 +137,7 @@ type PodChaosParams struct {
type PodFailureParams struct {
// +optional
- Duration string `json:"duration,omitempty"`
+ Duration *string `json:"duration,omitempty"`
}
type ContainerKillParams struct {
@@ -149,11 +149,12 @@ type ContainerKillParams struct {
type NetworkChaosSpec struct {
Source PodSelector `json:",inline"`
Target *PodSelector `json:"target,omitempty"`
+
// +optional
Action NetworkChaosAction `json:"action"`
// +optional
- Duration string `json:"duration,omitempty"`
+ Duration *string `json:"duration,omitempty"`
// +optional
Direction Direction `json:"direction,omitempty"`
// +optional
diff --git a/shardingsphere-operator/api/v1alpha1/zz_generated.deepcopy.go b/shardingsphere-operator/api/v1alpha1/zz_generated.deepcopy.go
index 16c7070..ec6047e 100644
--- a/shardingsphere-operator/api/v1alpha1/zz_generated.deepcopy.go
+++ b/shardingsphere-operator/api/v1alpha1/zz_generated.deepcopy.go
@@ -695,6 +695,11 @@ func (in *NetworkChaosSpec) DeepCopyInto(out *NetworkChaosSpec) {
*out = new(PodSelector)
(*in).DeepCopyInto(*out)
}
+ if in.Duration != nil {
+ in, out := &in.Duration, &out.Duration
+ *out = new(string)
+ **out = **in
+ }
in.Params.DeepCopyInto(&out.Params)
}
@@ -807,7 +812,7 @@ func (in *PodChaosParams) DeepCopyInto(out *PodChaosParams) {
if in.PodFailure != nil {
in, out := &in.PodFailure, &out.PodFailure
*out = new(PodFailureParams)
- **out = **in
+ (*in).DeepCopyInto(*out)
}
if in.ContainerKill != nil {
in, out := &in.ContainerKill, &out.ContainerKill
@@ -846,6 +851,11 @@ func (in *PodChaosSpec) DeepCopy() *PodChaosSpec {
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *PodFailureParams) DeepCopyInto(out *PodFailureParams) {
*out = *in
+ if in.Duration != nil {
+ in, out := &in.Duration, &out.Duration
+ *out = new(string)
+ **out = **in
+ }
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new PodFailureParams.
diff --git a/shardingsphere-operator/cmd/shardingsphere-operator/manager/option.go b/shardingsphere-operator/cmd/shardingsphere-operator/manager/option.go
index 59ac03e..f21d44d 100644
--- a/shardingsphere-operator/cmd/shardingsphere-operator/manager/option.go
+++ b/shardingsphere-operator/cmd/shardingsphere-operator/manager/option.go
@@ -25,11 +25,11 @@ import (
"github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/pkg/kubernetes/job"
- "github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/pkg/kubernetes/chaos"
batchV1 "k8s.io/api/batch/v1"
"github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/api/v1alpha1"
"github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/pkg/controllers"
+ sschaos "github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/pkg/kubernetes/chaosmesh"
"github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/pkg/kubernetes/configmap"
"github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/pkg/kubernetes/deployment"
"github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/pkg/kubernetes/service"
@@ -149,7 +149,7 @@ var featureGatesHandlers = map[string]FeatureGateHandler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Log: mgr.GetLogger(),
- Chaos: chaos.NewChaos(mgr.GetClient()),
+ Chaos: sschaos.NewChaos(mgr.GetClient()),
Job: job.NewJob(mgr.GetClient()),
ConfigMap: configmap.NewConfigMapClient(mgr.GetClient()),
Events: mgr.GetEventRecorderFor("shardingsphere-chaos-controller"),
diff --git a/shardingsphere-operator/pkg/controllers/shardingsphere_chaos_controller.go b/shardingsphere-operator/pkg/controllers/shardingsphere_chaos_controller.go
index 7a8096c..33b8486 100644
--- a/shardingsphere-operator/pkg/controllers/shardingsphere_chaos_controller.go
+++ b/shardingsphere-operator/pkg/controllers/shardingsphere_chaos_controller.go
@@ -24,13 +24,12 @@ import (
"strings"
"time"
- sschaosv1alpha1 "github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/api/v1alpha1"
- "github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/pkg/kubernetes/chaos"
+ "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"
"github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/pkg/kubernetes/job"
reconcile "github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/pkg/reconcile/shardingspherechaos"
- chaosv1alpha1 "github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
"github.com/go-logr/logr"
batchV1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
@@ -49,8 +48,6 @@ import (
const (
ShardingSphereChaosControllerName = "shardingsphere-chaos-controller"
VerifyJobCheck = "Verify"
-
- ssChaosDefaultEnqueueTime = 10 * time.Second
)
type JobCondition string
@@ -73,7 +70,7 @@ type ShardingSphereChaosReconciler struct { //
ClientSet *clientset.Clientset
Events record.EventRecorder
- Chaos chaos.Chaos
+ Chaos sschaos.Chaos
Job job.Job
ConfigMap configmap.ConfigMap
}
@@ -82,9 +79,9 @@ type ShardingSphereChaosReconciler struct { //
func (r *ShardingSphereChaosReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
logger := r.Log.WithValues(ShardingSphereChaosControllerName, req.NamespacedName)
- ssChaos, err := r.getRuntimeSSChaos(ctx, req.NamespacedName)
+ ssChaos, err := r.getRuntimeChaos(ctx, req.NamespacedName)
if err != nil {
- return ctrl.Result{}, err
+ return ctrl.Result{RequeueAfter: defaultRequeueTime}, err
}
if !ssChaos.ObjectMeta.DeletionTimestamp.IsZero() {
@@ -93,210 +90,276 @@ func (r *ShardingSphereChaosReconciler) Reconcile(ctx context.Context, req ctrl.
logger.Info("start reconcile chaos")
+ //FIXME
+ // fix result requeue
if err := r.reconcileChaos(ctx, ssChaos); err != nil {
- if err == reconcile.ErrChangedSpec {
- errHandle := r.handleChaosChange(ctx, req.NamespacedName)
- return ctrl.Result{}, errHandle
- }
-
- logger.Error(err, " unable to reconcile chaos")
- r.Events.Event(ssChaos, "Warning", "chaos err", err.Error())
- return ctrl.Result{}, err
+ logger.Error(err, "reconcile shardingspherechaos error")
+ r.Events.Event(ssChaos, "Warning", "shardingspherechaos error", err.Error())
+ return ctrl.Result{RequeueAfter: defaultRequeueTime}, err
}
if err := r.reconcileConfigMap(ctx, ssChaos); err != nil {
- logger.Error(err, "unable to reconcile configmap")
+ logger.Error(err, "reconcile configmap error")
r.Events.Event(ssChaos, "Warning", "configmap err", err.Error())
-
- return ctrl.Result{}, err
+ return ctrl.Result{RequeueAfter: defaultRequeueTime}, err
}
if err := r.reconcileJob(ctx, ssChaos); err != nil {
- logger.Error(err, "unable to reconcile job")
+ logger.Error(err, "reconcile job error")
r.Events.Event(ssChaos, "Warning", "job err", err.Error())
-
- return ctrl.Result{}, err
+ return ctrl.Result{RequeueAfter: defaultRequeueTime}, err
}
- if err := r.reconcileStatus(ctx, req.NamespacedName); err != nil {
+ if err := r.reconcileStatus(ctx, ssChaos); err != nil {
r.Events.Event(ssChaos, "Warning", "update status error", err.Error())
logger.Error(err, "failed to update status")
}
- return ctrl.Result{RequeueAfter: ssChaosDefaultEnqueueTime}, nil
+ return ctrl.Result{RequeueAfter: defaultRequeueTime}, nil
}
-func (r *ShardingSphereChaosReconciler) handleChaosChange(ctx context.Context, name types.NamespacedName) error {
- ssChaos, err := r.getRuntimeSSChaos(ctx, name)
- if err != nil {
- return err
- }
-
- if ssChaos.Status.Phase != sschaosv1alpha1.BeforeExperiment {
- ssChaos.Status.Phase = sschaosv1alpha1.AfterExperiment
- if err := r.Status().Update(ctx, ssChaos); err != nil {
- return err
- }
- }
- return nil
-}
-
-func (r *ShardingSphereChaosReconciler) getRuntimeSSChaos(ctx context.Context, name types.NamespacedName) (*sschaosv1alpha1.ShardingSphereChaos, error) {
- var rt = &sschaosv1alpha1.ShardingSphereChaos{}
+func (r *ShardingSphereChaosReconciler) getRuntimeChaos(ctx context.Context, name types.NamespacedName) (*v1alpha1.ShardingSphereChaos, error) {
+ var rt = &v1alpha1.ShardingSphereChaos{}
err := r.Get(ctx, name, rt)
return rt, client.IgnoreNotFound(err)
}
-func (r *ShardingSphereChaosReconciler) reconcileChaos(ctx context.Context, ssChao *sschaosv1alpha1.ShardingSphereChaos) error {
- logger := r.Log.WithValues("reconcile chaos", ssChao.Name)
+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))
- if ssChao.Status.Phase == sschaosv1alpha1.BeforeExperiment || ssChao.Status.Phase == "" {
+ if len(chaos.Status.Phase) == 0 || chaos.Status.Phase == v1alpha1.BeforeExperiment {
return nil
}
- namespaceName := types.NamespacedName{
- Namespace: ssChao.Namespace,
- Name: ssChao.Name,
+ namespacedName := types.NamespacedName{
+ Namespace: chaos.Namespace,
+ Name: chaos.Name,
}
- if ssChao.Spec.EmbedChaos.PodChaos != nil {
- chao, err := r.getPodChaosByNamespacedName(ctx, namespaceName)
- if err != nil {
- logger.Error(err, "pod chaos err")
+ if chaos.Spec.EmbedChaos.PodChaos != nil {
+ if err := r.reconcilePodChaos(ctx, chaos, namespacedName); err != nil {
+ logger.Error(err, "reconcile pod chaos error")
return err
}
- if chao != nil {
- return r.updatePodChaos(ctx, ssChao, chao)
- }
+ }
- return r.CreatePodChaos(ctx, ssChao)
- } else if ssChao.Spec.EmbedChaos.NetworkChaos != nil {
- chao, err := r.getNetworkChaosByNamespacedName(ctx, namespaceName)
- if err != nil {
- logger.Error(err, "network chao err")
+ if chaos.Spec.EmbedChaos.NetworkChaos != nil {
+ if err := r.reconcileNetworkChaos(ctx, chaos, namespacedName); err != nil {
+ logger.Error(err, "reconcile network chaos error")
return err
}
- if chao != nil {
- return r.updateNetWorkChaos(ctx, ssChao, chao)
+ }
+
+ // NOTE
+ // The phase will be updated after the chaos is updated successfully
+ /*
+ if chaos.Status.Phase != v1alpha1.BeforeExperiment {
+ chaos.Status.Phase = v1alpha1.AfterExperiment
+ if err := r.Status().Update(ctx, chaos); err != nil {
+ return err
+ }
}
+ */
- return r.CreateNetworkChaos(ctx, ssChao)
+ return nil
+}
+
+func (r *ShardingSphereChaosReconciler) reconcilePodChaos(ctx context.Context, chaos *v1alpha1.ShardingSphereChaos, namespacedName types.NamespacedName) error {
+ pc, err := r.getPodChaosByNamespacedName(ctx, namespacedName)
+ if err != nil {
+ return err
+ }
+ if pc != nil {
+ return r.updatePodChaos(ctx, chaos, pc)
+ }
+
+ return r.createPodChaos(ctx, chaos)
+}
+
+func (r *ShardingSphereChaosReconciler) getPodChaosByNamespacedName(ctx context.Context, namespacedName types.NamespacedName) (sschaos.PodChaos, error) {
+ pc, err := r.Chaos.GetPodChaosByNamespacedName(ctx, namespacedName)
+ if err != nil {
+ return nil, err
+ }
+ return pc, nil
+}
+
+func (r *ShardingSphereChaosReconciler) createPodChaos(ctx context.Context, chaos *v1alpha1.ShardingSphereChaos) error {
+ // podChaos := r.Chaos.NewPodChaos(ctx, chaos)
+ err := r.Chaos.CreatePodChaos(ctx, chaos)
+ if err != nil {
+ return err
+ }
+ r.Events.Event(chaos, "Normal", "Created", fmt.Sprintf("PodChaos %s", " is created successfully"))
+ return nil
+}
+
+func (r *ShardingSphereChaosReconciler) updatePodChaos(ctx context.Context, chaos *v1alpha1.ShardingSphereChaos, podChaos sschaos.PodChaos) error {
+ err := r.Chaos.UpdatePodChaos(ctx, podChaos, chaos)
+ if err != nil {
+ return err
}
+ r.Events.Event(chaos, "Normal", "applied", fmt.Sprintf("podChaos %s", "new changes updated"))
return nil
}
-func (r *ShardingSphereChaosReconciler) reconcileConfigMap(ctx context.Context, ssChaos *sschaosv1alpha1.ShardingSphereChaos) error {
- logger := r.Log.WithValues("reconcile configmap", ssChaos.Name)
- namespaceName := types.NamespacedName{Namespace: ssChaos.Namespace, Name: ssChaos.Name}
+func (r *ShardingSphereChaosReconciler) reconcileNetworkChaos(ctx context.Context, chaos *v1alpha1.ShardingSphereChaos, namespacedName types.NamespacedName) error {
+ nc, err := r.getNetworkChaosByNamespacedName(ctx, namespacedName)
+ if err != nil {
+ return err
+ }
+ if nc != nil {
+ return r.updateNetWorkChaos(ctx, chaos, nc)
+ }
+
+ return r.createNetworkChaos(ctx, chaos)
+}
+
+func (r *ShardingSphereChaosReconciler) reconcileConfigMap(ctx context.Context, chaos *v1alpha1.ShardingSphereChaos) error {
+ logger := r.Log.WithValues("reconcile configmap", chaos.Name)
+ namespaceName := types.NamespacedName{
+ Namespace: chaos.Namespace,
+ Name: chaos.Name,
+ }
- rConfigmap, err := r.getConfigMapByNamespacedName(ctx, namespaceName)
+ cm, err := r.getConfigMapByNamespacedName(ctx, namespaceName)
if err != nil {
logger.Error(err, "get configmap error")
return err
}
- if rConfigmap != nil {
- return r.updateConfigMap(ctx, ssChaos, rConfigmap)
+ if cm != nil {
+ return r.updateConfigMap(ctx, chaos, cm)
}
- err = r.CreateConfigMap(ctx, ssChaos)
+ err = r.createConfigMap(ctx, chaos)
if err != nil {
- r.Events.Event(ssChaos, "Warning", "Created", fmt.Sprintf("configmap created fail %s", err))
+ r.Events.Event(chaos, "Warning", "Created", fmt.Sprintf("configmap created fail %s", err))
return err
}
- r.Events.Event(ssChaos, "Normal", "Created", "configmap created successfully")
+ r.Events.Event(chaos, "Normal", "Created", "configmap created successfully")
return nil
}
-func (r *ShardingSphereChaosReconciler) reconcileJob(ctx context.Context, ssChaos *sschaosv1alpha1.ShardingSphereChaos) error {
- logger := r.Log.WithValues("reconcile job", ssChaos.Name)
+func (r *ShardingSphereChaosReconciler) reconcileJob(ctx context.Context, chaos *v1alpha1.ShardingSphereChaos) error {
+ logger := r.Log.WithValues("reconcile job", chaos.Name)
var nowInjectRequirement reconcile.InjectRequirement
- switch {
- case ssChaos.Status.Phase == "" || ssChaos.Status.Phase == sschaosv1alpha1.BeforeExperiment || ssChaos.Status.Phase == sschaosv1alpha1.AfterExperiment:
- nowInjectRequirement = reconcile.Experimental
- case ssChaos.Status.Phase == sschaosv1alpha1.InjectedChaos:
+ switch chaos.Status.Phase {
+ case v1alpha1.InjectedChaos:
nowInjectRequirement = reconcile.Pressure
- case ssChaos.Status.Phase == sschaosv1alpha1.RecoveredChaos:
+ case v1alpha1.RecoveredChaos:
nowInjectRequirement = reconcile.Verify
+ case v1alpha1.BeforeExperiment, v1alpha1.AfterExperiment:
+ fallthrough
+ default:
+ nowInjectRequirement = reconcile.Experimental
}
- namespaceName := types.NamespacedName{Namespace: ssChaos.Namespace, Name: reconcile.SetJobNamespaceName(ssChaos.Name, nowInjectRequirement)}
+ namespaceName := types.NamespacedName{Namespace: chaos.Namespace, Name: reconcile.MakeJobName(chaos.Name, nowInjectRequirement)}
- rJob, err := r.getJobByNamespacedName(ctx, namespaceName)
+ job, err := r.getJobByNamespacedName(ctx, namespaceName)
if err != nil {
logger.Error(err, "get job err")
return err
}
- if rJob != nil {
- return r.updateJob(ctx, nowInjectRequirement, ssChaos, rJob)
+ if job != nil {
+ return r.updateJob(ctx, nowInjectRequirement, chaos, job)
}
- err = r.createJob(ctx, nowInjectRequirement, ssChaos)
+ err = r.createJob(ctx, nowInjectRequirement, chaos)
if err != nil {
return err
}
- r.Events.Event(ssChaos, "Normal", "Created", fmt.Sprintf("%s job created successfully", string(nowInjectRequirement)))
+ r.Events.Event(chaos, "Normal", "Created", fmt.Sprintf("%s job created successfully", string(nowInjectRequirement)))
return nil
}
-func (r *ShardingSphereChaosReconciler) reconcileStatus(ctx context.Context, namespacedName types.NamespacedName) error {
- ssChaos, err := r.getRuntimeSSChaos(ctx, namespacedName)
- if err != nil {
- return err
- }
-
- setDefault(ssChaos)
+func (r *ShardingSphereChaosReconciler) reconcileStatus(ctx context.Context, chaos *v1alpha1.ShardingSphereChaos) error {
+ r.setDefaultStatus(chaos)
- jobName := getRequirement(ssChaos)
- rJob, err := r.getJobByNamespacedName(ctx, types.NamespacedName{Namespace: ssChaos.Namespace, Name: reconcile.SetJobNamespaceName(ssChaos.Name, jobName)})
- if err != nil || rJob == nil {
+ req := getInjectRequirement(chaos)
+ job, err := r.getJobByNamespacedName(ctx, types.NamespacedName{Namespace: chaos.Namespace, Name: reconcile.MakeJobName(chaos.Name, req)})
+ if err != nil || job == nil {
return err
}
- if ssChaos.Status.Phase == sschaosv1alpha1.BeforeExperiment && rJob.Status.Succeeded == 1 {
- ssChaos.Status.Phase = sschaosv1alpha1.AfterExperiment
+ if chaos.Status.Phase == v1alpha1.BeforeExperiment && job.Status.Succeeded == 1 {
+ chaos.Status.Phase = v1alpha1.AfterExperiment
}
- jobConditions := rJob.Status.Conditions
- condition := getJobCondition(jobConditions)
+ condition := getJobCondition(job.Status.Conditions)
if condition == FailureJob {
- r.Events.Event(ssChaos, "Warning", "failed", fmt.Sprintf("job: %s", rJob.Name))
+ r.Events.Event(chaos, "Warning", "failed", fmt.Sprintf("job: %s", job.Name))
}
- if ssChaos.Status.Phase == sschaosv1alpha1.RecoveredChaos {
- if err := r.updateRecoveredJob(ctx, ssChaos, rJob); err != nil {
- r.Events.Event(ssChaos, "Warning", "getPodLog", err.Error())
+
+ if chaos.Status.Phase == v1alpha1.RecoveredChaos {
+ if err := r.updateRecoveredJob(ctx, chaos, job); err != nil {
+ r.Events.Event(chaos, "Warning", "getPodLog", err.Error())
return err
}
}
- if err := r.updatePhaseStart(ctx, ssChaos); err != nil {
+ if err := r.updatePhaseStart(ctx, chaos); err != nil {
return err
}
- rt, err := r.getRuntimeSSChaos(ctx, namespacedName)
+ // sts := setRtStatus(chaos)
+ rt, err := r.getRuntimeChaos(ctx, types.NamespacedName{
+ Name: chaos.Name,
+ Namespace: chaos.Namespace,
+ })
if err != nil {
return err
}
- setRtStatus(rt, ssChaos)
+ rt.Status = chaos.Status
+
return r.Status().Update(ctx, rt)
}
-func getRequirement(ssChaos *sschaosv1alpha1.ShardingSphereChaos) reconcile.InjectRequirement {
+// NOTE
+/*
+func (r *ShardingSphereChaosReconciler) handleChaosChange(ctx context.Context, name types.NamespacedName) error {
+ ssChaos, err := r.getRuntimeChaos(ctx, name)
+ if err != nil {
+ return err
+ }
+
+ if ssChaos.Status.Phase != v1alpha1.BeforeExperiment {
+ ssChaos.Status.Phase = v1alpha1.AfterExperiment
+ if err := r.Status().Update(ctx, ssChaos); err != nil {
+ return err
+ }
+ }
+ return nil
+}
+*/
+
+// getInjectRequirement to get the coming job requirement
+// * BeforeExperiment: it hasn't been started, could start a new experiment
+// * AfterExperiment: it has been finished, could start a new experiment
+// * InjectChaos: it has been started, could start some pressure
+// * recoveredChaos: it has been recovered, could start to verify
+
+func getInjectRequirement(ssChaos *v1alpha1.ShardingSphereChaos) reconcile.InjectRequirement {
var jobName reconcile.InjectRequirement
- if ssChaos.Status.Phase == sschaosv1alpha1.BeforeExperiment || ssChaos.Status.Phase == sschaosv1alpha1.AfterExperiment {
+
+ if ssChaos.Status.Phase == v1alpha1.BeforeExperiment || ssChaos.Status.Phase == v1alpha1.AfterExperiment {
jobName = reconcile.Experimental
}
- if ssChaos.Status.Phase == sschaosv1alpha1.InjectedChaos {
+
+ if ssChaos.Status.Phase == v1alpha1.InjectedChaos {
jobName = reconcile.Pressure
}
- if ssChaos.Status.Phase == sschaosv1alpha1.RecoveredChaos {
+
+ if ssChaos.Status.Phase == v1alpha1.RecoveredChaos {
jobName = reconcile.Verify
}
+
return jobName
}
@@ -314,37 +377,21 @@ func getJobCondition(conditions []batchV1.JobCondition) JobCondition {
case p.Type == batchV1.JobFailureTarget:
ret = FailureJob
}
+
}
return ret
}
-func setDefault(ssChaos *sschaosv1alpha1.ShardingSphereChaos) {
- if ssChaos.Status.Phase == "" {
- ssChaos.Status.Phase = sschaosv1alpha1.BeforeExperiment
+func (r *ShardingSphereChaosReconciler) setDefaultStatus(chaos *v1alpha1.ShardingSphereChaos) {
+ if chaos.Status.Phase == "" {
+ chaos.Status.Phase = v1alpha1.BeforeExperiment
}
- if ssChaos.Status.Results == nil {
- ssChaos.Status.Results = []sschaosv1alpha1.Result{}
+ if chaos.Status.Results == nil {
+ chaos.Status.Results = []v1alpha1.Result{}
}
}
-func setRtStatus(rt *sschaosv1alpha1.ShardingSphereChaos, ssChaos *sschaosv1alpha1.ShardingSphereChaos) {
- rt.Status.Results = []sschaosv1alpha1.Result{}
- for i := range ssChaos.Status.Results {
- r := &ssChaos.Status.Results[i]
- rt.Status.Results = append(rt.Status.Results, sschaosv1alpha1.Result{
- Success: r.Success,
- Detail: sschaosv1alpha1.Detail{
- Time: metav1.Time{Time: time.Now()},
- Message: r.Detail.Message,
- },
- })
- }
-
- rt.Status.Phase = ssChaos.Status.Phase
- rt.Status.ChaosCondition = ssChaos.Status.ChaosCondition
-}
-
-func (r *ShardingSphereChaosReconciler) updateRecoveredJob(ctx context.Context, ssChaos *sschaosv1alpha1.ShardingSphereChaos, rJob *batchV1.Job) error {
+func (r *ShardingSphereChaosReconciler) updateRecoveredJob(ctx context.Context, ssChaos *v1alpha1.ShardingSphereChaos, rJob *batchV1.Job) error {
if isResultExist(rJob) {
return nil
}
@@ -366,7 +413,7 @@ func (r *ShardingSphereChaosReconciler) updateRecoveredJob(ctx context.Context,
Name: pod.Name,
}
condition := getJobCondition(rJob.Status.Conditions)
- result := &sschaosv1alpha1.Result{}
+ result := &v1alpha1.Result{}
if condition == CompleteJob {
log, err := r.getPodLog(ctx, podNamespacedName, logOpts)
@@ -375,13 +422,13 @@ func (r *ShardingSphereChaosReconciler) updateRecoveredJob(ctx context.Context,
}
if ssChaos.Spec.Expect.Verify == "" || ssChaos.Spec.Expect.Verify == log {
result.Success = true
- result.Detail = sschaosv1alpha1.Detail{
+ result.Detail = v1alpha1.Detail{
Time: metav1.Time{Time: time.Now()},
Message: fmt.Sprintf("%s: job succeeded", VerifyJobCheck),
}
} else {
result.Success = false
- result.Detail = sschaosv1alpha1.Detail{
+ result.Detail = v1alpha1.Detail{
Time: metav1.Time{Time: time.Now()},
Message: fmt.Sprintf("%s: %s", VerifyJobCheck, log),
}
@@ -394,7 +441,7 @@ func (r *ShardingSphereChaosReconciler) updateRecoveredJob(ctx context.Context,
return err
}
result.Success = false
- result.Detail = sschaosv1alpha1.Detail{
+ result.Detail = v1alpha1.Detail{
Time: metav1.Time{Time: time.Now()},
Message: fmt.Sprintf("%s: %s", VerifyJobCheck, log),
}
@@ -430,7 +477,7 @@ func isResultExist(rJob *batchV1.Job) bool {
return false
}
-func updateResult(results []sschaosv1alpha1.Result, r sschaosv1alpha1.Result, check string) []sschaosv1alpha1.Result {
+func updateResult(results []v1alpha1.Result, r v1alpha1.Result, check string) []v1alpha1.Result {
for i := range results {
msg := results[i].Detail.Message
if strings.HasPrefix(msg, check) && strings.HasPrefix(r.Detail.Message, check) {
@@ -456,25 +503,25 @@ func (r *ShardingSphereChaosReconciler) getPodLog(ctx context.Context, namespace
return string(ret), nil
}
-func (r *ShardingSphereChaosReconciler) updatePhaseStart(ctx context.Context, ssChaos *sschaosv1alpha1.ShardingSphereChaos) error {
- if ssChaos.Status.Phase != sschaosv1alpha1.BeforeExperiment {
+func (r *ShardingSphereChaosReconciler) updatePhaseStart(ctx context.Context, ssChaos *v1alpha1.ShardingSphereChaos) error {
+ if ssChaos.Status.Phase != v1alpha1.BeforeExperiment {
if err := r.updateChaosCondition(ctx, ssChaos); err != nil {
return err
}
- if ssChaos.Status.ChaosCondition == sschaosv1alpha1.AllInjected && ssChaos.Status.Phase == sschaosv1alpha1.AfterExperiment {
- ssChaos.Status.Phase = sschaosv1alpha1.InjectedChaos
+ if ssChaos.Status.ChaosCondition == v1alpha1.AllInjected && ssChaos.Status.Phase == v1alpha1.AfterExperiment {
+ ssChaos.Status.Phase = v1alpha1.InjectedChaos
}
- if ssChaos.Status.ChaosCondition == sschaosv1alpha1.AllRecovered && ssChaos.Status.Phase == sschaosv1alpha1.InjectedChaos {
- ssChaos.Status.Phase = sschaosv1alpha1.RecoveredChaos
+ if ssChaos.Status.ChaosCondition == v1alpha1.AllRecovered && ssChaos.Status.Phase == v1alpha1.InjectedChaos {
+ ssChaos.Status.Phase = v1alpha1.RecoveredChaos
}
}
return nil
}
-func (r *ShardingSphereChaosReconciler) updateChaosCondition(ctx context.Context, ssChaos *sschaosv1alpha1.ShardingSphereChaos) error {
+func (r *ShardingSphereChaosReconciler) updateChaosCondition(ctx context.Context, ssChaos *v1alpha1.ShardingSphereChaos) error {
namespacedName := types.NamespacedName{
Namespace: ssChaos.Namespace,
Name: ssChaos.Name,
@@ -484,19 +531,19 @@ func (r *ShardingSphereChaosReconciler) updateChaosCondition(ctx context.Context
if err != nil {
return err
}
- ssChaos.Status.ChaosCondition = r.Chaos.ConvertChaosStatus(ctx, ssChaos, chao)
+ ssChaos.Status.ChaosCondition = sschaos.ConvertChaosStatus(ctx, ssChaos, chao)
} else if ssChaos.Spec.EmbedChaos.NetworkChaos != nil {
chao, err := r.Chaos.GetNetworkChaosByNamespacedName(ctx, namespacedName)
if err != nil {
return err
}
- ssChaos.Status.ChaosCondition = r.Chaos.ConvertChaosStatus(ctx, ssChaos, chao)
+ ssChaos.Status.ChaosCondition = sschaos.ConvertChaosStatus(ctx, ssChaos, chao)
}
return nil
}
-func (r *ShardingSphereChaosReconciler) getNetworkChaosByNamespacedName(ctx context.Context, namespacedName types.NamespacedName) (reconcile.NetworkChaos, error) {
+func (r *ShardingSphereChaosReconciler) getNetworkChaosByNamespacedName(ctx context.Context, namespacedName types.NamespacedName) (sschaos.NetworkChaos, error) {
nc, err := r.Chaos.GetNetworkChaosByNamespacedName(ctx, namespacedName)
if err != nil {
return nil, err
@@ -504,14 +551,6 @@ func (r *ShardingSphereChaosReconciler) getNetworkChaosByNamespacedName(ctx cont
return nc, nil
}
-func (r *ShardingSphereChaosReconciler) getPodChaosByNamespacedName(ctx context.Context, namespacedName types.NamespacedName) (reconcile.PodChaos, error) {
- pc, err := r.Chaos.GetPodChaosByNamespacedName(ctx, namespacedName)
- if err != nil {
- return nil, err
- }
- return pc, nil
-}
-
func (r *ShardingSphereChaosReconciler) getConfigMapByNamespacedName(ctx context.Context, namespacedName types.NamespacedName) (*corev1.ConfigMap, error) {
config, err := r.ConfigMap.GetByNamespacedName(ctx, namespacedName)
if err != nil {
@@ -522,14 +561,14 @@ func (r *ShardingSphereChaosReconciler) getConfigMapByNamespacedName(ctx context
}
func (r *ShardingSphereChaosReconciler) getJobByNamespacedName(ctx context.Context, namespacedName types.NamespacedName) (*batchV1.Job, error) {
- injectJob, err := r.Job.GetByNamespacedName(ctx, namespacedName)
+ job, err := r.Job.GetByNamespacedName(ctx, namespacedName)
if err != nil {
return nil, err
}
- return injectJob, nil
+ return job, nil
}
-func (r *ShardingSphereChaosReconciler) updateConfigMap(ctx context.Context, chao *sschaosv1alpha1.ShardingSphereChaos, cur *corev1.ConfigMap) error {
+func (r *ShardingSphereChaosReconciler) updateConfigMap(ctx context.Context, chao *v1alpha1.ShardingSphereChaos, cur *corev1.ConfigMap) error {
exp := reconcile.UpdateConfigMap(chao, cur)
if exp == nil {
return nil
@@ -537,7 +576,7 @@ func (r *ShardingSphereChaosReconciler) updateConfigMap(ctx context.Context, cha
return r.Update(ctx, exp)
}
-func (r *ShardingSphereChaosReconciler) CreateConfigMap(ctx context.Context, chao *sschaosv1alpha1.ShardingSphereChaos) error {
+func (r *ShardingSphereChaosReconciler) createConfigMap(ctx context.Context, chao *v1alpha1.ShardingSphereChaos) error {
rConfigMap := reconcile.NewSSConfigMap(chao)
if err := ctrl.SetControllerReference(chao, rConfigMap, r.Scheme); err != nil {
return err
@@ -550,7 +589,7 @@ func (r *ShardingSphereChaosReconciler) CreateConfigMap(ctx context.Context, cha
return err
}
-func (r *ShardingSphereChaosReconciler) updateJob(ctx context.Context, requirement reconcile.InjectRequirement, chao *sschaosv1alpha1.ShardingSphereChaos, cur *batchV1.Job) error {
+func (r *ShardingSphereChaosReconciler) updateJob(ctx context.Context, requirement reconcile.InjectRequirement, chao *v1alpha1.ShardingSphereChaos, cur *batchV1.Job) error {
isEqual, err := reconcile.IsJobChanged(chao, requirement, cur)
if err != nil {
return err
@@ -564,7 +603,7 @@ func (r *ShardingSphereChaosReconciler) updateJob(ctx context.Context, requireme
return nil
}
-func (r *ShardingSphereChaosReconciler) createJob(ctx context.Context, requirement reconcile.InjectRequirement, chao *sschaosv1alpha1.ShardingSphereChaos) error {
+func (r *ShardingSphereChaosReconciler) createJob(ctx context.Context, requirement reconcile.InjectRequirement, chao *v1alpha1.ShardingSphereChaos) error {
injectJob, err := reconcile.NewJob(chao, requirement)
if err != nil {
return err
@@ -589,7 +628,7 @@ func (r *ShardingSphereChaosReconciler) createJob(ctx context.Context, requireme
if err := retry.OnError(backoff, func(e error) bool {
return true
}, func() error {
- return r.Get(ctx, types.NamespacedName{Namespace: chao.Namespace, Name: reconcile.SetJobNamespaceName(chao.Name, requirement)}, rJob)
+ return r.Get(ctx, types.NamespacedName{Namespace: chao.Namespace, Name: reconcile.MakeJobName(chao.Name, requirement)}, rJob)
}); err != nil {
return err
}
@@ -634,63 +673,29 @@ func (r *ShardingSphereChaosReconciler) createJob(ctx context.Context, requireme
return nil
}
-func (r *ShardingSphereChaosReconciler) updatePodChaos(ctx context.Context, chao *sschaosv1alpha1.ShardingSphereChaos, podChaos reconcile.PodChaos) error {
- err := r.Chaos.UpdatePodChaos(ctx, chao, podChaos)
- if err != nil {
- if err == reconcile.ErrNotChanged {
- return nil
- }
- return err
- }
- r.Events.Event(chao, "Normal", "applied", fmt.Sprintf("podChaos %s", "new changes updated"))
- return reconcile.ErrChangedSpec
-}
-
-func (r *ShardingSphereChaosReconciler) CreatePodChaos(ctx context.Context, chao *sschaosv1alpha1.ShardingSphereChaos) error {
- podChaos, err := r.Chaos.NewPodChaos(chao)
- if err != nil {
- return err
- }
- err = r.Chaos.CreatePodChaos(ctx, podChaos)
+func (r *ShardingSphereChaosReconciler) updateNetWorkChaos(ctx context.Context, chaos *v1alpha1.ShardingSphereChaos, networkChaos sschaos.NetworkChaos) error {
+ err := r.Chaos.UpdateNetworkChaos(ctx, networkChaos, chaos)
if err != nil {
return err
}
- r.Events.Event(chao, "Normal", "created", fmt.Sprintf("podChaos %s", " is created successfully"))
+ r.Events.Event(chaos, "Normal", "applied", fmt.Sprintf("networkChaos %s", "new changes updated"))
return nil
}
-func (r *ShardingSphereChaosReconciler) updateNetWorkChaos(ctx context.Context, chao *sschaosv1alpha1.ShardingSphereChaos, netWorkChaos reconcile.NetworkChaos) error {
- err := r.Chaos.UpdateNetworkChaos(ctx, chao, netWorkChaos)
- if err != nil {
- if err == reconcile.ErrNotChanged {
- return nil
- }
- return err
- }
- r.Events.Event(chao, "Normal", "applied", fmt.Sprintf("networkChaos %s", "new changes updated"))
- return reconcile.ErrChangedSpec
-}
-
-func (r *ShardingSphereChaosReconciler) CreateNetworkChaos(ctx context.Context, chao *sschaosv1alpha1.ShardingSphereChaos) error {
- networkChaos, err := r.Chaos.NewNetworkPodChaos(chao)
- if err != nil {
- return err
- }
- err = r.Chaos.CreateNetworkChaos(ctx, networkChaos)
+func (r *ShardingSphereChaosReconciler) createNetworkChaos(ctx context.Context, chaos *v1alpha1.ShardingSphereChaos) error {
+ err := r.Chaos.CreateNetworkChaos(ctx, chaos)
if err != nil {
return err
}
- r.Events.Event(chao, "Normal", "created", fmt.Sprintf("networkChaos %s", " is created successfully"))
+ r.Events.Event(chaos, "Normal", "created", fmt.Sprintf("networkChaos %s", " is created successfully"))
return nil
}
// SetupWithManager sets up the controller with the Manager.
func (r *ShardingSphereChaosReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
- For(&sschaosv1alpha1.ShardingSphereChaos{}).
- Owns(&chaosv1alpha1.PodChaos{}).
- Owns(&chaosv1alpha1.NetworkChaos{}).
+ For(&v1alpha1.ShardingSphereChaos{}).
Owns(&corev1.ConfigMap{}).
Owns(&batchV1.Job{}).
Complete(r)
diff --git a/shardingsphere-operator/pkg/kubernetes/chaos/chaos.go b/shardingsphere-operator/pkg/kubernetes/chaos/chaos.go
deleted file mode 100644
index 7166c23..0000000
--- a/shardingsphere-operator/pkg/kubernetes/chaos/chaos.go
+++ /dev/null
@@ -1,55 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package chaos
-
-import (
- "context"
-
- sschaos "github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/pkg/reconcile/shardingspherechaos"
- "k8s.io/apimachinery/pkg/types"
- "sigs.k8s.io/controller-runtime/pkg/client"
-)
-
-func NewChaos(client client.Client) Chaos {
- return ChaosClient{
- ChaosGetter: chaosMeshGetter{client},
- ChaosSetter: chaosMeshSetter{
- sschaos.NewChaosMeshHandler(client),
- },
- }
-}
-
-// Chaos interface contains setter and getter
-type Chaos interface {
- ChaosGetter
- ChaosSetter
-}
-
-type ChaosClient struct {
- ChaosGetter
- ChaosSetter
-}
-
-type ChaosGetter interface {
- GetPodChaosByNamespacedName(context.Context, types.NamespacedName) (sschaos.PodChaos, error)
- GetNetworkChaosByNamespacedName(context.Context, types.NamespacedName) (sschaos.NetworkChaos, error)
-}
-
-type ChaosSetter interface {
- sschaos.ChaosHandler
-}
diff --git a/shardingsphere-operator/pkg/kubernetes/chaos/chaos_mesh.go b/shardingsphere-operator/pkg/kubernetes/chaos/chaos_mesh.go
deleted file mode 100644
index 337668d..0000000
--- a/shardingsphere-operator/pkg/kubernetes/chaos/chaos_mesh.go
+++ /dev/null
@@ -1,60 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package chaos
-
-import (
- "context"
-
- sschaos "github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/pkg/reconcile/shardingspherechaos"
- chaosV1alpha1 "github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
- apierrors "k8s.io/apimachinery/pkg/api/errors"
- "k8s.io/apimachinery/pkg/types"
- "sigs.k8s.io/controller-runtime/pkg/client"
-)
-
-type chaosMeshGetter struct {
- client.Client
-}
-
-func (cg chaosMeshGetter) GetPodChaosByNamespacedName(ctx context.Context, namespacedName types.NamespacedName) (sschaos.PodChaos, error) {
- dp := &chaosV1alpha1.PodChaos{}
- if err := cg.Get(ctx, namespacedName, dp); err != nil {
- if apierrors.IsNotFound(err) {
- return nil, nil
- }
- return nil, err
- } else {
- return dp, nil
- }
-}
-
-func (cg chaosMeshGetter) GetNetworkChaosByNamespacedName(ctx context.Context, namespacedName types.NamespacedName) (sschaos.NetworkChaos, error) {
- dp := &chaosV1alpha1.NetworkChaos{}
- if err := cg.Get(ctx, namespacedName, dp); err != nil {
- if apierrors.IsNotFound(err) {
- return nil, nil
- }
- return nil, err
- } else {
- return dp, nil
- }
-}
-
-type chaosMeshSetter struct {
- sschaos.ChaosHandler
-}
diff --git a/shardingsphere-operator/pkg/reconcile/shardingspherechaos/chaos-mesh.go b/shardingsphere-operator/pkg/kubernetes/chaosmesh/builder.go
similarity index 85%
rename from shardingsphere-operator/pkg/reconcile/shardingspherechaos/chaos-mesh.go
rename to shardingsphere-operator/pkg/kubernetes/chaosmesh/builder.go
index c3b4e42..19986c9 100644
--- a/shardingsphere-operator/pkg/reconcile/shardingspherechaos/chaos-mesh.go
+++ b/shardingsphere-operator/pkg/kubernetes/chaosmesh/builder.go
@@ -15,21 +15,18 @@
* limitations under the License.
*/
-package shardingspherechaos
+package chaosmesh
import (
"context"
"errors"
- "reflect"
"strconv"
"github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/api/v1alpha1"
+
chaosv1alpha1 "github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
corev1 "k8s.io/api/core/v1"
- apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
- ctrl "sigs.k8s.io/controller-runtime"
- "sigs.k8s.io/controller-runtime/pkg/client"
)
const (
@@ -57,15 +54,9 @@ var (
ErrChangedSpec = errors.New("change spec")
)
-type chaosMeshHandler struct {
- r client.Client
-}
-
-func NewChaosMeshHandler(r client.Client) ChaosHandler {
- return &chaosMeshHandler{r}
-}
+type GenericChaos interface{}
-func (c *chaosMeshHandler) ConvertChaosStatus(ctx context.Context, ssChaos *v1alpha1.ShardingSphereChaos, chaos GenericChaos) v1alpha1.ChaosCondition {
+func ConvertChaosStatus(ctx context.Context, ssChaos *v1alpha1.ShardingSphereChaos, chaos GenericChaos) v1alpha1.ChaosCondition {
var status chaosv1alpha1.ChaosStatus
if ssChaos.Spec.EmbedChaos.PodChaos != nil {
if podChao, ok := chaos.(*chaosv1alpha1.PodChaos); ok && podChao != nil {
@@ -109,31 +100,7 @@ func judgeCondition(condition map[chaosv1alpha1.ChaosConditionType]bool, phase c
return v1alpha1.Unknown
}
-func (c *chaosMeshHandler) CreatePodChaos(ctx context.Context, chao PodChaos) error {
- podChao, ok := chao.(*chaosv1alpha1.PodChaos)
- if !ok {
- return ErrConvert
- }
- if err := c.r.Create(ctx, podChao); err != nil && !apierrors.IsAlreadyExists(err) {
- return err
- }
-
- return nil
-}
-
-func (c *chaosMeshHandler) CreateNetworkChaos(ctx context.Context, chao NetworkChaos) error {
- networkChao, ok := chao.(*chaosv1alpha1.NetworkChaos)
- if !ok {
- return ErrConvert
- }
- if err := c.r.Create(ctx, networkChao); err != nil && !apierrors.IsAlreadyExists(err) {
- return err
- }
-
- return nil
-}
-
-func (c *chaosMeshHandler) NewPodChaos(ssChao *v1alpha1.ShardingSphereChaos) (PodChaos, error) {
+func NewPodChaos(ssChao *v1alpha1.ShardingSphereChaos) (PodChaos, error) {
pcb := NewPodChaosBuilder()
pcb.SetName(ssChao.Name).SetNamespace(ssChao.Namespace).SetLabels(ssChao.Labels)
@@ -178,13 +145,16 @@ func (c *chaosMeshHandler) NewPodChaos(ssChao *v1alpha1.ShardingSphereChaos) (Po
pcb.SetContainerSelector(containerSelector)
podChao := pcb.Build()
- if err := ctrl.SetControllerReference(ssChao, podChao, c.r.Scheme()); err != nil {
- return nil, err
- }
+ // FIXME
+ /*
+ if err := ctrl.SetControllerReference(ssChao, podChao, c.r.Scheme()); err != nil {
+ return nil, err
+ }
+ */
return podChao, nil
}
-func (c *chaosMeshHandler) NewNetworkPodChaos(ssChao *v1alpha1.ShardingSphereChaos) (NetworkChaos, error) {
+func NewNetworkChaos(ssChao *v1alpha1.ShardingSphereChaos) (NetworkChaos, error) {
ncb := NewNetworkChaosBuilder()
ncb.SetName(ssChao.Name).SetNamespace(ssChao.Namespace).SetLabels(ssChao.Labels)
chao := ssChao.Spec.NetworkChaos
@@ -280,71 +250,15 @@ func (c *chaosMeshHandler) NewNetworkPodChaos(ssChao *v1alpha1.ShardingSphereCha
ncb.SetTcParameter(*tcParams)
networkChao := ncb.Build()
- if err := ctrl.SetControllerReference(ssChao, networkChao, c.r.Scheme()); err != nil {
- return nil, err
- }
+ // FIXME
+ /*
+ if err := ctrl.SetControllerReference(ssChao, networkChao, c.r.Scheme()); err != nil {
+ return nil, err
+ }
+ */
return networkChao, nil
}
-func (c *chaosMeshHandler) UpdateNetworkChaos(ctx context.Context, ssChaos *v1alpha1.ShardingSphereChaos, cur NetworkChaos) error {
- networkChao, err := c.NewNetworkPodChaos(ssChaos)
- if err != nil {
- return err
- }
-
- reExp, ok := networkChao.(*chaosv1alpha1.NetworkChaos)
- if !ok {
- return ErrConvert
- }
- reCur, ok := cur.(*chaosv1alpha1.NetworkChaos)
- if !ok {
- return ErrConvert
- }
- isEqual := reflect.DeepEqual(reExp.Spec, reCur.Spec)
- if isEqual {
- return ErrNotChanged
- }
-
- if err := c.r.Create(ctx, reCur); err != nil {
- return err
- }
-
- if err := c.r.Update(ctx, reExp); err != nil {
- return err
- }
-
- return nil
-}
-
-func (c *chaosMeshHandler) UpdatePodChaos(ctx context.Context, ssChaos *v1alpha1.ShardingSphereChaos, cur PodChaos) error {
- podChao, err := c.NewPodChaos(ssChaos)
- if err != nil {
- return err
- }
- reExp, ok := (podChao).(*chaosv1alpha1.PodChaos)
- if !ok {
- return ErrConvert
- }
- reCur, ok := cur.(*chaosv1alpha1.PodChaos)
- if !ok {
- return ErrConvert
- }
- isEqual := reflect.DeepEqual(reExp.Spec, reCur.Spec)
- if isEqual {
- return ErrNotChanged
- }
-
- if err := c.r.Delete(ctx, reCur); err != nil {
- return err
- }
-
- if err := c.CreatePodChaos(ctx, reExp); err != nil {
- return err
- }
-
- return nil
-}
-
type PodChaosBuilder interface {
SetNamespace(string) PodChaosBuilder
SetName(string) PodChaosBuilder
@@ -352,7 +266,7 @@ type PodChaosBuilder interface {
SetAnnotations(map[string]string) PodChaosBuilder
SetContainerSelector(*chaosv1alpha1.ContainerSelector) PodChaosBuilder
SetAction(string) PodChaosBuilder
- SetDuration(string) PodChaosBuilder
+ SetDuration(*string) PodChaosBuilder
SetGracePeriod(int64) PodChaosBuilder
Build() *chaosv1alpha1.PodChaos
}
@@ -469,13 +383,13 @@ func (p *podChaosBuilder) SetAction(action string) PodChaosBuilder {
return p
}
-func (p *podChaosBuilder) SetDuration(duration string) PodChaosBuilder {
- if duration == "" {
+func (p *podChaosBuilder) SetDuration(duration *string) PodChaosBuilder {
+ if *duration == "" {
//todo: change to default
ret := "1m"
p.podChaos.Spec.Duration = &ret
} else {
- p.podChaos.Spec.Duration = &duration
+ p.podChaos.Spec.Duration = duration
}
return p
}
@@ -497,7 +411,7 @@ type NetworkChaosBuilder interface {
SetPodSelector(*chaosv1alpha1.PodSelector) NetworkChaosBuilder
SetAction(string) NetworkChaosBuilder
SetDevice(string) NetworkChaosBuilder
- SetDuration(string) NetworkChaosBuilder
+ SetDuration(*string) NetworkChaosBuilder
SetDirection(string) NetworkChaosBuilder
SetTarget(*chaosv1alpha1.PodSelector) NetworkChaosBuilder
SetTargetDevice(string) NetworkChaosBuilder
@@ -564,8 +478,8 @@ func (n *netWorkChaosBuilder) SetDevice(device string) NetworkChaosBuilder {
return n
}
-func (n *netWorkChaosBuilder) SetDuration(duration string) NetworkChaosBuilder {
- n.netWorkChaos.Spec.Duration = &duration
+func (n *netWorkChaosBuilder) SetDuration(duration *string) NetworkChaosBuilder {
+ n.netWorkChaos.Spec.Duration = duration
return n
}
diff --git a/shardingsphere-operator/pkg/kubernetes/chaosmesh/chaosmesh.go b/shardingsphere-operator/pkg/kubernetes/chaosmesh/chaosmesh.go
new file mode 100644
index 0000000..30a006c
--- /dev/null
+++ b/shardingsphere-operator/pkg/kubernetes/chaosmesh/chaosmesh.go
@@ -0,0 +1,179 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package chaosmesh
+
+import (
+ "context"
+
+ "github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/api/v1alpha1"
+ chaosmeshapi "github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
+ apierrors "k8s.io/apimachinery/pkg/api/errors"
+ "k8s.io/apimachinery/pkg/types"
+ "sigs.k8s.io/controller-runtime/pkg/client"
+)
+
+// NewChaos creates a new Chaos
+func NewChaos(client client.Client) Chaos {
+ return chaosClient{
+ builder: builder{},
+ getter: getter{
+ client,
+ },
+ setter: setter{
+ client,
+ },
+ }
+}
+
+// Chaos interface contains setter and getter
+type Chaos interface {
+ Builder
+ Getter
+ Setter
+}
+
+type chaosClient struct {
+ builder
+ getter
+ setter
+}
+
+// Builder build Chaos from different parameters
+type Builder interface {
+ NewPodChaos(context.Context, *v1alpha1.ShardingSphereChaos) PodChaos
+ NewNetworkChaos(context.Context, *v1alpha1.ShardingSphereChaos) NetworkChaos
+}
+
+// Getter get Chaos from different parameters
+type Getter interface {
+ GetPodChaosByNamespacedName(context.Context, types.NamespacedName) (PodChaos, error)
+ GetNetworkChaosByNamespacedName(context.Context, types.NamespacedName) (NetworkChaos, error)
+}
+
+// Setter set Chaos from different parameters
+type Setter interface {
+ CreatePodChaos(context.Context, *v1alpha1.ShardingSphereChaos) error
+ UpdatePodChaos(context.Context, PodChaos, *v1alpha1.ShardingSphereChaos) error
+
+ CreateNetworkChaos(context.Context, *v1alpha1.ShardingSphereChaos) error
+ UpdateNetworkChaos(context.Context, NetworkChaos, *v1alpha1.ShardingSphereChaos) error
+}
+
+type getter struct {
+ client.Client
+}
+
+type PodChaos interface{}
+
+func (cg getter) GetPodChaosByNamespacedName(ctx context.Context, namespacedName types.NamespacedName) (PodChaos, error) {
+ chaos := &chaosmeshapi.PodChaos{}
+ if err := cg.Get(ctx, namespacedName, chaos); err != nil {
+ if apierrors.IsNotFound(err) {
+ return nil, nil
+ }
+ return nil, err
+ } else {
+ return chaos, nil
+ }
+}
+
+type NetworkChaos interface{}
+
+func (cg getter) GetNetworkChaosByNamespacedName(ctx context.Context, namespacedName types.NamespacedName) (NetworkChaos, error) {
+ chaos := &chaosmeshapi.NetworkChaos{}
+ if err := cg.Get(ctx, namespacedName, chaos); err != nil {
+ if apierrors.IsNotFound(err) {
+ return nil, nil
+ }
+ return nil, err
+ } else {
+ return chaos, nil
+ }
+}
+
+type builder struct{}
+
+func (blder builder) NewPodChaos(ctx context.Context, sschaos *v1alpha1.ShardingSphereChaos) PodChaos {
+ pc, _ := NewPodChaos(sschaos)
+ return pc
+}
+
+func (blder builder) NewNetworkChaos(ctx context.Context, sschaos *v1alpha1.ShardingSphereChaos) NetworkChaos {
+ nc, _ := NewNetworkChaos(sschaos)
+ return nc
+}
+
+type setter struct {
+ client.Client
+}
+
+// CreatePodChaos creates a new pod chaos
+func (cs setter) CreatePodChaos(ctx context.Context, sschaos *v1alpha1.ShardingSphereChaos) error {
+ pc, err := NewPodChaos(sschaos)
+ if err != nil {
+ return err
+ }
+ return cs.Client.Create(ctx, pc.(*chaosmeshapi.PodChaos))
+}
+
+// UpdatePodChaos updates a pod chaos
+func (cs setter) UpdatePodChaos(ctx context.Context, podChaos PodChaos, sschaos *v1alpha1.ShardingSphereChaos) error {
+ pc, err := NewPodChaos(sschaos)
+ if err != nil {
+ return err
+ }
+ s, ok := pc.(*chaosmeshapi.PodChaos)
+ if !ok {
+ return ErrConvert
+ }
+ t, ok := podChaos.(*chaosmeshapi.PodChaos)
+ if !ok {
+ return ErrConvert
+ }
+ t.Spec = s.Spec
+
+ return cs.Client.Update(ctx, t)
+}
+
+// CreateNetworkChaos creates a new network chaos
+func (cs setter) CreateNetworkChaos(ctx context.Context, sschaos *v1alpha1.ShardingSphereChaos) error {
+ nc, err := NewNetworkChaos(sschaos)
+ if err != nil {
+ return err
+ }
+ return cs.Client.Create(ctx, nc.(*chaosmeshapi.NetworkChaos))
+}
+
+// UpdateNetworkChaos updates a network chaos
+func (cs setter) UpdateNetworkChaos(ctx context.Context, networkChaos NetworkChaos, sschaos *v1alpha1.ShardingSphereChaos) error {
+ pc, err := NewNetworkChaos(sschaos)
+ if err != nil {
+ return err
+ }
+ s, ok := pc.(*chaosmeshapi.NetworkChaos)
+ if !ok {
+ return ErrConvert
+ }
+ t, ok := networkChaos.(*chaosmeshapi.NetworkChaos)
+ if !ok {
+ return ErrConvert
+ }
+ t.Spec = s.Spec
+
+ return cs.Client.Update(ctx, t)
+}
diff --git a/shardingsphere-operator/pkg/reconcile/shardingspherechaos/chaos.go b/shardingsphere-operator/pkg/reconcile/shardingspherechaos/chaos.go
deleted file mode 100644
index 7899fad..0000000
--- a/shardingsphere-operator/pkg/reconcile/shardingspherechaos/chaos.go
+++ /dev/null
@@ -1,45 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package shardingspherechaos
-
-import (
- "context"
-
- "github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/api/v1alpha1"
-)
-
-type ChaosHandler interface {
- NewPodChaos(ssChao *v1alpha1.ShardingSphereChaos) (PodChaos, error)
- NewNetworkPodChaos(ssChao *v1alpha1.ShardingSphereChaos) (NetworkChaos, error)
- UpdateNetworkChaos(ctx context.Context, ssChaos *v1alpha1.ShardingSphereChaos, cur NetworkChaos) error
- UpdatePodChaos(ctx context.Context, ssChaos *v1alpha1.ShardingSphereChaos, cur PodChaos) error
- CreatePodChaos(ctx context.Context, podChao PodChaos) error
- CreateNetworkChaos(ctx context.Context, networkChao NetworkChaos) error
- ConvertChaosStatus(ctx context.Context, ssChaos *v1alpha1.ShardingSphereChaos, chaos GenericChaos) v1alpha1.ChaosCondition
-}
-
-type GenericChaos interface {
-}
-
-type PodChaos interface {
- GenericChaos
-}
-
-type NetworkChaos interface {
- GenericChaos
-}
diff --git a/shardingsphere-operator/pkg/reconcile/shardingspherechaos/job.go b/shardingsphere-operator/pkg/reconcile/shardingspherechaos/job.go
index 273a8a9..b170858 100644
--- a/shardingsphere-operator/pkg/reconcile/shardingspherechaos/job.go
+++ b/shardingsphere-operator/pkg/reconcile/shardingspherechaos/job.go
@@ -54,18 +54,19 @@ const (
type InjectRequirement string
var (
+ //FIXME: pick another name for experimental
Experimental InjectRequirement = "experimental"
Pressure InjectRequirement = "pressure"
Verify InjectRequirement = "verify"
)
-func SetJobNamespaceName(name string, requirement InjectRequirement) string {
+func MakeJobName(name string, requirement InjectRequirement) string {
return fmt.Sprintf("%s-%s", name, string(requirement))
}
func NewJob(ssChaos *v1alpha1.ShardingSphereChaos, requirement InjectRequirement) (*v1.Job, error) {
jbd := NewJobBuilder()
- jbd.SetNamespace(ssChaos.Namespace).SetLabels(ssChaos.Labels).SetName(SetJobNamespaceName(ssChaos.Name, requirement))
+ jbd.SetNamespace(ssChaos.Namespace).SetLabels(ssChaos.Labels).SetName(MakeJobName(ssChaos.Name, requirement))
if v, ok := ssChaos.Annotations[completions]; ok {
value, err := MustInt32(v)
diff --git a/shardingsphere-operator/pkg/reconcile/shardingspherechaos/shardingspherechaos_suite_test.go b/shardingsphere-operator/pkg/reconcile/shardingspherechaos/shardingsphere_chaos_suite_test.go
similarity index 100%
rename from shardingsphere-operator/pkg/reconcile/shardingspherechaos/shardingspherechaos_suite_test.go
rename to shardingsphere-operator/pkg/reconcile/shardingspherechaos/shardingsphere_chaos_suite_test.go
diff --git a/shardingsphere-operator/pkg/reconcile/shardingspherechaos/shardingspherechaos_test.go b/shardingsphere-operator/pkg/reconcile/shardingspherechaos/shardingsphere_chaos_test.go
similarity index 95%
rename from shardingsphere-operator/pkg/reconcile/shardingspherechaos/shardingspherechaos_test.go
rename to shardingsphere-operator/pkg/reconcile/shardingspherechaos/shardingsphere_chaos_test.go
index 5f0b800..cd32c07 100644
--- a/shardingsphere-operator/pkg/reconcile/shardingspherechaos/shardingspherechaos_test.go
+++ b/shardingsphere-operator/pkg/reconcile/shardingspherechaos/shardingsphere_chaos_test.go
@@ -22,7 +22,8 @@ import (
"github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/api/v1alpha1"
"github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/pkg/controllers"
- "github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/pkg/kubernetes/chaos"
+
+ sschaos "github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/pkg/kubernetes/chaosmesh"
chaosV1alpha1 "github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
@@ -39,6 +40,7 @@ import (
)
var _ = Describe("ShardingSphereChaos", func() {
+ var d = "5m"
Context("check related resource created by ShardingSphereChaos Controller", func() {
var (
@@ -68,7 +70,7 @@ var _ = Describe("ShardingSphereChaos", func() {
Client: fakeClient,
Scheme: scheme,
Log: logf.Log,
- Chaos: chaos.NewChaos(fakeClient),
+ Chaos: sschaos.NewChaos(fakeClient),
}
ctx = context.Background()
ssChaos = &v1alpha1.ShardingSphereChaos{
@@ -94,7 +96,7 @@ var _ = Describe("ShardingSphereChaos", func() {
Action: v1alpha1.PodFailure,
Params: v1alpha1.PodChaosParams{
PodFailure: &v1alpha1.PodFailureParams{
- Duration: "5m",
+ Duration: &d,
},
},
},