Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for cronjobs in VPA #2195

Merged
merged 1 commit into from
Jul 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions vertical-pod-autoscaler/deploy/vpa-rbac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ rules:
- batch
resources:
- jobs
- cronjobs
verbs:
- get
- list
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

appsv1 "k8s.io/api/apps/v1"
batchv1 "k8s.io/api/batch/v1"
batchv1beta1 "k8s.io/api/batch/v1beta1"
corev1 "k8s.io/api/core/v1"
apimeta "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -48,10 +49,9 @@ const (
statefulSet wellKnownController = "StatefulSet"
replicationController wellKnownController = "ReplicationController"
job wellKnownController = "Job"
cronJob wellKnownController = "CronJob"
)

var wellKnownControllers = []wellKnownController{daemonSet, deployment, replicaSet, statefulSet, replicationController, job}

const (
discoveryResetPeriod time.Duration = 5 * time.Minute
)
Expand Down Expand Up @@ -102,6 +102,7 @@ func NewControllerFetcher(config *rest.Config, kubeClient kube_client.Interface,
statefulSet: factory.Apps().V1().StatefulSets().Informer(),
replicationController: factory.Core().V1().ReplicationControllers().Informer(),
job: factory.Batch().V1().Jobs().Informer(),
cronJob: factory.Batch().V1beta1().CronJobs().Informer(),
}

for kind, informer := range informersMap {
Expand Down Expand Up @@ -182,6 +183,12 @@ func getParentOfWellKnownController(informer cache.SharedIndexInformer, controll
return nil, fmt.Errorf("Failed to parse %s %s/%s", kind, namespace, name)
}
return getOwnerController(apiObj.OwnerReferences, namespace), nil
case (*batchv1beta1.CronJob):
apiObj, ok := obj.(*batchv1beta1.CronJob)
if !ok {
return nil, fmt.Errorf("Failed to parse %s %s/%s", kind, namespace, name)
}
return getOwnerController(apiObj.OwnerReferences, namespace), nil
case (*corev1.ReplicationController):
apiObj, ok := obj.(*corev1.ReplicationController)
if !ok {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ import (

appsv1 "k8s.io/api/apps/v1"
batchv1 "k8s.io/api/batch/v1"
batchv1beta1 "k8s.io/api/batch/v1beta1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/cache"
)

var wellKnownControllers = []wellKnownController{daemonSet, deployment, replicaSet, statefulSet, replicationController, job, cronJob}
var trueVar = true

func simpleControllerFetcher() *controllerFetcher {
Expand Down Expand Up @@ -153,18 +155,49 @@ func TestControllerFetcher(t *testing.T) {
},
{
key: &ControllerKeyWithAPIVersion{ControllerKey: ControllerKey{
Name: "test-job", Kind: "Job", Namespace: "test-namesapce"}},
Name: "test-job", Kind: "Job", Namespace: "test-namespace"}},
objects: []runtime.Object{&batchv1.Job{
TypeMeta: metav1.TypeMeta{
Kind: "Job",
},
ObjectMeta: metav1.ObjectMeta{
Name: "test-job",
Namespace: "test-namesapce",
Namespace: "test-namespace",
OwnerReferences: []metav1.OwnerReference{
{
Controller: &trueVar,
Kind: "CronJob",
Name: "test-cronjob",
},
},
},
}, &batchv1beta1.CronJob{
TypeMeta: metav1.TypeMeta{
Kind: "CronJob",
},
ObjectMeta: metav1.ObjectMeta{
Name: "test-cronjob",
Namespace: "test-namespace",
},
}},
expectedKey: &ControllerKeyWithAPIVersion{ControllerKey: ControllerKey{
Name: "test-cronjob", Kind: "CronJob", Namespace: "test-namespace"}}, // CronJob has no parent
expectedError: nil,
},
{
key: &ControllerKeyWithAPIVersion{ControllerKey: ControllerKey{
Name: "test-cronjob", Kind: "CronJob", Namespace: "test-namespace"}},
objects: []runtime.Object{&batchv1beta1.CronJob{
TypeMeta: metav1.TypeMeta{
Kind: "CronJob",
},
ObjectMeta: metav1.ObjectMeta{
Name: "test-cronjob",
Namespace: "test-namespace",
},
}},
expectedKey: &ControllerKeyWithAPIVersion{ControllerKey: ControllerKey{
Name: "test-job", Kind: "Job", Namespace: "test-namesapce"}}, // Job has no parent
Name: "test-cronjob", Kind: "CronJob", Namespace: "test-namespace"}}, // CronJob has no parent
expectedError: nil,
},
{
Expand Down
9 changes: 9 additions & 0 deletions vertical-pod-autoscaler/pkg/target/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

appsv1 "k8s.io/api/apps/v1"
batchv1 "k8s.io/api/batch/v1"
batchv1beta1 "k8s.io/api/batch/v1beta1"
corev1 "k8s.io/api/core/v1"
apimeta "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -61,6 +62,7 @@ const (
statefulSet wellKnownController = "StatefulSet"
replicationController wellKnownController = "ReplicationController"
job wellKnownController = "Job"
cronJob wellKnownController = "CronJob"
)

// NewVpaTargetSelectorFetcher returns new instance of VpaTargetSelectorFetcher
Expand All @@ -84,6 +86,7 @@ func NewVpaTargetSelectorFetcher(config *rest.Config, kubeClient kube_client.Int
statefulSet: factory.Apps().V1().StatefulSets().Informer(),
replicationController: factory.Core().V1().ReplicationControllers().Informer(),
job: factory.Batch().V1().Jobs().Informer(),
cronJob: factory.Batch().V1beta1().CronJobs().Informer(),
}

for kind, informer := range informersMap {
Expand Down Expand Up @@ -181,6 +184,12 @@ func getLabelSelector(informer cache.SharedIndexInformer, kind, namespace, name
return nil, fmt.Errorf("Failed to parse %s %s/%s", kind, namespace, name)
}
return metav1.LabelSelectorAsSelector(apiObj.Spec.Selector)
case (*batchv1beta1.CronJob):
apiObj, ok := obj.(*batchv1beta1.CronJob)
if !ok {
return nil, fmt.Errorf("Failed to parse %s %s/%s", kind, namespace, name)
}
return metav1.LabelSelectorAsSelector(metav1.SetAsLabelSelector(apiObj.Spec.JobTemplate.Spec.Template.Labels))
case (*corev1.ReplicationController):
apiObj, ok := obj.(*corev1.ReplicationController)
if !ok {
Expand Down