...

Source file src/github.com/chaos-mesh/chaos-mesh/api/v1alpha1/kinds.go

Documentation: github.com/chaos-mesh/chaos-mesh/api/v1alpha1

     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 v1alpha1
    17  
    18  import (
    19  	"sync"
    20  
    21  	"sigs.k8s.io/controller-runtime/pkg/client"
    22  )
    23  
    24  // +kubebuilder:object:generate=false
    25  
    26  // ChaosKindMap defines a map including all chaos kinds.
    27  type chaosKindMap struct {
    28  	sync.RWMutex
    29  	kinds map[string]*ChaosKind
    30  }
    31  
    32  func (c *chaosKindMap) register(name string, kind *ChaosKind) {
    33  	c.Lock()
    34  	defer c.Unlock()
    35  	c.kinds[name] = kind
    36  }
    37  
    38  // clone will build a new map with kinds, so if user add or delete entries of the map, the origin global map will not be affected.
    39  func (c *chaosKindMap) clone() map[string]*ChaosKind {
    40  	c.RLock()
    41  	defer c.RUnlock()
    42  
    43  	out := make(map[string]*ChaosKind)
    44  	for key, kind := range c.kinds {
    45  		out[key] = &ChaosKind{
    46  			chaos: kind.chaos,
    47  			list:  kind.list,
    48  		}
    49  	}
    50  
    51  	return out
    52  }
    53  
    54  // AllKinds returns all chaos kinds, key is name of Kind, value is an accessor for spawning Object and List
    55  func AllKinds() map[string]*ChaosKind {
    56  	return all.clone()
    57  }
    58  
    59  func AllKindsIncludeScheduleAndWorkflow() map[string]*ChaosKind {
    60  	all := chaosKindMap{
    61  		kinds: all.clone(),
    62  	}
    63  	all.register(KindSchedule, &ChaosKind{
    64  		chaos: &Schedule{},
    65  		list:  &ScheduleList{},
    66  	})
    67  	all.register(KindWorkflow, &ChaosKind{
    68  		chaos: &Workflow{},
    69  		list:  &WorkflowList{},
    70  	})
    71  
    72  	return all.kinds
    73  }
    74  
    75  // all is a ChaosKindMap instance.
    76  var all = &chaosKindMap{
    77  	kinds: make(map[string]*ChaosKind),
    78  }
    79  
    80  // +kubebuilder:object:generate=false
    81  
    82  // ChaosKind includes one kind of chaos and its list type
    83  type ChaosKind struct {
    84  	chaos client.Object
    85  	list  GenericChaosList
    86  }
    87  
    88  // SpawnObject will deepcopy a clean struct for the acquired kind as placeholder
    89  func (it *ChaosKind) SpawnObject() client.Object {
    90  	return it.chaos.DeepCopyObject().(client.Object)
    91  }
    92  
    93  // SpawnList will deepcopy a clean list for the acquired kind of chaos as placeholder
    94  func (it *ChaosKind) SpawnList() GenericChaosList {
    95  	return it.list.DeepCopyList()
    96  }
    97  
    98  // AllKinds returns all chaos kinds.
    99  func AllScheduleItemKinds() map[string]*ChaosKind {
   100  	return allScheduleItem.clone()
   101  }
   102  
   103  // allScheduleItem is a ChaosKindMap instance.
   104  var allScheduleItem = &chaosKindMap{
   105  	kinds: make(map[string]*ChaosKind),
   106  }
   107