Skip to content

Commit

Permalink
Merge pull request #2195 from bskiba/cronjob
Browse files Browse the repository at this point in the history
Add support for cronjobs in VPA
  • Loading branch information
k8s-ci-robot authored Jul 18, 2019
2 parents 969337e + 8ff3b4f commit 5ce7457
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 5 deletions.
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

0 comments on commit 5ce7457

Please sign in to comment.