Skip to content

Commit b43e04e

Browse files
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
1 parent 3d2ca7a commit b43e04e

File tree

2 files changed

+22
-22
lines changed

2 files changed

+22
-22
lines changed

backend/service/k8s/cronjob.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"github.com/golang/protobuf/ptypes/wrappers"
88
"google.golang.org/grpc/codes"
99
"google.golang.org/grpc/status"
10-
v1beta1 "k8s.io/api/batch/v1beta1"
10+
"k8s.io/api/batch/v1"
1111
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1212

1313
k8sapiv1 "github.com/lyft/clutch/backend/api/k8s/v1"
@@ -19,16 +19,16 @@ func (s *svc) DescribeCronJob(ctx context.Context, clientset, cluster, namespace
1919
return nil, err
2020
}
2121

22-
cronJobs, err := cs.BatchV1beta1().CronJobs(cs.Namespace()).List(ctx, metav1.ListOptions{
22+
cronJobs, err := cs.BatchV1().CronJobs(cs.Namespace()).List(ctx, metav1.ListOptions{
2323
FieldSelector: "metadata.name=" + name,
2424
})
2525
if err != nil {
2626
return nil, err
2727
}
28-
2928
if len(cronJobs.Items) == 1 {
3029
return ProtoForCronJob(cs.Cluster(), &cronJobs.Items[0]), nil
31-
} else if len(cronJobs.Items) > 1 {
30+
}
31+
if len(cronJobs.Items) > 1 {
3232
return nil, status.Error(codes.FailedPrecondition, "located multiple cron jobs")
3333
}
3434
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
4545
return nil, err
4646
}
4747

48-
cronJobList, err := cs.BatchV1beta1().CronJobs(cs.Namespace()).List(ctx, opts)
48+
cronJobList, err := cs.BatchV1().CronJobs(cs.Namespace()).List(ctx, opts)
4949
if err != nil {
5050
return nil, err
5151
}
@@ -55,7 +55,6 @@ func (s *svc) ListCronJobs(ctx context.Context, clientset, cluster, namespace st
5555
cronJob := d
5656
cronJobs = append(cronJobs, ProtoForCronJob(cs.Cluster(), &cronJob))
5757
}
58-
5958
return cronJobs, nil
6059
}
6160

@@ -66,10 +65,10 @@ func (s *svc) DeleteCronJob(ctx context.Context, clientset, cluster, namespace,
6665
}
6766

6867
opts := metav1.DeleteOptions{}
69-
return cs.BatchV1beta1().CronJobs(cs.Namespace()).Delete(ctx, name, opts)
68+
return cs.BatchV1().CronJobs(cs.Namespace()).Delete(ctx, name, opts)
7069
}
7170

72-
func ProtoForCronJob(cluster string, k8scronJob *v1beta1.CronJob) *k8sapiv1.CronJob {
71+
func ProtoForCronJob(cluster string, k8scronJob *v1.CronJob) *k8sapiv1.CronJob {
7372
clusterName := GetKubeClusterName(k8scronJob)
7473
if clusterName == "" {
7574
clusterName = cluster

backend/service/k8s/cronjob_test.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,25 @@ import (
66
"testing"
77

88
"github.com/stretchr/testify/assert"
9-
v1beta1 "k8s.io/api/batch/v1beta1"
9+
batchv1 "k8s.io/api/batch/v1"
1010
v1 "k8s.io/api/core/v1"
1111
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1212
"k8s.io/client-go/kubernetes/fake"
1313

1414
k8sapiv1 "github.com/lyft/clutch/backend/api/k8s/v1"
1515
)
1616

17-
func testCronService() *svc {
18-
cron := &v1beta1.CronJob{
17+
func testCronService(t *testing.T) *svc {
18+
var cs *fake.Clientset
19+
cron := &batchv1.CronJob{
1920
ObjectMeta: metav1.ObjectMeta{
2021
Name: "testing-cron-name",
2122
Namespace: "testing-namespace",
2223
Labels: map[string]string{"test": "foo"},
2324
Annotations: map[string]string{"test": "bar"},
2425
},
2526
}
26-
27-
cs := fake.NewSimpleClientset(cron)
27+
cs = fake.NewSimpleClientset(cron)
2828
return &svc{
2929
manager: &managerImpl{
3030
clientsets: map[string]*ctxClientsetImpl{"foo": {
@@ -37,18 +37,19 @@ func testCronService() *svc {
3737
}
3838

3939
func TestDescribeCron(t *testing.T) {
40-
s := testCronService()
40+
s := testCronService(t)
4141
cron, err := s.DescribeCronJob(context.Background(), "foo", "core-testing", "testing-namespace", "testing-cron-name")
4242
assert.NoError(t, err)
4343
assert.NotNil(t, cron)
4444
}
4545

4646
func TestListCron(t *testing.T) {
47-
s := testCronService()
47+
s := testCronService(t)
4848
opts := &k8sapiv1.ListOptions{Labels: map[string]string{"test": "foo"}}
4949
list, err := s.ListCronJobs(context.Background(), "foo", "core-testing", "testing-namespace", opts)
5050
assert.NoError(t, err)
5151
assert.Equal(t, 1, len(list))
52+
5253
// Not Found
5354
opts = &k8sapiv1.ListOptions{Labels: map[string]string{"unknown": "bar"}}
5455
list, err = s.ListCronJobs(context.Background(), "foo", "core-testing", "testing-namespace", opts)
@@ -57,7 +58,7 @@ func TestListCron(t *testing.T) {
5758
}
5859

5960
func TestDeleteCron(t *testing.T) {
60-
s := testCronService()
61+
s := testCronService(t)
6162
// Not found.
6263
err := s.DeleteCronJob(context.Background(), "foo", "core-testing", "testing-namespace", "abc")
6364
assert.Error(t, err)
@@ -78,14 +79,14 @@ func TestProtoForCron(t *testing.T) {
7879
inputClusterName string
7980
expectedClusterName string
8081
expectedName string
81-
cron *v1beta1.CronJob
82+
cron *batchv1.CronJob
8283
}{
8384
{
8485
id: "clustername already set",
8586
inputClusterName: "abc",
8687
expectedClusterName: "production",
8788
expectedName: "test1",
88-
cron: &v1beta1.CronJob{
89+
cron: &batchv1.CronJob{
8990
ObjectMeta: metav1.ObjectMeta{
9091
Labels: map[string]string{
9192
clutchLabelClusterName: "production",
@@ -99,20 +100,20 @@ func TestProtoForCron(t *testing.T) {
99100
inputClusterName: "staging",
100101
expectedClusterName: "staging",
101102
expectedName: "test2",
102-
cron: &v1beta1.CronJob{
103+
cron: &batchv1.CronJob{
103104
ObjectMeta: metav1.ObjectMeta{
104105
Labels: map[string]string{
105106
clutchLabelClusterName: "",
106107
},
107108
Name: "test2",
108109
},
109-
Spec: v1beta1.CronJobSpec{
110-
ConcurrencyPolicy: v1beta1.AllowConcurrent,
110+
Spec: batchv1.CronJobSpec{
111+
ConcurrencyPolicy: batchv1.AllowConcurrent,
111112
Schedule: "5 4 * * *",
112113
Suspend: &[]bool{true}[0],
113114
StartingDeadlineSeconds: &[]int64{69}[0],
114115
},
115-
Status: v1beta1.CronJobStatus{
116+
Status: batchv1.CronJobStatus{
116117
Active: []v1.ObjectReference{{}, {}},
117118
},
118119
},

0 commit comments

Comments
 (0)