Merge pull request #91603 from pancernik/v1beta1-requested-to-cap-ratio-encoding

Configure strict encoding for RequestedToCapacityRatioArgs
This commit is contained in:
Kubernetes Prow Robot 2020-06-22 21:59:41 -07:00 committed by GitHub
commit 819ad44c90
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 47 additions and 81 deletions

View File

@ -586,10 +586,6 @@ API rule violation: names_match,k8s.io/kube-controller-manager/config/v1alpha1,V
API rule violation: names_match,k8s.io/kube-controller-manager/config/v1alpha1,VolumeConfiguration,PersistentVolumeRecyclerConfiguration API rule violation: names_match,k8s.io/kube-controller-manager/config/v1alpha1,VolumeConfiguration,PersistentVolumeRecyclerConfiguration
API rule violation: names_match,k8s.io/kube-proxy/config/v1alpha1,KubeProxyConfiguration,IPTables API rule violation: names_match,k8s.io/kube-proxy/config/v1alpha1,KubeProxyConfiguration,IPTables
API rule violation: names_match,k8s.io/kube-scheduler/config/v1,Extender,EnableHTTPS API rule violation: names_match,k8s.io/kube-scheduler/config/v1,Extender,EnableHTTPS
API rule violation: names_match,k8s.io/kube-scheduler/config/v1beta1,ResourceSpec,Name
API rule violation: names_match,k8s.io/kube-scheduler/config/v1beta1,ResourceSpec,Weight
API rule violation: names_match,k8s.io/kube-scheduler/config/v1beta1,UtilizationShapePoint,Score
API rule violation: names_match,k8s.io/kube-scheduler/config/v1beta1,UtilizationShapePoint,Utilization
API rule violation: names_match,k8s.io/kubelet/config/v1beta1,KubeletConfiguration,IPTablesDropBit API rule violation: names_match,k8s.io/kubelet/config/v1beta1,KubeletConfiguration,IPTablesDropBit
API rule violation: names_match,k8s.io/kubelet/config/v1beta1,KubeletConfiguration,IPTablesMasqueradeBit API rule violation: names_match,k8s.io/kubelet/config/v1beta1,KubeletConfiguration,IPTablesMasqueradeBit
API rule violation: names_match,k8s.io/kubelet/config/v1beta1,KubeletConfiguration,ResolverConfig API rule violation: names_match,k8s.io/kubelet/config/v1beta1,KubeletConfiguration,ResolverConfig

View File

@ -21,7 +21,7 @@ import (
"testing" "testing"
"github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp"
v1 "k8s.io/api/core/v1" corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/kube-scheduler/config/v1beta1" "k8s.io/kube-scheduler/config/v1beta1"
@ -108,8 +108,8 @@ profiles:
{ {
Name: "PodTopologySpread", Name: "PodTopologySpread",
Args: &config.PodTopologySpreadArgs{ Args: &config.PodTopologySpreadArgs{
DefaultConstraints: []v1.TopologySpreadConstraint{ DefaultConstraints: []corev1.TopologySpreadConstraint{
{MaxSkew: 1, TopologyKey: "zone", WhenUnsatisfiable: v1.ScheduleAnyway}, {MaxSkew: 1, TopologyKey: "zone", WhenUnsatisfiable: corev1.ScheduleAnyway},
}, },
}, },
}, },
@ -178,11 +178,25 @@ profiles:
apiVersion: kubescheduler.config.k8s.io/v1beta1 apiVersion: kubescheduler.config.k8s.io/v1beta1
kind: InterPodAffinityArgs kind: InterPodAffinityArgs
`), `),
wantErr: "decoding .profiles[0].pluginConfig[0]: args for plugin NodeLabel were not of type NodeLabelArgs.kubescheduler.config.k8s.io, got InterPodAffinityArgs.kubescheduler.config.k8s.io", wantErr: `decoding .profiles[0].pluginConfig[0]: args for plugin NodeLabel were not of type NodeLabelArgs.kubescheduler.config.k8s.io, got InterPodAffinityArgs.kubescheduler.config.k8s.io`,
}, },
{ {
// TODO: do not replicate this case for v1beta1. name: "v1beta1 RequestedToCapacityRatioArgs shape encoding is strict",
name: "v1beta1 case insensitive RequestedToCapacityRatioArgs", data: []byte(`
apiVersion: kubescheduler.config.k8s.io/v1beta1
kind: KubeSchedulerConfiguration
profiles:
- pluginConfig:
- name: RequestedToCapacityRatio
args:
shape:
- Utilization: 1
Score: 2
`),
wantErr: `decoding .profiles[0].pluginConfig[0]: decoding args for plugin RequestedToCapacityRatio: strict decoder error for {"shape":[{"Score":2,"Utilization":1}]}: v1beta1.RequestedToCapacityRatioArgs.Shape: []v1beta1.UtilizationShapePoint: v1beta1.UtilizationShapePoint.ReadObject: found unknown field: Score, error found in #10 byte of ...|:[{"Score":2,"Utiliz|..., bigger context ...|{"shape":[{"Score":2,"Utilization":1}]}|...`,
},
{
name: "v1beta1 RequestedToCapacityRatioArgs resources encoding is strict",
data: []byte(` data: []byte(`
apiVersion: kubescheduler.config.k8s.io/v1beta1 apiVersion: kubescheduler.config.k8s.io/v1beta1
kind: KubeSchedulerConfiguration kind: KubeSchedulerConfiguration
@ -193,34 +207,11 @@ profiles:
shape: shape:
- utilization: 1 - utilization: 1
score: 2 score: 2
- Utilization: 3
Score: 4
resources: resources:
- name: Upper - Name: 1
weight: 1 Weight: 2
- Name: lower
weight: 2
`), `),
wantProfiles: []config.KubeSchedulerProfile{ wantErr: `decoding .profiles[0].pluginConfig[0]: decoding args for plugin RequestedToCapacityRatio: strict decoder error for {"resources":[{"Name":1,"Weight":2}],"shape":[{"score":2,"utilization":1}]}: v1beta1.RequestedToCapacityRatioArgs.Shape: []v1beta1.UtilizationShapePoint: Resources: []v1beta1.ResourceSpec: v1beta1.ResourceSpec.ReadObject: found unknown field: Name, error found in #10 byte of ...|":[{"Name":1,"Weight|..., bigger context ...|{"resources":[{"Name":1,"Weight":2}],"shape":[{"score":2,"utilization":|...`,
{
SchedulerName: "default-scheduler",
PluginConfig: []config.PluginConfig{
{
Name: "RequestedToCapacityRatio",
Args: &config.RequestedToCapacityRatioArgs{
Shape: []config.UtilizationShapePoint{
{Utilization: 1, Score: 2},
{Utilization: 3, Score: 4},
},
Resources: []config.ResourceSpec{
{Name: "Upper", Weight: 1},
{Name: "lower", Weight: 2},
},
},
},
},
},
},
}, },
{ {
name: "out-of-tree plugin args", name: "out-of-tree plugin args",
@ -311,7 +302,7 @@ profiles:
obj, gvk, err := decoder.Decode(tt.data, nil, nil) obj, gvk, err := decoder.Decode(tt.data, nil, nil)
if err != nil { if err != nil {
if tt.wantErr != err.Error() { if tt.wantErr != err.Error() {
t.Fatalf("got err %v, want %v", err.Error(), tt.wantErr) t.Fatalf("\ngot err:\n\t%v\nwant:\n\t%s", err, tt.wantErr)
} }
return return
} }
@ -367,7 +358,7 @@ func TestCodecsEncodePluginConfig(t *testing.T) {
{Utilization: 1, Score: 2}, {Utilization: 1, Score: 2},
}, },
Resources: []v1beta1.ResourceSpec{ Resources: []v1beta1.ResourceSpec{
{Name: "lower", Weight: 2}, {Name: "cpu", Weight: 2},
}, },
}, },
}, },
@ -424,18 +415,18 @@ profiles:
apiVersion: kubescheduler.config.k8s.io/v1beta1 apiVersion: kubescheduler.config.k8s.io/v1beta1
kind: RequestedToCapacityRatioArgs kind: RequestedToCapacityRatioArgs
resources: resources:
- Name: lower - name: cpu
Weight: 2 weight: 2
shape: shape:
- Score: 2 - score: 2
Utilization: 1 utilization: 1
name: RequestedToCapacityRatio name: RequestedToCapacityRatio
- args: - args:
apiVersion: kubescheduler.config.k8s.io/v1beta1 apiVersion: kubescheduler.config.k8s.io/v1beta1
kind: NodeResourcesLeastAllocatedArgs kind: NodeResourcesLeastAllocatedArgs
resources: resources:
- Name: mem - name: mem
Weight: 2 weight: 2
name: NodeResourcesLeastAllocated name: NodeResourcesLeastAllocated
- args: - args:
foo: bar foo: bar
@ -512,8 +503,8 @@ profiles:
apiVersion: kubescheduler.config.k8s.io/v1beta1 apiVersion: kubescheduler.config.k8s.io/v1beta1
kind: NodeResourcesMostAllocatedArgs kind: NodeResourcesMostAllocatedArgs
resources: resources:
- Name: cpu - name: cpu
Weight: 1 weight: 1
name: NodeResourcesMostAllocated name: NodeResourcesMostAllocated
- args: - args:
apiVersion: kubescheduler.config.k8s.io/v1beta1 apiVersion: kubescheduler.config.k8s.io/v1beta1

View File

@ -20,7 +20,7 @@ import (
"net" "net"
"strconv" "strconv"
v1 "k8s.io/api/core/v1" corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apiserver/pkg/util/feature" "k8s.io/apiserver/pkg/util/feature"
componentbaseconfigv1alpha1 "k8s.io/component-base/config/v1alpha1" componentbaseconfigv1alpha1 "k8s.io/component-base/config/v1alpha1"
@ -34,8 +34,8 @@ import (
) )
var defaultResourceSpec = []v1beta1.ResourceSpec{ var defaultResourceSpec = []v1beta1.ResourceSpec{
{Name: string(v1.ResourceCPU), Weight: 1}, {Name: string(corev1.ResourceCPU), Weight: 1},
{Name: string(v1.ResourceMemory), Weight: 1}, {Name: string(corev1.ResourceMemory), Weight: 1},
} }
func addDefaultingFuncs(scheme *runtime.Scheme) error { func addDefaultingFuncs(scheme *runtime.Scheme) error {
@ -203,15 +203,15 @@ func SetDefaults_PodTopologySpreadArgs(obj *v1beta1.PodTopologySpreadArgs) {
return return
} }
if obj.DefaultConstraints == nil { if obj.DefaultConstraints == nil {
obj.DefaultConstraints = []v1.TopologySpreadConstraint{ obj.DefaultConstraints = []corev1.TopologySpreadConstraint{
{ {
TopologyKey: v1.LabelHostname, TopologyKey: corev1.LabelHostname,
WhenUnsatisfiable: v1.ScheduleAnyway, WhenUnsatisfiable: corev1.ScheduleAnyway,
MaxSkew: 3, MaxSkew: 3,
}, },
{ {
TopologyKey: v1.LabelZoneFailureDomainStable, TopologyKey: corev1.LabelZoneFailureDomainStable,
WhenUnsatisfiable: v1.ScheduleAnyway, WhenUnsatisfiable: corev1.ScheduleAnyway,
MaxSkew: 5, MaxSkew: 5,
}, },
} }

View File

@ -17,9 +17,7 @@ limitations under the License.
package v1beta1 package v1beta1
import ( import (
gojson "encoding/json" corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
) )
@ -80,7 +78,7 @@ type PodTopologySpreadArgs struct {
// Empty by default. // Empty by default.
// +optional // +optional
// +listType=atomic // +listType=atomic
DefaultConstraints []v1.TopologySpreadConstraint `json:"defaultConstraints"` DefaultConstraints []corev1.TopologySpreadConstraint `json:"defaultConstraints"`
} }
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
@ -123,39 +121,20 @@ type NodeResourcesMostAllocatedArgs struct {
Resources []ResourceSpec `json:"resources,omitempty"` Resources []ResourceSpec `json:"resources,omitempty"`
} }
// TODO add JSON tags and remove custom unmarshalling in v1beta1.
// UtilizationShapePoint and ResourceSpec fields are not annotated with JSON tags in v1alpha2
// to maintain backward compatibility with the args shipped with v1.18.
// See https://github.com/kubernetes/kubernetes/pull/88585#discussion_r405021905
// UtilizationShapePoint represents single point of priority function shape. // UtilizationShapePoint represents single point of priority function shape.
type UtilizationShapePoint struct { type UtilizationShapePoint struct {
// Utilization (x axis). Valid values are 0 to 100. Fully utilized node maps to 100. // Utilization (x axis). Valid values are 0 to 100. Fully utilized node maps to 100.
Utilization int32 Utilization int32 `json:"utilization"`
// Score assigned to given utilization (y axis). Valid values are 0 to 10. // Score assigned to given utilization (y axis). Valid values are 0 to 10.
Score int32 Score int32 `json:"score"`
}
// UnmarshalJSON provides case insensitive unmarshalling for the type.
// TODO remove when copying to v1beta1.
func (t *UtilizationShapePoint) UnmarshalJSON(data []byte) error {
type internal *UtilizationShapePoint
return gojson.Unmarshal(data, internal(t))
} }
// ResourceSpec represents single resource and weight for bin packing of priority RequestedToCapacityRatioArguments. // ResourceSpec represents single resource and weight for bin packing of priority RequestedToCapacityRatioArguments.
type ResourceSpec struct { type ResourceSpec struct {
// Name of the resource to be managed by RequestedToCapacityRatio function. // Name of the resource to be managed by RequestedToCapacityRatio function.
Name string Name string `json:"name"`
// Weight of the resource. // Weight of the resource.
Weight int64 Weight int64 `json:"weight,omitempty"`
}
// UnmarshalJSON provides case insensitive unmarshalling for the type.
// TODO remove when copying to v1beta1.
func (t *ResourceSpec) UnmarshalJSON(data []byte) error {
type internal *ResourceSpec
return gojson.Unmarshal(data, internal(t))
} }
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object