Skip to content

Commit

Permalink
Optimize/debt suspend cronjob (#5256)
Browse files Browse the repository at this point in the history
* add suspend cronjob

* add cronjob to account controller manager cluster role
  • Loading branch information
bxy4543 authored Feb 7, 2025
1 parent 3f89c54 commit 53daadf
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
12 changes: 12 additions & 0 deletions controllers/account/config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,18 @@ rules:
- get
- patch
- update
- apiGroups:
- batch
resources:
- cronjobs
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- ""
resources:
Expand Down
27 changes: 24 additions & 3 deletions controllers/account/controllers/namespace_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"strings"
"time"

"k8s.io/utils/ptr"

"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

"github.com/minio/madmin-go/v3"
Expand All @@ -42,6 +44,7 @@ import (
kbv1alpha1 "github.com/apecloud/kubeblocks/apis/apps/v1alpha1"

"github.com/go-logr/logr"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -73,6 +76,7 @@ const (
//+kubebuilder:rbac:groups=core,resources=namespaces,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=core,resources=namespaces/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=core,resources=namespaces/finalizers,verbs=update
//+kubebuilder:rbac:groups=batch,resources=cronjobs,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups="",resources=secrets,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=apps.kubeblocks.io,resources=clusters,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=apps.kubeblocks.io,resources=clusters/status,verbs=get;update;patch
Expand Down Expand Up @@ -132,16 +136,16 @@ func (r *NamespaceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
}

func (r *NamespaceReconciler) SuspendUserResource(ctx context.Context, namespace string) error {
// suspend kb cluster
// limit0 resource quota
// suspend pod: deploy pod && clone unmanaged pod
// delete infra cr
// suspend cronjob
pipelines := []func(context.Context, string) error{
r.suspendKBCluster,
r.suspendOrphanPod,
r.limitResourceQuotaCreate,
r.deleteControlledPod,
//TODO how to suspend infra cr or delete infra cr
//r.suspendInfraResources,
r.suspendCronJob,
r.suspendObjectStorage,
}
for _, fn := range pipelines {
Expand Down Expand Up @@ -485,3 +489,20 @@ func (AnnotationChangedPredicate) Create(e event.CreateEvent) bool {
_, ok := e.Object.GetAnnotations()[v1.DebtNamespaceAnnoStatusKey]
return ok
}

func (r *NamespaceReconciler) suspendCronJob(ctx context.Context, namespace string) error {
cronJobList := batchv1.CronJobList{}
if err := r.Client.List(ctx, &cronJobList, client.InNamespace(namespace)); err != nil {
return err
}
for _, cronJob := range cronJobList.Items {
if cronJob.Spec.Suspend != nil && *cronJob.Spec.Suspend {
continue
}
cronJob.Spec.Suspend = ptr.To(true)
if err := r.Client.Update(ctx, &cronJob); err != nil {
return fmt.Errorf("failed to suspend cronjob %s: %w", cronJob.Name, err)
}
}
return nil
}
12 changes: 12 additions & 0 deletions controllers/account/deploy/manifests/deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,18 @@ rules:
- get
- patch
- update
- apiGroups:
- batch
resources:
- cronjobs
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- ""
resources:
Expand Down

0 comments on commit 53daadf

Please sign in to comment.