Skip to content

Commit

Permalink
adding status to Admission Controller
Browse files Browse the repository at this point in the history
  • Loading branch information
krzysied committed Jan 22, 2020
1 parent 5cbd278 commit 7de0eb1
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 1 deletion.
10 changes: 10 additions & 0 deletions vertical-pod-autoscaler/deploy/vpa-rbac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,16 @@ rules:
- get
- list
- watch
- apiGroups:
- "coordination.k8s.io"
resources:
- leases
verbs:
- create
- update
- get
- list
- watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
Expand Down
1 change: 1 addition & 0 deletions vertical-pod-autoscaler/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.12
require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/golang/mock v1.3.1
github.com/google/uuid v1.1.1
github.com/prometheus/client_golang v0.9.2
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4 // indirect
github.com/prometheus/common v0.4.1 // indirect
Expand Down
1 change: 1 addition & 0 deletions vertical-pod-autoscaler/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ github.com/google/gofuzz v1.0.0 h1:A8PeW59pxE9IoFRqBp37U+mSNaQoZ46F1f0f863XSXw=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gnostic v0.0.0-20170729233727-0c5108395e2d h1:7XGaL1e6bYS1yIonGp9761ExpPPV1ui0SAC59Yube9k=
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
Copyright 2020 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 logic

import (
"time"

"k8s.io/autoscaler/vertical-pod-autoscaler/pkg/utils/status"
clientset "k8s.io/client-go/kubernetes"
"k8s.io/klog"
)

const (
// VpaAdmissionControllerStatusName is the name of
// the Admission Controller status object.
VpaAdmissionControllerStatusName = "vpa-admission-controller"
// VpaAdmissionControllerStatusNamespace is the namespace of
// the Admission Controller status object.
VpaAdmissionControllerStatusNamespace = "kube-system"

updateInterval = 10 * time.Second
)

// StatusUpdater periodically updates Admission Controller status.
type StatusUpdater struct {
client *status.Client
stopCh chan struct{}
}

// NewStatusUpdater returns a new status updater.
func NewStatusUpdater(c clientset.Interface, holderIdentity string) *StatusUpdater {
return &StatusUpdater{
client: status.NewClient(
c,
VpaAdmissionControllerStatusName,
VpaAdmissionControllerStatusNamespace,
updateInterval,
holderIdentity,
),
stopCh: make(chan struct{}),
}
}

// Start starts status updates.
func (su *StatusUpdater) Start() {
go func() {
for {
select {
case <-su.stopCh:
return
case <-time.After(updateInterval):
if err := su.client.UpdateStatus(); err != nil {
klog.Errorf("Admission Controller status update failed: %v", err)
}
}
}
}()
}

// Stop stops status updates.
func (su *StatusUpdater) Stop() {
close(su.stopCh)
}
13 changes: 12 additions & 1 deletion vertical-pod-autoscaler/pkg/admission-controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ func main() {
}
recommendationProvider := logic.NewRecommendationProvider(limitRangeCalculator, vpa_api_util.NewCappingRecommendationProcessor(limitRangeCalculator), targetSelectorFetcher, vpaLister)

hostname, err := os.Hostname()
if err != nil {
klog.Fatalf("Unable to get hostname: %v", err)
}
statusUpdater := logic.NewStatusUpdater(kubeClient, hostname)
defer statusUpdater.Stop()

as := logic.NewAdmissionServer(recommendationProvider, podPreprocessor, vpaPreprocessor, limitRangeCalculator)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
as.Serve(w, r)
Expand All @@ -100,6 +107,10 @@ func main() {
TLSConfig: configTLS(clientset, certs.serverCert, certs.serverKey),
}
url := fmt.Sprintf("%v:%v", *webhookAddress, *webhookPort)
go selfRegistration(clientset, certs.caCert, &namespace, url, *registerByURL)
go func() {
selfRegistration(clientset, certs.caCert, &namespace, url, *registerByURL)
// Start status updates after the webhook is initialized.
statusUpdater.Start()
}()
server.ListenAndServeTLS("", "")
}

0 comments on commit 7de0eb1

Please sign in to comment.