diff --git a/pkg/rescheduler/evictions/utils.go b/pkg/rescheduler/evictions/utils.go new file mode 100644 index 000000000..738fd3f50 --- /dev/null +++ b/pkg/rescheduler/evictions/utils.go @@ -0,0 +1,58 @@ +/* +Copyright 2017 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 utils + +import ( + "k8s.io/kubernetes/pkg/client/clientset_generated/clientset" +) + +const ( + EvictionKind = "Eviction" + EvictionSubresource = "pods/eviction" +) + +// SupportEviction uses Discovery API to find out if the server support eviction subresource +// If support, it will return its groupVersion; Otherwise, it will return "" +func SupportEviction(clientset clientset.Interface) (string, error) { + discoveryClient := clientset.Discovery() + groupList, err := discoveryClient.ServerGroups() + if err != nil { + return "", err + } + foundPolicyGroup := false + var policyGroupVersion string + for _, group := range groupList.Groups { + if group.Name == "policy" { + foundPolicyGroup = true + policyGroupVersion = group.PreferredVersion.GroupVersion + break + } + } + if !foundPolicyGroup { + return "", nil + } + resourceList, err := discoveryClient.ServerResourcesForGroupVersion("v1") + if err != nil { + return "", err + } + for _, resource := range resourceList.APIResources { + if resource.Name == EvictionSubresource && resource.Kind == EvictionKind { + return policyGroupVersion, nil + } + } + return "", nil +}