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 "encoding/json" 20 21 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 22 ) 23 24 // +kubebuilder:object:root=true 25 // +kubebuilder:printcolumn:name="action",type=string,JSONPath=`.spec.action` 26 // +kubebuilder:printcolumn:name="duration",type=string,JSONPath=`.spec.duration` 27 // +chaos-mesh:experiment 28 // +chaos-mesh:oneshot=in.Spec.Action==NodeReset 29 30 // GCPChaos is the Schema for the gcpchaos API 31 type GCPChaos struct { 32 metav1.TypeMeta `json:",inline"` 33 metav1.ObjectMeta `json:"metadata,omitempty"` 34 35 Spec GCPChaosSpec `json:"spec"` 36 Status GCPChaosStatus `json:"status,omitempty"` 37 } 38 39 var _ InnerObjectWithCustomStatus = (*GCPChaos)(nil) 40 var _ InnerObjectWithSelector = (*GCPChaos)(nil) 41 var _ InnerObject = (*GCPChaos)(nil) 42 43 // GCPChaosAction represents the chaos action about gcp. 44 type GCPChaosAction string 45 46 const ( 47 // NodeStop represents the chaos action of stopping the node. 48 NodeStop GCPChaosAction = "node-stop" 49 // NodeReset represents the chaos action of resetting the node. 50 NodeReset GCPChaosAction = "node-reset" 51 // DiskLoss represents the chaos action of detaching the disk. 52 DiskLoss GCPChaosAction = "disk-loss" 53 ) 54 55 // GCPChaosSpec is the content of the specification for a GCPChaos 56 type GCPChaosSpec struct { 57 // Action defines the specific gcp chaos action. 58 // Supported action: node-stop / node-reset / disk-loss 59 // Default action: node-stop 60 // +kubebuilder:validation:Enum=node-stop;node-reset;disk-loss 61 Action GCPChaosAction `json:"action"` 62 63 // Duration represents the duration of the chaos action. 64 // +optional 65 Duration *string `json:"duration,omitempty" webhook:"Duration"` 66 67 // SecretName defines the name of kubernetes secret. It is used for GCP credentials. 68 // +optional 69 SecretName *string `json:"secretName,omitempty"` 70 71 GCPSelector `json:",inline"` 72 73 // RemoteCluster represents the remote cluster where the chaos will be deployed 74 // +optional 75 RemoteCluster string `json:"remoteCluster,omitempty"` 76 } 77 78 type GCPSelector struct { 79 // Project defines the ID of gcp project. 80 Project string `json:"project"` 81 82 // Zone defines the zone of gcp project. 83 Zone string `json:"zone"` 84 85 // Instance defines the name of the instance 86 Instance string `json:"instance"` 87 88 // The device name of disks to detach. 89 // Needed in disk-loss. 90 // +ui:form:when=action=='disk-loss' 91 // +optional 92 DeviceNames []string `json:"deviceNames,omitempty" webhook:"GCPDeviceNames,nilable"` 93 } 94 95 func (obj *GCPChaos) GetSelectorSpecs() map[string]interface{} { 96 return map[string]interface{}{ 97 ".": &obj.Spec.GCPSelector, 98 } 99 } 100 101 func (selector *GCPSelector) Id() string { 102 // TODO: handle the error here 103 // or ignore it is enough ? 104 json, _ := json.Marshal(selector) 105 106 return string(json) 107 } 108 109 // GCPChaosStatus represents the status of a GCPChaos 110 type GCPChaosStatus struct { 111 ChaosStatus `json:",inline"` 112 113 // The attached disk info strings. 114 // Needed in disk-loss. 115 AttachedDisksStrings []string `json:"attachedDiskStrings,omitempty"` 116 } 117 118 func (obj *GCPChaos) GetCustomStatus() interface{} { 119 return &obj.Status.AttachedDisksStrings 120 } 121