mirror of https://github.com/k3s-io/k3s
Merge pull request #73589 from nolancon/topology-manager-policies-strict-preferred
Add Policies Strict and Preferred for Topology Managerk3s-v1.15.3
commit
990695c839
|
@ -1,9 +1,11 @@
|
||||||
load("@io_bazel_rules_go//go:def.bzl", "go_library")
|
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
|
||||||
|
|
||||||
go_library(
|
go_library(
|
||||||
name = "go_default_library",
|
name = "go_default_library",
|
||||||
srcs = [
|
srcs = [
|
||||||
"policy.go",
|
"policy.go",
|
||||||
|
"policy_preferred.go",
|
||||||
|
"policy_strict.go",
|
||||||
"topology_manager.go",
|
"topology_manager.go",
|
||||||
],
|
],
|
||||||
importpath = "k8s.io/kubernetes/pkg/kubelet/cm/topologymanager",
|
importpath = "k8s.io/kubernetes/pkg/kubelet/cm/topologymanager",
|
||||||
|
@ -31,3 +33,12 @@ filegroup(
|
||||||
tags = ["automanaged"],
|
tags = ["automanaged"],
|
||||||
visibility = ["//visibility:public"],
|
visibility = ["//visibility:public"],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
go_test(
|
||||||
|
name = "go_default_test",
|
||||||
|
srcs = [
|
||||||
|
"policy_preferred_test.go",
|
||||||
|
"policy_strict_test.go",
|
||||||
|
],
|
||||||
|
embed = [":go_default_library"],
|
||||||
|
)
|
||||||
|
|
|
@ -0,0 +1,43 @@
|
||||||
|
/*
|
||||||
|
Copyright 2015 The Kubernetes Authors.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package topologymanager
|
||||||
|
|
||||||
|
import (
|
||||||
|
"k8s.io/kubernetes/pkg/kubelet/lifecycle"
|
||||||
|
)
|
||||||
|
|
||||||
|
type preferredPolicy struct{}
|
||||||
|
|
||||||
|
var _ Policy = &preferredPolicy{}
|
||||||
|
|
||||||
|
// PolicyPreferred policy name.
|
||||||
|
const PolicyPreferred string = "preferred"
|
||||||
|
|
||||||
|
// NewPreferredPolicy returns preferred policy.
|
||||||
|
func NewPreferredPolicy() Policy {
|
||||||
|
return &preferredPolicy{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *preferredPolicy) Name() string {
|
||||||
|
return string(PolicyPreferred)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *preferredPolicy) CanAdmitPodResult(admit bool) lifecycle.PodAdmitResult {
|
||||||
|
return lifecycle.PodAdmitResult{
|
||||||
|
Admit: true,
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,50 @@
|
||||||
|
/*
|
||||||
|
Copyright 2019 The Kubernetes Authors.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package topologymanager
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCanAdmitPodResult1(t *testing.T) {
|
||||||
|
tcases := []struct {
|
||||||
|
name string
|
||||||
|
admit bool
|
||||||
|
expected bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "Affinity is set to false in topology hints",
|
||||||
|
admit: false,
|
||||||
|
expected: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Affinity is set to true in topology hints",
|
||||||
|
admit: true,
|
||||||
|
expected: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tcases {
|
||||||
|
policy := NewPreferredPolicy()
|
||||||
|
admit := tc.admit
|
||||||
|
result := policy.CanAdmitPodResult(admit)
|
||||||
|
|
||||||
|
if result.Admit != tc.expected {
|
||||||
|
t.Errorf("Expected Admit field in result to be %t, got %t", tc.expected, result.Admit)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,50 @@
|
||||||
|
/*
|
||||||
|
Copyright 2015 The Kubernetes Authors.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package topologymanager
|
||||||
|
|
||||||
|
import (
|
||||||
|
"k8s.io/kubernetes/pkg/kubelet/lifecycle"
|
||||||
|
)
|
||||||
|
|
||||||
|
type strictPolicy struct{}
|
||||||
|
|
||||||
|
var _ Policy = &strictPolicy{}
|
||||||
|
|
||||||
|
// PolicyStrict policy name.
|
||||||
|
const PolicyStrict string = "strict"
|
||||||
|
|
||||||
|
// NewStrictPolicy returns strict policy.
|
||||||
|
func NewStrictPolicy() Policy {
|
||||||
|
return &strictPolicy{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *strictPolicy) Name() string {
|
||||||
|
return string(PolicyStrict)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *strictPolicy) CanAdmitPodResult(admit bool) lifecycle.PodAdmitResult {
|
||||||
|
if !admit {
|
||||||
|
return lifecycle.PodAdmitResult{
|
||||||
|
Admit: false,
|
||||||
|
Reason: "Topology Affinity Error",
|
||||||
|
Message: "Resources cannot be allocated with Topology Locality",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return lifecycle.PodAdmitResult{
|
||||||
|
Admit: true,
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,59 @@
|
||||||
|
/*
|
||||||
|
Copyright 2019 The Kubernetes Authors.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package topologymanager
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCanAdmitPodResult(t *testing.T) {
|
||||||
|
tcases := []struct {
|
||||||
|
name string
|
||||||
|
admit bool
|
||||||
|
expected bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "Affinity is set to false in topology hints",
|
||||||
|
admit: false,
|
||||||
|
expected: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Affinity is set to true in topology hints",
|
||||||
|
admit: true,
|
||||||
|
expected: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tcases {
|
||||||
|
policy := NewStrictPolicy()
|
||||||
|
admit := tc.admit
|
||||||
|
result := policy.CanAdmitPodResult(admit)
|
||||||
|
|
||||||
|
if result.Admit != tc.expected {
|
||||||
|
t.Errorf("Expected Admit field in result to be %t, got %t", tc.expected, result.Admit)
|
||||||
|
}
|
||||||
|
|
||||||
|
if tc.expected == false {
|
||||||
|
if len(result.Reason) == 0 {
|
||||||
|
t.Errorf("Expected Reason field to be not empty")
|
||||||
|
}
|
||||||
|
if len(result.Message) == 0 {
|
||||||
|
t.Errorf("Expected Message field to be not empty")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue