Skip to content

Commit

Permalink
Support upgrade CronJob api version (#2844)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
samrabelachew authored Nov 14, 2023
1 parent 3d2ca7a commit b43e04e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
15 changes: 7 additions & 8 deletions backend/service/k8s/cronjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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")
Expand All @@ -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
}
Expand All @@ -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
}

Expand All @@ -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
Expand Down
29 changes: 15 additions & 14 deletions backend/service/k8s/cronjob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@ 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"

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",
Labels: map[string]string{"test": "foo"},
Annotations: map[string]string{"test": "bar"},
},
}

cs := fake.NewSimpleClientset(cron)
cs = fake.NewSimpleClientset(cron)
return &svc{
manager: &managerImpl{
clientsets: map[string]*ctxClientsetImpl{"foo": {
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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",
Expand All @@ -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{{}, {}},
},
},
Expand Down

0 comments on commit b43e04e

Please sign in to comment.