1 // Copyright 2022 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==AzureVmRestart 29 30 // AzureChaos is the Schema for the azurechaos API 31 type AzureChaos struct { 32 metav1.TypeMeta `json:",inline"` 33 metav1.ObjectMeta `json:"metadata,omitempty"` 34 35 Spec AzureChaosSpec `json:"spec"` 36 Status AzureChaosStatus `json:"status,omitempty"` 37 } 38 39 var _ InnerObjectWithSelector = (*AzureChaos)(nil) 40 var _ InnerObject = (*AzureChaos)(nil) 41 42 // AzureChaosAction represents the chaos action about azure. 43 type AzureChaosAction string 44 45 const ( 46 // AzureVmStop represents the chaos action of stopping vm. 47 AzureVmStop AzureChaosAction = "vm-stop" 48 // AzureVmRestart represents the chaos action of restarting vm. 49 AzureVmRestart AzureChaosAction = "vm-restart" 50 // AzureDiskDetach represents the chaos action of detaching the disk from vm. 51 AzureDiskDetach AzureChaosAction = "disk-detach" 52 ) 53 54 // AzureChaosSpec is the content of the specification for an AzureChaos 55 type AzureChaosSpec struct { 56 // Action defines the specific azure chaos action. 57 // Supported action: vm-stop / vm-restart / disk-detach 58 // Default action: vm-stop 59 // +kubebuilder:validation:Enum=vm-stop;vm-restart;disk-detach 60 Action AzureChaosAction `json:"action"` 61 62 // Duration represents the duration of the chaos action. 63 // +optional 64 Duration *string `json:"duration,omitempty" webhook:"Duration"` 65 66 AzureSelector `json:",inline"` 67 } 68 69 // AzureChaosStatus represents the status of an AzureChaos 70 type AzureChaosStatus struct { 71 ChaosStatus `json:",inline"` 72 } 73 74 type AzureSelector struct { 75 // SubscriptionID defines the id of Azure subscription. 76 SubscriptionID string `json:"subscriptionID"` 77 78 // ResourceGroupName defines the name of ResourceGroup 79 ResourceGroupName string `json:"resourceGroupName"` 80 81 // VMName defines the name of Virtual Machine 82 VMName string `json:"vmName"` 83 84 // DiskName indicates the name of the disk. 85 // Needed in disk-detach. 86 // +optional 87 DiskName *string `json:"diskName,omitempty" webhook:"DiskName,nilable"` 88 89 // LUN indicates the Logical Unit Number of the data disk. 90 // Needed in disk-detach. 91 // +optional 92 LUN *int `json:"lun,omitempty" webhook:"LUN,nilable"` 93 94 // SecretName defines the name of kubernetes secret. It is used for Azure credentials. 95 // +optional 96 SecretName *string `json:"secretName,omitempty"` 97 98 // RemoteCluster represents the remote cluster where the chaos will be deployed 99 // +optional 100 RemoteCluster string `json:"remoteCluster,omitempty"` 101 } 102 103 func (obj *AzureChaos) GetSelectorSpecs() map[string]interface{} { 104 return map[string]interface{}{ 105 ".": &obj.Spec.AzureSelector, 106 } 107 } 108 109 func (selector *AzureSelector) Id() string { 110 // TODO: handle the error here 111 // or ignore it is enough ? 112 json, _ := json.Marshal(selector) 113 114 return string(json) 115 } 116