1

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

我想既然它只适用于原语,也许这是与 DeepCopy 相关的问题。但是,删除它并没有解决问题,我读到对象上的任何更新都必须在深层副本上,以避免弄乱缓存。

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

这是我的控制器的协调循环:

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"`
}

我希望当我kubectl apply对 Port 和 CustomEnv 字段进行更改的新 CR 时,它会修改部署,如Reconcile. 但是,只有端口被更新,而对容器的更改Env被忽略。

4

1 回答 1

1

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

这样做反而有效:

container := &newDeploy.Spec.Template.Spec.Containers[0]
于 2019-08-28T22:51:19.573 回答