Skip to content

Commit

Permalink
feat: wait tenant be green to do command (minio#2012)
Browse files Browse the repository at this point in the history
* wait tenant to do commond

* wait tenant to do commond

* wait tenant to do commond

* update cluster role

---------

Co-authored-by: guozhi.li <guozhi.li@daocloud.io>
  • Loading branch information
jiuker and guozhi.li authored Mar 5, 2024
1 parent 57c07bc commit 5fac040
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 33 deletions.
5 changes: 2 additions & 3 deletions helm/operator/templates/job.min.io_jobs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 1 addition & 10 deletions helm/operator/templates/operator-clusterrole.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -151,6 +141,7 @@ rules:
- apiGroups:
- minio.min.io
- sts.min.io
- job.min.io
resources:
- "*"
verbs:
Expand Down
6 changes: 4 additions & 2 deletions pkg/apis/job.min.io/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 33 additions & 5 deletions pkg/controller/job-controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
11 changes: 1 addition & 10 deletions resources/base/cluster-role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -150,6 +140,7 @@ rules:
- apiGroups:
- minio.min.io
- sts.min.io
- job.min.io
resources:
- "*"
verbs:
Expand Down
5 changes: 2 additions & 3 deletions resources/base/crds/job.min.io_miniojobs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5fac040

Please sign in to comment.