terraform-provider-kubernetes icon indicating copy to clipboard operation
terraform-provider-kubernetes copied to clipboard

Init container doesn't support restart_policy option (to transform it in a sidecar container)

Open arthurgur opened this issue 1 year ago • 4 comments

Terraform Version, Provider Version and Kubernetes Version

Terraform version: 1.6.2
Kubernetes provider version: 2.27.0
Kubernetes version: 1.19

Affected Resource(s)

Terraform Configuration Files

resource "kubernetes_deployment_v1" "example" {
  metadata {
    name = "terraform-example"
    labels = {
      test = "MyExampleApp"
    }
  }

  spec {
    replicas = 3

    selector {
      match_labels = {
        test = "MyExampleApp"
      }
    }

    template {
      metadata {
        labels = {
          test = "MyExampleApp"
        }
      }

      spec {
        container {
          name  = "example"
          image = "nginx:1.21.6"

        # Init container
        init_container {
          name  = "init-setup"
          image = "busybox:latest"
          restart_policy = "Always"
        }
      }
    }
  }
}

Debug Output

Terraform v1.6.2
on linux_amd64
Initializing plugins and modules...
╷
│ Error: Unsupported argument
│ 
│   on app.tf line XX, in resource "kubernetes_deployment_v1" "this":
│   XX:             restart_policy = "Always"
│ 
│ An argument named "restart_policy" is not expected here.
╵
Operation failed: failed running terraform plan (exit 1)

Panic Output

Steps to Reproduce

  1. Create a ressource kubernetes_deployment_v1 with init container and set the option restart_policy of the init container to "Always" to transform it in a sidecar.
  2. Apply your code

Expected Behavior

Pod with sidecar init container created

Actual Behavior

Option non supported

Important Factoids

References

  • https://kubernetes.io/docs/concepts/workloads/pods/sidecar-containers/

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

arthurgur avatar Mar 18 '24 10:03 arthurgur

Enabled by default with Kubernetes 1.29, a [feature gate](https://kubernetes.io/docs/reference/command-line-tools-reference/feature-gates/) named SidecarContainers allows you to specify a restartPolicy for containers listed in a Pod's initContainers field. 
These restartable sidecar containers are independent with other [init containers](https://kubernetes.io/docs/concepts/workloads/pods/init-containers/) and main application container within the same pod.
These can be started, stopped, or restarted without effecting the main application container and other init containers.

Hello @arthurgur, thanks for opening this issue! With this being marked as beta in v1.29, we can proceed with adding this into the provider. You are open to creating a PR that adds this since this is just adding a new field within https://github.com/hashicorp/terraform-provider-kubernetes/blob/bff3ae2a2cc4552fc8da1ba57720f652f8b1beaa/kubernetes/schema_container.go#L256 we can guide you in adding the field and also adding tests that check for this.

BBBmau avatar Mar 19 '24 00:03 BBBmau

Hello @BBBmau, thanks for your reply. Unfortunately I don't have rights to create a branch (or I dont know how, it's my first contribution).

arthurgur avatar Mar 19 '24 09:03 arthurgur

@arthurgur no worries, be sure to look at our contributing guidelines., I would look into also the section on Pull Request Lifecycle since this is your first contribution.

BBBmau avatar Mar 19 '24 16:03 BBBmau