k3s/vendor/k8s.io/kubernetes/pkg/scheduler/extender.go

473 lines
14 KiB
Go
Raw Normal View History

2019-01-12 04:58:27 +00:00
/*
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 scheduler
2019-01-12 04:58:27 +00:00
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"strings"
"time"
2020-08-10 17:43:49 +00:00
v1 "k8s.io/api/core/v1"
2019-01-12 04:58:27 +00:00
utilnet "k8s.io/apimachinery/pkg/util/net"
"k8s.io/apimachinery/pkg/util/sets"
restclient "k8s.io/client-go/rest"
2020-03-26 21:07:15 +00:00
extenderv1 "k8s.io/kube-scheduler/extender/v1"
2019-12-12 01:27:03 +00:00
schedulerapi "k8s.io/kubernetes/pkg/scheduler/apis/config"
"k8s.io/kubernetes/pkg/scheduler/framework"
2019-01-12 04:58:27 +00:00
)
const (
// DefaultExtenderTimeout defines the default extender timeout in second.
DefaultExtenderTimeout = 5 * time.Second
)
2020-08-10 17:43:49 +00:00
// HTTPExtender implements the Extender interface.
2019-01-12 04:58:27 +00:00
type HTTPExtender struct {
extenderURL string
preemptVerb string
filterVerb string
prioritizeVerb string
bindVerb string
2019-12-12 01:27:03 +00:00
weight int64
2019-01-12 04:58:27 +00:00
client *http.Client
nodeCacheCapable bool
managedResources sets.String
ignorable bool
}
2019-12-12 01:27:03 +00:00
func makeTransport(config *schedulerapi.Extender) (http.RoundTripper, error) {
2019-01-12 04:58:27 +00:00
var cfg restclient.Config
if config.TLSConfig != nil {
2019-04-07 17:07:55 +00:00
cfg.TLSClientConfig.Insecure = config.TLSConfig.Insecure
cfg.TLSClientConfig.ServerName = config.TLSConfig.ServerName
cfg.TLSClientConfig.CertFile = config.TLSConfig.CertFile
cfg.TLSClientConfig.KeyFile = config.TLSConfig.KeyFile
cfg.TLSClientConfig.CAFile = config.TLSConfig.CAFile
cfg.TLSClientConfig.CertData = config.TLSConfig.CertData
cfg.TLSClientConfig.KeyData = config.TLSConfig.KeyData
cfg.TLSClientConfig.CAData = config.TLSConfig.CAData
2019-01-12 04:58:27 +00:00
}
if config.EnableHTTPS {
hasCA := len(cfg.CAFile) > 0 || len(cfg.CAData) > 0
if !hasCA {
cfg.Insecure = true
}
}
tlsConfig, err := restclient.TLSConfigFor(&cfg)
if err != nil {
return nil, err
}
if tlsConfig != nil {
return utilnet.SetTransportDefaults(&http.Transport{
TLSClientConfig: tlsConfig,
}), nil
}
return utilnet.SetTransportDefaults(&http.Transport{}), nil
}
// NewHTTPExtender creates an HTTPExtender object.
2020-08-10 17:43:49 +00:00
func NewHTTPExtender(config *schedulerapi.Extender) (framework.Extender, error) {
if config.HTTPTimeout.Duration.Nanoseconds() == 0 {
config.HTTPTimeout.Duration = time.Duration(DefaultExtenderTimeout)
2019-01-12 04:58:27 +00:00
}
transport, err := makeTransport(config)
if err != nil {
return nil, err
}
client := &http.Client{
Transport: transport,
2020-08-10 17:43:49 +00:00
Timeout: config.HTTPTimeout.Duration,
2019-01-12 04:58:27 +00:00
}
managedResources := sets.NewString()
for _, r := range config.ManagedResources {
managedResources.Insert(string(r.Name))
}
return &HTTPExtender{
extenderURL: config.URLPrefix,
preemptVerb: config.PreemptVerb,
filterVerb: config.FilterVerb,
prioritizeVerb: config.PrioritizeVerb,
bindVerb: config.BindVerb,
weight: config.Weight,
client: client,
nodeCacheCapable: config.NodeCacheCapable,
managedResources: managedResources,
ignorable: config.Ignorable,
}, nil
}
2019-12-12 01:27:03 +00:00
// Equal is used to check if two extenders are equal
// ignoring the client field, exported for testing
func Equal(e1, e2 *HTTPExtender) bool {
if e1.extenderURL != e2.extenderURL {
return false
}
if e1.preemptVerb != e2.preemptVerb {
return false
}
if e1.prioritizeVerb != e2.prioritizeVerb {
return false
}
if e1.bindVerb != e2.bindVerb {
return false
}
if e1.weight != e2.weight {
return false
}
if e1.nodeCacheCapable != e2.nodeCacheCapable {
return false
}
if !e1.managedResources.Equal(e2.managedResources) {
return false
}
if e1.ignorable != e2.ignorable {
return false
}
return true
}
2019-04-07 17:07:55 +00:00
// Name returns extenderURL to identify the extender.
2019-01-12 04:58:27 +00:00
func (h *HTTPExtender) Name() string {
return h.extenderURL
}
// IsIgnorable returns true indicates scheduling should not fail when this extender
// is unavailable
func (h *HTTPExtender) IsIgnorable() bool {
return h.ignorable
}
2019-04-07 17:07:55 +00:00
// SupportsPreemption returns true if an extender supports preemption.
// An extender should have preempt verb defined and enabled its own node cache.
2019-01-12 04:58:27 +00:00
func (h *HTTPExtender) SupportsPreemption() bool {
return len(h.preemptVerb) > 0
}
// ProcessPreemption returns filtered candidate nodes and victims after running preemption logic in extender.
func (h *HTTPExtender) ProcessPreemption(
pod *v1.Pod,
2020-08-10 17:43:49 +00:00
nodeNameToVictims map[string]*extenderv1.Victims,
nodeInfos framework.NodeInfoLister,
) (map[string]*extenderv1.Victims, error) {
2019-01-12 04:58:27 +00:00
var (
2019-12-12 01:27:03 +00:00
result extenderv1.ExtenderPreemptionResult
args *extenderv1.ExtenderPreemptionArgs
2019-01-12 04:58:27 +00:00
)
if !h.SupportsPreemption() {
return nil, fmt.Errorf("preempt verb is not defined for extender %v but run into ProcessPreemption", h.extenderURL)
}
if h.nodeCacheCapable {
// If extender has cached node info, pass NodeNameToMetaVictims in args.
2020-08-10 17:43:49 +00:00
nodeNameToMetaVictims := convertToNodeNameToMetaVictims(nodeNameToVictims)
2019-12-12 01:27:03 +00:00
args = &extenderv1.ExtenderPreemptionArgs{
2019-01-12 04:58:27 +00:00
Pod: pod,
NodeNameToMetaVictims: nodeNameToMetaVictims,
}
} else {
2019-12-12 01:27:03 +00:00
args = &extenderv1.ExtenderPreemptionArgs{
2019-01-12 04:58:27 +00:00
Pod: pod,
NodeNameToVictims: nodeNameToVictims,
}
}
if err := h.send(h.preemptVerb, args, &result); err != nil {
return nil, err
}
// Extender will always return NodeNameToMetaVictims.
2020-08-10 17:43:49 +00:00
// So let's convert it to NodeNameToVictims by using <nodeInfos>.
newNodeNameToVictims, err := h.convertToNodeNameToVictims(result.NodeNameToMetaVictims, nodeInfos)
2019-01-12 04:58:27 +00:00
if err != nil {
return nil, err
}
2020-08-10 17:43:49 +00:00
// Do not override <nodeNameToVictims>.
return newNodeNameToVictims, nil
2019-01-12 04:58:27 +00:00
}
2020-08-10 17:43:49 +00:00
// convertToNodeNameToVictims converts "nodeNameToMetaVictims" from object identifiers,
2019-01-12 04:58:27 +00:00
// such as UIDs and names, to object pointers.
2020-08-10 17:43:49 +00:00
func (h *HTTPExtender) convertToNodeNameToVictims(
2019-12-12 01:27:03 +00:00
nodeNameToMetaVictims map[string]*extenderv1.MetaVictims,
2020-08-10 17:43:49 +00:00
nodeInfos framework.NodeInfoLister,
) (map[string]*extenderv1.Victims, error) {
nodeNameToVictims := map[string]*extenderv1.Victims{}
2019-01-12 04:58:27 +00:00
for nodeName, metaVictims := range nodeNameToMetaVictims {
2020-03-26 21:07:15 +00:00
nodeInfo, err := nodeInfos.Get(nodeName)
if err != nil {
return nil, err
}
2019-12-12 01:27:03 +00:00
victims := &extenderv1.Victims{
2019-01-12 04:58:27 +00:00
Pods: []*v1.Pod{},
}
for _, metaPod := range metaVictims.Pods {
2020-03-26 21:07:15 +00:00
pod, err := h.convertPodUIDToPod(metaPod, nodeInfo)
2019-01-12 04:58:27 +00:00
if err != nil {
return nil, err
}
victims.Pods = append(victims.Pods, pod)
}
2020-08-10 17:43:49 +00:00
nodeNameToVictims[nodeName] = victims
2019-01-12 04:58:27 +00:00
}
2020-08-10 17:43:49 +00:00
return nodeNameToVictims, nil
2019-01-12 04:58:27 +00:00
}
2020-03-26 21:07:15 +00:00
// convertPodUIDToPod returns v1.Pod object for given MetaPod and node info.
2019-01-12 04:58:27 +00:00
// The v1.Pod object is restored by nodeInfo.Pods().
2020-03-26 21:07:15 +00:00
// It returns an error if there's cache inconsistency between default scheduler
// and extender, i.e. when the pod is not found in nodeInfo.Pods.
2019-01-12 04:58:27 +00:00
func (h *HTTPExtender) convertPodUIDToPod(
2019-12-12 01:27:03 +00:00
metaPod *extenderv1.MetaPod,
2020-08-10 17:43:49 +00:00
nodeInfo *framework.NodeInfo) (*v1.Pod, error) {
for _, p := range nodeInfo.Pods {
if string(p.Pod.UID) == metaPod.UID {
return p.Pod, nil
2019-01-12 04:58:27 +00:00
}
}
2020-03-26 21:07:15 +00:00
return nil, fmt.Errorf("extender: %v claims to preempt pod (UID: %v) on node: %v, but the pod is not found on that node",
h.extenderURL, metaPod, nodeInfo.Node().Name)
2019-01-12 04:58:27 +00:00
}
// convertToNodeNameToMetaVictims converts from struct type to meta types.
func convertToNodeNameToMetaVictims(
2020-08-10 17:43:49 +00:00
nodeNameToVictims map[string]*extenderv1.Victims,
2019-12-12 01:27:03 +00:00
) map[string]*extenderv1.MetaVictims {
2020-08-10 17:43:49 +00:00
nodeNameToMetaVictims := map[string]*extenderv1.MetaVictims{}
for node, victims := range nodeNameToVictims {
2019-12-12 01:27:03 +00:00
metaVictims := &extenderv1.MetaVictims{
Pods: []*extenderv1.MetaPod{},
2019-01-12 04:58:27 +00:00
}
for _, pod := range victims.Pods {
2019-12-12 01:27:03 +00:00
metaPod := &extenderv1.MetaPod{
2019-01-12 04:58:27 +00:00
UID: string(pod.UID),
}
metaVictims.Pods = append(metaVictims.Pods, metaPod)
}
2020-08-10 17:43:49 +00:00
nodeNameToMetaVictims[node] = metaVictims
2019-01-12 04:58:27 +00:00
}
2020-08-10 17:43:49 +00:00
return nodeNameToMetaVictims
2019-01-12 04:58:27 +00:00
}
// Filter based on extender implemented predicate functions. The filtered list is
2019-09-27 21:51:53 +00:00
// expected to be a subset of the supplied list; otherwise the function returns an error.
// The failedNodes and failedAndUnresolvableNodes optionally contains the list
// of failed nodes and failure reasons, except nodes in the latter are
// unresolvable.
2019-01-12 04:58:27 +00:00
func (h *HTTPExtender) Filter(
pod *v1.Pod,
2020-03-26 21:07:15 +00:00
nodes []*v1.Node,
) (filteredList []*v1.Node, failedNodes, failedAndUnresolvableNodes extenderv1.FailedNodesMap, err error) {
2019-01-12 04:58:27 +00:00
var (
2019-12-12 01:27:03 +00:00
result extenderv1.ExtenderFilterResult
2019-01-12 04:58:27 +00:00
nodeList *v1.NodeList
nodeNames *[]string
nodeResult []*v1.Node
2019-12-12 01:27:03 +00:00
args *extenderv1.ExtenderArgs
2019-01-12 04:58:27 +00:00
)
2020-03-26 21:07:15 +00:00
fromNodeName := make(map[string]*v1.Node)
for _, n := range nodes {
fromNodeName[n.Name] = n
}
2019-01-12 04:58:27 +00:00
if h.filterVerb == "" {
return nodes, extenderv1.FailedNodesMap{}, extenderv1.FailedNodesMap{}, nil
2019-01-12 04:58:27 +00:00
}
if h.nodeCacheCapable {
nodeNameSlice := make([]string, 0, len(nodes))
for _, node := range nodes {
nodeNameSlice = append(nodeNameSlice, node.Name)
}
nodeNames = &nodeNameSlice
} else {
nodeList = &v1.NodeList{}
for _, node := range nodes {
nodeList.Items = append(nodeList.Items, *node)
}
}
2019-12-12 01:27:03 +00:00
args = &extenderv1.ExtenderArgs{
2019-01-12 04:58:27 +00:00
Pod: pod,
Nodes: nodeList,
NodeNames: nodeNames,
}
if err := h.send(h.filterVerb, args, &result); err != nil {
return nil, nil, nil, err
2019-01-12 04:58:27 +00:00
}
if result.Error != "" {
return nil, nil, nil, fmt.Errorf(result.Error)
2019-01-12 04:58:27 +00:00
}
if h.nodeCacheCapable && result.NodeNames != nil {
2019-09-27 21:51:53 +00:00
nodeResult = make([]*v1.Node, len(*result.NodeNames))
for i, nodeName := range *result.NodeNames {
2020-03-26 21:07:15 +00:00
if n, ok := fromNodeName[nodeName]; ok {
nodeResult[i] = n
2019-09-27 21:51:53 +00:00
} else {
return nil, nil, nil, fmt.Errorf(
2020-03-26 21:07:15 +00:00
"extender %q claims a filtered node %q which is not found in the input node list",
2019-09-27 21:51:53 +00:00
h.extenderURL, nodeName)
}
2019-01-12 04:58:27 +00:00
}
} else if result.Nodes != nil {
2019-09-27 21:51:53 +00:00
nodeResult = make([]*v1.Node, len(result.Nodes.Items))
2019-01-12 04:58:27 +00:00
for i := range result.Nodes.Items {
2019-09-27 21:51:53 +00:00
nodeResult[i] = &result.Nodes.Items[i]
2019-01-12 04:58:27 +00:00
}
}
return nodeResult, result.FailedNodes, result.FailedAndUnresolvableNodes, nil
2019-01-12 04:58:27 +00:00
}
// Prioritize based on extender implemented priority functions. Weight*priority is added
// up for each such priority function. The returned score is added to the score computed
// by Kubernetes scheduler. The total score is used to do the host selection.
2019-12-12 01:27:03 +00:00
func (h *HTTPExtender) Prioritize(pod *v1.Pod, nodes []*v1.Node) (*extenderv1.HostPriorityList, int64, error) {
2019-01-12 04:58:27 +00:00
var (
2019-12-12 01:27:03 +00:00
result extenderv1.HostPriorityList
2019-01-12 04:58:27 +00:00
nodeList *v1.NodeList
nodeNames *[]string
2019-12-12 01:27:03 +00:00
args *extenderv1.ExtenderArgs
2019-01-12 04:58:27 +00:00
)
if h.prioritizeVerb == "" {
2019-12-12 01:27:03 +00:00
result := extenderv1.HostPriorityList{}
2019-01-12 04:58:27 +00:00
for _, node := range nodes {
2019-12-12 01:27:03 +00:00
result = append(result, extenderv1.HostPriority{Host: node.Name, Score: 0})
2019-01-12 04:58:27 +00:00
}
return &result, 0, nil
}
if h.nodeCacheCapable {
nodeNameSlice := make([]string, 0, len(nodes))
for _, node := range nodes {
nodeNameSlice = append(nodeNameSlice, node.Name)
}
nodeNames = &nodeNameSlice
} else {
nodeList = &v1.NodeList{}
for _, node := range nodes {
nodeList.Items = append(nodeList.Items, *node)
}
}
2019-12-12 01:27:03 +00:00
args = &extenderv1.ExtenderArgs{
2019-01-12 04:58:27 +00:00
Pod: pod,
Nodes: nodeList,
NodeNames: nodeNames,
}
if err := h.send(h.prioritizeVerb, args, &result); err != nil {
return nil, 0, err
}
return &result, h.weight, nil
}
// Bind delegates the action of binding a pod to a node to the extender.
func (h *HTTPExtender) Bind(binding *v1.Binding) error {
2019-12-12 01:27:03 +00:00
var result extenderv1.ExtenderBindingResult
2019-01-12 04:58:27 +00:00
if !h.IsBinder() {
// This shouldn't happen as this extender wouldn't have become a Binder.
return fmt.Errorf("unexpected empty bindVerb in extender")
2019-01-12 04:58:27 +00:00
}
2019-12-12 01:27:03 +00:00
req := &extenderv1.ExtenderBindingArgs{
2019-01-12 04:58:27 +00:00
PodName: binding.Name,
PodNamespace: binding.Namespace,
PodUID: binding.UID,
Node: binding.Target.Name,
}
if err := h.send(h.bindVerb, req, &result); err != nil {
2019-01-12 04:58:27 +00:00
return err
}
if result.Error != "" {
return fmt.Errorf(result.Error)
}
return nil
}
// IsBinder returns whether this extender is configured for the Bind method.
func (h *HTTPExtender) IsBinder() bool {
return h.bindVerb != ""
}
// Helper function to send messages to the extender
func (h *HTTPExtender) send(action string, args interface{}, result interface{}) error {
out, err := json.Marshal(args)
if err != nil {
return err
}
url := strings.TrimRight(h.extenderURL, "/") + "/" + action
req, err := http.NewRequest("POST", url, bytes.NewReader(out))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
resp, err := h.client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("failed %v with extender at URL %v, code %v", action, url, resp.StatusCode)
2019-01-12 04:58:27 +00:00
}
return json.NewDecoder(resp.Body).Decode(result)
}
// IsInterested returns true if at least one extended resource requested by
// this pod is managed by this extender.
func (h *HTTPExtender) IsInterested(pod *v1.Pod) bool {
if h.managedResources.Len() == 0 {
return true
}
if h.hasManagedResources(pod.Spec.Containers) {
return true
}
if h.hasManagedResources(pod.Spec.InitContainers) {
return true
}
return false
}
func (h *HTTPExtender) hasManagedResources(containers []v1.Container) bool {
for i := range containers {
container := &containers[i]
for resourceName := range container.Resources.Requests {
if h.managedResources.Has(string(resourceName)) {
return true
}
}
for resourceName := range container.Resources.Limits {
if h.managedResources.Has(string(resourceName)) {
return true
}
}
}
return false
}