1

我有一个项目需要在 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"`
    .....
}

如果您能告诉我如何解决它,我将不胜感激。

4

1 回答 1

1

如果不杀死 pod 并使用新配置(包含更新的环境变量)重新启动它,则无法在正在运行的 pod 上执行此操作,因为这些是在 pod 启动时设置的。

一些选项:

  1. 杀死 pod 并使用新的环境变量重新创建它。
  2. 更新部署中的环境变量,而不是更新将推出更改的 pod。
  3. 考虑使用其他方式来存储这些键值对(例如:卷或自定义资源)。

你能再描述一下你的用例吗?

于 2021-11-03T13:52:05.830 回答