Skip to content

Commit

Permalink
New podTemplate NodeInfoProcessor implementation
Browse files Browse the repository at this point in the history
`nodeInfoWithPodTemplateProcessor` implements the `NodeInfoProcessor`
interface.
This NodeInfoProcessor can be used to assign Pods to a NodeInfo
during scale-up simulation.
The processor looks at PodTemplates to generates Pods from these
templates.
Only PodTemplates with a specific label are selected.
  • Loading branch information
clamoriniere committed Mar 27, 2021
1 parent 9eac752 commit 0356b02
Show file tree
Hide file tree
Showing 8 changed files with 455 additions and 345 deletions.
2 changes: 0 additions & 2 deletions cluster-autoscaler/core/scale_test_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import (
"k8s.io/autoscaler/cluster-autoscaler/processors/nodegroups"
"k8s.io/autoscaler/cluster-autoscaler/processors/nodegroupset"
"k8s.io/autoscaler/cluster-autoscaler/processors/nodeinfos"
"k8s.io/autoscaler/cluster-autoscaler/processors/podtemplates"
"k8s.io/autoscaler/cluster-autoscaler/processors/status"
"k8s.io/autoscaler/cluster-autoscaler/simulator"
"k8s.io/autoscaler/cluster-autoscaler/utils/errors"
Expand Down Expand Up @@ -143,7 +142,6 @@ func NewTestProcessors() *processors.AutoscalingProcessors {
NodeGroupManager: nodegroups.NewDefaultNodeGroupManager(),
NodeInfoProcessor: nodeinfos.NewDefaultNodeInfoProcessor(),
NodeGroupConfigProcessor: nodegroupconfig.NewDefaultNodeGroupConfigProcessor(),
PodTemplateListProcessor: podtemplates.NewDefaultPodTemplateListProcessor(),
}
}

Expand Down
8 changes: 0 additions & 8 deletions cluster-autoscaler/core/static_autoscaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,14 +251,6 @@ func (a *StaticAutoscaler) RunOnce(currentTime time.Time) errors.AutoscalerError
return errors.ToAutoscalerError(errors.ApiCallError, err)
}

extraDaemonsets, err := a.processors.PodTemplateListProcessor.ExtraDaemonsets(autoscalingContext)
if err != nil {
klog.Errorf("Failed to get extra daemonset list: %v", err)
return errors.ToAutoscalerError(errors.ApiCallError, err)
} else if len(extraDaemonsets) > 0 {
daemonsets = append(daemonsets, extraDaemonsets...)
}

// Call CloudProvider.Refresh before any other calls to cloud provider.
err = a.AutoscalingContext.CloudProvider.Refresh()
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions cluster-autoscaler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import (
"k8s.io/autoscaler/cluster-autoscaler/metrics"
ca_processors "k8s.io/autoscaler/cluster-autoscaler/processors"
"k8s.io/autoscaler/cluster-autoscaler/processors/nodegroupset"
"k8s.io/autoscaler/cluster-autoscaler/processors/podtemplates"
"k8s.io/autoscaler/cluster-autoscaler/processors/nodeinfos/podtemplates"
"k8s.io/autoscaler/cluster-autoscaler/simulator"
"k8s.io/autoscaler/cluster-autoscaler/utils/errors"
kube_util "k8s.io/autoscaler/cluster-autoscaler/utils/kubernetes"
Expand Down Expand Up @@ -332,7 +332,7 @@ func buildAutoscaler() (core.Autoscaler, error) {

// Enable PodTemplateListProcessor if needed
if autoscalingOptions.ExtraDaemonsetsFromPodTemplates {
opts.Processors.PodTemplateListProcessor = podtemplates.NewActivePodTemplateListProcessor(kubeClient)
opts.Processors.NodeInfoProcessor = podtemplates.NewNodeInfoWithPodTemplateProcessor(kubeClient)
}

// These metrics should be published only once.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/*
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 podtemplates

import (
"context"
"fmt"
"time"

apiv1 "k8s.io/api/core/v1"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
utilerrors "k8s.io/apimachinery/pkg/util/errors"

client "k8s.io/client-go/kubernetes"
v1lister "k8s.io/client-go/listers/core/v1"
"k8s.io/client-go/tools/cache"

ca_context "k8s.io/autoscaler/cluster-autoscaler/context"
"k8s.io/autoscaler/cluster-autoscaler/processors/nodeinfos"
"k8s.io/autoscaler/cluster-autoscaler/simulator"
"k8s.io/autoscaler/cluster-autoscaler/utils/errors"
schedulerframework "k8s.io/kubernetes/pkg/scheduler/framework"
)

const (
// PodTemplateDaemonSetLabelKey use as label key on PodTemplate corresponding to an extra Daemonset.
PodTemplateDaemonSetLabelKey = "cluster-autoscaler.kubernetes.io/daemonset-pod"
// PodTemplateDaemonSetLabelValueTrue use as PodTemplateDaemonSetLabelKey label value.
PodTemplateDaemonSetLabelValueTrue = "true"
)

// NewNodeInfoWithPodTemplateProcessor returns a default instance of NodeInfoProcessor.
func NewNodeInfoWithPodTemplateProcessor(kubeClient client.Interface) nodeinfos.NodeInfoProcessor {
internalContext, cancelFunc := context.WithCancel(context.Background())

return &nodeInfoWithPodTemplateProcessor{
ctx: internalContext,
cancelFunc: cancelFunc,
podTemplateLister: newPodTemplateLister(kubeClient, internalContext.Done()),
}
}

// nodeInfoWithPodTemplateProcessor add possible PodTemplates in nodeInfos.
type nodeInfoWithPodTemplateProcessor struct {
podTemplateLister v1lister.PodTemplateLister

ctx context.Context
cancelFunc func()
}

// Process returns unchanged nodeInfos.
func (p *nodeInfoWithPodTemplateProcessor) Process(ctx *ca_context.AutoscalingContext, nodeInfosForNodeGroups map[string]*schedulerframework.NodeInfo) (map[string]*schedulerframework.NodeInfo, error) {
// here we can use empty snapshot
clusterSnapshot := simulator.NewBasicClusterSnapshot()

// retrieve only once the podTemplates list.
// This list will be used for each NodeGroup.
podTemplates, err := p.podTemplateLister.List(labels.Everything())
if err != nil {
return nil, errors.ToAutoscalerError(errors.InternalError, err)
}

result := make(map[string]*schedulerframework.NodeInfo, len(nodeInfosForNodeGroups))
var errs []error
for id, nodeInfo := range nodeInfosForNodeGroups {
newNodeInfo, err := getNodeInfoWithPodTemplates(nodeInfo, podTemplates, clusterSnapshot, ctx.PredicateChecker)
if err != nil {
errs = append(errs, err)
}
result[id] = newNodeInfo
}

return result, utilerrors.NewAggregate(errs)
}

// CleanUp cleans up processor's internal structuxres.
func (p *nodeInfoWithPodTemplateProcessor) CleanUp() {
p.cancelFunc()
}

func newPodTemplateLister(kubeClient client.Interface, stopchannel <-chan struct{}) v1lister.PodTemplateLister {
podTemplateWatchOption := func(options *metav1.ListOptions) {
options.FieldSelector = fields.Everything().String()
options.LabelSelector = labels.SelectorFromSet(getDaemonsetPodTemplateLabelSet()).String()
}
listWatcher := cache.NewFilteredListWatchFromClient(kubeClient.CoreV1().RESTClient(), "podtemplates", apiv1.NamespaceAll, podTemplateWatchOption)
store, reflector := cache.NewNamespaceKeyedIndexerAndReflector(listWatcher, &apiv1.PodTemplate{}, time.Hour)
lister := v1lister.NewPodTemplateLister(store)
go reflector.Run(stopchannel)
return lister
}

func getNodeInfoWithPodTemplates(baseNodeInfo *schedulerframework.NodeInfo, podTemplates []*apiv1.PodTemplate, clusterSnapshot *simulator.BasicClusterSnapshot, predicateChecker simulator.PredicateChecker) (*schedulerframework.NodeInfo, error) {
node := baseNodeInfo.Node()
var pods []*apiv1.Pod

for _, podInfo := range baseNodeInfo.Pods {
pods = append(pods, podInfo.Pod)
}
if err := clusterSnapshot.AddNodeWithPods(node, pods); err != nil {
return nil, err
}
// clone baseNodeInfo to not modify the input object.
newNodeInfo := baseNodeInfo.Clone()
for _, podTpl := range podTemplates {
newPod := newPod(podTpl, node.Name)
err := predicateChecker.CheckPredicates(clusterSnapshot, newPod, node.Name)
if err == nil {
newNodeInfo.AddPod(newPod)
} else if err.ErrorType() == simulator.NotSchedulablePredicateError {
// ok; we are just skipping this daemonset
} else {
// unexpected error
return nil, fmt.Errorf("unexpected error while calling PredicateChecker; %v", err)
}
}

return newNodeInfo, nil
}

func getDaemonsetPodTemplateLabelSet() labels.Set {
daemonsetPodTemplateLabels := map[string]string{
PodTemplateDaemonSetLabelKey: PodTemplateDaemonSetLabelValueTrue,
}
return labels.Set(daemonsetPodTemplateLabels)
}

func newPod(pt *apiv1.PodTemplate, nodeName string) *apiv1.Pod {
newPod := &apiv1.Pod{Spec: pt.Template.Spec, ObjectMeta: pt.Template.ObjectMeta}
newPod.Namespace = pt.Namespace
newPod.Name = fmt.Sprintf("%s-pod", pt.Name)
newPod.Spec.NodeName = nodeName
return newPod
}
Loading

0 comments on commit 0356b02

Please sign in to comment.