Skip to content

Commit 83324e7

Browse files
authored
Add restart_policy attribute to container spec (#2786)
1 parent b7cb0b8 commit 83324e7

File tree

9 files changed

+37
-9
lines changed

9 files changed

+37
-9
lines changed

.changelog/2786.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
* Add support for sidecar containers via restart_policy field in init_container spec
3+
````

docs/data-sources/pod_v1.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -868,6 +868,7 @@ Read-Only:
868868
- `tty` (Boolean)
869869
- `volume_mount` (List of Object) (see [below for nested schema](#nestedobjatt--spec--init_container--volume_mount))
870870
- `working_dir` (String)
871+
- `restart_policy` (String)
871872

872873
<a id="nestedobjatt--spec--init_container--env"></a>
873874
### Nested Schema for `spec.init_container.env`

docs/resources/daemon_set_v1.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -978,6 +978,8 @@ Optional:
978978
- `volume_mount` (Block List) Pod volumes to mount into the container's filesystem. Cannot be updated. (see [below for nested schema](#nestedblock--spec--template--spec--init_container--volume_mount))
979979
- `volume_device` (Block List) Raw volume devices to attach into the container's filesystem as raw block devices. Cannot be updated. More info: https://kubernetes.io/docs/reference/kubernetes-api/workload-resources/pod-v1/#volumes-1 (see [below for nested schema](#nestedblock--spec--template--spec--init_container--volume_device))
980980
- `working_dir` (String) Container's working directory. If not specified, the container runtime's default will be used, which might be configured in the container image. Cannot be updated.
981+
- `restart_policy` (String) Restart policy for designating init container as a sidecar. Can only be `Always`. More info: https://kubernetes.io/docs/concepts/workloads/pods/sidecar-containers/#pod-sidecar-containers.
982+
981983

982984
<a id="nestedblock--spec--template--spec--init_container--env"></a>
983985
### Nested Schema for `spec.template.spec.init_container.env`

docs/resources/deployment_v1.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,8 @@ Optional:
986986
- `volume_mount` (Block List) Pod volumes to mount into the container's filesystem. Cannot be updated. (see [below for nested schema](#nestedblock--spec--template--spec--init_container--volume_mount))
987987
- `volume_device` (Block List) Raw volume devices to attach into the container's filesystem as raw block devices. Cannot be updated. More info: https://kubernetes.io/docs/reference/kubernetes-api/workload-resources/pod-v1/#volumes-1 (see [below for nested schema](#nestedblock--spec--template--spec--init_container--volume_device))
988988
- `working_dir` (String) Container's working directory. If not specified, the container runtime's default will be used, which might be configured in the container image. Cannot be updated.
989+
- `restart_policy` (String) Restart policy for designating init container as a sidecar. Can only be `Always`. More info: https://kubernetes.io/docs/concepts/workloads/pods/sidecar-containers/#pod-sidecar-containers.
990+
989991

990992
<a id="nestedblock--spec--template--spec--init_container--env"></a>
991993
### Nested Schema for `spec.template.spec.init_container.env`

docs/resources/pod_v1.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,7 @@ Optional:
375375
- `volume_device` (Block List) Raw volume devices to attach into the container's filesystem as raw block devices. Cannot be updated. More info: https://kubernetes.io/docs/reference/kubernetes-api/workload-resources/pod-v1/#volumes-1 (see [below for nested schema](#nestedblock--spec--container--volume_device))
376376
- `working_dir` (String) Container's working directory. If not specified, the container runtime's default will be used, which might be configured in the container image. Cannot be updated.
377377

378+
378379
<a id="nestedblock--spec--container--env"></a>
379380
### Nested Schema for `spec.container.env`
380381

@@ -936,6 +937,7 @@ Optional:
936937
- `volume_mount` (Block List) Pod volumes to mount into the container's filesystem. Cannot be updated. (see [below for nested schema](#nestedblock--spec--init_container--volume_mount))
937938
- `volume_device` (Block List) Raw volume devices to attach into the container's filesystem as raw block devices. Cannot be updated. More info: https://kubernetes.io/docs/reference/kubernetes-api/workload-resources/pod-v1/#volumes-1 (see [below for nested schema](#nestedblock--spec--init_container--volume_device))
938939
- `working_dir` (String) Container's working directory. If not specified, the container runtime's default will be used, which might be configured in the container image. Cannot be updated.
940+
- `restart_policy` (String) Restart policy for designating init container as a sidecar. Can only be `Always`. More info: https://kubernetes.io/docs/concepts/workloads/pods/sidecar-containers/#pod-sidecar-containers.
939941

940942
<a id="nestedblock--spec--init_container--env"></a>
941943
### Nested Schema for `spec.init_container.env`

docs/resources/stateful_set_v1.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,7 @@ Optional:
10091009
- `volume_mount` (Block List) Pod volumes to mount into the container's filesystem. Cannot be updated. (see [below for nested schema](#nestedblock--spec--template--spec--init_container--volume_mount))
10101010
- `volume_device` (Block List) Raw volume devices to attach into the container's filesystem as raw block devices. Cannot be updated. More info: https://kubernetes.io/docs/reference/kubernetes-api/workload-resources/pod-v1/#volumes-1 (see [below for nested schema](#nestedblock--spec--template--spec--init_container--volume_device))
10111011
- `working_dir` (String) Container's working directory. If not specified, the container runtime's default will be used, which might be configured in the container image. Cannot be updated.
1012+
- `restart_policy` (String) Restart policy for designating init container as a sidecar. Can only be `Always`. More info: https://kubernetes.io/docs/concepts/workloads/pods/sidecar-containers/#pod-sidecar-containers.
10121013

10131014
<a id="nestedblock--spec--template--spec--init_container--env"></a>
10141015
### Nested Schema for `spec.template.spec.init_container.env`

kubernetes/resource_kubernetes_deployment_v1_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,12 @@ func TestAccKubernetesDeploymentV1_initContainerForceNew(t *testing.T) {
146146
resource.TestCheckResourceAttr(resourceName, "spec.0.template.0.spec.0.init_container.0.image", imageName),
147147
resource.TestCheckResourceAttr(resourceName, "spec.0.template.0.spec.0.init_container.0.resources.0.requests.memory", "64Mi"),
148148
resource.TestCheckResourceAttr(resourceName, "spec.0.template.0.spec.0.init_container.0.env.2.value", "testvar"),
149+
resource.TestCheckResourceAttr(resourceName, "spec.0.template.0.spec.0.init_container.0.restart_policy", "Always"),
149150
resource.TestCheckResourceAttr(resourceName, "spec.0.template.0.spec.0.init_container.1.name", "initcontainer2"),
150151
resource.TestCheckResourceAttr(resourceName, "spec.0.template.0.spec.0.init_container.1.image", imageName1),
151152
resource.TestCheckResourceAttr(resourceName, "spec.0.template.0.spec.0.init_container.1.command.2", initCommand),
152153
resource.TestCheckResourceAttr(resourceName, "spec.0.template.0.spec.0.init_container.1.image_pull_policy", "IfNotPresent"),
154+
resource.TestCheckResourceAttr(resourceName, "spec.0.template.0.spec.0.init_container.1.restart_policy", ""),
153155
),
154156
},
155157
{ // Test for non-empty plans. No modification.
@@ -1615,10 +1617,11 @@ resource "kubernetes_deployment_v1" "test" {
16151617
name = "initcontainer1"
16161618
image = "%s"
16171619
image_pull_policy = "IfNotPresent"
1620+
restart_policy = "Always"
16181621
command = [
16191622
"sh",
16201623
"-c",
1621-
"printenv SECRETENV CONFIGENV LIMITS_CPU CUSTOM",
1624+
"tail -F /dev/null",
16221625
]
16231626
env {
16241627
name = "SECRETENV"

kubernetes/schema_container.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,16 @@ func containerFields(isUpdatable bool) map[string]*schema.Schema {
615615
Description: "Security options the pod should run with. More info: https://kubernetes.io/docs/tasks/configure-pod-container/security-context/",
616616
Elem: securityContextSchema(isUpdatable),
617617
},
618+
"restart_policy": {
619+
Type: schema.TypeString,
620+
Optional: true,
621+
Computed: true,
622+
ForceNew: !isUpdatable,
623+
Description: "Restart policy for designating init container as a sidecar. Can only be `Always`. More info: https://kubernetes.io/docs/concepts/workloads/pods/sidecar-containers/#pod-sidecar-containers.",
624+
ValidateFunc: validation.StringInSlice([]string{
625+
string(api.RestartPolicyAlways),
626+
}, false),
627+
},
618628
"startup_probe": {
619629
Type: schema.TypeList,
620630
Optional: true,
@@ -749,7 +759,6 @@ func probeSchema() *schema.Resource {
749759
return &schema.Resource{
750760
Schema: h,
751761
}
752-
753762
}
754763

755764
func securityContextSchema(isUpdatable bool) *schema.Resource {

kubernetes/structures_container.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ func flattenContainerSecurityContext(in *v1.SecurityContext) []interface{} {
5353
att["se_linux_options"] = flattenSeLinuxOptions(in.SELinuxOptions)
5454
}
5555
return []interface{}{att}
56-
5756
}
5857

5958
func flattenSecurityCapabilities(in *v1.Capabilities) []interface{} {
@@ -289,11 +288,9 @@ func flattenContainerVolumeMounts(in []v1.VolumeMount) []interface{} {
289288

290289
if v.MountPath != "" {
291290
m["mount_path"] = v.MountPath
292-
293291
}
294292
if v.Name != "" {
295293
m["name"] = v.Name
296-
297294
}
298295
if v.SubPath != "" {
299296
m["sub_path"] = v.SubPath
@@ -428,7 +425,6 @@ func flattenContainers(in []v1.Container, serviceAccountRegex string) ([]interfa
428425
if v.Lifecycle != nil {
429426
c["lifecycle"] = flattenLifeCycle(v.Lifecycle)
430427
}
431-
432428
if v.SecurityContext != nil {
433429
c["security_context"] = flattenContainerSecurityContext(v.SecurityContext)
434430
}
@@ -441,6 +437,9 @@ func flattenContainers(in []v1.Container, serviceAccountRegex string) ([]interfa
441437
if len(v.EnvFrom) > 0 {
442438
c["env_from"] = flattenContainerEnvFroms(v.EnvFrom)
443439
}
440+
if v.RestartPolicy != nil {
441+
c["restart_policy"] = string(*v.RestartPolicy)
442+
}
444443

445444
if len(v.VolumeMounts) > 0 {
446445
for num, m := range v.VolumeMounts {
@@ -581,6 +580,11 @@ func expandContainers(ctrs []interface{}) ([]v1.Container, error) {
581580
if v, ok := ctr["working_dir"].(string); ok && v != "" {
582581
cs[i].WorkingDir = v
583582
}
583+
584+
if v, ok := ctr["restart_policy"].(string); ok && v != "" {
585+
policy := v1.ContainerRestartPolicy(v)
586+
cs[i].RestartPolicy = &policy
587+
}
584588
}
585589
return cs, nil
586590
}
@@ -613,6 +617,7 @@ func expandHTTPHeaders(l []interface{}) []v1.HTTPHeader {
613617
}
614618
return headers
615619
}
620+
616621
func expandContainerSecurityContext(l []interface{}) (*v1.SecurityContext, error) {
617622
if len(l) == 0 || l[0] == nil {
618623
return &v1.SecurityContext{}, nil
@@ -787,8 +792,8 @@ func expandLifecycleHandlers(l []interface{}) *v1.LifecycleHandler {
787792
obj.TCPSocket = expandTCPSocket(v)
788793
}
789794
return &obj
790-
791795
}
796+
792797
func expandLifeCycle(l []interface{}) *v1.Lifecycle {
793798
if len(l) == 0 || l[0] == nil {
794799
return &v1.Lifecycle{}
@@ -945,8 +950,8 @@ func expandConfigMapKeyRef(r []interface{}) *v1.ConfigMapKeySelector {
945950
obj.Optional = ptr.To(v.(bool))
946951
}
947952
return obj
948-
949953
}
954+
950955
func expandFieldRef(r []interface{}) *v1.ObjectFieldSelector {
951956
if len(r) == 0 || r[0] == nil {
952957
return &v1.ObjectFieldSelector{}
@@ -962,6 +967,7 @@ func expandFieldRef(r []interface{}) *v1.ObjectFieldSelector {
962967
}
963968
return obj
964969
}
970+
965971
func expandResourceFieldRef(r []interface{}) (*v1.ResourceFieldSelector, error) {
966972
if len(r) == 0 || r[0] == nil {
967973
return &v1.ResourceFieldSelector{}, nil
@@ -1045,7 +1051,6 @@ func expandEnvValueFrom(r []interface{}) (*v1.EnvVarSource, error) {
10451051
}
10461052
}
10471053
return obj, nil
1048-
10491054
}
10501055

10511056
func expandConfigMapRef(r []interface{}) *v1.ConfigMapEnvSource {

0 commit comments

Comments
 (0)