Update CronJob version to v1 (#1793)

* Update CronJob version to v1 (from v1beta1, unavaliable from 1.25 onwards).

* Try both batch/v1beta1 (for legacy) and batch/v1

* Revert previous commit. Use c.GVR() to find the correct version. Make
cronjob (un)suspend version aware.

* Simply changes by dropping support for deprecated batch/v1beta1 version
mine
SammyA 2022-10-18 14:30:47 +02:00 committed by GitHub
parent c66002d986
commit 8d40c60339
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 8 deletions

View File

@ -17,7 +17,6 @@ import (
const (
maxJobNameSize = 42
cronJobGVR = "batch/v1beta1/cronjobs"
jobGVR = "batch/v1/jobs"
)
@ -42,7 +41,7 @@ func (c *CronJob) Run(path string) error {
return fmt.Errorf("user is not authorized to run jobs")
}
o, err := c.GetFactory().Get(cronJobGVR, path, true, labels.Everything())
o, err := c.GetFactory().Get(c.GVR(), path, true, labels.Everything())
if err != nil {
return err
}
@ -63,7 +62,7 @@ func (c *CronJob) Run(path string) error {
Labels: cj.Spec.JobTemplate.Labels,
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: "batch/v1beta1",
APIVersion: c.gvr.G() + "/" + c.gvr.V(),
Kind: "CronJob",
BlockOwnerDeletion: &true,
Name: cj.Name,
@ -113,23 +112,22 @@ func (c *CronJob) ScanSA(ctx context.Context, fqn string, wait bool) (Refs, erro
// ToggleSuspend toggles suspend/resume on a CronJob.
func (c *CronJob) ToggleSuspend(ctx context.Context, path string) error {
ns, n := client.Namespaced(path)
auth, err := c.Client().CanI(cronJobGVR, ns, []string{client.GetVerb, client.UpdateVerb})
auth, err := c.Client().CanI(c.GVR(), ns, []string{client.GetVerb, client.UpdateVerb})
if err != nil {
return err
}
if !auth {
return fmt.Errorf("user is not authorized to run jobs")
return fmt.Errorf("user is not authorized to (un)suspend cronjobs")
}
dial, err := c.Client().Dial()
if err != nil {
return err
}
cj, err := dial.BatchV1beta1().CronJobs(ns).Get(ctx, n, metav1.GetOptions{})
cj, err := dial.BatchV1().CronJobs(ns).Get(ctx, n, metav1.GetOptions{})
if err != nil {
return err
}
if cj.Spec.Suspend != nil {
current := !*cj.Spec.Suspend
cj.Spec.Suspend = &current
@ -137,7 +135,7 @@ func (c *CronJob) ToggleSuspend(ctx context.Context, path string) error {
true := true
cj.Spec.Suspend = &true
}
_, err = dial.BatchV1beta1().CronJobs(ns).Update(ctx, cj, metav1.UpdateOptions{})
_, err = dial.BatchV1().CronJobs(ns).Update(ctx, cj, metav1.UpdateOptions{})
return err
}