为什么 client.Update(...) 忽略非原始值?
Why does client.Update(...) ignore non-primitive values?
我正在尝试修改非拥有对象的规范作为我的自定义资源 Reconcile
的一部分,但它似乎忽略了所有非原始字段。我正在使用控制器运行时。
我想因为它只在基元上工作,所以可能是与 DeepCopy 相关的问题。然而,删除它并没有解决问题,我读到任何对象更新都必须在深拷贝上以避免弄乱缓存。
我也尝试设置 client.FieldOwner(...)
,因为它说这是在服务器端完成的更新所必需的。我不确定将其设置为什么,所以我将其设置为 req.NamespacedName.String()
。那也没用。
这是我的控制器的 Reconcile 循环:
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
对端口和 CustomEnv 字段进行更改的新 CR 时,它会修改部署,如 Reconcile
中所述。但是,只更新了端口,而忽略了对容器的更改Env
。
问题是我需要一个指向我正在修改的容器的指针。
这样做反而奏效了:
container := &newDeploy.Spec.Template.Spec.Containers[0]
我正在尝试修改非拥有对象的规范作为我的自定义资源 Reconcile
的一部分,但它似乎忽略了所有非原始字段。我正在使用控制器运行时。
我想因为它只在基元上工作,所以可能是与 DeepCopy 相关的问题。然而,删除它并没有解决问题,我读到任何对象更新都必须在深拷贝上以避免弄乱缓存。
我也尝试设置 client.FieldOwner(...)
,因为它说这是在服务器端完成的更新所必需的。我不确定将其设置为什么,所以我将其设置为 req.NamespacedName.String()
。那也没用。
这是我的控制器的 Reconcile 循环:
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
对端口和 CustomEnv 字段进行更改的新 CR 时,它会修改部署,如 Reconcile
中所述。但是,只更新了端口,而忽略了对容器的更改Env
。
问题是我需要一个指向我正在修改的容器的指针。
这样做反而奏效了:
container := &newDeploy.Spec.Template.Spec.Containers[0]