Skip to content

Commit

Permalink
fix(controller): back CJ API version to batch/v1beta1
Browse files Browse the repository at this point in the history
  • Loading branch information
nettoclaudio committed Oct 7, 2021
1 parent 4ed8f34 commit b4ac46b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 32 deletions.
29 changes: 15 additions & 14 deletions controllers/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/willf/bitset"
autoscalingv2beta2 "k8s.io/api/autoscaling/v2beta2"
batchv1 "k8s.io/api/batch/v1"
batchv1beta1 "k8s.io/api/batch/v1beta1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -325,7 +326,7 @@ func (r *RpaasInstanceReconciler) reconcileCronJobForSessionTickets(ctx context.

newCronJob := newCronJobForSessionTickets(instance)

var cj batchv1.CronJob
var cj batchv1beta1.CronJob
cjName := types.NamespacedName{
Name: newCronJob.Name,
Namespace: newCronJob.Namespace,
Expand Down Expand Up @@ -358,7 +359,7 @@ func (r *RpaasInstanceReconciler) reconcileCronJobForSessionTickets(ctx context.
return r.Client.Update(ctx, newCronJob)
}

func newCronJobForSessionTickets(instance *v1alpha1.RpaasInstance) *batchv1.CronJob {
func newCronJobForSessionTickets(instance *v1alpha1.RpaasInstance) *batchv1beta1.CronJob {
enabled := isTLSSessionTicketEnabled(instance)

keyLength := v1alpha1.DefaultSessionTicketKeyLength
Expand All @@ -376,9 +377,9 @@ func newCronJobForSessionTickets(instance *v1alpha1.RpaasInstance) *batchv1.Cron
image = instance.Spec.TLSSessionResumption.SessionTicket.Image
}
var jobsHistoryLimit int32 = 1
return &batchv1.CronJob{
return &batchv1beta1.CronJob{
TypeMeta: metav1.TypeMeta{
APIVersion: "batch/v1",
APIVersion: "batch/v1beta1",
Kind: "CronJob",
},
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -393,11 +394,11 @@ func newCronJobForSessionTickets(instance *v1alpha1.RpaasInstance) *batchv1.Cron
},
Labels: labelsForRpaasInstance(instance),
},
Spec: batchv1.CronJobSpec{
Spec: batchv1beta1.CronJobSpec{
Schedule: minutesIntervalToSchedule(rotationInterval),
SuccessfulJobsHistoryLimit: &jobsHistoryLimit,
FailedJobsHistoryLimit: &jobsHistoryLimit,
JobTemplate: batchv1.JobTemplateSpec{
JobTemplate: batchv1beta1.JobTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{},
Labels: labelsForRpaasInstance(instance),
Expand Down Expand Up @@ -705,7 +706,7 @@ func (r *RpaasInstanceReconciler) reconcileCacheSnapshot(ctx context.Context, in
}

func (r *RpaasInstanceReconciler) reconcileCacheSnapshotCronJob(ctx context.Context, instance *v1alpha1.RpaasInstance, plan *v1alpha1.RpaasPlan) error {
foundCronJob := &batchv1.CronJob{}
foundCronJob := &batchv1beta1.CronJob{}
cronName := nameForCronJob(instance.Name + cacheSnapshotCronJobSuffix)
err := r.Client.Get(ctx, types.NamespacedName{Name: cronName, Namespace: instance.Namespace}, foundCronJob)
if err != nil && !k8sErrors.IsNotFound(err) {
Expand All @@ -727,7 +728,7 @@ func (r *RpaasInstanceReconciler) reconcileCacheSnapshotCronJob(ctx context.Cont

func (r *RpaasInstanceReconciler) destroyCacheSnapshotCronJob(ctx context.Context, instance *v1alpha1.RpaasInstance) error {
cronName := nameForCronJob(instance.Name + cacheSnapshotCronJobSuffix)
cronJob := &batchv1.CronJob{}
cronJob := &batchv1beta1.CronJob{}

err := r.Client.Get(ctx, types.NamespacedName{Name: cronName, Namespace: instance.Namespace}, cronJob)
isNotFound := k8sErrors.IsNotFound(err)
Expand Down Expand Up @@ -1196,7 +1197,7 @@ func newHPA(instance *v1alpha1.RpaasInstance) autoscalingv2beta2.HorizontalPodAu
}
}

func newCronJob(instance *v1alpha1.RpaasInstance, plan *v1alpha1.RpaasPlan) *batchv1.CronJob {
func newCronJob(instance *v1alpha1.RpaasInstance, plan *v1alpha1.RpaasPlan) *batchv1beta1.CronJob {
cronName := nameForCronJob(instance.Name + cacheSnapshotCronJobSuffix)

schedule := defaultCacheSnapshotSchedule
Expand All @@ -1219,7 +1220,7 @@ func newCronJob(instance *v1alpha1.RpaasInstance, plan *v1alpha1.RpaasPlan) *bat

var jobsHistoryLimit int32 = 1

return &batchv1.CronJob{
return &batchv1beta1.CronJob{
ObjectMeta: metav1.ObjectMeta{
Name: cronName,
Namespace: instance.Namespace,
Expand All @@ -1233,15 +1234,15 @@ func newCronJob(instance *v1alpha1.RpaasInstance, plan *v1alpha1.RpaasPlan) *bat
Labels: labelsForRpaasInstance(instance),
},
TypeMeta: metav1.TypeMeta{
APIVersion: "batch/v1",
APIVersion: "batch/v1beta1",
Kind: "CronJob",
},
Spec: batchv1.CronJobSpec{
Spec: batchv1beta1.CronJobSpec{
Schedule: schedule,
ConcurrencyPolicy: batchv1.ForbidConcurrent,
ConcurrencyPolicy: batchv1beta1.ForbidConcurrent,
SuccessfulJobsHistoryLimit: &jobsHistoryLimit,
FailedJobsHistoryLimit: &jobsHistoryLimit,
JobTemplate: batchv1.JobTemplateSpec{
JobTemplate: batchv1beta1.JobTemplateSpec{
Spec: batchv1.JobSpec{
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Expand Down
32 changes: 16 additions & 16 deletions controllers/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/stretchr/testify/require"
nginxv1alpha1 "github.com/tsuru/nginx-operator/api/v1alpha1"
autoscalingv2beta2 "k8s.io/api/autoscaling/v2beta2"
batchv1 "k8s.io/api/batch/v1"
batchv1beta1 "k8s.io/api/batch/v1beta1"
corev1 "k8s.io/api/core/v1"
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
Expand Down Expand Up @@ -1073,7 +1073,7 @@ func Test_reconcileCacheSnapshotCronJobCreation(t *testing.T) {
err := reconciler.reconcileCacheSnapshotCronJob(ctx, instance1, plan)
require.NoError(t, err)

cronJob := &batchv1.CronJob{}
cronJob := &batchv1beta1.CronJob{}
err = reconciler.Client.Get(context.TODO(), types.NamespacedName{Name: instance1.Name + "-snapshot-cron-job", Namespace: instance1.Namespace}, cronJob)
require.NoError(t, err)

Expand All @@ -1098,11 +1098,11 @@ func Test_reconcileCacheSnapshotCronJobUpdate(t *testing.T) {
instance1 := newEmptyRpaasInstance()
instance1.Name = "instance-1"

previousCronJob := &batchv1.CronJob{
previousCronJob := &batchv1beta1.CronJob{
ObjectMeta: metav1.ObjectMeta{
Name: instance1.Name + "-snapshot-cronjob",
},
Spec: batchv1.CronJobSpec{
Spec: batchv1beta1.CronJobSpec{
Schedule: "old-schedule",
},
}
Expand All @@ -1122,7 +1122,7 @@ func Test_reconcileCacheSnapshotCronJobUpdate(t *testing.T) {
err := reconciler.reconcileCacheSnapshotCronJob(ctx, instance1, plan)
require.NoError(t, err)

cronJob := &batchv1.CronJob{}
cronJob := &batchv1beta1.CronJob{}
err = reconciler.Client.Get(context.TODO(), types.NamespacedName{Name: instance1.Name + "-snapshot-cron-job", Namespace: instance1.Namespace}, cronJob)
require.NoError(t, err)

Expand All @@ -1136,7 +1136,7 @@ func Test_destroySnapshotCronJob(t *testing.T) {
instance1 := newEmptyRpaasInstance()
instance1.Name = "instance-1"

cronJob := &batchv1.CronJob{
cronJob := &batchv1beta1.CronJob{
ObjectMeta: metav1.ObjectMeta{
Name: instance1.Name + "-snapshot-cron-job",
Namespace: instance1.Namespace,
Expand All @@ -1148,7 +1148,7 @@ func Test_destroySnapshotCronJob(t *testing.T) {
err := reconciler.destroyCacheSnapshotCronJob(ctx, instance1)
require.NoError(t, err)

cronJob = &batchv1.CronJob{}
cronJob = &batchv1beta1.CronJob{}

err = reconciler.Client.Get(context.TODO(), types.NamespacedName{Name: instance1.Name + "-snapshot-cron-job", Namespace: instance1.Namespace}, cronJob)
require.True(t, k8sErrors.IsNotFound(err))
Expand Down Expand Up @@ -1840,7 +1840,7 @@ func TestReconcile(t *testing.T) {
{Name: "cache-vol", MountPath: "/var/cache/nginx/rpaas"},
}, initContainer.VolumeMounts)

cronJob := &batchv1.CronJob{}
cronJob := &batchv1beta1.CronJob{}
err = reconciler.Client.Get(context.TODO(), types.NamespacedName{Name: "my-instance-snapshot-cron-job", Namespace: rpaas.Namespace}, cronJob)
require.NoError(t, err)

Expand Down Expand Up @@ -1950,7 +1950,7 @@ func TestReconcileRpaasInstance_reconcileTLSSessionResumption(t *testing.T) {
name string
instance *v1alpha1.RpaasInstance
objects []runtime.Object
assert func(t *testing.T, err error, gotSecret *corev1.Secret, gotCronJob *batchv1.CronJob)
assert func(t *testing.T, err error, gotSecret *corev1.Secret, gotCronJob *batchv1beta1.CronJob)
}{
{
name: "when no TLS session resumption is enabled",
Expand All @@ -1974,7 +1974,7 @@ func TestReconcileRpaasInstance_reconcileTLSSessionResumption(t *testing.T) {
},
},
},
assert: func(t *testing.T, err error, gotSecret *corev1.Secret, gotCronJob *batchv1.CronJob) {
assert: func(t *testing.T, err error, gotSecret *corev1.Secret, gotCronJob *batchv1beta1.CronJob) {
require.NoError(t, err)
require.NotNil(t, gotSecret)

Expand Down Expand Up @@ -2066,7 +2066,7 @@ func TestReconcileRpaasInstance_reconcileTLSSessionResumption(t *testing.T) {
Namespace: "default",
},
},
&batchv1.CronJob{
&batchv1beta1.CronJob{
ObjectMeta: metav1.ObjectMeta{
Name: "my-instance-session-tickets",
Namespace: "default",
Expand All @@ -2089,7 +2089,7 @@ func TestReconcileRpaasInstance_reconcileTLSSessionResumption(t *testing.T) {
},
},
},
assert: func(t *testing.T, err error, gotSecret *corev1.Secret, gotCronJob *batchv1.CronJob) {
assert: func(t *testing.T, err error, gotSecret *corev1.Secret, gotCronJob *batchv1beta1.CronJob) {
require.NoError(t, err)
require.NotNil(t, gotSecret)
require.NotNil(t, gotCronJob)
Expand All @@ -2116,7 +2116,7 @@ func TestReconcileRpaasInstance_reconcileTLSSessionResumption(t *testing.T) {
Namespace: "default",
},
},
&batchv1.CronJob{
&batchv1beta1.CronJob{
ObjectMeta: metav1.ObjectMeta{
Name: "my-instance-session-tickets",
Namespace: "default",
Expand All @@ -2132,7 +2132,7 @@ func TestReconcileRpaasInstance_reconcileTLSSessionResumption(t *testing.T) {
TLSSessionResumption: &v1alpha1.TLSSessionResumption{},
},
},
assert: func(t *testing.T, err error, gotSecret *corev1.Secret, gotCronJob *batchv1.CronJob) {
assert: func(t *testing.T, err error, gotSecret *corev1.Secret, gotCronJob *batchv1beta1.CronJob) {
require.NoError(t, err)
assert.Empty(t, gotSecret.Name)
assert.Empty(t, gotCronJob.Name)
Expand Down Expand Up @@ -2167,7 +2167,7 @@ func TestReconcileRpaasInstance_reconcileTLSSessionResumption(t *testing.T) {
},
},
},
assert: func(t *testing.T, err error, gotSecret *corev1.Secret, gotCronJob *batchv1.CronJob) {
assert: func(t *testing.T, err error, gotSecret *corev1.Secret, gotCronJob *batchv1beta1.CronJob) {
require.NoError(t, err)

expectedKeys := 2
Expand Down Expand Up @@ -2200,7 +2200,7 @@ func TestReconcileRpaasInstance_reconcileTLSSessionResumption(t *testing.T) {
}
r.Client.Get(context.TODO(), secretName, &secret)

var cronJob batchv1.CronJob
var cronJob batchv1beta1.CronJob
cronJobName := types.NamespacedName{
Name: tt.instance.Name + sessionTicketsCronJobSuffix,
Namespace: tt.instance.Namespace,
Expand Down
4 changes: 2 additions & 2 deletions controllers/rpaasinstance_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/go-logr/logr"
cmv1 "github.com/jetstack/cert-manager/pkg/apis/certmanager/v1"
nginxv1alpha1 "github.com/tsuru/nginx-operator/api/v1alpha1"
batchv1 "k8s.io/api/batch/v1"
batchv1beta1 "k8s.io/api/batch/v1beta1"
corev1 "k8s.io/api/core/v1"
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -254,7 +254,7 @@ func (r *RpaasInstanceReconciler) SetupWithManager(mgr ctrl.Manager) error {
Owns(&corev1.ConfigMap{}).
Owns(&corev1.Secret{}).
Owns(&corev1.PersistentVolumeClaim{}).
Owns(&batchv1.CronJob{}).
Owns(&batchv1beta1.CronJob{}).
Owns(&nginxv1alpha1.Nginx{}).
Owns(&cmv1.Certificate{}).
Complete(r)
Expand Down

0 comments on commit b4ac46b

Please sign in to comment.