Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/resources/service.md
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,8 @@ Optional:
- `healthcheck` (Block List, Max: 1) A test to perform to check that the container is healthy (see [below for nested schema](#nestedblock--task_spec--container_spec--healthcheck))
- `hostname` (String) The hostname to use for the container, as a valid RFC 1123 hostname
- `hosts` (Block Set) A list of hostname/IP mappings to add to the container's hosts file (see [below for nested schema](#nestedblock--task_spec--container_spec--hosts))
- `init` (Boolean) Configured whether an init process should be injected for the container. If unset this will default to the
`dockerd` defaults.
- `isolation` (String) Isolation technology of the containers running the service. (Windows only). Defaults to `default`.
- `labels` (Block Set) User-defined key/value metadata (see [below for nested schema](#nestedblock--task_spec--container_spec--labels))
- `mounts` (Block Set) Specification for mounts to be added to containers created as part of the service (see [below for nested schema](#nestedblock--task_spec--container_spec--mounts))
Expand Down
6 changes: 6 additions & 0 deletions internal/provider/resource_docker_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,12 @@ func resourceDockerService() *schema.Resource {
Elem: &schema.Schema{Type: schema.TypeString},
Description: "List of Linux capabilities to drop from the container",
},
"init": {
Type: schema.TypeBool,
Description: "Configured whether an init process should be injected for this container. If unset this will default to the `dockerd` defaults.",
Optional: true,
Computed: true,
},
},
},
},
Expand Down
6 changes: 3 additions & 3 deletions internal/provider/resource_docker_service_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func resourceDockerServiceCreate(ctx context.Context, d *schema.ResourceData, me
var err error
client := meta.(*ProviderConfig).DockerClient

serviceSpec, err := createServiceSpec(d)
serviceSpec, err := createServiceSpec(d, meta)
if err != nil {
return diag.FromErr(err)
}
Expand Down Expand Up @@ -132,7 +132,7 @@ func resourceDockerServiceReadRefreshFunc(ctx context.Context,
d.Set("name", service.Spec.Name)
d.Set("labels", mapToLabelSet(service.Spec.Labels))

if err = d.Set("task_spec", flattenTaskSpec(service.Spec.TaskTemplate, d)); err != nil {
if err = d.Set("task_spec", flattenTaskSpec(service.Spec.TaskTemplate, d, meta)); err != nil {
log.Printf("[WARN] failed to set task spec from API: %s", err)
}
if err = d.Set("mode", flattenServiceMode(service.Spec.Mode)); err != nil {
Expand Down Expand Up @@ -169,7 +169,7 @@ func resourceDockerServiceUpdate(ctx context.Context, d *schema.ResourceData, me
return diag.FromErr(err)
}

serviceSpec, err := createServiceSpec(d)
serviceSpec, err := createServiceSpec(d, meta)
if err != nil {
return diag.FromErr(err)
}
Expand Down
29 changes: 21 additions & 8 deletions internal/provider/resource_docker_service_structures.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ import (
// flatten API objects to the terraform schema
// ////////////
// see https://learn.hashicorp.com/tutorials/terraform/provider-create?in=terraform/providers#add-flattening-functions
func flattenTaskSpec(in swarm.TaskSpec, d *schema.ResourceData) []interface{} {
func flattenTaskSpec(in swarm.TaskSpec, d *schema.ResourceData, meta interface{}) []interface{} {
m := make(map[string]interface{})
if in.ContainerSpec != nil {
m["container_spec"] = flattenContainerSpec(in.ContainerSpec)
m["container_spec"] = flattenContainerSpec(in.ContainerSpec, meta)
}
if in.Resources != nil {
m["resources"] = flattenTaskResources(in.Resources)
Expand Down Expand Up @@ -111,7 +111,8 @@ func flattenServiceEndpointSpec(in *swarm.EndpointSpec) []interface{} {
}

// /// start TaskSpec
func flattenContainerSpec(in *swarm.ContainerSpec) []interface{} {
func flattenContainerSpec(in *swarm.ContainerSpec, meta interface{}) []interface{} {
client := meta.(*ProviderConfig).DockerClient
out := make([]interface{}, 0)
m := make(map[string]interface{})
if len(in.Image) > 0 {
Expand Down Expand Up @@ -183,6 +184,11 @@ func flattenContainerSpec(in *swarm.ContainerSpec) []interface{} {
if len(in.CapabilityDrop) > 0 {
m["cap_drop"] = in.CapabilityDrop
}
if client.ClientVersion() >= "1.37" {
if in.Init != nil {
m["init"] = *in.Init
}
}
out = append(out, m)
return out
}
Expand Down Expand Up @@ -574,7 +580,7 @@ func flattenServicePorts(in []swarm.PortConfig) []interface{} {
// create API object from the terraform resource schema
// ////////////
// createServiceSpec creates the service spec: https://docs.docker.com/engine/api/v1.32/#operation/ServiceCreate
func createServiceSpec(d *schema.ResourceData) (swarm.ServiceSpec, error) {
func createServiceSpec(d *schema.ResourceData, meta interface{}) (swarm.ServiceSpec, error) {
serviceSpec := swarm.ServiceSpec{
Annotations: swarm.Annotations{
Name: d.Get("name").(string),
Expand All @@ -587,7 +593,7 @@ func createServiceSpec(d *schema.ResourceData) (swarm.ServiceSpec, error) {
}
serviceSpec.Labels = labels

taskTemplate, err := createServiceTaskSpec(d)
taskTemplate, err := createServiceTaskSpec(d, meta)
if err != nil {
return serviceSpec, err
}
Expand Down Expand Up @@ -630,15 +636,15 @@ func createServiceLabels(d *schema.ResourceData) (map[string]string, error) {

// == start taskSpec
// createServiceTaskSpec creates the task template for the service
func createServiceTaskSpec(d *schema.ResourceData) (swarm.TaskSpec, error) {
func createServiceTaskSpec(d *schema.ResourceData, meta interface{}) (swarm.TaskSpec, error) {
taskSpec := swarm.TaskSpec{}
if v, ok := d.GetOk("task_spec"); ok {
if len(v.([]interface{})) > 0 {
for _, rawTaskSpec := range v.([]interface{}) {
rawTaskSpec := rawTaskSpec.(map[string]interface{})

if rawContainerSpec, ok := rawTaskSpec["container_spec"]; ok {
containerSpec, err := createContainerSpec(rawContainerSpec)
containerSpec, err := createContainerSpec(rawContainerSpec, meta)
if err != nil {
return taskSpec, err
}
Expand Down Expand Up @@ -693,7 +699,8 @@ func createServiceTaskSpec(d *schema.ResourceData) (swarm.TaskSpec, error) {
}

// createContainerSpec creates the container spec
func createContainerSpec(v interface{}) (*swarm.ContainerSpec, error) {
func createContainerSpec(v interface{}, meta interface{}) (*swarm.ContainerSpec, error) {
client := meta.(*ProviderConfig).DockerClient
containerSpec := swarm.ContainerSpec{}
if len(v.([]interface{})) > 0 {
for _, rawContainerSpec := range v.([]interface{}) {
Expand Down Expand Up @@ -965,6 +972,12 @@ func createContainerSpec(v interface{}) (*swarm.ContainerSpec, error) {
containerSpec.CapabilityDrop = append(containerSpec.CapabilityDrop, cap.(string))
}
}
if client.ClientVersion() >= "1.37" {
if value, ok := rawContainerSpec["init"]; ok {
v := value.(bool)
containerSpec.Init = &v
}
}

}
}
Expand Down