...

Source file src/github.com/chaos-mesh/chaos-mesh/controllers/utils/controller/finished.go

Documentation: github.com/chaos-mesh/chaos-mesh/controllers/utils/controller

     1  // Copyright 2021 Chaos Mesh Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  // http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  
    16  package controller
    17  
    18  import (
    19  	"time"
    20  
    21  	"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
    22  )
    23  
    24  func IsChaosFinished(obj v1alpha1.InnerObject, now time.Time) bool {
    25  	finished, _ := IsChaosFinishedWithUntilStop(obj, now)
    26  	return finished
    27  }
    28  
    29  func IsChaosFinishedWithUntilStop(obj v1alpha1.InnerObject, now time.Time) (bool, time.Duration) {
    30  	status := obj.GetStatus()
    31  	if obj.IsOneShot() {
    32  		finished := true
    33  		if len(status.Experiment.Records) == 0 {
    34  			finished = false
    35  		} else {
    36  			for _, record := range status.Experiment.Records {
    37  				if record.Phase != v1alpha1.Injected {
    38  					finished = false
    39  				}
    40  			}
    41  		}
    42  		// this oneshot chaos hasn't finished, retry after 1 second
    43  		return finished, time.Duration(time.Second)
    44  	}
    45  
    46  	finished := true
    47  
    48  	if status.Experiment.DesiredPhase == v1alpha1.RunningPhase {
    49  		finished = false
    50  	} else {
    51  		// If one of the record has not been recovered, it's not finished
    52  		for _, record := range status.Experiment.Records {
    53  			if record.Phase != v1alpha1.NotInjected {
    54  				finished = false
    55  			}
    56  		}
    57  	}
    58  
    59  	durationExceeded, untilStop, err := obj.DurationExceeded(now)
    60  	if err != nil {
    61  		return finished, untilStop
    62  	}
    63  	if durationExceeded {
    64  		return finished, untilStop
    65  	}
    66  
    67  	return false, untilStop
    68  }
    69