You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by "moomman (via GitHub)" <gi...@apache.org> on 2023/04/18 10:06:40 UTC

[GitHub] [shardingsphere-on-cloud] moomman opened a new pull request, #316: feat: add verify Spec && update reconcile logic

moomman opened a new pull request, #316:
URL: https://github.com/apache/shardingsphere-on-cloud/pull/316

   ### Type of change:
   
   - [ ] Bugfix
   - [x] New feature provided
   - [ ] Improve performance
   - [ ] Backport patches
   
   ### What this PR does / why we need it:
   
   refrence issue: #290 
   
   ### Pre-submission checklist:
   
   * [ ] Did you explain what problem does this PR solve? Or what new features have been added?
   * [ ] Have you added corresponding test cases?
   * [ ] Have you modified the corresponding document?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [shardingsphere-on-cloud] moomman commented on a diff in pull request #316: feat: add verify Spec && update reconcile logic

Posted by "moomman (via GitHub)" <gi...@apache.org>.
moomman commented on code in PR #316:
URL: https://github.com/apache/shardingsphere-on-cloud/pull/316#discussion_r1173519804


##########
shardingsphere-operator/pkg/controllers/shardingsphere_chaos_controller.go:
##########
@@ -213,10 +246,137 @@ func (r *ShardingSphereChaosReconciler) reconcileStatus(ctx context.Context, nam
 	if err != nil {
 		return err
 	}
-	rt.Status = ssChaos.Status
+	setRtStatus(rt, ssChaos)
 	return r.Status().Update(ctx, rt)
 }
 
+func SetDefault(ssChaos *sschaosv1alpha1.ShardingSphereChaos) {
+	if ssChaos.Status.Phase == "" {
+		ssChaos.Status.Phase = sschaosv1alpha1.PhaseBeforeExperiment
+	}
+	if len(ssChaos.Status.Result) <= 0 || ssChaos.Status.Result == nil {
+		ssChaos.Status.Result = []sschaosv1alpha1.Result{}
+	}
+}
+
+func setRtStatus(rt *sschaosv1alpha1.ShardingSphereChaos, ssChaos *sschaosv1alpha1.ShardingSphereChaos) {
+	rt.Status.Result = []sschaosv1alpha1.Result{}
+	for i := range ssChaos.Status.Result {
+		rt.Status.Result = append(rt.Status.Result, sschaosv1alpha1.Result{
+			Success: ssChaos.Status.Result[i].Success,
+			Details: sschaosv1alpha1.Details{
+				Time: metav1.Time{Time: time.Now()},
+				Msg:  ssChaos.Status.Result[i].Details.Msg,
+			},
+		})
+	}
+	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 {
+	if checkResultExist(rJob) {
+		return nil
+	}
+	check := "Verify"

Review Comment:
   solved



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [shardingsphere-on-cloud] sonarcloud[bot] commented on pull request #316: feat: add verify Spec && update reconcile logic

Posted by "sonarcloud[bot] (via GitHub)" <gi...@apache.org>.
sonarcloud[bot] commented on PR #316:
URL: https://github.com/apache/shardingsphere-on-cloud/pull/316#issuecomment-1519071851

   Kudos, SonarCloud Quality Gate passed!&nbsp; &nbsp; [![Quality Gate passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/passed-16px.png 'Quality Gate passed')](https://sonarcloud.io/dashboard?id=apache_shardingsphere-on-cloud&pullRequest=316)
   
   [![Bug](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/bug-16px.png 'Bug')](https://sonarcloud.io/project/issues?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=BUG) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=BUG) [0 Bugs](https://sonarcloud.io/project/issues?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=BUG)  
   [![Vulnerability](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/vulnerability-16px.png 'Vulnerability')](https://sonarcloud.io/project/issues?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=VULNERABILITY) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=VULNERABILITY)  
   [![Security Hotspot](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/security_hotspot-16px.png 'Security Hotspot')](https://sonarcloud.io/project/security_hotspots?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=SECURITY_HOTSPOT) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/security_hotspots?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=SECURITY_HOTSPOT)  
   [![Code Smell](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/code_smell-16px.png 'Code Smell')](https://sonarcloud.io/project/issues?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=CODE_SMELL) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=CODE_SMELL) [0 Code Smells](https://sonarcloud.io/project/issues?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=CODE_SMELL)
   
   [![No Coverage information](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/CoverageChart/NoCoverageInfo-16px.png 'No Coverage information')](https://sonarcloud.io/component_measures?id=apache_shardingsphere-on-cloud&pullRequest=316) No Coverage information  
   [![No Duplication information](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/Duplications/NoDuplicationInfo-16px.png 'No Duplication information')](https://sonarcloud.io/component_measures?id=apache_shardingsphere-on-cloud&pullRequest=316&metric=duplicated_lines_density&view=list) No Duplication information
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [shardingsphere-on-cloud] mlycore commented on a diff in pull request #316: feat: add verify Spec && update reconcile logic

Posted by "mlycore (via GitHub)" <gi...@apache.org>.
mlycore commented on code in PR #316:
URL: https://github.com/apache/shardingsphere-on-cloud/pull/316#discussion_r1172428707


##########
shardingsphere-operator/api/v1alpha1/shardingsphere_chaos_types.go:
##########
@@ -78,13 +80,26 @@ const (
 type ShardingSphereChaosStatus struct {
 	ChaosCondition ChaosCondition `json:"chaosCondition"`
 	Phase          Phase          `json:"phase"`
+	Result         []Result       `json:"result"`

Review Comment:
   Use plural to keep it's semantic. Please update `Result` to `Results`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [shardingsphere-on-cloud] moomman commented on a diff in pull request #316: feat: add verify Spec && update reconcile logic

Posted by "moomman (via GitHub)" <gi...@apache.org>.
moomman commented on code in PR #316:
URL: https://github.com/apache/shardingsphere-on-cloud/pull/316#discussion_r1173523555


##########
shardingsphere-operator/pkg/controllers/shardingsphere_chaos_controller.go:
##########
@@ -213,10 +246,137 @@ func (r *ShardingSphereChaosReconciler) reconcileStatus(ctx context.Context, nam
 	if err != nil {
 		return err
 	}
-	rt.Status = ssChaos.Status
+	setRtStatus(rt, ssChaos)
 	return r.Status().Update(ctx, rt)
 }
 
+func SetDefault(ssChaos *sschaosv1alpha1.ShardingSphereChaos) {
+	if ssChaos.Status.Phase == "" {
+		ssChaos.Status.Phase = sschaosv1alpha1.PhaseBeforeExperiment
+	}
+	if len(ssChaos.Status.Result) <= 0 || ssChaos.Status.Result == nil {
+		ssChaos.Status.Result = []sschaosv1alpha1.Result{}
+	}
+}
+
+func setRtStatus(rt *sschaosv1alpha1.ShardingSphereChaos, ssChaos *sschaosv1alpha1.ShardingSphereChaos) {
+	rt.Status.Result = []sschaosv1alpha1.Result{}
+	for i := range ssChaos.Status.Result {
+		rt.Status.Result = append(rt.Status.Result, sschaosv1alpha1.Result{
+			Success: ssChaos.Status.Result[i].Success,
+			Details: sschaosv1alpha1.Details{
+				Time: metav1.Time{Time: time.Now()},
+				Msg:  ssChaos.Status.Result[i].Details.Msg,
+			},
+		})
+	}
+	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 {
+	if checkResultExist(rJob) {
+		return nil
+	}
+	check := "Verify"
+	for i := range ssChaos.Status.Result {
+		if len(ssChaos.Status.Result[i].Details.Msg) >= len(check) && ssChaos.Status.Result[i].Details.Msg[:len(check)] == check {
+			return nil
+		}
+	}
+	logOpts := &v1.PodLogOptions{}
+	pod, err := r.getPodHaveLog(ctx, rJob)
+	if err != nil {
+		return err
+	}
+	containerNamespacedname := types.NamespacedName{
+		Namespace: pod.Namespace,
+		Name:      pod.Name,
+	}
+	result := &sschaosv1alpha1.Result{}
+	if rJob.Status.Succeeded == 1 {
+		log, err := r.getPodLog(ctx, containerNamespacedname, logOpts)
+		if ssChaos.Spec.Expect.Verify == "" || ssChaos.Spec.Expect.Verify == log {
+			result.Success = true
+			result.Details = sschaosv1alpha1.Details{

Review Comment:
   solved
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [shardingsphere-on-cloud] sonarcloud[bot] commented on pull request #316: feat: add verify Spec && update reconcile logic

Posted by "sonarcloud[bot] (via GitHub)" <gi...@apache.org>.
sonarcloud[bot] commented on PR #316:
URL: https://github.com/apache/shardingsphere-on-cloud/pull/316#issuecomment-1519014242

   Kudos, SonarCloud Quality Gate passed!&nbsp; &nbsp; [![Quality Gate passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/passed-16px.png 'Quality Gate passed')](https://sonarcloud.io/dashboard?id=apache_shardingsphere-on-cloud&pullRequest=316)
   
   [![Bug](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/bug-16px.png 'Bug')](https://sonarcloud.io/project/issues?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=BUG) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=BUG) [0 Bugs](https://sonarcloud.io/project/issues?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=BUG)  
   [![Vulnerability](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/vulnerability-16px.png 'Vulnerability')](https://sonarcloud.io/project/issues?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=VULNERABILITY) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=VULNERABILITY)  
   [![Security Hotspot](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/security_hotspot-16px.png 'Security Hotspot')](https://sonarcloud.io/project/security_hotspots?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=SECURITY_HOTSPOT) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/security_hotspots?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=SECURITY_HOTSPOT)  
   [![Code Smell](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/code_smell-16px.png 'Code Smell')](https://sonarcloud.io/project/issues?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=CODE_SMELL) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=CODE_SMELL) [0 Code Smells](https://sonarcloud.io/project/issues?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=CODE_SMELL)
   
   [![No Coverage information](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/CoverageChart/NoCoverageInfo-16px.png 'No Coverage information')](https://sonarcloud.io/component_measures?id=apache_shardingsphere-on-cloud&pullRequest=316) No Coverage information  
   [![No Duplication information](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/Duplications/NoDuplicationInfo-16px.png 'No Duplication information')](https://sonarcloud.io/component_measures?id=apache_shardingsphere-on-cloud&pullRequest=316&metric=duplicated_lines_density&view=list) No Duplication information
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [shardingsphere-on-cloud] moomman commented on a diff in pull request #316: feat: add verify Spec && update reconcile logic

Posted by "moomman (via GitHub)" <gi...@apache.org>.
moomman commented on code in PR #316:
URL: https://github.com/apache/shardingsphere-on-cloud/pull/316#discussion_r1173526498


##########
shardingsphere-operator/pkg/controllers/shardingsphere_chaos_controller.go:
##########
@@ -213,10 +246,137 @@ func (r *ShardingSphereChaosReconciler) reconcileStatus(ctx context.Context, nam
 	if err != nil {
 		return err
 	}
-	rt.Status = ssChaos.Status
+	setRtStatus(rt, ssChaos)
 	return r.Status().Update(ctx, rt)
 }
 
+func SetDefault(ssChaos *sschaosv1alpha1.ShardingSphereChaos) {
+	if ssChaos.Status.Phase == "" {
+		ssChaos.Status.Phase = sschaosv1alpha1.PhaseBeforeExperiment
+	}
+	if len(ssChaos.Status.Result) <= 0 || ssChaos.Status.Result == nil {
+		ssChaos.Status.Result = []sschaosv1alpha1.Result{}
+	}
+}
+
+func setRtStatus(rt *sschaosv1alpha1.ShardingSphereChaos, ssChaos *sschaosv1alpha1.ShardingSphereChaos) {
+	rt.Status.Result = []sschaosv1alpha1.Result{}
+	for i := range ssChaos.Status.Result {
+		rt.Status.Result = append(rt.Status.Result, sschaosv1alpha1.Result{
+			Success: ssChaos.Status.Result[i].Success,
+			Details: sschaosv1alpha1.Details{
+				Time: metav1.Time{Time: time.Now()},
+				Msg:  ssChaos.Status.Result[i].Details.Msg,
+			},
+		})
+	}
+	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 {
+	if checkResultExist(rJob) {
+		return nil
+	}
+	check := "Verify"
+	for i := range ssChaos.Status.Result {
+		if len(ssChaos.Status.Result[i].Details.Msg) >= len(check) && ssChaos.Status.Result[i].Details.Msg[:len(check)] == check {
+			return nil
+		}
+	}
+	logOpts := &v1.PodLogOptions{}
+	pod, err := r.getPodHaveLog(ctx, rJob)
+	if err != nil {
+		return err
+	}
+	containerNamespacedname := types.NamespacedName{
+		Namespace: pod.Namespace,
+		Name:      pod.Name,
+	}
+	result := &sschaosv1alpha1.Result{}
+	if rJob.Status.Succeeded == 1 {
+		log, err := r.getPodLog(ctx, containerNamespacedname, logOpts)
+		if ssChaos.Spec.Expect.Verify == "" || ssChaos.Spec.Expect.Verify == log {
+			result.Success = true
+			result.Details = sschaosv1alpha1.Details{
+				Time: metav1.Time{Time: time.Now()},
+				Msg:  fmt.Sprintf("%s: job succeeded", check),
+			}
+		} else {
+			if err != nil {
+				return err
+			}
+			result.Success = false
+			result.Details = sschaosv1alpha1.Details{
+				Time: metav1.Time{Time: time.Now()},
+				Msg:  fmt.Sprintf("%s: %s", check, log),
+			}
+		}
+	}
+
+	if rJob.Status.Failed == 1 {

Review Comment:
   i can not change rJob



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [shardingsphere-on-cloud] Xu-Wentao commented on a diff in pull request #316: feat: add verify Spec && update reconcile logic

Posted by "Xu-Wentao (via GitHub)" <gi...@apache.org>.
Xu-Wentao commented on code in PR #316:
URL: https://github.com/apache/shardingsphere-on-cloud/pull/316#discussion_r1174818081


##########
shardingsphere-operator/pkg/controllers/shardingsphere_chaos_controller.go:
##########
@@ -153,57 +167,88 @@ func (r *ShardingSphereChaosReconciler) reconcileChaos(ctx context.Context, ssCh
 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}
-	rConfigmap, isExist, err := r.getConfigMapByNamespacedName(ctx, namespaceName)
+	rConfigmap, err := r.getConfigMapByNamespacedName(ctx, namespaceName)
 	if err != nil {
 		logger.Error(err, "get configmap error")
 		return err
 	}
 
-	if isExist {
+	if rConfigmap != nil {
 		return r.updateConfigMap(ctx, ssChaos, rConfigmap)
 	}
 
-	return r.CreateConfigMap(ctx, ssChaos)
-}
-
-func (r *ShardingSphereChaosReconciler) reconcileJob(ctx context.Context, ssChaos *sschaosv1alpha1.ShardingSphereChaos) error {
-	logger := r.Log.WithValues("reconcile job", ssChaos.Name)
-	namespaceName := types.NamespacedName{Namespace: ssChaos.Namespace, Name: ssChaos.Name}
-	rJob, isExist, err := r.getJobByNamespacedName(ctx, namespaceName)
+	err = r.CreateConfigMap(ctx, ssChaos)
 	if err != nil {
-		logger.Error(err, "get job err")
+		r.Events.Event(ssChaos, "Warning", "Created", fmt.Sprintf("configmap created fail %s", err))
 		return err
 	}
+
+	r.Events.Event(ssChaos, "Normal", "Created", "configmap created successfully")
+	return nil
+}
+
+func (r *ShardingSphereChaosReconciler) reconcileJob(ctx context.Context, ssChaos *sschaosv1alpha1.ShardingSphereChaos) error {
 	var nowInjectRequirement reconcile.InjectRequirement
 	if ssChaos.Status.Phase == "" || ssChaos.Status.Phase == sschaosv1alpha1.PhaseBeforeExperiment || ssChaos.Status.Phase == sschaosv1alpha1.PhaseAfterExperiment {
 		nowInjectRequirement = reconcile.Experimental
 	}
-	if ssChaos.Status.Phase == sschaosv1alpha1.PhaseInChaos || ssChaos.Status.Phase == sschaosv1alpha1.PhaseRecoveredChaos {
+	if ssChaos.Status.Phase == sschaosv1alpha1.PhaseInChaos {
 		nowInjectRequirement = reconcile.Pressure
 	}
-	if isExist {
+	if ssChaos.Status.Phase == sschaosv1alpha1.PhaseRecoveredChaos {
+		nowInjectRequirement = reconcile.Verify
+	}

Review Comment:
   use switch case is better
   ```go
   switch ssChaos.Status.Phase {
   	case "", sschaosv1alpha1.PhaseBeforeExperiment, sschaosv1alpha1.PhaseAfterExperiment:
   		nowInjectRequirement = reconcile.Experimental
   	case sschaosv1alpha1.PhaseInChaos:
   		nowInjectRequirement = reconcile.Pressure
   	case sschaosv1alpha1.PhaseRecoveredChaos:
   		nowInjectRequirement = reconcile.Verify
   	}
   ```



##########
shardingsphere-operator/pkg/controllers/shardingsphere_chaos_controller.go:
##########
@@ -213,10 +258,183 @@ func (r *ShardingSphereChaosReconciler) reconcileStatus(ctx context.Context, nam
 	if err != nil {
 		return err
 	}
-	rt.Status = ssChaos.Status
+	setRtStatus(rt, ssChaos)
 	return r.Status().Update(ctx, rt)
 }
 
+func getRequirement(ssChaos *sschaosv1alpha1.ShardingSphereChaos) reconcile.InjectRequirement {
+	var jobName reconcile.InjectRequirement
+	if ssChaos.Status.Phase == sschaosv1alpha1.PhaseBeforeExperiment || ssChaos.Status.Phase == sschaosv1alpha1.PhaseAfterExperiment {
+		jobName = reconcile.Experimental
+	}
+	if ssChaos.Status.Phase == sschaosv1alpha1.PhaseInChaos {
+		jobName = reconcile.Pressure
+	}
+	if ssChaos.Status.Phase == sschaosv1alpha1.PhaseRecoveredChaos {
+		jobName = reconcile.Verify
+	}
+	return jobName
+}
+
+func getJobSuccessAndFail(conditions []batchV1.JobCondition) (bool, bool) {
+	var (
+		failed    = false
+		succeeded = false
+	)
+
+	for i := range conditions {
+		r := conditions[i]
+		if r.Type == batchV1.JobComplete {
+			if r.Status == v1.ConditionTrue {
+				succeeded = true
+			}
+			if r.Status == v1.ConditionFalse {
+				succeeded = false
+			}
+		}
+
+		if r.Type == batchV1.JobFailed {
+			if r.Status == v1.ConditionTrue {
+				failed = true
+			}
+			if r.Status == v1.ConditionFalse {
+				failed = false
+			}
+		}
+	}
+
+	return succeeded, failed
+}
+
+func setDefault(ssChaos *sschaosv1alpha1.ShardingSphereChaos) {
+	if ssChaos.Status.Phase == "" {
+		ssChaos.Status.Phase = sschaosv1alpha1.PhaseBeforeExperiment
+	}
+	if ssChaos.Status.Result == nil || len(ssChaos.Status.Result) == 0 {
+		ssChaos.Status.Result = []sschaosv1alpha1.Result{}
+	}
+}
+
+func setRtStatus(rt *sschaosv1alpha1.ShardingSphereChaos, ssChaos *sschaosv1alpha1.ShardingSphereChaos) {
+	rt.Status.Result = []sschaosv1alpha1.Result{}
+	for i := range ssChaos.Status.Result {
+		r := &ssChaos.Status.Result[i]
+		rt.Status.Result = append(rt.Status.Result, sschaosv1alpha1.Result{
+			Success: r.Success,
+			Detail: sschaosv1alpha1.Detail{
+				Time: metav1.Time{Time: time.Now()},
+				Msg:  r.Detail.Msg,
+			},
+		})
+	}
+	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 {
+	if isResultExist(rJob) {
+		return nil
+	}
+	for i := range ssChaos.Status.Result {
+		r := &ssChaos.Status.Result[i]
+		if len(r.Detail.Msg) >= len(VerifyJobCheck) && r.Detail.Msg[:len(VerifyJobCheck)] == VerifyJobCheck {

Review Comment:
   equals `strings.HasPrefix(r.Detail.Msg, VerifyJobCheck)` ?



##########
shardingsphere-operator/pkg/controllers/shardingsphere_chaos_controller.go:
##########
@@ -213,10 +258,183 @@ func (r *ShardingSphereChaosReconciler) reconcileStatus(ctx context.Context, nam
 	if err != nil {
 		return err
 	}
-	rt.Status = ssChaos.Status
+	setRtStatus(rt, ssChaos)
 	return r.Status().Update(ctx, rt)
 }
 
+func getRequirement(ssChaos *sschaosv1alpha1.ShardingSphereChaos) reconcile.InjectRequirement {
+	var jobName reconcile.InjectRequirement
+	if ssChaos.Status.Phase == sschaosv1alpha1.PhaseBeforeExperiment || ssChaos.Status.Phase == sschaosv1alpha1.PhaseAfterExperiment {
+		jobName = reconcile.Experimental
+	}
+	if ssChaos.Status.Phase == sschaosv1alpha1.PhaseInChaos {
+		jobName = reconcile.Pressure
+	}
+	if ssChaos.Status.Phase == sschaosv1alpha1.PhaseRecoveredChaos {
+		jobName = reconcile.Verify
+	}
+	return jobName
+}
+
+func getJobSuccessAndFail(conditions []batchV1.JobCondition) (bool, bool) {
+	var (
+		failed    = false
+		succeeded = false
+	)
+
+	for i := range conditions {
+		r := conditions[i]
+		if r.Type == batchV1.JobComplete {
+			if r.Status == v1.ConditionTrue {
+				succeeded = true
+			}
+			if r.Status == v1.ConditionFalse {
+				succeeded = false
+			}
+		}
+
+		if r.Type == batchV1.JobFailed {
+			if r.Status == v1.ConditionTrue {
+				failed = true
+			}
+			if r.Status == v1.ConditionFalse {
+				failed = false
+			}
+		}
+	}
+
+	return succeeded, failed
+}
+
+func setDefault(ssChaos *sschaosv1alpha1.ShardingSphereChaos) {
+	if ssChaos.Status.Phase == "" {
+		ssChaos.Status.Phase = sschaosv1alpha1.PhaseBeforeExperiment
+	}
+	if ssChaos.Status.Result == nil || len(ssChaos.Status.Result) == 0 {
+		ssChaos.Status.Result = []sschaosv1alpha1.Result{}
+	}
+}
+
+func setRtStatus(rt *sschaosv1alpha1.ShardingSphereChaos, ssChaos *sschaosv1alpha1.ShardingSphereChaos) {
+	rt.Status.Result = []sschaosv1alpha1.Result{}
+	for i := range ssChaos.Status.Result {
+		r := &ssChaos.Status.Result[i]
+		rt.Status.Result = append(rt.Status.Result, sschaosv1alpha1.Result{
+			Success: r.Success,
+			Detail: sschaosv1alpha1.Detail{
+				Time: metav1.Time{Time: time.Now()},
+				Msg:  r.Detail.Msg,
+			},
+		})
+	}
+	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 {
+	if isResultExist(rJob) {
+		return nil
+	}
+	for i := range ssChaos.Status.Result {
+		r := &ssChaos.Status.Result[i]
+		if len(r.Detail.Msg) >= len(VerifyJobCheck) && r.Detail.Msg[:len(VerifyJobCheck)] == VerifyJobCheck {
+			return nil
+		}
+	}
+	logOpts := &v1.PodLogOptions{}
+	pod, err := r.getPodHaveLog(ctx, rJob)
+	if err != nil {
+		return err
+	}
+	podNamespacedName := types.NamespacedName{
+		Namespace: pod.Namespace,
+		Name:      pod.Name,
+	}
+	succeeded, failed := getJobSuccessAndFail(rJob.Status.Conditions)
+	result := &sschaosv1alpha1.Result{}
+	if succeeded {
+		log, err := r.getPodLog(ctx, podNamespacedName, logOpts)
+		if ssChaos.Spec.Expect.Verify == "" || ssChaos.Spec.Expect.Verify == log {
+			result.Success = true
+			result.Detail = sschaosv1alpha1.Detail{
+				Time: metav1.Time{Time: time.Now()},
+				Msg:  fmt.Sprintf("%s: job succeeded", VerifyJobCheck),
+			}
+		} else {
+			if err != nil {
+				return err
+			}

Review Comment:
   move err check to where it created.



##########
shardingsphere-operator/pkg/controllers/shardingsphere_chaos_controller.go:
##########
@@ -213,10 +258,183 @@ func (r *ShardingSphereChaosReconciler) reconcileStatus(ctx context.Context, nam
 	if err != nil {
 		return err
 	}
-	rt.Status = ssChaos.Status
+	setRtStatus(rt, ssChaos)
 	return r.Status().Update(ctx, rt)
 }
 
+func getRequirement(ssChaos *sschaosv1alpha1.ShardingSphereChaos) reconcile.InjectRequirement {
+	var jobName reconcile.InjectRequirement
+	if ssChaos.Status.Phase == sschaosv1alpha1.PhaseBeforeExperiment || ssChaos.Status.Phase == sschaosv1alpha1.PhaseAfterExperiment {
+		jobName = reconcile.Experimental
+	}
+	if ssChaos.Status.Phase == sschaosv1alpha1.PhaseInChaos {
+		jobName = reconcile.Pressure
+	}
+	if ssChaos.Status.Phase == sschaosv1alpha1.PhaseRecoveredChaos {
+		jobName = reconcile.Verify
+	}
+	return jobName
+}
+
+func getJobSuccessAndFail(conditions []batchV1.JobCondition) (bool, bool) {
+	var (
+		failed    = false
+		succeeded = false
+	)
+
+	for i := range conditions {
+		r := conditions[i]
+		if r.Type == batchV1.JobComplete {
+			if r.Status == v1.ConditionTrue {
+				succeeded = true
+			}
+			if r.Status == v1.ConditionFalse {
+				succeeded = false
+			}
+		}
+
+		if r.Type == batchV1.JobFailed {
+			if r.Status == v1.ConditionTrue {
+				failed = true
+			}
+			if r.Status == v1.ConditionFalse {
+				failed = false
+			}
+		}
+	}
+
+	return succeeded, failed
+}
+
+func setDefault(ssChaos *sschaosv1alpha1.ShardingSphereChaos) {
+	if ssChaos.Status.Phase == "" {
+		ssChaos.Status.Phase = sschaosv1alpha1.PhaseBeforeExperiment
+	}
+	if ssChaos.Status.Result == nil || len(ssChaos.Status.Result) == 0 {

Review Comment:
   no need to determine `len(ssChaos.Status.Result) == 0`, coz it's equals `ssChaos.Status.Result = []sschaosv1alpha1.Result{}`
   



##########
shardingsphere-operator/pkg/controllers/shardingsphere_chaos_controller.go:
##########
@@ -325,25 +530,20 @@ func (r *ShardingSphereChaosReconciler) CreateConfigMap(ctx context.Context, cha
 }
 
 func (r *ShardingSphereChaosReconciler) updateJob(ctx context.Context, requirement reconcile.InjectRequirement, chao *sschaosv1alpha1.ShardingSphereChaos, cur *batchV1.Job) error {
-	exp, err := reconcile.UpdateJob(chao, requirement, cur)
+	isEqual, err := reconcile.JudgeJobEqual(chao, requirement, cur)

Review Comment:
   maybe `isJobChanged` is a better name 



##########
shardingsphere-operator/pkg/controllers/shardingsphere_chaos_controller.go:
##########
@@ -213,10 +258,183 @@ func (r *ShardingSphereChaosReconciler) reconcileStatus(ctx context.Context, nam
 	if err != nil {
 		return err
 	}
-	rt.Status = ssChaos.Status
+	setRtStatus(rt, ssChaos)
 	return r.Status().Update(ctx, rt)
 }
 
+func getRequirement(ssChaos *sschaosv1alpha1.ShardingSphereChaos) reconcile.InjectRequirement {
+	var jobName reconcile.InjectRequirement
+	if ssChaos.Status.Phase == sschaosv1alpha1.PhaseBeforeExperiment || ssChaos.Status.Phase == sschaosv1alpha1.PhaseAfterExperiment {
+		jobName = reconcile.Experimental
+	}
+	if ssChaos.Status.Phase == sschaosv1alpha1.PhaseInChaos {
+		jobName = reconcile.Pressure
+	}
+	if ssChaos.Status.Phase == sschaosv1alpha1.PhaseRecoveredChaos {
+		jobName = reconcile.Verify
+	}
+	return jobName
+}
+
+func getJobSuccessAndFail(conditions []batchV1.JobCondition) (bool, bool) {
+	var (
+		failed    = false
+		succeeded = false
+	)
+
+	for i := range conditions {
+		r := conditions[i]
+		if r.Type == batchV1.JobComplete {
+			if r.Status == v1.ConditionTrue {
+				succeeded = true
+			}
+			if r.Status == v1.ConditionFalse {
+				succeeded = false
+			}
+		}
+
+		if r.Type == batchV1.JobFailed {
+			if r.Status == v1.ConditionTrue {
+				failed = true
+			}
+			if r.Status == v1.ConditionFalse {
+				failed = false
+			}
+		}
+	}
+
+	return succeeded, failed
+}
+
+func setDefault(ssChaos *sschaosv1alpha1.ShardingSphereChaos) {
+	if ssChaos.Status.Phase == "" {
+		ssChaos.Status.Phase = sschaosv1alpha1.PhaseBeforeExperiment
+	}
+	if ssChaos.Status.Result == nil || len(ssChaos.Status.Result) == 0 {
+		ssChaos.Status.Result = []sschaosv1alpha1.Result{}
+	}
+}
+
+func setRtStatus(rt *sschaosv1alpha1.ShardingSphereChaos, ssChaos *sschaosv1alpha1.ShardingSphereChaos) {
+	rt.Status.Result = []sschaosv1alpha1.Result{}
+	for i := range ssChaos.Status.Result {
+		r := &ssChaos.Status.Result[i]
+		rt.Status.Result = append(rt.Status.Result, sschaosv1alpha1.Result{
+			Success: r.Success,
+			Detail: sschaosv1alpha1.Detail{
+				Time: metav1.Time{Time: time.Now()},
+				Msg:  r.Detail.Msg,
+			},
+		})
+	}
+	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 {
+	if isResultExist(rJob) {
+		return nil
+	}
+	for i := range ssChaos.Status.Result {
+		r := &ssChaos.Status.Result[i]
+		if len(r.Detail.Msg) >= len(VerifyJobCheck) && r.Detail.Msg[:len(VerifyJobCheck)] == VerifyJobCheck {
+			return nil
+		}
+	}
+	logOpts := &v1.PodLogOptions{}
+	pod, err := r.getPodHaveLog(ctx, rJob)
+	if err != nil {
+		return err
+	}
+	podNamespacedName := types.NamespacedName{
+		Namespace: pod.Namespace,
+		Name:      pod.Name,
+	}
+	succeeded, failed := getJobSuccessAndFail(rJob.Status.Conditions)
+	result := &sschaosv1alpha1.Result{}
+	if succeeded {
+		log, err := r.getPodLog(ctx, podNamespacedName, logOpts)
+		if ssChaos.Spec.Expect.Verify == "" || ssChaos.Spec.Expect.Verify == log {
+			result.Success = true
+			result.Detail = sschaosv1alpha1.Detail{
+				Time: metav1.Time{Time: time.Now()},
+				Msg:  fmt.Sprintf("%s: job succeeded", VerifyJobCheck),
+			}
+		} else {
+			if err != nil {
+				return err
+			}
+			result.Success = false
+			result.Detail = sschaosv1alpha1.Detail{
+				Time: metav1.Time{Time: time.Now()},
+				Msg:  fmt.Sprintf("%s: %s", VerifyJobCheck, log),
+			}
+		}
+	}
+
+	if failed {
+		log, err := r.getPodLog(ctx, podNamespacedName, logOpts)
+		if err != nil {
+			return err
+		}
+		result.Success = false
+		result.Detail = sschaosv1alpha1.Detail{
+			Time: metav1.Time{Time: time.Now()},
+			Msg:  fmt.Sprintf("%s: %s", VerifyJobCheck, log),
+		}
+	}
+	ssChaos.Status.Result = updateResult(ssChaos.Status.Result, *result, VerifyJobCheck)
+	return nil
+}
+
+func (r *ShardingSphereChaosReconciler) getPodHaveLog(ctx context.Context, rJob *batchV1.Job) (*v1.Pod, error) {
+	pods := &v1.PodList{}
+	if err := r.List(ctx, pods, client.MatchingLabels{"controller-uid": rJob.Spec.Template.Labels["controller-uid"]}); err != nil {
+		return nil, err
+	}
+	var pod *v1.Pod
+	for i := range pods.Items {
+		pod = &pods.Items[i]
+		break
+	}

Review Comment:
   check if  `pods.Item`  is empty first.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [shardingsphere-on-cloud] sonarcloud[bot] commented on pull request #316: feat: add verify Spec && update reconcile logic

Posted by "sonarcloud[bot] (via GitHub)" <gi...@apache.org>.
sonarcloud[bot] commented on PR #316:
URL: https://github.com/apache/shardingsphere-on-cloud/pull/316#issuecomment-1519040919

   Kudos, SonarCloud Quality Gate passed!&nbsp; &nbsp; [![Quality Gate passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/passed-16px.png 'Quality Gate passed')](https://sonarcloud.io/dashboard?id=apache_shardingsphere-on-cloud&pullRequest=316)
   
   [![Bug](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/bug-16px.png 'Bug')](https://sonarcloud.io/project/issues?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=BUG) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=BUG) [0 Bugs](https://sonarcloud.io/project/issues?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=BUG)  
   [![Vulnerability](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/vulnerability-16px.png 'Vulnerability')](https://sonarcloud.io/project/issues?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=VULNERABILITY) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=VULNERABILITY)  
   [![Security Hotspot](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/security_hotspot-16px.png 'Security Hotspot')](https://sonarcloud.io/project/security_hotspots?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=SECURITY_HOTSPOT) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/security_hotspots?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=SECURITY_HOTSPOT)  
   [![Code Smell](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/code_smell-16px.png 'Code Smell')](https://sonarcloud.io/project/issues?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=CODE_SMELL) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=CODE_SMELL) [0 Code Smells](https://sonarcloud.io/project/issues?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=CODE_SMELL)
   
   [![No Coverage information](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/CoverageChart/NoCoverageInfo-16px.png 'No Coverage information')](https://sonarcloud.io/component_measures?id=apache_shardingsphere-on-cloud&pullRequest=316) No Coverage information  
   [![No Duplication information](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/Duplications/NoDuplicationInfo-16px.png 'No Duplication information')](https://sonarcloud.io/component_measures?id=apache_shardingsphere-on-cloud&pullRequest=316&metric=duplicated_lines_density&view=list) No Duplication information
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [shardingsphere-on-cloud] moomman commented on a diff in pull request #316: feat: add verify Spec && update reconcile logic

Posted by "moomman (via GitHub)" <gi...@apache.org>.
moomman commented on code in PR #316:
URL: https://github.com/apache/shardingsphere-on-cloud/pull/316#discussion_r1173503988


##########
shardingsphere-operator/pkg/controllers/shardingsphere_chaos_controller.go:
##########
@@ -178,33 +190,54 @@ func (r *ShardingSphereChaosReconciler) reconcileJob(ctx context.Context, ssChao
 	if ssChaos.Status.Phase == "" || ssChaos.Status.Phase == sschaosv1alpha1.PhaseBeforeExperiment || ssChaos.Status.Phase == sschaosv1alpha1.PhaseAfterExperiment {
 		nowInjectRequirement = reconcile.Experimental
 	}
-	if ssChaos.Status.Phase == sschaosv1alpha1.PhaseInChaos || ssChaos.Status.Phase == sschaosv1alpha1.PhaseRecoveredChaos {
+	if ssChaos.Status.Phase == sschaosv1alpha1.PhaseInChaos {
 		nowInjectRequirement = reconcile.Pressure
 	}
+	if ssChaos.Status.Phase == sschaosv1alpha1.PhaseRecoveredChaos {
+		nowInjectRequirement = reconcile.Verify
+	}
 	if isExist {
 		return r.updateJob(ctx, nowInjectRequirement, ssChaos, rJob)
 	}
 
-	return r.createJob(ctx, nowInjectRequirement, ssChaos)
+	err = r.createJob(ctx, nowInjectRequirement, ssChaos)
+	if err != nil {
+		r.Events.Event(ssChaos, "Warning", "Created", fmt.Sprintf("job created fail %s", err))
+		return err
+	}
+
+	r.Events.Event(ssChaos, "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
 	}
-	if ssChaos.Status.Phase == "" {
-		ssChaos.Status.Phase = sschaosv1alpha1.PhaseBeforeExperiment
-	}
-	rJob := &batchV1.Job{}
-	if err := r.Get(ctx, namespacedName, rJob); err != nil {
+
+	SetDefault(ssChaos)
+
+	rJob, found, err := r.getJobByNamespacedName(ctx, namespacedName)
+	if err != nil || !found || rJob == nil {
 		return err
 	}
 
 	if ssChaos.Status.Phase == sschaosv1alpha1.PhaseBeforeExperiment && rJob.Status.Succeeded == 1 {
 		ssChaos.Status.Phase = sschaosv1alpha1.PhaseAfterExperiment
 	}
 
+	if rJob.Status.Failed == 1 {

Review Comment:
   rJob type is k8s batchV1.job,I can not change it



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [shardingsphere-on-cloud] sonarcloud[bot] commented on pull request #316: feat: add verify Spec && update reconcile logic

Posted by "sonarcloud[bot] (via GitHub)" <gi...@apache.org>.
sonarcloud[bot] commented on PR #316:
URL: https://github.com/apache/shardingsphere-on-cloud/pull/316#issuecomment-1519599241

   Kudos, SonarCloud Quality Gate passed!&nbsp; &nbsp; [![Quality Gate passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/passed-16px.png 'Quality Gate passed')](https://sonarcloud.io/dashboard?id=apache_shardingsphere-on-cloud&pullRequest=316)
   
   [![Bug](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/bug-16px.png 'Bug')](https://sonarcloud.io/project/issues?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=BUG) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=BUG) [0 Bugs](https://sonarcloud.io/project/issues?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=BUG)  
   [![Vulnerability](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/vulnerability-16px.png 'Vulnerability')](https://sonarcloud.io/project/issues?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=VULNERABILITY) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=VULNERABILITY)  
   [![Security Hotspot](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/security_hotspot-16px.png 'Security Hotspot')](https://sonarcloud.io/project/security_hotspots?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=SECURITY_HOTSPOT) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/security_hotspots?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=SECURITY_HOTSPOT)  
   [![Code Smell](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/code_smell-16px.png 'Code Smell')](https://sonarcloud.io/project/issues?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=CODE_SMELL) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=CODE_SMELL) [0 Code Smells](https://sonarcloud.io/project/issues?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=CODE_SMELL)
   
   [![No Coverage information](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/CoverageChart/NoCoverageInfo-16px.png 'No Coverage information')](https://sonarcloud.io/component_measures?id=apache_shardingsphere-on-cloud&pullRequest=316) No Coverage information  
   [![No Duplication information](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/Duplications/NoDuplicationInfo-16px.png 'No Duplication information')](https://sonarcloud.io/component_measures?id=apache_shardingsphere-on-cloud&pullRequest=316&metric=duplicated_lines_density&view=list) No Duplication information
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [shardingsphere-on-cloud] moomman commented on a diff in pull request #316: feat: add verify Spec && update reconcile logic

Posted by "moomman (via GitHub)" <gi...@apache.org>.
moomman commented on code in PR #316:
URL: https://github.com/apache/shardingsphere-on-cloud/pull/316#discussion_r1173508309


##########
shardingsphere-operator/pkg/controllers/shardingsphere_chaos_controller.go:
##########
@@ -213,10 +246,137 @@ func (r *ShardingSphereChaosReconciler) reconcileStatus(ctx context.Context, nam
 	if err != nil {
 		return err
 	}
-	rt.Status = ssChaos.Status
+	setRtStatus(rt, ssChaos)
 	return r.Status().Update(ctx, rt)
 }
 
+func SetDefault(ssChaos *sschaosv1alpha1.ShardingSphereChaos) {
+	if ssChaos.Status.Phase == "" {
+		ssChaos.Status.Phase = sschaosv1alpha1.PhaseBeforeExperiment
+	}
+	if len(ssChaos.Status.Result) <= 0 || ssChaos.Status.Result == nil {

Review Comment:
   solved



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [shardingsphere-on-cloud] moomman commented on a diff in pull request #316: feat: add verify Spec && update reconcile logic

Posted by "moomman (via GitHub)" <gi...@apache.org>.
moomman commented on code in PR #316:
URL: https://github.com/apache/shardingsphere-on-cloud/pull/316#discussion_r1173512833


##########
shardingsphere-operator/pkg/controllers/shardingsphere_chaos_controller.go:
##########
@@ -213,10 +246,137 @@ func (r *ShardingSphereChaosReconciler) reconcileStatus(ctx context.Context, nam
 	if err != nil {
 		return err
 	}
-	rt.Status = ssChaos.Status
+	setRtStatus(rt, ssChaos)
 	return r.Status().Update(ctx, rt)
 }
 
+func SetDefault(ssChaos *sschaosv1alpha1.ShardingSphereChaos) {
+	if ssChaos.Status.Phase == "" {
+		ssChaos.Status.Phase = sschaosv1alpha1.PhaseBeforeExperiment
+	}
+	if len(ssChaos.Status.Result) <= 0 || ssChaos.Status.Result == nil {
+		ssChaos.Status.Result = []sschaosv1alpha1.Result{}
+	}
+}
+
+func setRtStatus(rt *sschaosv1alpha1.ShardingSphereChaos, ssChaos *sschaosv1alpha1.ShardingSphereChaos) {
+	rt.Status.Result = []sschaosv1alpha1.Result{}
+	for i := range ssChaos.Status.Result {
+		rt.Status.Result = append(rt.Status.Result, sschaosv1alpha1.Result{
+			Success: ssChaos.Status.Result[i].Success,
+			Details: sschaosv1alpha1.Details{
+				Time: metav1.Time{Time: time.Now()},
+				Msg:  ssChaos.Status.Result[i].Details.Msg,
+			},
+		})
+	}
+	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 {
+	if checkResultExist(rJob) {

Review Comment:
   solved



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [shardingsphere-on-cloud] moomman commented on a diff in pull request #316: feat: add verify Spec && update reconcile logic

Posted by "moomman (via GitHub)" <gi...@apache.org>.
moomman commented on code in PR #316:
URL: https://github.com/apache/shardingsphere-on-cloud/pull/316#discussion_r1173520996


##########
shardingsphere-operator/pkg/controllers/shardingsphere_chaos_controller.go:
##########
@@ -213,10 +246,137 @@ func (r *ShardingSphereChaosReconciler) reconcileStatus(ctx context.Context, nam
 	if err != nil {
 		return err
 	}
-	rt.Status = ssChaos.Status
+	setRtStatus(rt, ssChaos)
 	return r.Status().Update(ctx, rt)
 }
 
+func SetDefault(ssChaos *sschaosv1alpha1.ShardingSphereChaos) {
+	if ssChaos.Status.Phase == "" {
+		ssChaos.Status.Phase = sschaosv1alpha1.PhaseBeforeExperiment
+	}
+	if len(ssChaos.Status.Result) <= 0 || ssChaos.Status.Result == nil {
+		ssChaos.Status.Result = []sschaosv1alpha1.Result{}
+	}
+}
+
+func setRtStatus(rt *sschaosv1alpha1.ShardingSphereChaos, ssChaos *sschaosv1alpha1.ShardingSphereChaos) {
+	rt.Status.Result = []sschaosv1alpha1.Result{}
+	for i := range ssChaos.Status.Result {
+		rt.Status.Result = append(rt.Status.Result, sschaosv1alpha1.Result{
+			Success: ssChaos.Status.Result[i].Success,
+			Details: sschaosv1alpha1.Details{
+				Time: metav1.Time{Time: time.Now()},
+				Msg:  ssChaos.Status.Result[i].Details.Msg,
+			},
+		})
+	}
+	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 {
+	if checkResultExist(rJob) {
+		return nil
+	}
+	check := "Verify"
+	for i := range ssChaos.Status.Result {
+		if len(ssChaos.Status.Result[i].Details.Msg) >= len(check) && ssChaos.Status.Result[i].Details.Msg[:len(check)] == check {
+			return nil
+		}
+	}
+	logOpts := &v1.PodLogOptions{}
+	pod, err := r.getPodHaveLog(ctx, rJob)
+	if err != nil {
+		return err
+	}
+	containerNamespacedname := types.NamespacedName{
+		Namespace: pod.Namespace,
+		Name:      pod.Name,
+	}
+	result := &sschaosv1alpha1.Result{}
+	if rJob.Status.Succeeded == 1 {
+		log, err := r.getPodLog(ctx, containerNamespacedname, logOpts)
+		if ssChaos.Spec.Expect.Verify == "" || ssChaos.Spec.Expect.Verify == log {
+			result.Success = true

Review Comment:
   i can not change rJob 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [shardingsphere-on-cloud] moomman commented on a diff in pull request #316: feat: add verify Spec && update reconcile logic

Posted by "moomman (via GitHub)" <gi...@apache.org>.
moomman commented on code in PR #316:
URL: https://github.com/apache/shardingsphere-on-cloud/pull/316#discussion_r1173514757


##########
shardingsphere-operator/pkg/controllers/shardingsphere_chaos_controller.go:
##########
@@ -213,10 +246,137 @@ func (r *ShardingSphereChaosReconciler) reconcileStatus(ctx context.Context, nam
 	if err != nil {
 		return err
 	}
-	rt.Status = ssChaos.Status
+	setRtStatus(rt, ssChaos)
 	return r.Status().Update(ctx, rt)
 }
 
+func SetDefault(ssChaos *sschaosv1alpha1.ShardingSphereChaos) {
+	if ssChaos.Status.Phase == "" {
+		ssChaos.Status.Phase = sschaosv1alpha1.PhaseBeforeExperiment
+	}
+	if len(ssChaos.Status.Result) <= 0 || ssChaos.Status.Result == nil {
+		ssChaos.Status.Result = []sschaosv1alpha1.Result{}
+	}
+}
+
+func setRtStatus(rt *sschaosv1alpha1.ShardingSphereChaos, ssChaos *sschaosv1alpha1.ShardingSphereChaos) {
+	rt.Status.Result = []sschaosv1alpha1.Result{}
+	for i := range ssChaos.Status.Result {
+		rt.Status.Result = append(rt.Status.Result, sschaosv1alpha1.Result{
+			Success: ssChaos.Status.Result[i].Success,
+			Details: sschaosv1alpha1.Details{
+				Time: metav1.Time{Time: time.Now()},
+				Msg:  ssChaos.Status.Result[i].Details.Msg,
+			},
+		})
+	}
+	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 {
+	if checkResultExist(rJob) {
+		return nil
+	}
+	check := "Verify"
+	for i := range ssChaos.Status.Result {
+		if len(ssChaos.Status.Result[i].Details.Msg) >= len(check) && ssChaos.Status.Result[i].Details.Msg[:len(check)] == check {
+			return nil
+		}
+	}
+	logOpts := &v1.PodLogOptions{}
+	pod, err := r.getPodHaveLog(ctx, rJob)
+	if err != nil {
+		return err
+	}
+	containerNamespacedname := types.NamespacedName{

Review Comment:
   yes,i will change it



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [shardingsphere-on-cloud] sonarcloud[bot] commented on pull request #316: feat: add verify Spec && update reconcile logic

Posted by "sonarcloud[bot] (via GitHub)" <gi...@apache.org>.
sonarcloud[bot] commented on PR #316:
URL: https://github.com/apache/shardingsphere-on-cloud/pull/316#issuecomment-1519015352

   Kudos, SonarCloud Quality Gate passed!&nbsp; &nbsp; [![Quality Gate passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/passed-16px.png 'Quality Gate passed')](https://sonarcloud.io/dashboard?id=apache_shardingsphere-on-cloud&pullRequest=316)
   
   [![Bug](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/bug-16px.png 'Bug')](https://sonarcloud.io/project/issues?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=BUG) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=BUG) [0 Bugs](https://sonarcloud.io/project/issues?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=BUG)  
   [![Vulnerability](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/vulnerability-16px.png 'Vulnerability')](https://sonarcloud.io/project/issues?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=VULNERABILITY) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=VULNERABILITY)  
   [![Security Hotspot](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/security_hotspot-16px.png 'Security Hotspot')](https://sonarcloud.io/project/security_hotspots?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=SECURITY_HOTSPOT) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/security_hotspots?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=SECURITY_HOTSPOT)  
   [![Code Smell](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/code_smell-16px.png 'Code Smell')](https://sonarcloud.io/project/issues?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=CODE_SMELL) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=CODE_SMELL) [0 Code Smells](https://sonarcloud.io/project/issues?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=CODE_SMELL)
   
   [![No Coverage information](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/CoverageChart/NoCoverageInfo-16px.png 'No Coverage information')](https://sonarcloud.io/component_measures?id=apache_shardingsphere-on-cloud&pullRequest=316) No Coverage information  
   [![No Duplication information](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/Duplications/NoDuplicationInfo-16px.png 'No Duplication information')](https://sonarcloud.io/component_measures?id=apache_shardingsphere-on-cloud&pullRequest=316&metric=duplicated_lines_density&view=list) No Duplication information
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [shardingsphere-on-cloud] mlycore merged pull request #316: feat: add verify Spec && update reconcile logic

Posted by "mlycore (via GitHub)" <gi...@apache.org>.
mlycore merged PR #316:
URL: https://github.com/apache/shardingsphere-on-cloud/pull/316


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [shardingsphere-on-cloud] sonarcloud[bot] commented on pull request #316: feat: add verify Spec && update reconcile logic

Posted by "sonarcloud[bot] (via GitHub)" <gi...@apache.org>.
sonarcloud[bot] commented on PR #316:
URL: https://github.com/apache/shardingsphere-on-cloud/pull/316#issuecomment-1518914913

   Kudos, SonarCloud Quality Gate passed!&nbsp; &nbsp; [![Quality Gate passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/passed-16px.png 'Quality Gate passed')](https://sonarcloud.io/dashboard?id=apache_shardingsphere-on-cloud&pullRequest=316)
   
   [![Bug](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/bug-16px.png 'Bug')](https://sonarcloud.io/project/issues?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=BUG) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=BUG) [0 Bugs](https://sonarcloud.io/project/issues?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=BUG)  
   [![Vulnerability](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/vulnerability-16px.png 'Vulnerability')](https://sonarcloud.io/project/issues?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=VULNERABILITY) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=VULNERABILITY)  
   [![Security Hotspot](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/security_hotspot-16px.png 'Security Hotspot')](https://sonarcloud.io/project/security_hotspots?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=SECURITY_HOTSPOT) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/security_hotspots?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=SECURITY_HOTSPOT)  
   [![Code Smell](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/code_smell-16px.png 'Code Smell')](https://sonarcloud.io/project/issues?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=CODE_SMELL) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=CODE_SMELL) [0 Code Smells](https://sonarcloud.io/project/issues?id=apache_shardingsphere-on-cloud&pullRequest=316&resolved=false&types=CODE_SMELL)
   
   [![No Coverage information](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/CoverageChart/NoCoverageInfo-16px.png 'No Coverage information')](https://sonarcloud.io/component_measures?id=apache_shardingsphere-on-cloud&pullRequest=316) No Coverage information  
   [![No Duplication information](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/Duplications/NoDuplicationInfo-16px.png 'No Duplication information')](https://sonarcloud.io/component_measures?id=apache_shardingsphere-on-cloud&pullRequest=316&metric=duplicated_lines_density&view=list) No Duplication information
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [shardingsphere-on-cloud] Xu-Wentao commented on a diff in pull request #316: feat: add verify Spec && update reconcile logic

Posted by "Xu-Wentao (via GitHub)" <gi...@apache.org>.
Xu-Wentao commented on code in PR #316:
URL: https://github.com/apache/shardingsphere-on-cloud/pull/316#discussion_r1173425900


##########
shardingsphere-operator/pkg/controllers/shardingsphere_chaos_controller.go:
##########
@@ -178,33 +190,54 @@ func (r *ShardingSphereChaosReconciler) reconcileJob(ctx context.Context, ssChao
 	if ssChaos.Status.Phase == "" || ssChaos.Status.Phase == sschaosv1alpha1.PhaseBeforeExperiment || ssChaos.Status.Phase == sschaosv1alpha1.PhaseAfterExperiment {
 		nowInjectRequirement = reconcile.Experimental
 	}
-	if ssChaos.Status.Phase == sschaosv1alpha1.PhaseInChaos || ssChaos.Status.Phase == sschaosv1alpha1.PhaseRecoveredChaos {
+	if ssChaos.Status.Phase == sschaosv1alpha1.PhaseInChaos {
 		nowInjectRequirement = reconcile.Pressure
 	}
+	if ssChaos.Status.Phase == sschaosv1alpha1.PhaseRecoveredChaos {
+		nowInjectRequirement = reconcile.Verify
+	}
 	if isExist {
 		return r.updateJob(ctx, nowInjectRequirement, ssChaos, rJob)
 	}
 
-	return r.createJob(ctx, nowInjectRequirement, ssChaos)
+	err = r.createJob(ctx, nowInjectRequirement, ssChaos)
+	if err != nil {
+		r.Events.Event(ssChaos, "Warning", "Created", fmt.Sprintf("job created fail %s", err))
+		return err
+	}
+
+	r.Events.Event(ssChaos, "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
 	}
-	if ssChaos.Status.Phase == "" {
-		ssChaos.Status.Phase = sschaosv1alpha1.PhaseBeforeExperiment
-	}
-	rJob := &batchV1.Job{}
-	if err := r.Get(ctx, namespacedName, rJob); err != nil {
+
+	SetDefault(ssChaos)
+
+	rJob, found, err := r.getJobByNamespacedName(ctx, namespacedName)
+	if err != nil || !found || rJob == nil {

Review Comment:
   1. recommend to use `rJob, err` as return values.  if `err == nil && rJob == nil` equals ` !found`.
   2. if  `err == nil` and `!found` this  will returns `nil` err, make sure this is want you want to returns.



##########
shardingsphere-operator/pkg/controllers/shardingsphere_chaos_controller.go:
##########
@@ -213,10 +246,137 @@ func (r *ShardingSphereChaosReconciler) reconcileStatus(ctx context.Context, nam
 	if err != nil {
 		return err
 	}
-	rt.Status = ssChaos.Status
+	setRtStatus(rt, ssChaos)
 	return r.Status().Update(ctx, rt)
 }
 
+func SetDefault(ssChaos *sschaosv1alpha1.ShardingSphereChaos) {

Review Comment:
   renamed `SetDefault` to `setDefault` if this function will not be called out of package.



##########
shardingsphere-operator/pkg/controllers/shardingsphere_chaos_controller.go:
##########
@@ -213,10 +246,137 @@ func (r *ShardingSphereChaosReconciler) reconcileStatus(ctx context.Context, nam
 	if err != nil {
 		return err
 	}
-	rt.Status = ssChaos.Status
+	setRtStatus(rt, ssChaos)
 	return r.Status().Update(ctx, rt)
 }
 
+func SetDefault(ssChaos *sschaosv1alpha1.ShardingSphereChaos) {
+	if ssChaos.Status.Phase == "" {
+		ssChaos.Status.Phase = sschaosv1alpha1.PhaseBeforeExperiment
+	}
+	if len(ssChaos.Status.Result) <= 0 || ssChaos.Status.Result == nil {
+		ssChaos.Status.Result = []sschaosv1alpha1.Result{}
+	}
+}
+
+func setRtStatus(rt *sschaosv1alpha1.ShardingSphereChaos, ssChaos *sschaosv1alpha1.ShardingSphereChaos) {
+	rt.Status.Result = []sschaosv1alpha1.Result{}
+	for i := range ssChaos.Status.Result {
+		rt.Status.Result = append(rt.Status.Result, sschaosv1alpha1.Result{
+			Success: ssChaos.Status.Result[i].Success,
+			Details: sschaosv1alpha1.Details{
+				Time: metav1.Time{Time: time.Now()},
+				Msg:  ssChaos.Status.Result[i].Details.Msg,
+			},
+		})
+	}
+	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 {
+	if checkResultExist(rJob) {

Review Comment:
   `isResultExist` is more readable for the func, coz you want to get a boolean value.



##########
shardingsphere-operator/pkg/controllers/shardingsphere_chaos_controller.go:
##########
@@ -213,10 +246,137 @@ func (r *ShardingSphereChaosReconciler) reconcileStatus(ctx context.Context, nam
 	if err != nil {
 		return err
 	}
-	rt.Status = ssChaos.Status
+	setRtStatus(rt, ssChaos)
 	return r.Status().Update(ctx, rt)
 }
 
+func SetDefault(ssChaos *sschaosv1alpha1.ShardingSphereChaos) {
+	if ssChaos.Status.Phase == "" {
+		ssChaos.Status.Phase = sschaosv1alpha1.PhaseBeforeExperiment
+	}
+	if len(ssChaos.Status.Result) <= 0 || ssChaos.Status.Result == nil {
+		ssChaos.Status.Result = []sschaosv1alpha1.Result{}
+	}
+}
+
+func setRtStatus(rt *sschaosv1alpha1.ShardingSphereChaos, ssChaos *sschaosv1alpha1.ShardingSphereChaos) {
+	rt.Status.Result = []sschaosv1alpha1.Result{}
+	for i := range ssChaos.Status.Result {
+		rt.Status.Result = append(rt.Status.Result, sschaosv1alpha1.Result{
+			Success: ssChaos.Status.Result[i].Success,
+			Details: sschaosv1alpha1.Details{
+				Time: metav1.Time{Time: time.Now()},
+				Msg:  ssChaos.Status.Result[i].Details.Msg,
+			},
+		})
+	}
+	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 {
+	if checkResultExist(rJob) {
+		return nil
+	}
+	check := "Verify"
+	for i := range ssChaos.Status.Result {
+		if len(ssChaos.Status.Result[i].Details.Msg) >= len(check) && ssChaos.Status.Result[i].Details.Msg[:len(check)] == check {

Review Comment:
   use a pointer is a better choice.



##########
shardingsphere-operator/pkg/controllers/shardingsphere_chaos_controller.go:
##########
@@ -213,10 +246,137 @@ func (r *ShardingSphereChaosReconciler) reconcileStatus(ctx context.Context, nam
 	if err != nil {
 		return err
 	}
-	rt.Status = ssChaos.Status
+	setRtStatus(rt, ssChaos)
 	return r.Status().Update(ctx, rt)
 }
 
+func SetDefault(ssChaos *sschaosv1alpha1.ShardingSphereChaos) {
+	if ssChaos.Status.Phase == "" {
+		ssChaos.Status.Phase = sschaosv1alpha1.PhaseBeforeExperiment
+	}
+	if len(ssChaos.Status.Result) <= 0 || ssChaos.Status.Result == nil {
+		ssChaos.Status.Result = []sschaosv1alpha1.Result{}
+	}
+}
+
+func setRtStatus(rt *sschaosv1alpha1.ShardingSphereChaos, ssChaos *sschaosv1alpha1.ShardingSphereChaos) {
+	rt.Status.Result = []sschaosv1alpha1.Result{}
+	for i := range ssChaos.Status.Result {
+		rt.Status.Result = append(rt.Status.Result, sschaosv1alpha1.Result{
+			Success: ssChaos.Status.Result[i].Success,
+			Details: sschaosv1alpha1.Details{
+				Time: metav1.Time{Time: time.Now()},
+				Msg:  ssChaos.Status.Result[i].Details.Msg,
+			},
+		})
+	}
+	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 {
+	if checkResultExist(rJob) {
+		return nil
+	}
+	check := "Verify"
+	for i := range ssChaos.Status.Result {
+		if len(ssChaos.Status.Result[i].Details.Msg) >= len(check) && ssChaos.Status.Result[i].Details.Msg[:len(check)] == check {
+			return nil
+		}
+	}
+	logOpts := &v1.PodLogOptions{}
+	pod, err := r.getPodHaveLog(ctx, rJob)
+	if err != nil {
+		return err
+	}
+	containerNamespacedname := types.NamespacedName{
+		Namespace: pod.Namespace,
+		Name:      pod.Name,
+	}
+	result := &sschaosv1alpha1.Result{}
+	if rJob.Status.Succeeded == 1 {
+		log, err := r.getPodLog(ctx, containerNamespacedname, logOpts)
+		if ssChaos.Spec.Expect.Verify == "" || ssChaos.Spec.Expect.Verify == log {
+			result.Success = true

Review Comment:
   `rJob.Status.Succeeded == 1` `result.Success = true` 
   unify format if is possible



##########
shardingsphere-operator/pkg/controllers/shardingsphere_chaos_controller.go:
##########
@@ -213,10 +246,137 @@ func (r *ShardingSphereChaosReconciler) reconcileStatus(ctx context.Context, nam
 	if err != nil {
 		return err
 	}
-	rt.Status = ssChaos.Status
+	setRtStatus(rt, ssChaos)
 	return r.Status().Update(ctx, rt)
 }
 
+func SetDefault(ssChaos *sschaosv1alpha1.ShardingSphereChaos) {
+	if ssChaos.Status.Phase == "" {
+		ssChaos.Status.Phase = sschaosv1alpha1.PhaseBeforeExperiment
+	}
+	if len(ssChaos.Status.Result) <= 0 || ssChaos.Status.Result == nil {
+		ssChaos.Status.Result = []sschaosv1alpha1.Result{}
+	}
+}
+
+func setRtStatus(rt *sschaosv1alpha1.ShardingSphereChaos, ssChaos *sschaosv1alpha1.ShardingSphereChaos) {
+	rt.Status.Result = []sschaosv1alpha1.Result{}
+	for i := range ssChaos.Status.Result {
+		rt.Status.Result = append(rt.Status.Result, sschaosv1alpha1.Result{
+			Success: ssChaos.Status.Result[i].Success,
+			Details: sschaosv1alpha1.Details{
+				Time: metav1.Time{Time: time.Now()},
+				Msg:  ssChaos.Status.Result[i].Details.Msg,
+			},
+		})
+	}
+	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 {
+	if checkResultExist(rJob) {
+		return nil
+	}
+	check := "Verify"
+	for i := range ssChaos.Status.Result {
+		if len(ssChaos.Status.Result[i].Details.Msg) >= len(check) && ssChaos.Status.Result[i].Details.Msg[:len(check)] == check {
+			return nil
+		}
+	}
+	logOpts := &v1.PodLogOptions{}
+	pod, err := r.getPodHaveLog(ctx, rJob)
+	if err != nil {
+		return err
+	}
+	containerNamespacedname := types.NamespacedName{

Review Comment:
   you mean `podNamespacedName` ?



##########
shardingsphere-operator/pkg/controllers/shardingsphere_chaos_controller.go:
##########
@@ -330,20 +490,14 @@ func (r *ShardingSphereChaosReconciler) updateJob(ctx context.Context, requireme
 		return err
 	}
 	if exp != nil {
-		if err := r.Delete(ctx, cur); err != nil {
-			return err
-		}
-		if err := ctrl.SetControllerReference(chao, exp, r.Scheme); err != nil {
-			return err
-		}
-		if err := r.Create(ctx, exp); err != nil {
+		if err := r.Delete(ctx, cur); err != nil && !apierrors.IsNotFound(err) {

Review Comment:
   i saw that `reconcile.UpdateJob` will return nil, nil if job no need to update, it is as your expect? the name of `UpdateJob` makes me confused.



##########
shardingsphere-operator/pkg/controllers/shardingsphere_chaos_controller.go:
##########
@@ -213,10 +246,137 @@ func (r *ShardingSphereChaosReconciler) reconcileStatus(ctx context.Context, nam
 	if err != nil {
 		return err
 	}
-	rt.Status = ssChaos.Status
+	setRtStatus(rt, ssChaos)
 	return r.Status().Update(ctx, rt)
 }
 
+func SetDefault(ssChaos *sschaosv1alpha1.ShardingSphereChaos) {
+	if ssChaos.Status.Phase == "" {
+		ssChaos.Status.Phase = sschaosv1alpha1.PhaseBeforeExperiment
+	}
+	if len(ssChaos.Status.Result) <= 0 || ssChaos.Status.Result == nil {
+		ssChaos.Status.Result = []sschaosv1alpha1.Result{}
+	}
+}
+
+func setRtStatus(rt *sschaosv1alpha1.ShardingSphereChaos, ssChaos *sschaosv1alpha1.ShardingSphereChaos) {
+	rt.Status.Result = []sschaosv1alpha1.Result{}
+	for i := range ssChaos.Status.Result {
+		rt.Status.Result = append(rt.Status.Result, sschaosv1alpha1.Result{

Review Comment:
   use pointer is more safe and efficient `r := &ssChaos.Status.Result[i]` 



##########
shardingsphere-operator/pkg/controllers/shardingsphere_chaos_controller.go:
##########
@@ -178,33 +190,54 @@ func (r *ShardingSphereChaosReconciler) reconcileJob(ctx context.Context, ssChao
 	if ssChaos.Status.Phase == "" || ssChaos.Status.Phase == sschaosv1alpha1.PhaseBeforeExperiment || ssChaos.Status.Phase == sschaosv1alpha1.PhaseAfterExperiment {
 		nowInjectRequirement = reconcile.Experimental
 	}
-	if ssChaos.Status.Phase == sschaosv1alpha1.PhaseInChaos || ssChaos.Status.Phase == sschaosv1alpha1.PhaseRecoveredChaos {
+	if ssChaos.Status.Phase == sschaosv1alpha1.PhaseInChaos {
 		nowInjectRequirement = reconcile.Pressure
 	}
+	if ssChaos.Status.Phase == sschaosv1alpha1.PhaseRecoveredChaos {
+		nowInjectRequirement = reconcile.Verify
+	}
 	if isExist {
 		return r.updateJob(ctx, nowInjectRequirement, ssChaos, rJob)
 	}
 
-	return r.createJob(ctx, nowInjectRequirement, ssChaos)
+	err = r.createJob(ctx, nowInjectRequirement, ssChaos)
+	if err != nil {
+		r.Events.Event(ssChaos, "Warning", "Created", fmt.Sprintf("job created fail %s", err))
+		return err
+	}
+
+	r.Events.Event(ssChaos, "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
 	}
-	if ssChaos.Status.Phase == "" {
-		ssChaos.Status.Phase = sschaosv1alpha1.PhaseBeforeExperiment
-	}
-	rJob := &batchV1.Job{}
-	if err := r.Get(ctx, namespacedName, rJob); err != nil {
+
+	SetDefault(ssChaos)
+
+	rJob, found, err := r.getJobByNamespacedName(ctx, namespacedName)
+	if err != nil || !found || rJob == nil {
 		return err
 	}
 
 	if ssChaos.Status.Phase == sschaosv1alpha1.PhaseBeforeExperiment && rJob.Status.Succeeded == 1 {
 		ssChaos.Status.Phase = sschaosv1alpha1.PhaseAfterExperiment
 	}
 
+	if rJob.Status.Failed == 1 {

Review Comment:
   i think use `boolean` to set the type of `rJob.Status.Failed` is more simple if the value of Failed can just be 0 or 1.



##########
shardingsphere-operator/pkg/controllers/shardingsphere_chaos_controller.go:
##########
@@ -213,10 +246,137 @@ func (r *ShardingSphereChaosReconciler) reconcileStatus(ctx context.Context, nam
 	if err != nil {
 		return err
 	}
-	rt.Status = ssChaos.Status
+	setRtStatus(rt, ssChaos)
 	return r.Status().Update(ctx, rt)
 }
 
+func SetDefault(ssChaos *sschaosv1alpha1.ShardingSphereChaos) {
+	if ssChaos.Status.Phase == "" {
+		ssChaos.Status.Phase = sschaosv1alpha1.PhaseBeforeExperiment
+	}
+	if len(ssChaos.Status.Result) <= 0 || ssChaos.Status.Result == nil {
+		ssChaos.Status.Result = []sschaosv1alpha1.Result{}
+	}
+}
+
+func setRtStatus(rt *sschaosv1alpha1.ShardingSphereChaos, ssChaos *sschaosv1alpha1.ShardingSphereChaos) {
+	rt.Status.Result = []sschaosv1alpha1.Result{}
+	for i := range ssChaos.Status.Result {
+		rt.Status.Result = append(rt.Status.Result, sschaosv1alpha1.Result{
+			Success: ssChaos.Status.Result[i].Success,
+			Details: sschaosv1alpha1.Details{
+				Time: metav1.Time{Time: time.Now()},
+				Msg:  ssChaos.Status.Result[i].Details.Msg,
+			},
+		})
+	}
+	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 {
+	if checkResultExist(rJob) {
+		return nil
+	}
+	check := "Verify"

Review Comment:
   set `Verify` as const is better.



##########
shardingsphere-operator/pkg/controllers/shardingsphere_chaos_controller.go:
##########
@@ -213,10 +246,137 @@ func (r *ShardingSphereChaosReconciler) reconcileStatus(ctx context.Context, nam
 	if err != nil {
 		return err
 	}
-	rt.Status = ssChaos.Status
+	setRtStatus(rt, ssChaos)
 	return r.Status().Update(ctx, rt)
 }
 
+func SetDefault(ssChaos *sschaosv1alpha1.ShardingSphereChaos) {
+	if ssChaos.Status.Phase == "" {
+		ssChaos.Status.Phase = sschaosv1alpha1.PhaseBeforeExperiment
+	}
+	if len(ssChaos.Status.Result) <= 0 || ssChaos.Status.Result == nil {

Review Comment:
   1. No need to check the length of `ssChaos.Status.Result`. (BTW: `len` of a slice will not be less than `0`, just `len() == 0` is enough.)
   2.  Check `ssChaos.Status.Result` is `nil` first, otherwise this may get `panic`. 
   



##########
shardingsphere-operator/pkg/controllers/shardingsphere_chaos_controller.go:
##########
@@ -213,10 +246,137 @@ func (r *ShardingSphereChaosReconciler) reconcileStatus(ctx context.Context, nam
 	if err != nil {
 		return err
 	}
-	rt.Status = ssChaos.Status
+	setRtStatus(rt, ssChaos)
 	return r.Status().Update(ctx, rt)
 }
 
+func SetDefault(ssChaos *sschaosv1alpha1.ShardingSphereChaos) {
+	if ssChaos.Status.Phase == "" {
+		ssChaos.Status.Phase = sschaosv1alpha1.PhaseBeforeExperiment
+	}
+	if len(ssChaos.Status.Result) <= 0 || ssChaos.Status.Result == nil {
+		ssChaos.Status.Result = []sschaosv1alpha1.Result{}
+	}
+}
+
+func setRtStatus(rt *sschaosv1alpha1.ShardingSphereChaos, ssChaos *sschaosv1alpha1.ShardingSphereChaos) {
+	rt.Status.Result = []sschaosv1alpha1.Result{}
+	for i := range ssChaos.Status.Result {
+		rt.Status.Result = append(rt.Status.Result, sschaosv1alpha1.Result{
+			Success: ssChaos.Status.Result[i].Success,
+			Details: sschaosv1alpha1.Details{
+				Time: metav1.Time{Time: time.Now()},
+				Msg:  ssChaos.Status.Result[i].Details.Msg,
+			},
+		})
+	}
+	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 {
+	if checkResultExist(rJob) {
+		return nil
+	}
+	check := "Verify"
+	for i := range ssChaos.Status.Result {
+		if len(ssChaos.Status.Result[i].Details.Msg) >= len(check) && ssChaos.Status.Result[i].Details.Msg[:len(check)] == check {
+			return nil
+		}
+	}
+	logOpts := &v1.PodLogOptions{}
+	pod, err := r.getPodHaveLog(ctx, rJob)
+	if err != nil {
+		return err
+	}
+	containerNamespacedname := types.NamespacedName{
+		Namespace: pod.Namespace,
+		Name:      pod.Name,
+	}
+	result := &sschaosv1alpha1.Result{}
+	if rJob.Status.Succeeded == 1 {
+		log, err := r.getPodLog(ctx, containerNamespacedname, logOpts)
+		if ssChaos.Spec.Expect.Verify == "" || ssChaos.Spec.Expect.Verify == log {
+			result.Success = true
+			result.Details = sschaosv1alpha1.Details{
+				Time: metav1.Time{Time: time.Now()},
+				Msg:  fmt.Sprintf("%s: job succeeded", check),
+			}
+		} else {
+			if err != nil {
+				return err
+			}
+			result.Success = false
+			result.Details = sschaosv1alpha1.Details{
+				Time: metav1.Time{Time: time.Now()},
+				Msg:  fmt.Sprintf("%s: %s", check, log),
+			}
+		}
+	}
+
+	if rJob.Status.Failed == 1 {

Review Comment:
   the same problem above.



##########
shardingsphere-operator/pkg/controllers/shardingsphere_chaos_controller.go:
##########
@@ -213,10 +246,137 @@ func (r *ShardingSphereChaosReconciler) reconcileStatus(ctx context.Context, nam
 	if err != nil {
 		return err
 	}
-	rt.Status = ssChaos.Status
+	setRtStatus(rt, ssChaos)
 	return r.Status().Update(ctx, rt)
 }
 
+func SetDefault(ssChaos *sschaosv1alpha1.ShardingSphereChaos) {
+	if ssChaos.Status.Phase == "" {
+		ssChaos.Status.Phase = sschaosv1alpha1.PhaseBeforeExperiment
+	}
+	if len(ssChaos.Status.Result) <= 0 || ssChaos.Status.Result == nil {
+		ssChaos.Status.Result = []sschaosv1alpha1.Result{}
+	}
+}
+
+func setRtStatus(rt *sschaosv1alpha1.ShardingSphereChaos, ssChaos *sschaosv1alpha1.ShardingSphereChaos) {
+	rt.Status.Result = []sschaosv1alpha1.Result{}
+	for i := range ssChaos.Status.Result {
+		rt.Status.Result = append(rt.Status.Result, sschaosv1alpha1.Result{
+			Success: ssChaos.Status.Result[i].Success,
+			Details: sschaosv1alpha1.Details{
+				Time: metav1.Time{Time: time.Now()},
+				Msg:  ssChaos.Status.Result[i].Details.Msg,
+			},
+		})
+	}
+	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 {
+	if checkResultExist(rJob) {
+		return nil
+	}
+	check := "Verify"
+	for i := range ssChaos.Status.Result {
+		if len(ssChaos.Status.Result[i].Details.Msg) >= len(check) && ssChaos.Status.Result[i].Details.Msg[:len(check)] == check {
+			return nil
+		}
+	}
+	logOpts := &v1.PodLogOptions{}
+	pod, err := r.getPodHaveLog(ctx, rJob)
+	if err != nil {
+		return err
+	}
+	containerNamespacedname := types.NamespacedName{
+		Namespace: pod.Namespace,
+		Name:      pod.Name,
+	}
+	result := &sschaosv1alpha1.Result{}
+	if rJob.Status.Succeeded == 1 {
+		log, err := r.getPodLog(ctx, containerNamespacedname, logOpts)
+		if ssChaos.Spec.Expect.Verify == "" || ssChaos.Spec.Expect.Verify == log {
+			result.Success = true
+			result.Details = sschaosv1alpha1.Details{
+				Time: metav1.Time{Time: time.Now()},
+				Msg:  fmt.Sprintf("%s: job succeeded", check),
+			}
+		} else {
+			if err != nil {
+				return err
+			}
+			result.Success = false
+			result.Details = sschaosv1alpha1.Details{
+				Time: metav1.Time{Time: time.Now()},
+				Msg:  fmt.Sprintf("%s: %s", check, log),
+			}
+		}
+	}
+
+	if rJob.Status.Failed == 1 {
+		log, err := r.getPodLog(ctx, containerNamespacedname, logOpts)
+		if err != nil {
+			return err
+		}
+		result.Success = false
+		result.Details = sschaosv1alpha1.Details{
+			Time: metav1.Time{Time: time.Now()},
+			Msg:  fmt.Sprintf("%s: %s", check, log),
+		}
+	}
+	ssChaos.Status.Result = appendResult(*result, ssChaos.Status.Result, check)
+	return nil
+}
+
+func (r *ShardingSphereChaosReconciler) getPodHaveLog(ctx context.Context, rJob *batchV1.Job) (*v1.Pod, error) {
+	pods := &v1.PodList{}
+	if err := r.List(ctx, pods, client.MatchingLabels{"controller-uid": rJob.Spec.Template.Labels["controller-uid"]}); err != nil {
+		return nil, err
+	}
+	var pod *v1.Pod
+	for i := range pods.Items {
+		pod = &pods.Items[i]
+		break
+	}

Review Comment:
   this is used to get the first `pod` from `pods`? will the `pod` be `nil`?



##########
shardingsphere-operator/pkg/controllers/shardingsphere_chaos_controller.go:
##########
@@ -213,10 +246,137 @@ func (r *ShardingSphereChaosReconciler) reconcileStatus(ctx context.Context, nam
 	if err != nil {
 		return err
 	}
-	rt.Status = ssChaos.Status
+	setRtStatus(rt, ssChaos)
 	return r.Status().Update(ctx, rt)
 }
 
+func SetDefault(ssChaos *sschaosv1alpha1.ShardingSphereChaos) {
+	if ssChaos.Status.Phase == "" {
+		ssChaos.Status.Phase = sschaosv1alpha1.PhaseBeforeExperiment
+	}
+	if len(ssChaos.Status.Result) <= 0 || ssChaos.Status.Result == nil {
+		ssChaos.Status.Result = []sschaosv1alpha1.Result{}
+	}
+}
+
+func setRtStatus(rt *sschaosv1alpha1.ShardingSphereChaos, ssChaos *sschaosv1alpha1.ShardingSphereChaos) {
+	rt.Status.Result = []sschaosv1alpha1.Result{}
+	for i := range ssChaos.Status.Result {
+		rt.Status.Result = append(rt.Status.Result, sschaosv1alpha1.Result{
+			Success: ssChaos.Status.Result[i].Success,
+			Details: sschaosv1alpha1.Details{
+				Time: metav1.Time{Time: time.Now()},
+				Msg:  ssChaos.Status.Result[i].Details.Msg,
+			},
+		})
+	}
+	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 {
+	if checkResultExist(rJob) {
+		return nil
+	}
+	check := "Verify"
+	for i := range ssChaos.Status.Result {
+		if len(ssChaos.Status.Result[i].Details.Msg) >= len(check) && ssChaos.Status.Result[i].Details.Msg[:len(check)] == check {
+			return nil
+		}
+	}
+	logOpts := &v1.PodLogOptions{}
+	pod, err := r.getPodHaveLog(ctx, rJob)
+	if err != nil {
+		return err
+	}
+	containerNamespacedname := types.NamespacedName{
+		Namespace: pod.Namespace,
+		Name:      pod.Name,
+	}
+	result := &sschaosv1alpha1.Result{}
+	if rJob.Status.Succeeded == 1 {
+		log, err := r.getPodLog(ctx, containerNamespacedname, logOpts)
+		if ssChaos.Spec.Expect.Verify == "" || ssChaos.Spec.Expect.Verify == log {
+			result.Success = true
+			result.Details = sschaosv1alpha1.Details{
+				Time: metav1.Time{Time: time.Now()},
+				Msg:  fmt.Sprintf("%s: job succeeded", check),
+			}
+		} else {
+			if err != nil {
+				return err
+			}
+			result.Success = false
+			result.Details = sschaosv1alpha1.Details{
+				Time: metav1.Time{Time: time.Now()},
+				Msg:  fmt.Sprintf("%s: %s", check, log),
+			}
+		}
+	}
+
+	if rJob.Status.Failed == 1 {
+		log, err := r.getPodLog(ctx, containerNamespacedname, logOpts)
+		if err != nil {
+			return err
+		}
+		result.Success = false
+		result.Details = sschaosv1alpha1.Details{
+			Time: metav1.Time{Time: time.Now()},
+			Msg:  fmt.Sprintf("%s: %s", check, log),
+		}
+	}
+	ssChaos.Status.Result = appendResult(*result, ssChaos.Status.Result, check)
+	return nil
+}
+
+func (r *ShardingSphereChaosReconciler) getPodHaveLog(ctx context.Context, rJob *batchV1.Job) (*v1.Pod, error) {
+	pods := &v1.PodList{}
+	if err := r.List(ctx, pods, client.MatchingLabels{"controller-uid": rJob.Spec.Template.Labels["controller-uid"]}); err != nil {
+		return nil, err
+	}
+	var pod *v1.Pod
+	for i := range pods.Items {
+		pod = &pods.Items[i]
+		break
+	}
+	return pod, nil
+}
+
+func checkResultExist(rJob *batchV1.Job) bool {
+	for _, cmd := range rJob.Spec.Template.Spec.Containers[0].Args {
+		if strings.Contains(cmd, string(reconcile.Verify)) {
+			return true
+		}
+	}
+	return false
+}
+
+func appendResult(r sschaosv1alpha1.Result, results []sschaosv1alpha1.Result, check string) []sschaosv1alpha1.Result {

Review Comment:
   set `results` as first args and `r` as the second, this func will be used like `append(alist, aelement)`



##########
shardingsphere-operator/pkg/controllers/shardingsphere_chaos_controller.go:
##########
@@ -213,10 +246,137 @@ func (r *ShardingSphereChaosReconciler) reconcileStatus(ctx context.Context, nam
 	if err != nil {
 		return err
 	}
-	rt.Status = ssChaos.Status
+	setRtStatus(rt, ssChaos)
 	return r.Status().Update(ctx, rt)
 }
 
+func SetDefault(ssChaos *sschaosv1alpha1.ShardingSphereChaos) {
+	if ssChaos.Status.Phase == "" {
+		ssChaos.Status.Phase = sschaosv1alpha1.PhaseBeforeExperiment
+	}
+	if len(ssChaos.Status.Result) <= 0 || ssChaos.Status.Result == nil {
+		ssChaos.Status.Result = []sschaosv1alpha1.Result{}
+	}
+}
+
+func setRtStatus(rt *sschaosv1alpha1.ShardingSphereChaos, ssChaos *sschaosv1alpha1.ShardingSphereChaos) {
+	rt.Status.Result = []sschaosv1alpha1.Result{}
+	for i := range ssChaos.Status.Result {
+		rt.Status.Result = append(rt.Status.Result, sschaosv1alpha1.Result{
+			Success: ssChaos.Status.Result[i].Success,
+			Details: sschaosv1alpha1.Details{
+				Time: metav1.Time{Time: time.Now()},
+				Msg:  ssChaos.Status.Result[i].Details.Msg,
+			},
+		})
+	}
+	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 {
+	if checkResultExist(rJob) {
+		return nil
+	}
+	check := "Verify"
+	for i := range ssChaos.Status.Result {
+		if len(ssChaos.Status.Result[i].Details.Msg) >= len(check) && ssChaos.Status.Result[i].Details.Msg[:len(check)] == check {
+			return nil
+		}
+	}
+	logOpts := &v1.PodLogOptions{}
+	pod, err := r.getPodHaveLog(ctx, rJob)
+	if err != nil {
+		return err
+	}
+	containerNamespacedname := types.NamespacedName{
+		Namespace: pod.Namespace,
+		Name:      pod.Name,
+	}
+	result := &sschaosv1alpha1.Result{}
+	if rJob.Status.Succeeded == 1 {
+		log, err := r.getPodLog(ctx, containerNamespacedname, logOpts)
+		if ssChaos.Spec.Expect.Verify == "" || ssChaos.Spec.Expect.Verify == log {
+			result.Success = true
+			result.Details = sschaosv1alpha1.Details{

Review Comment:
   use `Detail` instead of `Details` if is not a list.



##########
shardingsphere-operator/pkg/controllers/shardingsphere_chaos_controller.go:
##########
@@ -213,10 +246,137 @@ func (r *ShardingSphereChaosReconciler) reconcileStatus(ctx context.Context, nam
 	if err != nil {
 		return err
 	}
-	rt.Status = ssChaos.Status
+	setRtStatus(rt, ssChaos)
 	return r.Status().Update(ctx, rt)
 }
 
+func SetDefault(ssChaos *sschaosv1alpha1.ShardingSphereChaos) {
+	if ssChaos.Status.Phase == "" {
+		ssChaos.Status.Phase = sschaosv1alpha1.PhaseBeforeExperiment
+	}
+	if len(ssChaos.Status.Result) <= 0 || ssChaos.Status.Result == nil {
+		ssChaos.Status.Result = []sschaosv1alpha1.Result{}
+	}
+}
+
+func setRtStatus(rt *sschaosv1alpha1.ShardingSphereChaos, ssChaos *sschaosv1alpha1.ShardingSphereChaos) {
+	rt.Status.Result = []sschaosv1alpha1.Result{}
+	for i := range ssChaos.Status.Result {
+		rt.Status.Result = append(rt.Status.Result, sschaosv1alpha1.Result{
+			Success: ssChaos.Status.Result[i].Success,
+			Details: sschaosv1alpha1.Details{
+				Time: metav1.Time{Time: time.Now()},
+				Msg:  ssChaos.Status.Result[i].Details.Msg,
+			},
+		})
+	}
+	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 {
+	if checkResultExist(rJob) {
+		return nil
+	}
+	check := "Verify"
+	for i := range ssChaos.Status.Result {
+		if len(ssChaos.Status.Result[i].Details.Msg) >= len(check) && ssChaos.Status.Result[i].Details.Msg[:len(check)] == check {
+			return nil
+		}
+	}
+	logOpts := &v1.PodLogOptions{}
+	pod, err := r.getPodHaveLog(ctx, rJob)
+	if err != nil {
+		return err
+	}
+	containerNamespacedname := types.NamespacedName{
+		Namespace: pod.Namespace,
+		Name:      pod.Name,
+	}
+	result := &sschaosv1alpha1.Result{}
+	if rJob.Status.Succeeded == 1 {
+		log, err := r.getPodLog(ctx, containerNamespacedname, logOpts)
+		if ssChaos.Spec.Expect.Verify == "" || ssChaos.Spec.Expect.Verify == log {
+			result.Success = true
+			result.Details = sschaosv1alpha1.Details{
+				Time: metav1.Time{Time: time.Now()},
+				Msg:  fmt.Sprintf("%s: job succeeded", check),
+			}
+		} else {
+			if err != nil {
+				return err
+			}
+			result.Success = false
+			result.Details = sschaosv1alpha1.Details{
+				Time: metav1.Time{Time: time.Now()},
+				Msg:  fmt.Sprintf("%s: %s", check, log),
+			}
+		}
+	}
+
+	if rJob.Status.Failed == 1 {
+		log, err := r.getPodLog(ctx, containerNamespacedname, logOpts)
+		if err != nil {
+			return err
+		}
+		result.Success = false
+		result.Details = sschaosv1alpha1.Details{
+			Time: metav1.Time{Time: time.Now()},
+			Msg:  fmt.Sprintf("%s: %s", check, log),
+		}
+	}
+	ssChaos.Status.Result = appendResult(*result, ssChaos.Status.Result, check)
+	return nil
+}
+
+func (r *ShardingSphereChaosReconciler) getPodHaveLog(ctx context.Context, rJob *batchV1.Job) (*v1.Pod, error) {
+	pods := &v1.PodList{}
+	if err := r.List(ctx, pods, client.MatchingLabels{"controller-uid": rJob.Spec.Template.Labels["controller-uid"]}); err != nil {
+		return nil, err
+	}
+	var pod *v1.Pod
+	for i := range pods.Items {
+		pod = &pods.Items[i]
+		break
+	}
+	return pod, nil
+}
+
+func checkResultExist(rJob *batchV1.Job) bool {
+	for _, cmd := range rJob.Spec.Template.Spec.Containers[0].Args {
+		if strings.Contains(cmd, string(reconcile.Verify)) {
+			return true
+		}
+	}
+	return false
+}
+
+func appendResult(r sschaosv1alpha1.Result, results []sschaosv1alpha1.Result, check string) []sschaosv1alpha1.Result {
+	for i := range results {
+		msg := results[i].Details.Msg
+		if len(msg) >= len(check) && msg[:len(check)] == check && r.Details.Msg[:len(check)] == check {
+			results[i] = r
+			return results
+		}
+	}

Review Comment:
   what are these code for ? update results? 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [shardingsphere-on-cloud] moomman commented on a diff in pull request #316: feat: add verify Spec && update reconcile logic

Posted by "moomman (via GitHub)" <gi...@apache.org>.
moomman commented on code in PR #316:
URL: https://github.com/apache/shardingsphere-on-cloud/pull/316#discussion_r1173504815


##########
shardingsphere-operator/pkg/controllers/shardingsphere_chaos_controller.go:
##########
@@ -213,10 +246,137 @@ func (r *ShardingSphereChaosReconciler) reconcileStatus(ctx context.Context, nam
 	if err != nil {
 		return err
 	}
-	rt.Status = ssChaos.Status
+	setRtStatus(rt, ssChaos)
 	return r.Status().Update(ctx, rt)
 }
 
+func SetDefault(ssChaos *sschaosv1alpha1.ShardingSphereChaos) {

Review Comment:
   sure



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org