我有一个项目需要在 pod 容器中注入或更新环境变量kubebuilder
,使用and controller-runtime
,
我的计划如下:
func Reconcile(){
// get added environment variables
// get matched pods
// update container env
}
我尝试更新后,出现以下错误
"namespace": "default", "error": "Pod \"busybox\" is invalid: spec: Forbidden: pod updates may not change fields other than `spec.containers[*].image`, `spec.initContainers[*].image`, `spec.activeDeadlineSeconds` or `spec.tolerations` (only additions to existing tolerations)\n core.PodSpec{\n \tVolumes: []core.Volume{{Name: \"default-token-bcr86\", VolumeSource: core.VolumeSource{Secret: &core.SecretVolumeSource{SecretName: \"default-token-bcr86\", DefaultMode: &420}}}},\n \tInitContainers: nil,\n \tContainers: []core.Container{\n \t\t{\n \t\t\t... // 5 identical fields\n \t\t\tPorts: nil,\n \t\t\tEnvFrom: nil,\n- \t\t\tEnv: []core.EnvVar{
# pod.yml of busybox
apiVersion: v1
kind: Pod
metadata:
name: busybox
namespace: default
labels:
match: "test"
spec:
containers:
- image: busybox
command:
- sleep
- "3600"
imagePullPolicy: IfNotPresent
name: busybox
restartPolicy: Always
但似乎无法在源代码中更新env
// k8s.io/api/core/v1/types.go
// A single application container that you want to run within a pod.
type Container struct {
.....
// List of sources to populate environment variables in the container.
// The keys defined within a source must be a C_IDENTIFIER. All invalid keys
// will be reported as an event when the container is starting. When a key exists in multiple
// sources, the value associated with the last source will take precedence.
// Values defined by an Env with a duplicate key will take precedence.
// Cannot be updated.
// +optional
EnvFrom []EnvFromSource `json:"envFrom,omitempty" protobuf:"bytes,19,rep,name=envFrom"`
// List of environment variables to set in the container.
// Cannot be updated.
// +optional
// +patchMergeKey=name
// +patchStrategy=merge
Env []EnvVar `json:"env,omitempty" patchStrategy:"merge" patchMergeKey:"name" protobuf:"bytes,7,rep,name=env"`
.....
}
如果您能告诉我如何解决它,我将不胜感激。