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==Ec2Restart 29 30 // AWSChaos is the Schema for the awschaos API 31 type AWSChaos struct { 32 metav1.TypeMeta `json:",inline"` 33 metav1.ObjectMeta `json:"metadata,omitempty"` 34 35 Spec AWSChaosSpec `json:"spec"` 36 Status AWSChaosStatus `json:"status,omitempty"` 37 } 38 39 var _ InnerObjectWithSelector = (*AWSChaos)(nil) 40 var _ InnerObject = (*AWSChaos)(nil) 41 42 // AWSChaosAction represents the chaos action about aws. 43 type AWSChaosAction string 44 45 const ( 46 // Ec2Stop represents the chaos action of stopping ec2. 47 Ec2Stop AWSChaosAction = "ec2-stop" 48 // Ec2Restart represents the chaos action of restarting ec2. 49 Ec2Restart AWSChaosAction = "ec2-restart" 50 // DetachVolume represents the chaos action of detaching the volume of ec2. 51 DetachVolume AWSChaosAction = "detach-volume" 52 ) 53 54 // AWSChaosSpec is the content of the specification for an AWSChaos 55 type AWSChaosSpec struct { 56 // Action defines the specific aws chaos action. 57 // Supported action: ec2-stop / ec2-restart / detach-volume 58 // Default action: ec2-stop 59 // +kubebuilder:validation:Enum=ec2-stop;ec2-restart;detach-volume 60 Action AWSChaosAction `json:"action"` 61 62 // Duration represents the duration of the chaos action. 63 // +optional 64 Duration *string `json:"duration,omitempty" webhook:"Duration"` 65 66 // SecretName defines the name of kubernetes secret. 67 // +optional 68 SecretName *string `json:"secretName,omitempty" webhook:",nilable"` 69 70 AWSSelector `json:",inline"` 71 72 // RemoteCluster represents the remote cluster where the chaos will be deployed 73 // +optional 74 RemoteCluster string `json:"remoteCluster,omitempty"` 75 } 76 77 // AWSChaosStatus represents the status of an AWSChaos 78 type AWSChaosStatus struct { 79 ChaosStatus `json:",inline"` 80 } 81 82 type AWSSelector struct { 83 // TODO: it would be better to split them into multiple different selector and implementation 84 // but to keep the minimal modification on current implementation, it hasn't been splited. 85 86 // Endpoint indicates the endpoint of the aws server. Just used it in test now. 87 // +ui:form:ignore 88 // +optional 89 Endpoint *string `json:"endpoint,omitempty"` 90 91 // AWSRegion defines the region of aws. 92 AWSRegion string `json:"awsRegion"` 93 94 // Ec2Instance indicates the ID of the ec2 instance. 95 Ec2Instance string `json:"ec2Instance"` 96 97 // EbsVolume indicates the ID of the EBS volume. 98 // Needed in detach-volume. 99 // +ui:form:when=action=='detach-volume' 100 // +optional 101 EbsVolume *string `json:"volumeID,omitempty" webhook:"EbsVolume,nilable"` 102 103 // DeviceName indicates the name of the device. 104 // Needed in detach-volume. 105 // +ui:form:when=action=='detach-volume' 106 // +optional 107 DeviceName *string `json:"deviceName,omitempty" webhook:"AWSDeviceName,nilable"` 108 } 109 110 func (obj *AWSChaos) GetSelectorSpecs() map[string]interface{} { 111 return map[string]interface{}{ 112 ".": &obj.Spec.AWSSelector, 113 } 114 } 115 116 func (selector *AWSSelector) Id() string { 117 // TODO: handle the error here 118 // or ignore it is enough ? 119 json, _ := json.Marshal(selector) 120 121 return string(json) 122 } 123