Skip to content

Commit

Permalink
feat: compatible with cronjob v1beta1 (#6687)
Browse files Browse the repository at this point in the history
  • Loading branch information
ldming authored Feb 26, 2024
1 parent 9f91f8c commit 74dd34c
Show file tree
Hide file tree
Showing 19 changed files with 388 additions and 294 deletions.
29 changes: 15 additions & 14 deletions cmd/dataprotection/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import (
"github.com/apecloud/kubeblocks/pkg/constant"
intctrlutil "github.com/apecloud/kubeblocks/pkg/controllerutil"
dptypes "github.com/apecloud/kubeblocks/pkg/dataprotection/types"
dputils "github.com/apecloud/kubeblocks/pkg/dataprotection/utils"
viper "github.com/apecloud/kubeblocks/pkg/viperx"
)

Expand Down Expand Up @@ -203,6 +204,19 @@ func main() {
os.Exit(1)
}

cli, err := discoverycli.NewDiscoveryClientForConfig(mgr.GetConfig())
if err != nil {
setupLog.Error(err, "unable to create discovery client")
os.Exit(1)
}

ver, err := cli.ServerVersion()
if err != nil {
setupLog.Error(err, "unable to discover version info")
os.Exit(1)
}
viper.SetDefault(constant.CfgKeyServerInfo, *ver)

if err = (&dpcontrollers.ActionSetReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Expand Down Expand Up @@ -250,7 +264,7 @@ func main() {
}

if err = (&dpcontrollers.BackupScheduleReconciler{
Client: mgr.GetClient(),
Client: dputils.NewCompatClient(mgr.GetClient()),
Scheme: mgr.GetScheme(),
Recorder: mgr.GetEventRecorderFor("backup-schedule-controller"),
}).SetupWithManager(mgr); err != nil {
Expand Down Expand Up @@ -303,19 +317,6 @@ func main() {
os.Exit(1)
}

cli, err := discoverycli.NewDiscoveryClientForConfig(mgr.GetConfig())
if err != nil {
setupLog.Error(err, "unable to create discovery client")
os.Exit(1)
}

ver, err := cli.ServerVersion()
if err != nil {
setupLog.Error(err, "unable to discover version info")
os.Exit(1)
}
viper.SetDefault(constant.CfgKeyServerInfo, *ver)

setupLog.Info("starting manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
setupLog.Error(err, "problem running manager")
Expand Down
3 changes: 2 additions & 1 deletion controllers/apps/component_hscale_volume_populator.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/apecloud/kubeblocks/pkg/controller/factory"
"github.com/apecloud/kubeblocks/pkg/controller/plan"
intctrlutil "github.com/apecloud/kubeblocks/pkg/controllerutil"
dputils "github.com/apecloud/kubeblocks/pkg/dataprotection/utils"
)

type dataClone interface {
Expand Down Expand Up @@ -454,7 +455,7 @@ func isVolumeSnapshotEnabled(ctx context.Context, cli client.Client,
return false, client.IgnoreNotFound(err)
}

return intctrlutil.IsVolumeSnapshotEnabled(ctx, cli, pvc.Spec.VolumeName)
return dputils.IsVolumeSnapshotEnabled(ctx, cli, pvc.Spec.VolumeName)
}

func getBackupMethods(backupPolicy *dpv1alpha1.BackupPolicy, useVolumeSnapshot bool) []string {
Expand Down
10 changes: 5 additions & 5 deletions controllers/dataprotection/backup_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,15 @@ func (r *BackupReconciler) SetupWithManager(mgr ctrl.Manager) error {
Owns(&batchv1.Job{}).
Watches(&batchv1.Job{}, handler.EnqueueRequestsFromMapFunc(r.parseBackupJob))

if intctrlutil.InVolumeSnapshotV1Beta1() {
b.Owns(&vsv1beta1.VolumeSnapshot{}, builder.Predicates{})
} else {
if dputils.SupportsVolumeSnapshotV1() {
b.Owns(&vsv1.VolumeSnapshot{}, builder.Predicates{})
} else {
b.Owns(&vsv1beta1.VolumeSnapshot{}, builder.Predicates{})
}
return b.Complete(r)
}

func (r *BackupReconciler) parseBackupJob(ctx context.Context, object client.Object) []reconcile.Request {
func (r *BackupReconciler) parseBackupJob(_ context.Context, object client.Object) []reconcile.Request {
job := object.(*batchv1.Job)
var requests []reconcile.Request
backupName := job.Labels[dptypes.BackupNameLabelKey]
Expand Down Expand Up @@ -397,7 +397,7 @@ func (r *BackupReconciler) handleRunningPhase(
return r.updateStatusIfFailed(reqCtx, backup, request.Backup, err)
}

actionCtx := action.Context{
actionCtx := action.ActionContext{
Ctx: reqCtx.Ctx,
Client: r.Client,
Recorder: r.Recorder,
Expand Down
15 changes: 11 additions & 4 deletions controllers/dataprotection/backupschedule_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"time"

batchv1 "k8s.io/api/batch/v1"
batchv1beta1 "k8s.io/api/batch/v1beta1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8sruntime "k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -91,10 +92,16 @@ func (r *BackupScheduleReconciler) Reconcile(ctx context.Context, req ctrl.Reque

// SetupWithManager sets up the controller with the Manager.
func (r *BackupScheduleReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&dpv1alpha1.BackupSchedule{}).
Owns(&batchv1.CronJob{}).
Complete(r)
b := ctrl.NewControllerManagedBy(mgr).
For(&dpv1alpha1.BackupSchedule{})

// Compatible with kubernetes versions prior to K8s 1.21, only supports batch v1beta1.
if dputils.SupportsCronJobV1() {
b.Owns(&batchv1.CronJob{})
} else {
b.Owns(&batchv1beta1.CronJob{})
}
return b.Complete(r)
}

func (r *BackupScheduleReconciler) deleteExternalResources(
Expand Down
1 change: 1 addition & 0 deletions deploy/helm/templates/rbac/clusterrole.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ rules:
resources:
- backups
verbs:
- create
- get
- patch
- update
Expand Down
4 changes: 0 additions & 4 deletions pkg/controllerutil/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"path/filepath"
"testing"

snapshotv1beta1 "github.com/kubernetes-csi/external-snapshotter/client/v3/apis/volumesnapshot/v1beta1"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

Expand Down Expand Up @@ -100,9 +99,6 @@ var _ = BeforeSuite(func() {
err = appsv1alpha1.AddToScheme(scheme)
Expect(err).NotTo(HaveOccurred())

err = snapshotv1beta1.AddToScheme(scheme)
Expect(err).NotTo(HaveOccurred())

k8sClient, err = client.New(cfg, client.Options{Scheme: scheme})
Expect(err).NotTo(HaveOccurred())
Expect(k8sClient).NotTo(BeNil())
Expand Down
192 changes: 0 additions & 192 deletions pkg/controllerutil/volumesnapshot.go

This file was deleted.

4 changes: 2 additions & 2 deletions pkg/dataprotection/action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (

type Action interface {
// Execute executes the action.
Execute(ctx Context) (*dpv1alpha1.ActionStatus, error)
Execute(actCtx ActionContext) (*dpv1alpha1.ActionStatus, error)

// GetName returns the Name of the action.
GetName() string
Expand All @@ -41,7 +41,7 @@ type Action interface {
Type() dpv1alpha1.ActionType
}

type Context struct {
type ActionContext struct {
Ctx context.Context
Client client.Client
Recorder record.EventRecorder
Expand Down
Loading

0 comments on commit 74dd34c

Please sign in to comment.