Skip to content

Commit d7e95f3

Browse files
authored
Merge pull request #16 from RainbowMango/pr_sync_14
Sync APIs from karmada repo based on v1.4.0
2 parents 1b543e6 + 721c916 commit d7e95f3

20 files changed

+1158
-906
lines changed

Diff for: cluster/mutation/mutation.go

+194
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
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+
}

Diff for: cluster/mutation/mutation_test.go

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package mutation
2+
3+
import (
4+
"math"
5+
"reflect"
6+
"testing"
7+
8+
"k8s.io/apimachinery/pkg/api/resource"
9+
10+
clusterapis "github.com/karmada-io/api/cluster"
11+
)
12+
13+
func TestStandardizeClusterResourceModels(t *testing.T) {
14+
testCases := map[string]struct {
15+
models []clusterapis.ResourceModel
16+
expectedModels []clusterapis.ResourceModel
17+
}{
18+
"sort models": {
19+
models: []clusterapis.ResourceModel{
20+
{
21+
Grade: 2,
22+
Ranges: []clusterapis.ResourceModelRange{
23+
{
24+
Name: clusterapis.ResourceCPU,
25+
Min: *resource.NewQuantity(2, resource.DecimalSI),
26+
Max: *resource.NewQuantity(math.MaxInt64, resource.DecimalSI),
27+
},
28+
},
29+
},
30+
{
31+
Grade: 1,
32+
Ranges: []clusterapis.ResourceModelRange{
33+
{
34+
Name: clusterapis.ResourceCPU,
35+
Min: *resource.NewQuantity(0, resource.DecimalSI),
36+
Max: *resource.NewQuantity(2, resource.DecimalSI),
37+
},
38+
},
39+
},
40+
},
41+
expectedModels: []clusterapis.ResourceModel{
42+
{
43+
Grade: 1,
44+
Ranges: []clusterapis.ResourceModelRange{
45+
{
46+
Name: clusterapis.ResourceCPU,
47+
Min: *resource.NewQuantity(0, resource.DecimalSI),
48+
Max: *resource.NewQuantity(2, resource.DecimalSI),
49+
},
50+
},
51+
},
52+
{
53+
Grade: 2,
54+
Ranges: []clusterapis.ResourceModelRange{
55+
{
56+
Name: clusterapis.ResourceCPU,
57+
Min: *resource.NewQuantity(2, resource.DecimalSI),
58+
Max: *resource.NewQuantity(math.MaxInt64, resource.DecimalSI),
59+
},
60+
},
61+
},
62+
},
63+
},
64+
"start with 0": {
65+
models: []clusterapis.ResourceModel{
66+
{
67+
Grade: 1,
68+
Ranges: []clusterapis.ResourceModelRange{
69+
{
70+
Name: clusterapis.ResourceCPU,
71+
Min: *resource.NewQuantity(1, resource.DecimalSI),
72+
Max: *resource.NewQuantity(math.MaxInt64, resource.DecimalSI),
73+
},
74+
},
75+
},
76+
},
77+
expectedModels: []clusterapis.ResourceModel{
78+
{
79+
Grade: 1,
80+
Ranges: []clusterapis.ResourceModelRange{
81+
{
82+
Name: clusterapis.ResourceCPU,
83+
Min: *resource.NewQuantity(0, resource.DecimalSI),
84+
Max: *resource.NewQuantity(math.MaxInt64, resource.DecimalSI),
85+
},
86+
},
87+
},
88+
},
89+
},
90+
"end with MaxInt64": {
91+
models: []clusterapis.ResourceModel{
92+
{
93+
Grade: 1,
94+
Ranges: []clusterapis.ResourceModelRange{
95+
{
96+
Name: clusterapis.ResourceCPU,
97+
Min: *resource.NewQuantity(0, resource.DecimalSI),
98+
Max: *resource.NewQuantity(2, resource.DecimalSI),
99+
},
100+
},
101+
},
102+
},
103+
expectedModels: []clusterapis.ResourceModel{
104+
{
105+
Grade: 1,
106+
Ranges: []clusterapis.ResourceModelRange{
107+
{
108+
Name: clusterapis.ResourceCPU,
109+
Min: *resource.NewQuantity(0, resource.DecimalSI),
110+
Max: *resource.NewQuantity(math.MaxInt64, resource.DecimalSI),
111+
},
112+
},
113+
},
114+
},
115+
},
116+
}
117+
118+
for name, testCase := range testCases {
119+
StandardizeClusterResourceModels(testCase.models)
120+
if !reflect.DeepEqual(testCase.models, testCase.expectedModels) {
121+
t.Errorf("expected sorted resource models for %q, but it did not work", name)
122+
return
123+
}
124+
}
125+
}

Diff for: cluster/types.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ type ResourceModel struct {
199199
// ResourceModelRange describes the detail of each modeling quota that ranges from min to max.
200200
// Please pay attention, by default, the value of min can be inclusive, and the value of max cannot be inclusive.
201201
// E.g. in an interval, min = 2, max =10 is set, which means the interval [2,10).
202-
// This rule ensure that all intervals have the same meaning. If the last interval is +¡Þ,
202+
// This rule ensure that all intervals have the same meaning. If the last interval is +8,
203203
// it is definitely unreachable. Therefore, we define the right interval as the open interval.
204204
// For a valid interval, the value on the right is greater than the value on the left,
205205
// in other words, max must be greater than min.

Diff for: cluster/v1alpha1/events.go

-13
This file was deleted.

Diff for: cluster/v1alpha1/types.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ type ResourceModel struct {
211211
// ResourceModelRange describes the detail of each modeling quota that ranges from min to max.
212212
// Please pay attention, by default, the value of min can be inclusive, and the value of max cannot be inclusive.
213213
// E.g. in an interval, min = 2, max =10 is set, which means the interval [2,10).
214-
// This rule ensure that all intervals have the same meaning. If the last interval is +¡Þ,
214+
// This rule ensure that all intervals have the same meaning. If the last interval is +8,
215215
// it is definitely unreachable. Therefore, we define the right interval as the open interval.
216216
// For a valid interval, the value on the right is greater than the value on the left,
217217
// in other words, max must be greater than min.

Diff for: config/v1alpha1/interpretercontext_types.go

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"k8s.io/apimachinery/pkg/runtime"
66
"k8s.io/apimachinery/pkg/types"
77

8-
workv1alpha1 "github.com/karmada-io/api/work/v1alpha1"
98
workv1alpha2 "github.com/karmada-io/api/work/v1alpha2"
109
)
1110

0 commit comments

Comments
 (0)