Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize/debt suspend cronjob #5256

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading
Loading