...

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  )
    35  
    36  type AllChaosStatus struct {
    37  	Injecting int `json:"injecting"`
    38  	Running   int `json:"running"`
    39  	Finished  int `json:"finished"`
    40  	Paused    int `json:"paused"`
    41  }
    42  
    43  type ScheduleStatus string
    44  
    45  const (
    46  	ScheduleRunning ScheduleStatus = "running"
    47  	SchedulePaused  ScheduleStatus = "paused"
    48  )
    49  
    50  func GetChaosStatus(obj v1alpha1.InnerObject) ChaosStatus {
    51  	selected := false
    52  	allInjected := false
    53  	for _, c := range obj.GetStatus().Conditions {
    54  		if c.Status == corev1.ConditionTrue {
    55  			switch c.Type {
    56  			case v1alpha1.ConditionPaused:
    57  				return Paused
    58  			case v1alpha1.ConditionSelected:
    59  				selected = true
    60  			case v1alpha1.ConditionAllInjected:
    61  				allInjected = true
    62  			}
    63  		}
    64  	}
    65  
    66  	if controller.IsChaosFinished(obj, time.Now()) {
    67  		return Finished
    68  	}
    69  
    70  	if selected && allInjected {
    71  		return Running
    72  	}
    73  
    74  	return Injecting
    75  }
    76  
    77  func GetScheduleStatus(sch v1alpha1.Schedule) ScheduleStatus {
    78  	if sch.IsPaused() {
    79  		return SchedulePaused
    80  	}
    81  
    82  	return ScheduleRunning
    83  }
    84