Skip to content

Commit

Permalink
Support running addon resizer on the control plane
Browse files Browse the repository at this point in the history
  • Loading branch information
chelseychen committed May 8, 2024
1 parent ac7c53e commit e7146bd
Show file tree
Hide file tree
Showing 291 changed files with 33,309 additions and 104 deletions.
3 changes: 3 additions & 0 deletions addon-resizer/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.21

require (
github.com/golang/glog v1.2.1
github.com/google/go-cmp v0.6.0
github.com/prometheus/client_model v0.6.1
github.com/prometheus/common v0.52.3
github.com/spf13/pflag v1.0.5
Expand All @@ -16,6 +17,7 @@ require (
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
github.com/go-logr/logr v1.3.0 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
Expand All @@ -33,6 +35,7 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pkg/errors v0.9.1 // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/oauth2 v0.18.0 // indirect
golang.org/x/sys v0.18.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions addon-resizer/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/emicklei/go-restful/v3 v3.11.0 h1:rAQeMHw1c7zTmncogyy8VvRZwtkmkZ4FxERmMY4rD+g=
github.com/emicklei/go-restful/v3 v3.11.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc=
github.com/evanphx/json-patch v4.12.0+incompatible h1:4onqiflcdA9EOZ4RxV643DvftH5pOlLGNtQ5lPWQu84=
github.com/evanphx/json-patch v4.12.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
github.com/go-logr/logr v1.3.0 h1:2y3SDp0ZXuc6/cjLSZ+Q3ir+QB9T/iG5yYRXqsagWSY=
github.com/go-logr/logr v1.3.0/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-openapi/jsonpointer v0.19.6 h1:eCs3fxoIi3Wh6vtgmLTOjdhSpiqphQ+DaPn38N2ZdrE=
Expand Down Expand Up @@ -63,6 +65,8 @@ github.com/onsi/ginkgo/v2 v2.13.0 h1:0jY9lJquiL8fcf3M4LAXN5aMlS/b2BV86HFFPCPMgE4
github.com/onsi/ginkgo/v2 v2.13.0/go.mod h1:TE309ZR8s5FsKKpuB1YAQYBzCaAfUgatB/xlT/ETL/o=
github.com/onsi/gomega v1.29.0 h1:KIA/t2t5UBzoirT4H9tsML45GEbo3ouUnBHsCfD2tVg=
github.com/onsi/gomega v1.29.0/go.mod h1:9sxs+SwGrKI0+PWe4Fxa9tFQQBG5xSsSbMXOI8PPpoQ=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E=
Expand Down
10 changes: 5 additions & 5 deletions addon-resizer/nanny/election.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ package nanny

import (
"context"
"github.com/golang/glog"
"os"
"time"
"github.com/golang/glog"

corev1 "k8s.io/api/core/v1"
typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/tools/leaderelection"
"k8s.io/client-go/tools/leaderelection/resourcelock"
"k8s.io/client-go/tools/record"
Expand All @@ -37,8 +37,8 @@ type Config struct {
// wait to force acquire leadership. This is measured against time of
// last observed ack.
LeaseDuration time.Duration
// RenewDeadline is the duration that the acting master will retry
// refreshing leadership before giving up.
// RenewDeadline is the duration that the acting control plane will
// retry refreshing leadership before giving up.
RenewDeadline time.Duration
// RetryPeriod is the duration to wait between tries of actions.
RetryPeriod time.Duration
Expand All @@ -59,7 +59,7 @@ func LeadOrDie(cfg Config, clientset kubernetes.Interface, doLead func()) {
}

lock, err := resourcelock.New(
resourcelock.EndpointsLeasesResourceLock,
resourcelock.LeasesResourceLock,
cfg.SystemNamespace,
"addon-resizer",
clientset.CoreV1(),
Expand Down
8 changes: 6 additions & 2 deletions addon-resizer/nanny/estimator.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ func (e LinearEstimator) scale(clusterSize uint64) *corev1.ResourceRequirements
return calculateResources(clusterSize, e.Resources)
}

// updatedResourceEstimator creates a copy of original estimator
// with updated resources.
func (e LinearEstimator) updatedResourceEstimator(resources []Resource) ResourceEstimator {
newEstimator := LinearEstimator{
Resources: resources,
Expand Down Expand Up @@ -75,9 +77,11 @@ func (e ExponentialEstimator) scale(clusterSize uint64) *corev1.ResourceRequirem
return calculateResources(n, e.Resources)
}

// updatedResourceEstimator creates a copy of original estimator
// with updated resources.
func (e ExponentialEstimator) updatedResourceEstimator(resources []Resource) ResourceEstimator {
newEstimator := ExponentialEstimator{
Resources: resources,
Resources: resources,
MinClusterSize: e.MinClusterSize,
ScaleFactor: e.ScaleFactor,
}
Expand Down Expand Up @@ -121,4 +125,4 @@ func calculateResources(clusterSize uint64, resources []Resource) *corev1.Resour
Limits: limits,
Requests: requests,
}
}
}
51 changes: 25 additions & 26 deletions addon-resizer/nanny/kubernetes_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,12 @@ const (
)

type kubernetesClient struct {
namespace string
deployment string
pod string
container string
clientset *kubernetes.Clientset
useMetrics bool
runOnMaster bool
namespace string
deployment string
pod string
container string
clientset *kubernetes.Clientset
useMetrics bool
}

// CountNodes returns the number of nodes in the cluster:
Expand Down Expand Up @@ -185,25 +184,26 @@ func (k *kubernetesClient) countResourcesThroughMetrics(resourceName string) (ui
}

func (k *kubernetesClient) ContainerResources() (*corev1.ResourceRequirements, error) {
if k.runOnMaster {
var containers []corev1.Container
// When addon resizer runs in a different pod, it cannot get pod name from env,
// so the pod name will be empty. In that case, get container information
// from the deployment instead.
if k.pod == "" {
dep, err := k.clientset.AppsV1().Deployments(k.namespace).Get(context.Background(), k.deployment, metav1.GetOptions{})
if err != nil {
return nil, err
}
for _, container := range dep.Spec.Template.Spec.Containers {
if container.Name == k.container {
return &container.Resources, nil
}
}
containers = dep.Spec.Template.Spec.Containers
} else {
pod, err := k.clientset.CoreV1().Pods(k.namespace).Get(context.Background(), k.pod, metav1.GetOptions{})
if err != nil {
return nil, err
}
for _, container := range pod.Spec.Containers {
if container.Name == k.container {
return &container.Resources, nil
}
containers = pod.Spec.Containers
}
for _, container := range containers {
if container.Name == k.container {
return &container.Resources, nil
}
}
return nil, fmt.Errorf("Container %s was not found in deployment %s in namespace %s.", k.container, k.deployment, k.namespace)
Expand Down Expand Up @@ -275,15 +275,14 @@ func getPodSelectorExcludingDonePodsOrDie() string {
}

// NewKubernetesClient gives a KubernetesClient with the given dependencies.
func NewKubernetesClient(namespace, deployment, pod, container string, clientset *kubernetes.Clientset, useMetrics, runOnMaster bool) KubernetesClient {
func NewKubernetesClient(namespace, deployment, pod, container string, clientset *kubernetes.Clientset, useMetrics bool) KubernetesClient {
result := &kubernetesClient{
namespace: namespace,
deployment: deployment,
pod: pod,
container: container,
clientset: clientset,
useMetrics: useMetrics,
runOnMaster: runOnMaster,
namespace: namespace,
deployment: deployment,
pod: pod,
container: container,
clientset: clientset,
useMetrics: useMetrics,
}
return result
}
}
Loading

0 comments on commit e7146bd

Please sign in to comment.