|
| 1 | +package mutation |
| 2 | + |
| 3 | +import ( |
| 4 | + "math" |
| 5 | + "sort" |
| 6 | + |
| 7 | + corev1 "k8s.io/api/core/v1" |
| 8 | + "k8s.io/apimachinery/pkg/api/resource" |
| 9 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 10 | + |
| 11 | + clusterapis "github.com/karmada-io/api/cluster" |
| 12 | +) |
| 13 | + |
| 14 | +const ( |
| 15 | + // GB is a conversion value from GB to Bytes. |
| 16 | + GB = 1024 * 1024 * 1024 |
| 17 | +) |
| 18 | + |
| 19 | +// MutateCluster mutates required fields of the Cluster. |
| 20 | +func MutateCluster(cluster *clusterapis.Cluster) { |
| 21 | + MutateClusterTaints(cluster.Spec.Taints) |
| 22 | +} |
| 23 | + |
| 24 | +// MutateClusterTaints add TimeAdded field for cluster NoExecute taints only if TimeAdded not set. |
| 25 | +func MutateClusterTaints(taints []corev1.Taint) { |
| 26 | + for i := range taints { |
| 27 | + if taints[i].Effect == corev1.TaintEffectNoExecute && taints[i].TimeAdded == nil { |
| 28 | + now := metav1.Now() |
| 29 | + taints[i].TimeAdded = &now |
| 30 | + } |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +// StandardizeClusterResourceModels set cluster resource models for given order and boundary value. |
| 35 | +func StandardizeClusterResourceModels(models []clusterapis.ResourceModel) { |
| 36 | + if len(models) == 0 { |
| 37 | + return |
| 38 | + } |
| 39 | + |
| 40 | + sort.Slice(models, func(i, j int) bool { |
| 41 | + return models[i].Grade < models[j].Grade |
| 42 | + }) |
| 43 | + |
| 44 | + // The Min value of the first grade(usually 0) always acts as zero. |
| 45 | + for index := range models[0].Ranges { |
| 46 | + models[0].Ranges[index].Min.Set(0) |
| 47 | + } |
| 48 | + |
| 49 | + // The Max value of the last grade always acts as MaxInt64. |
| 50 | + for index := range models[len(models)-1].Ranges { |
| 51 | + models[len(models)-1].Ranges[index].Max.Set(math.MaxInt64) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +// SetDefaultClusterResourceModels set default cluster resource models for cluster. |
| 56 | +func SetDefaultClusterResourceModels(cluster *clusterapis.Cluster) { |
| 57 | + cluster.Spec.ResourceModels = []clusterapis.ResourceModel{ |
| 58 | + { |
| 59 | + Grade: 0, |
| 60 | + Ranges: []clusterapis.ResourceModelRange{ |
| 61 | + { |
| 62 | + Name: clusterapis.ResourceCPU, |
| 63 | + Min: *resource.NewQuantity(0, resource.DecimalSI), |
| 64 | + Max: *resource.NewQuantity(1, resource.DecimalSI), |
| 65 | + }, |
| 66 | + { |
| 67 | + Name: clusterapis.ResourceMemory, |
| 68 | + Min: *resource.NewQuantity(0, resource.BinarySI), |
| 69 | + Max: *resource.NewQuantity(4*GB, resource.BinarySI), |
| 70 | + }, |
| 71 | + }, |
| 72 | + }, |
| 73 | + { |
| 74 | + Grade: 1, |
| 75 | + Ranges: []clusterapis.ResourceModelRange{ |
| 76 | + { |
| 77 | + Name: clusterapis.ResourceCPU, |
| 78 | + Min: *resource.NewQuantity(1, resource.DecimalSI), |
| 79 | + Max: *resource.NewQuantity(2, resource.DecimalSI), |
| 80 | + }, |
| 81 | + { |
| 82 | + Name: clusterapis.ResourceMemory, |
| 83 | + Min: *resource.NewQuantity(4*GB, resource.BinarySI), |
| 84 | + Max: *resource.NewQuantity(16*GB, resource.BinarySI), |
| 85 | + }, |
| 86 | + }, |
| 87 | + }, |
| 88 | + { |
| 89 | + Grade: 2, |
| 90 | + Ranges: []clusterapis.ResourceModelRange{ |
| 91 | + { |
| 92 | + Name: clusterapis.ResourceCPU, |
| 93 | + Min: *resource.NewQuantity(2, resource.DecimalSI), |
| 94 | + Max: *resource.NewQuantity(4, resource.DecimalSI), |
| 95 | + }, |
| 96 | + { |
| 97 | + Name: clusterapis.ResourceMemory, |
| 98 | + Min: *resource.NewQuantity(16*GB, resource.BinarySI), |
| 99 | + Max: *resource.NewQuantity(32*GB, resource.BinarySI), |
| 100 | + }, |
| 101 | + }, |
| 102 | + }, |
| 103 | + { |
| 104 | + Grade: 3, |
| 105 | + Ranges: []clusterapis.ResourceModelRange{ |
| 106 | + { |
| 107 | + Name: clusterapis.ResourceCPU, |
| 108 | + Min: *resource.NewQuantity(4, resource.DecimalSI), |
| 109 | + Max: *resource.NewQuantity(8, resource.DecimalSI), |
| 110 | + }, |
| 111 | + { |
| 112 | + Name: clusterapis.ResourceMemory, |
| 113 | + Min: *resource.NewQuantity(32*GB, resource.BinarySI), |
| 114 | + Max: *resource.NewQuantity(64*GB, resource.BinarySI), |
| 115 | + }, |
| 116 | + }, |
| 117 | + }, |
| 118 | + { |
| 119 | + Grade: 4, |
| 120 | + Ranges: []clusterapis.ResourceModelRange{ |
| 121 | + { |
| 122 | + Name: clusterapis.ResourceCPU, |
| 123 | + Min: *resource.NewQuantity(8, resource.DecimalSI), |
| 124 | + Max: *resource.NewQuantity(16, resource.DecimalSI), |
| 125 | + }, |
| 126 | + { |
| 127 | + Name: clusterapis.ResourceMemory, |
| 128 | + Min: *resource.NewQuantity(64*GB, resource.BinarySI), |
| 129 | + Max: *resource.NewQuantity(128*GB, resource.BinarySI), |
| 130 | + }, |
| 131 | + }, |
| 132 | + }, |
| 133 | + { |
| 134 | + Grade: 5, |
| 135 | + Ranges: []clusterapis.ResourceModelRange{ |
| 136 | + { |
| 137 | + Name: clusterapis.ResourceCPU, |
| 138 | + Min: *resource.NewQuantity(16, resource.DecimalSI), |
| 139 | + Max: *resource.NewQuantity(32, resource.DecimalSI), |
| 140 | + }, |
| 141 | + { |
| 142 | + Name: clusterapis.ResourceMemory, |
| 143 | + Min: *resource.NewQuantity(128*GB, resource.BinarySI), |
| 144 | + Max: *resource.NewQuantity(256*GB, resource.BinarySI), |
| 145 | + }, |
| 146 | + }, |
| 147 | + }, |
| 148 | + { |
| 149 | + Grade: 6, |
| 150 | + Ranges: []clusterapis.ResourceModelRange{ |
| 151 | + { |
| 152 | + Name: clusterapis.ResourceCPU, |
| 153 | + Min: *resource.NewQuantity(32, resource.DecimalSI), |
| 154 | + Max: *resource.NewQuantity(64, resource.DecimalSI), |
| 155 | + }, |
| 156 | + { |
| 157 | + Name: clusterapis.ResourceMemory, |
| 158 | + Min: *resource.NewQuantity(256*GB, resource.BinarySI), |
| 159 | + Max: *resource.NewQuantity(512*GB, resource.BinarySI), |
| 160 | + }, |
| 161 | + }, |
| 162 | + }, |
| 163 | + { |
| 164 | + Grade: 7, |
| 165 | + Ranges: []clusterapis.ResourceModelRange{ |
| 166 | + { |
| 167 | + Name: clusterapis.ResourceCPU, |
| 168 | + Min: *resource.NewQuantity(64, resource.DecimalSI), |
| 169 | + Max: *resource.NewQuantity(128, resource.DecimalSI), |
| 170 | + }, |
| 171 | + { |
| 172 | + Name: clusterapis.ResourceMemory, |
| 173 | + Min: *resource.NewQuantity(512*GB, resource.BinarySI), |
| 174 | + Max: *resource.NewQuantity(1024*GB, resource.BinarySI), |
| 175 | + }, |
| 176 | + }, |
| 177 | + }, |
| 178 | + { |
| 179 | + Grade: 8, |
| 180 | + Ranges: []clusterapis.ResourceModelRange{ |
| 181 | + { |
| 182 | + Name: clusterapis.ResourceCPU, |
| 183 | + Min: *resource.NewQuantity(128, resource.DecimalSI), |
| 184 | + Max: *resource.NewQuantity(math.MaxInt64, resource.DecimalSI), |
| 185 | + }, |
| 186 | + { |
| 187 | + Name: clusterapis.ResourceMemory, |
| 188 | + Min: *resource.NewQuantity(1024*GB, resource.BinarySI), |
| 189 | + Max: *resource.NewQuantity(math.MaxInt64, resource.BinarySI), |
| 190 | + }, |
| 191 | + }, |
| 192 | + }, |
| 193 | + } |
| 194 | +} |
0 commit comments