diff --git a/pkg/kubelet/cm/topologymanager/BUILD b/pkg/kubelet/cm/topologymanager/BUILD index cdd0dbce85..7a639ac996 100644 --- a/pkg/kubelet/cm/topologymanager/BUILD +++ b/pkg/kubelet/cm/topologymanager/BUILD @@ -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( name = "go_default_library", srcs = [ "policy.go", + "policy_preferred.go", + "policy_strict.go", "topology_manager.go", ], importpath = "k8s.io/kubernetes/pkg/kubelet/cm/topologymanager", @@ -31,3 +33,12 @@ filegroup( tags = ["automanaged"], visibility = ["//visibility:public"], ) + +go_test( + name = "go_default_test", + srcs = [ + "policy_preferred_test.go", + "policy_strict_test.go", + ], + embed = [":go_default_library"], +) diff --git a/pkg/kubelet/cm/topologymanager/policy_preferred.go b/pkg/kubelet/cm/topologymanager/policy_preferred.go new file mode 100644 index 0000000000..36d7f3e4bf --- /dev/null +++ b/pkg/kubelet/cm/topologymanager/policy_preferred.go @@ -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, + } +} diff --git a/pkg/kubelet/cm/topologymanager/policy_preferred_test.go b/pkg/kubelet/cm/topologymanager/policy_preferred_test.go new file mode 100644 index 0000000000..04fb436cc5 --- /dev/null +++ b/pkg/kubelet/cm/topologymanager/policy_preferred_test.go @@ -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) + } + } +} diff --git a/pkg/kubelet/cm/topologymanager/policy_strict.go b/pkg/kubelet/cm/topologymanager/policy_strict.go new file mode 100644 index 0000000000..530e7d22a0 --- /dev/null +++ b/pkg/kubelet/cm/topologymanager/policy_strict.go @@ -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, + } +} diff --git a/pkg/kubelet/cm/topologymanager/policy_strict_test.go b/pkg/kubelet/cm/topologymanager/policy_strict_test.go new file mode 100644 index 0000000000..63ddd43ca1 --- /dev/null +++ b/pkg/kubelet/cm/topologymanager/policy_strict_test.go @@ -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") + } + } + } +}