...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/status/status.go

Documentation: github.com/chaos-mesh/chaos-mesh/pkg/status

     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 status
    17  
    18  import (
    19  	"time"
    20  
    21  	corev1 "k8s.io/api/core/v1"
    22  
    23  	"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
    24  	"github.com/chaos-mesh/chaos-mesh/controllers/utils/controller"
    25  )
    26  
    27  type ChaosStatus string
    28  
    29  const (
    30  	Injecting ChaosStatus = "injecting"
    31  	Running   ChaosStatus = "running"
    32  	Finished  ChaosStatus = "finished"
    33  	Paused    ChaosStatus = "paused"
    34  	Deleting  ChaosStatus = "deleting"
    35  )
    36  
    37  type AllChaosStatus struct {
    38  	Injecting int `json:"injecting"`
    39  	Running   int `json:"running"`
    40  	Finished  int `json:"finished"`
    41  	Paused    int `json:"paused"`
    42  	Deleting  int `json:"deleting"`
    43  }
    44  
    45  type ScheduleStatus string
    46  
    47  const (
    48  	ScheduleRunning ScheduleStatus = "running"
    49  	SchedulePaused  ScheduleStatus = "paused"
    50  )
    51  
    52  // GetChaosStatus returns the status of chaos object.
    53  func GetChaosStatus(obj v1alpha1.InnerObject) ChaosStatus {
    54  	if obj.IsDeleted() {
    55  		return Deleting
    56  	}
    57  
    58  	selected := false
    59  	allInjected := false
    60  	for _, c := range obj.GetStatus().Conditions {
    61  		if c.Status == corev1.ConditionTrue {
    62  			switch c.Type {
    63  			// If ConditionPaused is true, represent the chaos experiment is paused.
    64  			case v1alpha1.ConditionPaused:
    65  				return Paused
    66  			case v1alpha1.ConditionSelected:
    67  				selected = true
    68  			case v1alpha1.ConditionAllInjected:
    69  				allInjected = true
    70  			}
    71  		}
    72  	}
    73  
    74  	if controller.IsChaosFinished(obj, time.Now()) {
    75  		return Finished
    76  	}
    77  
    78  	// Only when the target objects are successfully selected and injected,
    79  	// it means that the chaos experiment is running well.
    80  	if selected && allInjected {
    81  		return Running
    82  	}
    83  
    84  	return Injecting
    85  }
    86  
    87  func GetScheduleStatus(sch v1alpha1.Schedule) ScheduleStatus {
    88  	if sch.IsPaused() {
    89  		return SchedulePaused
    90  	}
    91  
    92  	return ScheduleRunning
    93  }
    94