无法更新自定义资源的状态字段

Kad*_*rma 5 go kubernetes kubernetes-custom-resources operator-sdk

我正在使用 Operator-sdk 构建一个基于 GO 的操作员来管理名为“Module拥有部署”的自定义资源。我的_types.go看起来像这样:

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status
//+kubebuilder:subresource:scale:specpath=.spec.workerSettings.replicas,statuspath=.status.replicas,selectorpath=.status.selector
//+kubebuilder:printcolumn:name="Replicas",type="integer",JSONPath=".spec.workerSettings.replicas",description="The number of pods for this Module"
//+kubebuilder:printcolumn:name="Ready",type="integer",JSONPath=".status.readyReplicas",description="The number of ready pods for this Module"
//+kubebuilder:resource:shortName=mod;mods

// Module is the Schema for the Module API
type Module struct {
    metav1.TypeMeta   `json:",inline"`
    metav1.ObjectMeta `json:"metadata,omitempty"`

    Spec   ModuleSpec   `json:"spec,omitempty"`
    Status ModuleStatus `json:"status,omitempty"`
}

// ModuleStatus defines the observed state of Module
type ModuleStatus struct {
    Replicas      int32              `json:"replicas"`
    ReadyReplicas int32              `json:"readyReplicas"`
    Selector      string             `json:"selector"`
    Conditions    []metav1.Condition `json:"conditions"`
}
Run Code Online (Sandbox Code Playgroud)

在协调中,我尝试设置ReadyReplicasCR 字段的值,该值对应于我的自定义资源 ( Module) 拥有的部署的类似字段。

func (r *ModuleReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
    var mod modulev1.Module
    err := r.Get(ctx, req.NamespacedName, &mod)
    if errors.IsNotFound(err) {
        return ctrl.Result{}, nil
    } else if err != nil {
        return ctrl.Result{Requeue: true}, err
    }

    var deploy appsv1.Deployment
    if created, err := r.createDeployIfNotExists(ctx, &deploy, &mod); err != nil {
        return ctrl.Result{}, err
    } else if created {
        return ctrl.Result{Requeue: true}, nil
    }

    ...
   
    // trying to set status the CR
    mod.Status.ReadyReplicas = deploy.Status.ReadyReplicas
    if r.Client.Status().Update(ctx, &mod); err != nil {
        log.Error(err, "failed to update Module status")
        return ctrl.Result{}, err
    }

    return ctrl.Result{}, nil
Run Code Online (Sandbox Code Playgroud)

但没有任何改变..r.Client.Status().Update(ctx, &mod)默默地执行,没有任何错误,但相同的服装资源实例返回到与所有状态字段的默认值协调。

v1alpha1.ModuleStatus {Replicas: 0, ReadyReplicas: 0, Selector: "", Conditions: []k8s.io/apimachinery/pkg/apis/meta/v1.Condition len: 0, cap: 0, nil}
Run Code Online (Sandbox Code Playgroud)

我检查了自定义资源的清单“在集群上”的外观,它根本没有状态部分。其中它似乎在我的 CRD 中正确生成:

通过运行“make manifest”命令生成的 CRD

请帮我弄清楚我缺少什么。