From b43e04ed55f08e7f33de135bb79b74e1d23aeba5 Mon Sep 17 00:00:00 2001 From: samrabelachew Date: Tue, 14 Nov 2023 10:00:59 -0800 Subject: [PATCH] Support upgrade CronJob api version (#2844) Support upgrade CronJob api version ### Description According to kube's deprecation [guide](https://kubernetes.io/docs/reference/using-api/deprecation-guide/#cronjob-v125), CronJob api on batch/v1beta1 should be deprecated on v1.25 in favor of batch/v1 which was introduced in v1.21. Let's add its support and eventually remove the legacy api as well. ### Testing Performed See unit tests --- backend/service/k8s/cronjob.go | 15 +++++++-------- backend/service/k8s/cronjob_test.go | 29 +++++++++++++++-------------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/backend/service/k8s/cronjob.go b/backend/service/k8s/cronjob.go index d3688d1662..a23d5ceebb 100644 --- a/backend/service/k8s/cronjob.go +++ b/backend/service/k8s/cronjob.go @@ -7,7 +7,7 @@ import ( "github.com/golang/protobuf/ptypes/wrappers" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" - v1beta1 "k8s.io/api/batch/v1beta1" + "k8s.io/api/batch/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" k8sapiv1 "github.com/lyft/clutch/backend/api/k8s/v1" @@ -19,16 +19,16 @@ func (s *svc) DescribeCronJob(ctx context.Context, clientset, cluster, namespace return nil, err } - cronJobs, err := cs.BatchV1beta1().CronJobs(cs.Namespace()).List(ctx, metav1.ListOptions{ + cronJobs, err := cs.BatchV1().CronJobs(cs.Namespace()).List(ctx, metav1.ListOptions{ FieldSelector: "metadata.name=" + name, }) if err != nil { return nil, err } - if len(cronJobs.Items) == 1 { return ProtoForCronJob(cs.Cluster(), &cronJobs.Items[0]), nil - } else if len(cronJobs.Items) > 1 { + } + if len(cronJobs.Items) > 1 { return nil, status.Error(codes.FailedPrecondition, "located multiple cron jobs") } return nil, status.Error(codes.NotFound, "unable to locate specified cron job") @@ -45,7 +45,7 @@ func (s *svc) ListCronJobs(ctx context.Context, clientset, cluster, namespace st return nil, err } - cronJobList, err := cs.BatchV1beta1().CronJobs(cs.Namespace()).List(ctx, opts) + cronJobList, err := cs.BatchV1().CronJobs(cs.Namespace()).List(ctx, opts) if err != nil { return nil, err } @@ -55,7 +55,6 @@ func (s *svc) ListCronJobs(ctx context.Context, clientset, cluster, namespace st cronJob := d cronJobs = append(cronJobs, ProtoForCronJob(cs.Cluster(), &cronJob)) } - return cronJobs, nil } @@ -66,10 +65,10 @@ func (s *svc) DeleteCronJob(ctx context.Context, clientset, cluster, namespace, } opts := metav1.DeleteOptions{} - return cs.BatchV1beta1().CronJobs(cs.Namespace()).Delete(ctx, name, opts) + return cs.BatchV1().CronJobs(cs.Namespace()).Delete(ctx, name, opts) } -func ProtoForCronJob(cluster string, k8scronJob *v1beta1.CronJob) *k8sapiv1.CronJob { +func ProtoForCronJob(cluster string, k8scronJob *v1.CronJob) *k8sapiv1.CronJob { clusterName := GetKubeClusterName(k8scronJob) if clusterName == "" { clusterName = cluster diff --git a/backend/service/k8s/cronjob_test.go b/backend/service/k8s/cronjob_test.go index 90cbdabcd2..ec3d646ba2 100644 --- a/backend/service/k8s/cronjob_test.go +++ b/backend/service/k8s/cronjob_test.go @@ -6,7 +6,7 @@ import ( "testing" "github.com/stretchr/testify/assert" - v1beta1 "k8s.io/api/batch/v1beta1" + batchv1 "k8s.io/api/batch/v1" v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes/fake" @@ -14,8 +14,9 @@ import ( k8sapiv1 "github.com/lyft/clutch/backend/api/k8s/v1" ) -func testCronService() *svc { - cron := &v1beta1.CronJob{ +func testCronService(t *testing.T) *svc { + var cs *fake.Clientset + cron := &batchv1.CronJob{ ObjectMeta: metav1.ObjectMeta{ Name: "testing-cron-name", Namespace: "testing-namespace", @@ -23,8 +24,7 @@ func testCronService() *svc { Annotations: map[string]string{"test": "bar"}, }, } - - cs := fake.NewSimpleClientset(cron) + cs = fake.NewSimpleClientset(cron) return &svc{ manager: &managerImpl{ clientsets: map[string]*ctxClientsetImpl{"foo": { @@ -37,18 +37,19 @@ func testCronService() *svc { } func TestDescribeCron(t *testing.T) { - s := testCronService() + s := testCronService(t) cron, err := s.DescribeCronJob(context.Background(), "foo", "core-testing", "testing-namespace", "testing-cron-name") assert.NoError(t, err) assert.NotNil(t, cron) } func TestListCron(t *testing.T) { - s := testCronService() + s := testCronService(t) opts := &k8sapiv1.ListOptions{Labels: map[string]string{"test": "foo"}} list, err := s.ListCronJobs(context.Background(), "foo", "core-testing", "testing-namespace", opts) assert.NoError(t, err) assert.Equal(t, 1, len(list)) + // Not Found opts = &k8sapiv1.ListOptions{Labels: map[string]string{"unknown": "bar"}} list, err = s.ListCronJobs(context.Background(), "foo", "core-testing", "testing-namespace", opts) @@ -57,7 +58,7 @@ func TestListCron(t *testing.T) { } func TestDeleteCron(t *testing.T) { - s := testCronService() + s := testCronService(t) // Not found. err := s.DeleteCronJob(context.Background(), "foo", "core-testing", "testing-namespace", "abc") assert.Error(t, err) @@ -78,14 +79,14 @@ func TestProtoForCron(t *testing.T) { inputClusterName string expectedClusterName string expectedName string - cron *v1beta1.CronJob + cron *batchv1.CronJob }{ { id: "clustername already set", inputClusterName: "abc", expectedClusterName: "production", expectedName: "test1", - cron: &v1beta1.CronJob{ + cron: &batchv1.CronJob{ ObjectMeta: metav1.ObjectMeta{ Labels: map[string]string{ clutchLabelClusterName: "production", @@ -99,20 +100,20 @@ func TestProtoForCron(t *testing.T) { inputClusterName: "staging", expectedClusterName: "staging", expectedName: "test2", - cron: &v1beta1.CronJob{ + cron: &batchv1.CronJob{ ObjectMeta: metav1.ObjectMeta{ Labels: map[string]string{ clutchLabelClusterName: "", }, Name: "test2", }, - Spec: v1beta1.CronJobSpec{ - ConcurrencyPolicy: v1beta1.AllowConcurrent, + Spec: batchv1.CronJobSpec{ + ConcurrencyPolicy: batchv1.AllowConcurrent, Schedule: "5 4 * * *", Suspend: &[]bool{true}[0], StartingDeadlineSeconds: &[]int64{69}[0], }, - Status: v1beta1.CronJobStatus{ + Status: batchv1.CronJobStatus{ Active: []v1.ObjectReference{{}, {}}, }, },