-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from RainbowMango/pr_sync_14
Sync APIs from karmada repo based on v1.4.0
- Loading branch information
Showing
20 changed files
with
1,158 additions
and
906 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
package mutation | ||
|
||
import ( | ||
"math" | ||
"sort" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/api/resource" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
clusterapis "github.com/karmada-io/api/cluster" | ||
) | ||
|
||
const ( | ||
// GB is a conversion value from GB to Bytes. | ||
GB = 1024 * 1024 * 1024 | ||
) | ||
|
||
// MutateCluster mutates required fields of the Cluster. | ||
func MutateCluster(cluster *clusterapis.Cluster) { | ||
MutateClusterTaints(cluster.Spec.Taints) | ||
} | ||
|
||
// MutateClusterTaints add TimeAdded field for cluster NoExecute taints only if TimeAdded not set. | ||
func MutateClusterTaints(taints []corev1.Taint) { | ||
for i := range taints { | ||
if taints[i].Effect == corev1.TaintEffectNoExecute && taints[i].TimeAdded == nil { | ||
now := metav1.Now() | ||
taints[i].TimeAdded = &now | ||
} | ||
} | ||
} | ||
|
||
// StandardizeClusterResourceModels set cluster resource models for given order and boundary value. | ||
func StandardizeClusterResourceModels(models []clusterapis.ResourceModel) { | ||
if len(models) == 0 { | ||
return | ||
} | ||
|
||
sort.Slice(models, func(i, j int) bool { | ||
return models[i].Grade < models[j].Grade | ||
}) | ||
|
||
// The Min value of the first grade(usually 0) always acts as zero. | ||
for index := range models[0].Ranges { | ||
models[0].Ranges[index].Min.Set(0) | ||
} | ||
|
||
// The Max value of the last grade always acts as MaxInt64. | ||
for index := range models[len(models)-1].Ranges { | ||
models[len(models)-1].Ranges[index].Max.Set(math.MaxInt64) | ||
} | ||
} | ||
|
||
// SetDefaultClusterResourceModels set default cluster resource models for cluster. | ||
func SetDefaultClusterResourceModels(cluster *clusterapis.Cluster) { | ||
cluster.Spec.ResourceModels = []clusterapis.ResourceModel{ | ||
{ | ||
Grade: 0, | ||
Ranges: []clusterapis.ResourceModelRange{ | ||
{ | ||
Name: clusterapis.ResourceCPU, | ||
Min: *resource.NewQuantity(0, resource.DecimalSI), | ||
Max: *resource.NewQuantity(1, resource.DecimalSI), | ||
}, | ||
{ | ||
Name: clusterapis.ResourceMemory, | ||
Min: *resource.NewQuantity(0, resource.BinarySI), | ||
Max: *resource.NewQuantity(4*GB, resource.BinarySI), | ||
}, | ||
}, | ||
}, | ||
{ | ||
Grade: 1, | ||
Ranges: []clusterapis.ResourceModelRange{ | ||
{ | ||
Name: clusterapis.ResourceCPU, | ||
Min: *resource.NewQuantity(1, resource.DecimalSI), | ||
Max: *resource.NewQuantity(2, resource.DecimalSI), | ||
}, | ||
{ | ||
Name: clusterapis.ResourceMemory, | ||
Min: *resource.NewQuantity(4*GB, resource.BinarySI), | ||
Max: *resource.NewQuantity(16*GB, resource.BinarySI), | ||
}, | ||
}, | ||
}, | ||
{ | ||
Grade: 2, | ||
Ranges: []clusterapis.ResourceModelRange{ | ||
{ | ||
Name: clusterapis.ResourceCPU, | ||
Min: *resource.NewQuantity(2, resource.DecimalSI), | ||
Max: *resource.NewQuantity(4, resource.DecimalSI), | ||
}, | ||
{ | ||
Name: clusterapis.ResourceMemory, | ||
Min: *resource.NewQuantity(16*GB, resource.BinarySI), | ||
Max: *resource.NewQuantity(32*GB, resource.BinarySI), | ||
}, | ||
}, | ||
}, | ||
{ | ||
Grade: 3, | ||
Ranges: []clusterapis.ResourceModelRange{ | ||
{ | ||
Name: clusterapis.ResourceCPU, | ||
Min: *resource.NewQuantity(4, resource.DecimalSI), | ||
Max: *resource.NewQuantity(8, resource.DecimalSI), | ||
}, | ||
{ | ||
Name: clusterapis.ResourceMemory, | ||
Min: *resource.NewQuantity(32*GB, resource.BinarySI), | ||
Max: *resource.NewQuantity(64*GB, resource.BinarySI), | ||
}, | ||
}, | ||
}, | ||
{ | ||
Grade: 4, | ||
Ranges: []clusterapis.ResourceModelRange{ | ||
{ | ||
Name: clusterapis.ResourceCPU, | ||
Min: *resource.NewQuantity(8, resource.DecimalSI), | ||
Max: *resource.NewQuantity(16, resource.DecimalSI), | ||
}, | ||
{ | ||
Name: clusterapis.ResourceMemory, | ||
Min: *resource.NewQuantity(64*GB, resource.BinarySI), | ||
Max: *resource.NewQuantity(128*GB, resource.BinarySI), | ||
}, | ||
}, | ||
}, | ||
{ | ||
Grade: 5, | ||
Ranges: []clusterapis.ResourceModelRange{ | ||
{ | ||
Name: clusterapis.ResourceCPU, | ||
Min: *resource.NewQuantity(16, resource.DecimalSI), | ||
Max: *resource.NewQuantity(32, resource.DecimalSI), | ||
}, | ||
{ | ||
Name: clusterapis.ResourceMemory, | ||
Min: *resource.NewQuantity(128*GB, resource.BinarySI), | ||
Max: *resource.NewQuantity(256*GB, resource.BinarySI), | ||
}, | ||
}, | ||
}, | ||
{ | ||
Grade: 6, | ||
Ranges: []clusterapis.ResourceModelRange{ | ||
{ | ||
Name: clusterapis.ResourceCPU, | ||
Min: *resource.NewQuantity(32, resource.DecimalSI), | ||
Max: *resource.NewQuantity(64, resource.DecimalSI), | ||
}, | ||
{ | ||
Name: clusterapis.ResourceMemory, | ||
Min: *resource.NewQuantity(256*GB, resource.BinarySI), | ||
Max: *resource.NewQuantity(512*GB, resource.BinarySI), | ||
}, | ||
}, | ||
}, | ||
{ | ||
Grade: 7, | ||
Ranges: []clusterapis.ResourceModelRange{ | ||
{ | ||
Name: clusterapis.ResourceCPU, | ||
Min: *resource.NewQuantity(64, resource.DecimalSI), | ||
Max: *resource.NewQuantity(128, resource.DecimalSI), | ||
}, | ||
{ | ||
Name: clusterapis.ResourceMemory, | ||
Min: *resource.NewQuantity(512*GB, resource.BinarySI), | ||
Max: *resource.NewQuantity(1024*GB, resource.BinarySI), | ||
}, | ||
}, | ||
}, | ||
{ | ||
Grade: 8, | ||
Ranges: []clusterapis.ResourceModelRange{ | ||
{ | ||
Name: clusterapis.ResourceCPU, | ||
Min: *resource.NewQuantity(128, resource.DecimalSI), | ||
Max: *resource.NewQuantity(math.MaxInt64, resource.DecimalSI), | ||
}, | ||
{ | ||
Name: clusterapis.ResourceMemory, | ||
Min: *resource.NewQuantity(1024*GB, resource.BinarySI), | ||
Max: *resource.NewQuantity(math.MaxInt64, resource.BinarySI), | ||
}, | ||
}, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package mutation | ||
|
||
import ( | ||
"math" | ||
"reflect" | ||
"testing" | ||
|
||
"k8s.io/apimachinery/pkg/api/resource" | ||
|
||
clusterapis "github.com/karmada-io/api/cluster" | ||
) | ||
|
||
func TestStandardizeClusterResourceModels(t *testing.T) { | ||
testCases := map[string]struct { | ||
models []clusterapis.ResourceModel | ||
expectedModels []clusterapis.ResourceModel | ||
}{ | ||
"sort models": { | ||
models: []clusterapis.ResourceModel{ | ||
{ | ||
Grade: 2, | ||
Ranges: []clusterapis.ResourceModelRange{ | ||
{ | ||
Name: clusterapis.ResourceCPU, | ||
Min: *resource.NewQuantity(2, resource.DecimalSI), | ||
Max: *resource.NewQuantity(math.MaxInt64, resource.DecimalSI), | ||
}, | ||
}, | ||
}, | ||
{ | ||
Grade: 1, | ||
Ranges: []clusterapis.ResourceModelRange{ | ||
{ | ||
Name: clusterapis.ResourceCPU, | ||
Min: *resource.NewQuantity(0, resource.DecimalSI), | ||
Max: *resource.NewQuantity(2, resource.DecimalSI), | ||
}, | ||
}, | ||
}, | ||
}, | ||
expectedModels: []clusterapis.ResourceModel{ | ||
{ | ||
Grade: 1, | ||
Ranges: []clusterapis.ResourceModelRange{ | ||
{ | ||
Name: clusterapis.ResourceCPU, | ||
Min: *resource.NewQuantity(0, resource.DecimalSI), | ||
Max: *resource.NewQuantity(2, resource.DecimalSI), | ||
}, | ||
}, | ||
}, | ||
{ | ||
Grade: 2, | ||
Ranges: []clusterapis.ResourceModelRange{ | ||
{ | ||
Name: clusterapis.ResourceCPU, | ||
Min: *resource.NewQuantity(2, resource.DecimalSI), | ||
Max: *resource.NewQuantity(math.MaxInt64, resource.DecimalSI), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"start with 0": { | ||
models: []clusterapis.ResourceModel{ | ||
{ | ||
Grade: 1, | ||
Ranges: []clusterapis.ResourceModelRange{ | ||
{ | ||
Name: clusterapis.ResourceCPU, | ||
Min: *resource.NewQuantity(1, resource.DecimalSI), | ||
Max: *resource.NewQuantity(math.MaxInt64, resource.DecimalSI), | ||
}, | ||
}, | ||
}, | ||
}, | ||
expectedModels: []clusterapis.ResourceModel{ | ||
{ | ||
Grade: 1, | ||
Ranges: []clusterapis.ResourceModelRange{ | ||
{ | ||
Name: clusterapis.ResourceCPU, | ||
Min: *resource.NewQuantity(0, resource.DecimalSI), | ||
Max: *resource.NewQuantity(math.MaxInt64, resource.DecimalSI), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"end with MaxInt64": { | ||
models: []clusterapis.ResourceModel{ | ||
{ | ||
Grade: 1, | ||
Ranges: []clusterapis.ResourceModelRange{ | ||
{ | ||
Name: clusterapis.ResourceCPU, | ||
Min: *resource.NewQuantity(0, resource.DecimalSI), | ||
Max: *resource.NewQuantity(2, resource.DecimalSI), | ||
}, | ||
}, | ||
}, | ||
}, | ||
expectedModels: []clusterapis.ResourceModel{ | ||
{ | ||
Grade: 1, | ||
Ranges: []clusterapis.ResourceModelRange{ | ||
{ | ||
Name: clusterapis.ResourceCPU, | ||
Min: *resource.NewQuantity(0, resource.DecimalSI), | ||
Max: *resource.NewQuantity(math.MaxInt64, resource.DecimalSI), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for name, testCase := range testCases { | ||
StandardizeClusterResourceModels(testCase.models) | ||
if !reflect.DeepEqual(testCase.models, testCase.expectedModels) { | ||
t.Errorf("expected sorted resource models for %q, but it did not work", name) | ||
return | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.