From 715939d794926d6a8226a275a89f39e9027b5d5a Mon Sep 17 00:00:00 2001 From: Joakim Olsson Date: Fri, 23 Sep 2022 18:38:02 +0200 Subject: [PATCH] chore: update to batch/v1 instead of the deprecated batch/v1beta1 --- main.go | 21 +++++----- main_test.go | 109 +++++++++++++++++++++++++++------------------------ 2 files changed, 68 insertions(+), 62 deletions(-) diff --git a/main.go b/main.go index 581564f..d689964 100644 --- a/main.go +++ b/main.go @@ -3,19 +3,20 @@ package main import ( "context" "fmt" - "github.com/multiplay/go-slack/chat" - "github.com/multiplay/go-slack/webhook" - "github.com/robfig/cron" - "gopkg.in/alecthomas/kingpin.v2" "io" - v1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/client-go/kubernetes" - "k8s.io/client-go/kubernetes/typed/batch/v1beta1" - "k8s.io/client-go/rest" "os" "os/signal" "syscall" "time" + + "github.com/multiplay/go-slack/chat" + "github.com/multiplay/go-slack/webhook" + "github.com/robfig/cron" + "gopkg.in/alecthomas/kingpin.v2" + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/client-go/kubernetes" + batchv1 "k8s.io/client-go/kubernetes/typed/batch/v1" + "k8s.io/client-go/rest" ) var checkFunc = doCheck @@ -56,7 +57,7 @@ func doCheck(client Client, slackUrl string, ic chan os.Signal, sleepTime time.D _, _ = fmt.Fprintf(out, "Got SIGTERM signal, exiting\n") return nil default: - cronJobs, err := client.BatchV1beta1().CronJobs("").List(context.Background(), v1.ListOptions{}) + cronJobs, err := client.BatchV1().CronJobs("").List(context.Background(), v1.ListOptions{}) if err != nil { return fmt.Errorf("error getting cronjobs: %w", err) } @@ -92,7 +93,7 @@ func doCheck(client Client, slackUrl string, ic chan os.Signal, sleepTime time.D } type Client interface { - BatchV1beta1() v1beta1.BatchV1beta1Interface + BatchV1() batchv1.BatchV1Interface } type ClientProvider interface { diff --git a/main_test.go b/main_test.go index 9bda952..4b80e7c 100644 --- a/main_test.go +++ b/main_test.go @@ -14,14 +14,14 @@ import ( "time" "github.com/sanity-io/litter" - beta1 "k8s.io/api/batch/v1beta1" + cronjobv1 "k8s.io/api/batch/v1" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/watch" - v1beta12 "k8s.io/client-go/applyconfigurations/batch/v1beta1" + applyv1 "k8s.io/client-go/applyconfigurations/batch/v1" "k8s.io/client-go/discovery" "k8s.io/client-go/kubernetes" - "k8s.io/client-go/kubernetes/typed/batch/v1beta1" + batchv1 "k8s.io/client-go/kubernetes/typed/batch/v1" "k8s.io/client-go/rest" ) @@ -113,7 +113,7 @@ func Test_doCheck(t *testing.T) { client: &brokenClient{ batchApi: &batchApi{ cronApi: &cronApi{ - listFn: func(_ context.Context, _ v1.ListOptions) (*beta1.CronJobList, error) { + listFn: func(_ context.Context, _ v1.ListOptions) (*cronjobv1.CronJobList, error) { return nil, errors.New("error") }, }, @@ -128,8 +128,8 @@ func Test_doCheck(t *testing.T) { client: &brokenClient{ batchApi: &batchApi{ cronApi: &cronApi{ - listFn: func(_ context.Context, _ v1.ListOptions) (*beta1.CronJobList, error) { - return &beta1.CronJobList{}, nil + listFn: func(_ context.Context, _ v1.ListOptions) (*cronjobv1.CronJobList, error) { + return &cronjobv1.CronJobList{}, nil }, }, }, @@ -144,11 +144,11 @@ func Test_doCheck(t *testing.T) { client: &brokenClient{ batchApi: &batchApi{ cronApi: &cronApi{ - listFn: func(_ context.Context, _ v1.ListOptions) (*beta1.CronJobList, error) { - return &beta1.CronJobList{ - Items: []beta1.CronJob{ + listFn: func(_ context.Context, _ v1.ListOptions) (*cronjobv1.CronJobList, error) { + return &cronjobv1.CronJobList{ + Items: []cronjobv1.CronJob{ { - Spec: beta1.CronJobSpec{Suspend: boolP(true)}, + Spec: cronjobv1.CronJobSpec{Suspend: boolP(true)}, }, }, }, nil @@ -166,11 +166,11 @@ func Test_doCheck(t *testing.T) { client: &brokenClient{ batchApi: &batchApi{ cronApi: &cronApi{ - listFn: func(_ context.Context, _ v1.ListOptions) (*beta1.CronJobList, error) { - return &beta1.CronJobList{ - Items: []beta1.CronJob{ + listFn: func(_ context.Context, _ v1.ListOptions) (*cronjobv1.CronJobList, error) { + return &cronjobv1.CronJobList{ + Items: []cronjobv1.CronJob{ { - Spec: beta1.CronJobSpec{Schedule: "abc"}, + Spec: cronjobv1.CronJobSpec{Schedule: "abc"}, }, }, }, nil @@ -187,16 +187,16 @@ func Test_doCheck(t *testing.T) { client: &brokenClient{ batchApi: &batchApi{ cronApi: &cronApi{ - listFn: func(_ context.Context, _ v1.ListOptions) (*beta1.CronJobList, error) { - return &beta1.CronJobList{ - Items: []beta1.CronJob{ + listFn: func(_ context.Context, _ v1.ListOptions) (*cronjobv1.CronJobList, error) { + return &cronjobv1.CronJobList{ + Items: []cronjobv1.CronJob{ { ObjectMeta: v1.ObjectMeta{CreationTimestamp: v1.Time{Time: time.Now()}}, - Spec: beta1.CronJobSpec{Schedule: "* * * * *", Suspend: boolP(false)}, + Spec: cronjobv1.CronJobSpec{Schedule: "* * * * *", Suspend: boolP(false)}, }, { - Spec: beta1.CronJobSpec{Schedule: "* * * * *"}, - Status: beta1.CronJobStatus{LastScheduleTime: &v1.Time{Time: time.Now()}}, + Spec: cronjobv1.CronJobSpec{Schedule: "* * * * *"}, + Status: cronjobv1.CronJobStatus{LastScheduleTime: &v1.Time{Time: time.Now()}}, }, }, }, nil @@ -214,13 +214,13 @@ func Test_doCheck(t *testing.T) { client: &brokenClient{ batchApi: &batchApi{ cronApi: &cronApi{ - listFn: func(_ context.Context, _ v1.ListOptions) (*beta1.CronJobList, error) { - return &beta1.CronJobList{ - Items: []beta1.CronJob{ + listFn: func(_ context.Context, _ v1.ListOptions) (*cronjobv1.CronJobList, error) { + return &cronjobv1.CronJobList{ + Items: []cronjobv1.CronJob{ { ObjectMeta: v1.ObjectMeta{Name: "some-name", Namespace: "some-ns"}, - Spec: beta1.CronJobSpec{Schedule: "* * * * *"}, - Status: beta1.CronJobStatus{LastScheduleTime: &v1.Time{Time: time.Now().Add(-3 * time.Minute)}}, + Spec: cronjobv1.CronJobSpec{Schedule: "* * * * *"}, + Status: cronjobv1.CronJobStatus{LastScheduleTime: &v1.Time{Time: time.Now().Add(-3 * time.Minute)}}, }, }, }, nil @@ -240,13 +240,13 @@ func Test_doCheck(t *testing.T) { client: &brokenClient{ batchApi: &batchApi{ cronApi: &cronApi{ - listFn: func(_ context.Context, _ v1.ListOptions) (*beta1.CronJobList, error) { - return &beta1.CronJobList{ - Items: []beta1.CronJob{ + listFn: func(_ context.Context, _ v1.ListOptions) (*cronjobv1.CronJobList, error) { + return &cronjobv1.CronJobList{ + Items: []cronjobv1.CronJob{ { ObjectMeta: v1.ObjectMeta{Name: "some-name", Namespace: "some-ns"}, - Spec: beta1.CronJobSpec{Schedule: "* * * * *"}, - Status: beta1.CronJobStatus{LastScheduleTime: &v1.Time{Time: time.Now().Add(-3 * time.Minute)}}, + Spec: cronjobv1.CronJobSpec{Schedule: "* * * * *"}, + Status: cronjobv1.CronJobStatus{LastScheduleTime: &v1.Time{Time: time.Now().Add(-3 * time.Minute)}}, }, }, }, nil @@ -266,13 +266,13 @@ func Test_doCheck(t *testing.T) { client: &brokenClient{ batchApi: &batchApi{ cronApi: &cronApi{ - listFn: func(_ context.Context, _ v1.ListOptions) (*beta1.CronJobList, error) { - return &beta1.CronJobList{ - Items: []beta1.CronJob{ + listFn: func(_ context.Context, _ v1.ListOptions) (*cronjobv1.CronJobList, error) { + return &cronjobv1.CronJobList{ + Items: []cronjobv1.CronJob{ { ObjectMeta: v1.ObjectMeta{Name: "some-name", Namespace: "some-ns"}, - Spec: beta1.CronJobSpec{Schedule: "* * * * *"}, - Status: beta1.CronJobStatus{LastScheduleTime: &v1.Time{Time: time.Now().Add(-3 * time.Minute)}}, + Spec: cronjobv1.CronJobSpec{Schedule: "* * * * *"}, + Status: cronjobv1.CronJobStatus{LastScheduleTime: &v1.Time{Time: time.Now().Add(-3 * time.Minute)}}, }, }, }, nil @@ -382,46 +382,51 @@ func (b brokenClientProvider) Provide() (Client, error) { var _ ClientProvider = &brokenClientProvider{} type brokenClient struct { - batchApi v1beta1.BatchV1beta1Interface + batchApi batchv1.BatchV1Interface } -func (b brokenClient) BatchV1beta1() v1beta1.BatchV1beta1Interface { +func (b brokenClient) BatchV1() batchv1.BatchV1Interface { return b.batchApi } var _ Client = &brokenClient{} type batchApi struct { - cronApi v1beta1.CronJobInterface + cronApi batchv1.CronJobInterface } func (b batchApi) RESTClient() rest.Interface { panic("implement me") } -func (b batchApi) CronJobs(namespace string) v1beta1.CronJobInterface { +func (b batchApi) CronJobs(namespace string) batchv1.CronJobInterface { return b.cronApi } -var _ v1beta1.BatchV1beta1Interface = &batchApi{} - -type cronApi struct { - listFn func(ctx context.Context, opts v1.ListOptions) (*beta1.CronJobList, error) +func (b batchApi) Jobs(namespace string) batchv1.JobInterface { + //TODO implement me + panic("implement me") } -func (c cronApi) List(ctx context.Context, opts v1.ListOptions) (*beta1.CronJobList, error) { +var _ batchv1.BatchV1Interface = &batchApi{} + +type cronApi struct { + listFn func(ctx context.Context, opts v1.ListOptions) (*cronjobv1.CronJobList, error) +} + +func (c cronApi) List(ctx context.Context, opts v1.ListOptions) (*cronjobv1.CronJobList, error) { return c.listFn(ctx, opts) } -func (c cronApi) Create(ctx context.Context, cronJob *beta1.CronJob, opts v1.CreateOptions) (*beta1.CronJob, error) { +func (c cronApi) Create(ctx context.Context, cronJob *cronjobv1.CronJob, opts v1.CreateOptions) (*cronjobv1.CronJob, error) { panic("implement me") } -func (c cronApi) Update(ctx context.Context, cronJob *beta1.CronJob, opts v1.UpdateOptions) (*beta1.CronJob, error) { +func (c cronApi) Update(ctx context.Context, cronJob *cronjobv1.CronJob, opts v1.UpdateOptions) (*cronjobv1.CronJob, error) { panic("implement me") } -func (c cronApi) UpdateStatus(ctx context.Context, cronJob *beta1.CronJob, opts v1.UpdateOptions) (*beta1.CronJob, error) { +func (c cronApi) UpdateStatus(ctx context.Context, cronJob *cronjobv1.CronJob, opts v1.UpdateOptions) (*cronjobv1.CronJob, error) { panic("implement me") } @@ -433,7 +438,7 @@ func (c cronApi) DeleteCollection(ctx context.Context, opts v1.DeleteOptions, li panic("implement me") } -func (c cronApi) Get(ctx context.Context, name string, opts v1.GetOptions) (*beta1.CronJob, error) { +func (c cronApi) Get(ctx context.Context, name string, opts v1.GetOptions) (*cronjobv1.CronJob, error) { panic("implement me") } @@ -441,19 +446,19 @@ func (c cronApi) Watch(ctx context.Context, opts v1.ListOptions) (watch.Interfac panic("implement me") } -func (c cronApi) Patch(ctx context.Context, name string, pt types.PatchType, data []byte, opts v1.PatchOptions, subresources ...string) (result *beta1.CronJob, err error) { +func (c cronApi) Patch(ctx context.Context, name string, pt types.PatchType, data []byte, opts v1.PatchOptions, subresources ...string) (result *cronjobv1.CronJob, err error) { panic("implement me") } -func (c cronApi) Apply(ctx context.Context, cronJob *v1beta12.CronJobApplyConfiguration, opts v1.ApplyOptions) (result *beta1.CronJob, err error) { +func (c cronApi) Apply(ctx context.Context, cronJob *applyv1.CronJobApplyConfiguration, opts v1.ApplyOptions) (result *cronjobv1.CronJob, err error) { panic("implement me") } -func (c cronApi) ApplyStatus(ctx context.Context, cronJob *v1beta12.CronJobApplyConfiguration, opts v1.ApplyOptions) (result *beta1.CronJob, err error) { +func (c cronApi) ApplyStatus(ctx context.Context, cronJob *applyv1.CronJobApplyConfiguration, opts v1.ApplyOptions) (result *cronjobv1.CronJob, err error) { panic("implement me") } -var _ v1beta1.CronJobInterface = &cronApi{} +var _ batchv1.CronJobInterface = &cronApi{} func boolP(b bool) *bool { return &b