我正在尝试将非拥有对象的规范修改为“自定义资源”的Reconcile的一部分,但似乎它会忽略不是原语的任何字段。我正在使用 Controller 运行时。

我认为,因为它仅适用于原语,也许这是与DeepCopy有关的问题。但是,删除它并不能解决问题,而且我读到,对象的所有更新都必须在深拷贝上,以避免弄乱缓存。

我还尝试设置client.FieldOwner(...),因为它说这是服务器端完成更新所必需的。我不确定将其设置为什么,所以我将其设置为req.NamespacedName.String()。那也不起作用。

这是我的 Controller 的协调循环:

func (r *MyCustomObjectReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
    // ...

    var myCustomObject customv1.MyCustomObject
    if err := r.Get(ctx, req.NamespacedName, &myCustomObject); err != nil {
        log.Error(err, "unable to fetch ReleaseDefinition")
        return ctrl.Result{}, client.IgnoreNotFound(err)
    }

    // ...

    deployList := &kappsv1.DeploymentList{}
    labels := map[string]string{
        "mylabel": myCustomObject.Name,
    }
    if err := r.List(ctx, deployList, client.MatchingLabels(labels)); err != nil {
        log.Error(err, "unable to fetch Deployments")
        return ctrl.Result{}, err
    }

    // make a deep copy to avoid messing up the cache (used by other controllers)
    myCustomObjectSpec := myCustomObject.Spec.DeepCopy()

    // the two fields of my CRD that affect the Deployments
    port := myCustomObjectSpec.Port // type: *int32
    customenv := myCustomObjectSpec.CustomEnv // type: map[string]string

    for _, dep := range deployList.Items {
        newDeploy := dep.DeepCopy() // already returns a pointer

        // Do these things:
        // 1. replace first container's containerPort with myCustomObjectSpec.Port
        // 2. replace first container's Env with values from myCustomObjectSpec.CustomEnv
                // 3. Update the Deployment

        container := newDeploy.Spec.Template.Spec.Containers[0]

        // 1. Replace container's port
        container.Ports[0].ContainerPort = *port

        envVars := make([]kcorev1.EnvVar, 0, len(customenv))
        for key, val := range customenv {
            envVars = append(envVars, kcorev1.EnvVar{
                Name:  key,
                Value: val,
            })
        }

        // 2. Replace container's Env variables
        container.Env = envVars

        // 3. Perform update for deployment (port works, env gets ignored)
        if err := r.Update(ctx, newDeploy); err != nil {
            log.Error(err, "unable to update deployment", "deployment", dep.Name)
            return ctrl.Result{}, err
        }
    }
    return ctrl.Result{}, nil
}

我的CRD的规格如下:
// MyCustomObjectSpec defines the desired state of MyCustomObject
type MyCustomObjectSpec struct {
    // CustomEnv is a list of environment variables to set in the containers.
    // +optional
    CustomEnv map[string]string `json:"customEnv,omitempty"`

    // Port is the port that the backend container is listening on.
    // +optional
    Port *int32 `json:"port,omitempty"`
}

我期望当我对Port和CustomEnv字段进行更改后对新CR进行kubectl apply编码时,它将按照Reconcile中的描述修改部署。但是,仅端口被更新,并且对容器的Env的更改将被忽略。

最佳答案

问题是我需要一个指向正在修改的容器的指针。

而是这样做:

container := &newDeploy.Spec.Template.Spec.Containers[0]

关于kubernetes - 为什么client.Update(...)会忽略非原始值?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57697912/

10-16 06:45