Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kubernetes_env does not allow applying environment variable to a daemonset. #2426

Open
chrismaes87 opened this issue Feb 19, 2024 · 3 comments
Labels
acknowledged Issue has undergone initial review and is in our work queue. bug

Comments

@chrismaes87
Copy link

chrismaes87 commented Feb 19, 2024

Terraform Version, Provider Version and Kubernetes Version

Terraform version: 1.7.3
Kubernetes provider version: 2.25.2
Kubernetes version: 1.27

Affected Resource(s)

  • kubernetes_env

Terraform Configuration Files


locals {
  containers = {
    "aws-node" = {
      container      = "aws-node"
      init_container = null
    }
    "aws-eks-nodeagent" = {
      container      = "aws-eks-nodeagent"
      init_container = null
    }
    "aws-vpc-cni-init" = {
      container      = null
      init_container = "aws-vpc-cni-init"
    }
  }

  envs = [
    {
      name  = "AWS_VPC_K8S_CNI_CUSTOM_NETWORK_CFG"
      value = true
    }
  ]
}

resource "kubernetes_env" "aws_node-env" {
  for_each       = local.containers
  kind           = "DaemonSet"
  container      = each.value.container
  init_container = each.value.init_container
  metadata {
    name      = "aws-node"
    namespace = "kube-system"
  }

  api_version = "apps/v1"
  force       = true

  dynamic "env" {
    for_each = local.envs
    content {
      name  = env.value["name"]
      value = env.value["value"]
    }
  }
}

Debug Output

Panic Output

Steps to Reproduce

  1. terraform apply works fine without error

Expected Behavior

kubectl -n kube-system get daemonset aws-node -o yaml | grep -C 1 AWS_VPC_K8S_CNI_CUSTOM_NETWORK_CFG

should give something like (value true three times)

          value: "9001"
        - name: AWS_VPC_K8S_CNI_CUSTOM_NETWORK_CFG
          value: "true"
--
              fieldPath: spec.nodeName
        - name: AWS_VPC_K8S_CNI_CUSTOM_NETWORK_CFG
          value: "true"
--
          value: "false"
        - name: AWS_VPC_K8S_CNI_CUSTOM_NETWORK_CFG
          value: "true"

Actual Behavior

the value is never true on all three resources. I will get something like

kubectl -n kube-system get daemonset aws-node -o yaml | grep -C 1 AWS_VPC_K8S_CNI_CUSTOM_NETWORK_CFG

          value: "9001"
        - name: AWS_VPC_K8S_CNI_CUSTOM_NETWORK_CFG
          value: "true"
--
              fieldPath: spec.nodeName
        - name: AWS_VPC_K8S_CNI_CUSTOM_NETWORK_CFG
        - name: ENI_CONFIG_LABEL_DEF
--
          value: "false"
        - name: AWS_VPC_K8S_CNI_CUSTOM_NETWORK_CFG
        - name: ENI_CONFIG_LABEL_DEF

it seems like kubernetes_env applies the environment variable to one container, and it gets removed from the others. I want the environment variable to be set on all containers of the daemonset.

Important Factoids

The problem is not linked to me parametrizing the three kubernetes_env resources. I have tested with three separate resources with everything hardcoded and this gave the exact same behaviour.

References

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment
@sheneska
Copy link
Contributor

Hi @chrismaes87, you can try setting the field_manager to being the containers' value, for example field_manager = each.value.container. While this may work for this specific issue, there are some limitations here that would cause it not to work in every case. Please keep in mind that modifying resources that are outside of terraform is not encouraged and should be used only as a last resort.

@chrismaes87
Copy link
Author

I tried this, and also set force=true but to no avail. He is managing to change the field value in some way, but not for all containers at once.

@joewragg
Copy link

joewragg commented Mar 4, 2024

I'm also seeing the same issue. Can we get fix for this it appears that either container or init_container are mandatory

@iBrandyJackson iBrandyJackson added the acknowledged Issue has undergone initial review and is in our work queue. label Mar 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
acknowledged Issue has undergone initial review and is in our work queue. bug
Projects
None yet
Development

No branches or pull requests

4 participants