From 5fac040c1fa533dd6f8f333baa0b2a7abdbffb46 Mon Sep 17 00:00:00 2001 From: jiuker <2818723467@qq.com> Date: Wed, 6 Mar 2024 06:49:25 +0800 Subject: [PATCH] feat: wait tenant be green to do command (#2012) * wait tenant to do commond * wait tenant to do commond * wait tenant to do commond * update cluster role --------- Co-authored-by: guozhi.li --- helm/operator/templates/job.min.io_jobs.yaml | 5 +-- .../templates/operator-clusterrole.yaml | 11 +----- pkg/apis/job.min.io/v1alpha1/types.go | 6 ++- pkg/controller/job-controller.go | 38 ++++++++++++++++--- resources/base/cluster-role.yaml | 11 +----- resources/base/crds/job.min.io_miniojobs.yaml | 5 +-- 6 files changed, 43 insertions(+), 33 deletions(-) diff --git a/helm/operator/templates/job.min.io_jobs.yaml b/helm/operator/templates/job.min.io_jobs.yaml index 37df0e4cd5b..9f30ffe3723 100644 --- a/helm/operator/templates/job.min.io_jobs.yaml +++ b/helm/operator/templates/job.min.io_jobs.yaml @@ -98,11 +98,10 @@ spec: - result type: object type: array + message: + type: string phase: type: string - required: - - commands - - phase type: object type: object served: true diff --git a/helm/operator/templates/operator-clusterrole.yaml b/helm/operator/templates/operator-clusterrole.yaml index 318760e8305..0e551ffae45 100644 --- a/helm/operator/templates/operator-clusterrole.yaml +++ b/helm/operator/templates/operator-clusterrole.yaml @@ -3,16 +3,6 @@ kind: ClusterRole metadata: name: minio-operator-role rules: - - apiGroups: - - "job.min.io" - resources: - - miniojobs - verbs: - - list - - get - - update - - delete - - watch - apiGroups: - "apiextensions.k8s.io" resources: @@ -151,6 +141,7 @@ rules: - apiGroups: - minio.min.io - sts.min.io + - job.min.io resources: - "*" verbs: diff --git a/pkg/apis/job.min.io/v1alpha1/types.go b/pkg/apis/job.min.io/v1alpha1/types.go index e5ec7c943c3..b2be66de756 100644 --- a/pkg/apis/job.min.io/v1alpha1/types.go +++ b/pkg/apis/job.min.io/v1alpha1/types.go @@ -123,10 +123,12 @@ type TenantRef struct { // MinIOJobStatus Status of MinioJob resource type MinIOJobStatus struct { - // *Required* + + // +optional Phase string `json:"phase"` - // *Required* + + // +optional CommandsStatus []CommandStatus `json:"commands"` + // +optional + Message string `json:"message"` } // CommandStatus Status of MinioJob command execution diff --git a/pkg/controller/job-controller.go b/pkg/controller/job-controller.go index 3bf825272d7..fad24501521 100644 --- a/pkg/controller/job-controller.go +++ b/pkg/controller/job-controller.go @@ -9,12 +9,13 @@ import ( "time" "github.com/minio/minio-go/v7/pkg/set" - "k8s.io/apimachinery/pkg/api/meta" - "github.com/minio/operator/pkg/apis/job.min.io/v1alpha1" + miniov2 "github.com/minio/operator/pkg/apis/minio.min.io/v2" clientset "github.com/minio/operator/pkg/client/clientset/versioned" jobinformers "github.com/minio/operator/pkg/client/informers/externalversions/job.min.io/v1alpha1" joblisters "github.com/minio/operator/pkg/client/listers/job.min.io/v1alpha1" + "k8s.io/apimachinery/pkg/api/errors" + "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/runtime" "k8s.io/client-go/kubernetes" @@ -207,17 +208,40 @@ func (c *JobController) SyncHandler(key string) (Result, error) { runtime.HandleError(fmt.Errorf("Invalid resource key: %s", key)) return WrapResult(Result{}, nil) } - namespace, tenantName := key2NamespaceName(key) + namespace, jobName := key2NamespaceName(key) + ctx := context.Background() jobCR := v1alpha1.MinIOJob{ ObjectMeta: metav1.ObjectMeta{ - Name: tenantName, + Name: jobName, Namespace: namespace, }, } - err := c.k8sClient.Get(context.Background(), client.ObjectKeyFromObject(&jobCR), &jobCR) + err := c.k8sClient.Get(ctx, client.ObjectKeyFromObject(&jobCR), &jobCR) + if err != nil { + // job cr have gone + if errors.IsNotFound(err) { + return WrapResult(Result{}, nil) + } + return WrapResult(Result{}, err) + } + // get tenant + tenant := &miniov2.Tenant{ + ObjectMeta: metav1.ObjectMeta{ + Namespace: jobCR.Spec.TenantRef.Namespace, + Name: jobCR.Spec.TenantRef.Name, + }, + } + err = c.k8sClient.Get(ctx, client.ObjectKeyFromObject(tenant), tenant) if err != nil { + jobCR.Status.Phase = "Error" + jobCR.Status.Message = fmt.Sprintf("Get tenant %s/%s error:%v", jobCR.Spec.TenantRef.Namespace, jobCR.Spec.TenantRef.Name, err) + err = c.updateJobStatus(ctx, &jobCR) + return WrapResult(Result{}, err) + } + if tenant.Status.HealthStatus != miniov2.HealthStatusGreen { return WrapResult(Result{RequeueAfter: time.Second * 5}, nil) } + fmt.Println("will do somthing next") // Loop through the different supported operations. for _, val := range jobCR.Spec.Commands { operation := val.Operation @@ -227,3 +251,7 @@ func (c *JobController) SyncHandler(key string) (Result, error) { } return WrapResult(Result{}, err) } + +func (c *JobController) updateJobStatus(ctx context.Context, job *v1alpha1.MinIOJob) error { + return c.k8sClient.Status().Update(ctx, job) +} diff --git a/resources/base/cluster-role.yaml b/resources/base/cluster-role.yaml index ca33cc7a16d..316607ab051 100644 --- a/resources/base/cluster-role.yaml +++ b/resources/base/cluster-role.yaml @@ -3,16 +3,6 @@ kind: ClusterRole metadata: name: minio-operator-role rules: - - apiGroups: - - "job.min.io" - resources: - - miniojobs - verbs: - - list - - get - - update - - delete - - watch - apiGroups: - "apiextensions.k8s.io" resources: @@ -150,6 +140,7 @@ rules: - apiGroups: - minio.min.io - sts.min.io + - job.min.io resources: - "*" verbs: diff --git a/resources/base/crds/job.min.io_miniojobs.yaml b/resources/base/crds/job.min.io_miniojobs.yaml index 37df0e4cd5b..9f30ffe3723 100644 --- a/resources/base/crds/job.min.io_miniojobs.yaml +++ b/resources/base/crds/job.min.io_miniojobs.yaml @@ -98,11 +98,10 @@ spec: - result type: object type: array + message: + type: string phase: type: string - required: - - commands - - phase type: object type: object served: true