Skip to content

Commit

Permalink
Merge pull request #16 from RainbowMango/pr_sync_14
Browse files Browse the repository at this point in the history
Sync APIs from karmada repo based on v1.4.0
  • Loading branch information
karmada-bot authored Dec 8, 2022
2 parents 1b543e6 + 721c916 commit d7e95f3
Show file tree
Hide file tree
Showing 20 changed files with 1,158 additions and 906 deletions.
194 changes: 194 additions & 0 deletions cluster/mutation/mutation.go
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),
},
},
},
}
}
125 changes: 125 additions & 0 deletions cluster/mutation/mutation_test.go
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
}
}
}
2 changes: 1 addition & 1 deletion cluster/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ type ResourceModel struct {
// ResourceModelRange describes the detail of each modeling quota that ranges from min to max.
// Please pay attention, by default, the value of min can be inclusive, and the value of max cannot be inclusive.
// E.g. in an interval, min = 2, max =10 is set, which means the interval [2,10).
// This rule ensure that all intervals have the same meaning. If the last interval is +¡Þ,
// This rule ensure that all intervals have the same meaning. If the last interval is +8,
// it is definitely unreachable. Therefore, we define the right interval as the open interval.
// For a valid interval, the value on the right is greater than the value on the left,
// in other words, max must be greater than min.
Expand Down
13 changes: 0 additions & 13 deletions cluster/v1alpha1/events.go

This file was deleted.

2 changes: 1 addition & 1 deletion cluster/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ type ResourceModel struct {
// ResourceModelRange describes the detail of each modeling quota that ranges from min to max.
// Please pay attention, by default, the value of min can be inclusive, and the value of max cannot be inclusive.
// E.g. in an interval, min = 2, max =10 is set, which means the interval [2,10).
// This rule ensure that all intervals have the same meaning. If the last interval is +¡Þ,
// This rule ensure that all intervals have the same meaning. If the last interval is +8,
// it is definitely unreachable. Therefore, we define the right interval as the open interval.
// For a valid interval, the value on the right is greater than the value on the left,
// in other words, max must be greater than min.
Expand Down
1 change: 0 additions & 1 deletion config/v1alpha1/interpretercontext_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"

workv1alpha1 "github.com/karmada-io/api/work/v1alpha1"
workv1alpha2 "github.com/karmada-io/api/work/v1alpha2"
)

Expand Down
Loading

0 comments on commit d7e95f3

Please sign in to comment.