Skip to content

Commit

Permalink
feat: support get miniojob error by kubectl (#2243)
Browse files Browse the repository at this point in the history
* 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 <mail@ramondeklein.nl>

---------

Co-authored-by: jiuker <jiuker@minio.io>
Co-authored-by: Ramon de Klein <mail@ramondeklein.nl>
  • Loading branch information
3 people committed Jul 29, 2024
1 parent 54be9cb commit 7f53934
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 27 deletions.
8 changes: 4 additions & 4 deletions helm/operator/templates/job.min.io_jobs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions pkg/apis/job.min.io/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 22 additions & 17 deletions pkg/controller/job-controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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))
Expand All @@ -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{
Expand All @@ -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"))
Expand All @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions resources/base/crds/job.min.io_miniojobs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 7f53934

Please sign in to comment.