From 7f539340aca45054d5e751c1fd935acfc81ae6d7 Mon Sep 17 00:00:00 2001 From: jiuker <2818723467@qq.com> Date: Mon, 29 Jul 2024 08:25:17 +0800 Subject: [PATCH] feat: support get miniojob error by kubectl (#2243) * log the error before return log the error before return * refactor log refactor log * fields * Apply suggestions from code review Co-authored-by: Ramon de Klein --------- Co-authored-by: jiuker Co-authored-by: Ramon de Klein --- helm/operator/templates/job.min.io_jobs.yaml | 8 ++-- pkg/apis/job.min.io/v1alpha1/types.go | 4 +- pkg/controller/job-controller.go | 39 +++++++++++-------- resources/base/crds/job.min.io_miniojobs.yaml | 8 ++-- 4 files changed, 32 insertions(+), 27 deletions(-) diff --git a/helm/operator/templates/job.min.io_jobs.yaml b/helm/operator/templates/job.min.io_jobs.yaml index 044c0d6e343..a5e69c1ef2f 100644 --- a/helm/operator/templates/job.min.io_jobs.yaml +++ b/helm/operator/templates/job.min.io_jobs.yaml @@ -18,12 +18,12 @@ spec: scope: Namespaced versions: - additionalPrinterColumns: - - jsonPath: .spec.tenant.name - name: Tenant - type: string - - jsonPath: .spec.status.phase + - jsonPath: .status.phase name: Phase type: string + - jsonPath: .status.message + name: Message + type: string name: v1alpha1 schema: openAPIV3Schema: diff --git a/pkg/apis/job.min.io/v1alpha1/types.go b/pkg/apis/job.min.io/v1alpha1/types.go index e2387ae900b..f593c536a84 100644 --- a/pkg/apis/job.min.io/v1alpha1/types.go +++ b/pkg/apis/job.min.io/v1alpha1/types.go @@ -33,8 +33,8 @@ const ( // +k8s:defaulter-gen=true // +kubebuilder:subresource:status // +kubebuilder:resource:scope=Namespaced,shortName=miniojob,singular=miniojob -// +kubebuilder:printcolumn:name="Tenant",type=string,JSONPath=`.spec.tenant.name` -// +kubebuilder:printcolumn:name="Phase",type=string,JSONPath=`.spec.status.phase` +// +kubebuilder:printcolumn:name="Phase",type=string,JSONPath=`.status.phase` +// +kubebuilder:printcolumn:name="Message",type=string,JSONPath=`.status.message` // +kubebuilder:metadata:annotations=operator.min.io/version=v6.0.1 // MinIOJob is a top-level type. A client is created for it diff --git a/pkg/controller/job-controller.go b/pkg/controller/job-controller.go index a84ba5822f3..e04446b7340 100644 --- a/pkg/controller/job-controller.go +++ b/pkg/controller/job-controller.go @@ -186,7 +186,7 @@ func (c *JobController) HandleObject(obj metav1.Object) { // SyncHandler compares the current Job state with the desired, and attempts to // converge the two. It then updates the Status block of the Job resource // with the current status of the resource. -func (c *JobController) SyncHandler(key string) (Result, error) { +func (c *JobController) SyncHandler(key string) (_ Result, err error) { // Convert the namespace/name string into a distinct namespace and name if key == "" { runtime.HandleError(fmt.Errorf("Invalid resource key: %s", key)) @@ -200,7 +200,7 @@ func (c *JobController) SyncHandler(key string) (Result, error) { Namespace: namespace, }, } - err := c.k8sClient.Get(ctx, client.ObjectKeyFromObject(&jobCR), &jobCR) + err = c.k8sClient.Get(ctx, client.ObjectKeyFromObject(&jobCR), &jobCR) if err != nil { // job cr have gone globalIntervalJobStatus.Delete(fmt.Sprintf("%s/%s", jobCR.Namespace, jobCR.Name)) @@ -210,17 +210,28 @@ func (c *JobController) SyncHandler(key string) (Result, error) { return WrapResult(Result{}, err) } - if !IsSTSEnabled() { - c.recorder.Eventf(&jobCR, corev1.EventTypeWarning, "STSDisabled", "JobCR cannot work with STS disabled") - return WrapResult(Result{}, nil) - } - // if job cr is Success, do nothing if jobCR.Status.Phase == miniojob.MinioJobPhaseSuccess { // delete the job status globalIntervalJobStatus.Delete(fmt.Sprintf("%s/%s", jobCR.Namespace, jobCR.Name)) return WrapResult(Result{}, nil) } + + defer func() { + if err != nil { + if jobCR.Status.Phase != miniojob.MinioJobPhaseSuccess { + jobCR.Status.Phase = miniojob.MinioJobPhaseError + jobCR.Status.Message = err.Error() + err = c.updateJobStatus(ctx, &jobCR) + } + } + }() + + if !IsSTSEnabled() { + c.recorder.Eventf(&jobCR, corev1.EventTypeWarning, "STSDisabled", "JobCR cannot work with STS disabled") + return WrapResult(Result{}, fmt.Errorf("JobCR cannot work with STS disabled")) + } + // get tenant tenant := &miniov2.Tenant{ ObjectMeta: metav1.ObjectMeta{ @@ -230,19 +241,16 @@ func (c *JobController) SyncHandler(key string) (Result, error) { } err = c.k8sClient.Get(ctx, client.ObjectKeyFromObject(tenant), tenant) if err != nil { - jobCR.Status.Phase = miniojob.MinioJobPhaseError - 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) + return WrapResult(Result{}, fmt.Errorf("get tenant %s/%s error: %w", jobCR.Spec.TenantRef.Namespace, jobCR.Spec.TenantRef.Name, err)) } if tenant.Status.HealthStatus != miniov2.HealthStatusGreen { - return WrapResult(Result{RequeueAfter: time.Second * 5}, nil) + return WrapResult(Result{RequeueAfter: time.Second * 5}, fmt.Errorf("get tenant %s/%s error: %w", jobCR.Spec.TenantRef.Namespace, jobCR.Spec.TenantRef.Name, err)) } // check sa pbs := &stsv1beta1.PolicyBindingList{} err = c.k8sClient.List(ctx, pbs, client.InNamespace(namespace)) if err != nil { - return WrapResult(Result{}, err) + return WrapResult(Result{}, fmt.Errorf("list policybinding error: %w", err)) } if len(pbs.Items) == 0 { return WrapResult(Result{}, fmt.Errorf("no policybinding found")) @@ -262,10 +270,7 @@ func (c *JobController) SyncHandler(key string) (Result, error) { } err = intervalJob.CreateCommandJob(ctx, c.k8sClient, STSDefaultPort) if err != nil { - jobCR.Status.Phase = miniojob.MinioJobPhaseError - jobCR.Status.Message = fmt.Sprintf("Create job error:%v", err) - err = c.updateJobStatus(ctx, &jobCR) - return WrapResult(Result{}, err) + return WrapResult(Result{}, fmt.Errorf("create job error: %w", err)) } // update status jobCR.Status = intervalJob.GetMinioJobStatus(ctx) diff --git a/resources/base/crds/job.min.io_miniojobs.yaml b/resources/base/crds/job.min.io_miniojobs.yaml index 044c0d6e343..a5e69c1ef2f 100644 --- a/resources/base/crds/job.min.io_miniojobs.yaml +++ b/resources/base/crds/job.min.io_miniojobs.yaml @@ -18,12 +18,12 @@ spec: scope: Namespaced versions: - additionalPrinterColumns: - - jsonPath: .spec.tenant.name - name: Tenant - type: string - - jsonPath: .spec.status.phase + - jsonPath: .status.phase name: Phase type: string + - jsonPath: .status.message + name: Message + type: string name: v1alpha1 schema: openAPIV3Schema: