Skip to content

Commit

Permalink
Upgrade to Docker v25 (#1032)
Browse files Browse the repository at this point in the history
This breaks #898 out from
the buildx stack so we can apply it the original provider.
  • Loading branch information
blampe authored Mar 18, 2024
1 parent dccb649 commit a4ba0e3
Show file tree
Hide file tree
Showing 6 changed files with 466 additions and 155 deletions.
292 changes: 292 additions & 0 deletions patches/0006-upgrade-docker-sdk.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,292 @@
diff --git a/internal/provider/resource_docker_container_funcs.go b/internal/provider/resource_docker_container_funcs.go
index 9f902d4..bde327f 100644
--- a/internal/provider/resource_docker_container_funcs.go
+++ b/internal/provider/resource_docker_container_funcs.go
@@ -379,7 +379,7 @@ func resourceDockerContainerCreate(ctx context.Context, d *schema.ResourceData,
hostConfig.StorageOpt = mapTypeMapValsToString(v.(map[string]interface{}))
}

- var retContainer container.ContainerCreateCreatedBody
+ var retContainer container.CreateResponse

// TODO mavogel add platform later which comes from API v1.41. Currently we pass nil
if retContainer, err = client.ContainerCreate(ctx, config, hostConfig, networkingConfig, nil, d.Get("name").(string)); err != nil {
diff --git a/internal/provider/resource_docker_volume.go b/internal/provider/resource_docker_volume.go
index dd010cd..fefa276 100644
--- a/internal/provider/resource_docker_volume.go
+++ b/internal/provider/resource_docker_volume.go
@@ -6,7 +6,6 @@ import (
"log"
"time"

- "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/volume"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
@@ -80,7 +79,7 @@ func resourceDockerVolume() *schema.Resource {
func resourceDockerVolumeCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*ProviderConfig).DockerClient

- createOpts := volume.VolumeCreateBody{}
+ createOpts := volume.CreateOptions{}

if v, ok := d.GetOk("name"); ok {
createOpts.Name = v.(string)
@@ -96,7 +95,7 @@ func resourceDockerVolumeCreate(ctx context.Context, d *schema.ResourceData, met
}

var err error
- var retVolume types.Volume
+ var retVolume volume.Volume
retVolume, err = client.VolumeCreate(ctx, createOpts)

if err != nil {
diff --git a/internal/provider/resource_docker_volume_test.go b/internal/provider/resource_docker_volume_test.go
index 7898747..585ca2c 100644
--- a/internal/provider/resource_docker_volume_test.go
+++ b/internal/provider/resource_docker_volume_test.go
@@ -5,13 +5,13 @@ import (
"fmt"
"testing"

- "github.com/docker/docker/api/types"
+ "github.com/docker/docker/api/types/volume"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func TestAccDockerVolume_basic(t *testing.T) {
- var v types.Volume
+ var v volume.Volume

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
@@ -39,7 +39,7 @@ func TestAccDockerVolume_basic(t *testing.T) {
}

func TestAccDockerVolume_full(t *testing.T) {
- var v types.Volume
+ var v volume.Volume

testCheckVolumeInspect := func(*terraform.State) error {
if v.Driver != "local" {
@@ -88,7 +88,7 @@ func TestAccDockerVolume_full(t *testing.T) {
}

func TestAccDockerVolume_labels(t *testing.T) {
- var v types.Volume
+ var v volume.Volume

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
@@ -127,7 +127,7 @@ func TestAccDockerVolume_labels(t *testing.T) {
})
}

-func checkDockerVolumeCreated(n string, volume *types.Volume) resource.TestCheckFunc {
+func checkDockerVolumeCreated(n string, volume *volume.Volume) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
diff --git a/internal/provider/provider.go b/internal/provider/provider.go
index 7feca79..29fcba3 100644
--- a/internal/provider/provider.go
+++ b/internal/provider/provider.go
@@ -11,7 +11,7 @@ import (
"strings"

"github.com/docker/cli/cli/config/configfile"
- "github.com/docker/docker/api/types"
+ "github.com/docker/docker/api/types/registry"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -217,17 +217,17 @@ func configure(version string, p *schema.Provider) func(context.Context, *schema
// AuthConfigs represents authentication options to use for the
// PushImage method accommodating the new X-Registry-Config header
type AuthConfigs struct {
- Configs map[string]types.AuthConfig `json:"configs"`
+ Configs map[string]registry.AuthConfig `json:"configs"`
}

// Take the given registry_auth schemas and return a map of registry auth configurations
func providerSetToRegistryAuth(authList *schema.Set) (*AuthConfigs, error) {
authConfigs := AuthConfigs{
- Configs: make(map[string]types.AuthConfig),
+ Configs: make(map[string]registry.AuthConfig),
}

for _, auth := range authList.List() {
- authConfig := types.AuthConfig{}
+ authConfig := registry.AuthConfig{}
address := auth.(map[string]interface{})["address"].(string)
authConfig.ServerAddress = normalizeRegistryAddress(address)
registryHostname := convertToHostname(authConfig.ServerAddress)
diff --git a/internal/provider/resource_docker_registry_image_funcs.go b/internal/provider/resource_docker_registry_image_funcs.go
index 33ba474..509b26a 100644
--- a/internal/provider/resource_docker_registry_image_funcs.go
+++ b/internal/provider/resource_docker_registry_image_funcs.go
@@ -14,6 +14,7 @@ import (

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
+ "github.com/docker/docker/api/types/registry"
"github.com/docker/docker/client"
"github.com/docker/go-units"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
@@ -143,11 +144,11 @@ func createImageBuildOptions(buildOptions map[string]interface{}) types.ImageBui
return ulimits
}

- readAuthConfigs := func(options []interface{}) map[string]types.AuthConfig {
- authConfigs := make(map[string]types.AuthConfig, len(options))
+ readAuthConfigs := func(options []interface{}) map[string]registry.AuthConfig {
+ authConfigs := make(map[string]registry.AuthConfig, len(options))
for _, v := range options {
authOptions := v.(map[string]interface{})
- auth := types.AuthConfig{
+ auth := registry.AuthConfig{
Username: authOptions["user_name"].(string),
Password: authOptions["password"].(string),
Auth: authOptions["auth"].(string),
@@ -201,7 +202,7 @@ func createImageBuildOptions(buildOptions map[string]interface{}) types.ImageBui
func pushDockerRegistryImage(ctx context.Context, client *client.Client, pushOpts internalPushImageOptions, username string, password string) error {
pushOptions := types.ImagePushOptions{}
if username != "" {
- auth := types.AuthConfig{Username: username, Password: password}
+ auth := registry.AuthConfig{Username: username, Password: password}
authBytes, err := json.Marshal(auth)
if err != nil {
return fmt.Errorf("Error creating push options: %s", err)
@@ -239,11 +240,11 @@ func pushDockerRegistryImage(ctx context.Context, client *client.Client, pushOpt

func getAuthConfigForRegistry(
registryWithoutProtocol string,
- providerConfig *ProviderConfig) (types.AuthConfig, error) {
+ providerConfig *ProviderConfig) (registry.AuthConfig, error) {
if authConfig, ok := providerConfig.AuthConfigs.Configs[registryWithoutProtocol]; ok {
return authConfig, nil
}
- return types.AuthConfig{}, fmt.Errorf("no auth config found for registry %s in auth configs: %#v", registryWithoutProtocol, providerConfig.AuthConfigs.Configs)
+ return registry.AuthConfig{}, fmt.Errorf("no auth config found for registry %s in auth configs: %#v", registryWithoutProtocol, providerConfig.AuthConfigs.Configs)
}

func buildHttpClientForRegistry(registryAddressWithProtocol string, insecureSkipVerify bool) *http.Client {
diff --git a/internal/provider/resource_docker_service_funcs.go b/internal/provider/resource_docker_service_funcs.go
index 333b584..478013a 100644
--- a/internal/provider/resource_docker_service_funcs.go
+++ b/internal/provider/resource_docker_service_funcs.go
@@ -12,6 +12,7 @@ import (
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/filters"
+ "github.com/docker/docker/api/types/registry"
"github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/client"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
@@ -593,25 +594,25 @@ func terminalState(state swarm.TaskState) bool {
}

// authToServiceAuth maps the auth to AuthConfiguration
-func authToServiceAuth(auths []interface{}) types.AuthConfig {
+func authToServiceAuth(auths []interface{}) registry.AuthConfig {
if len(auths) == 0 {
- return types.AuthConfig{}
+ return registry.AuthConfig{}
}
// it's maxItems = 1
auth := auths[0].(map[string]interface{})
if auth["username"] != nil && len(auth["username"].(string)) > 0 && auth["password"] != nil && len(auth["password"].(string)) > 0 {
- return types.AuthConfig{
+ return registry.AuthConfig{
Username: auth["username"].(string),
Password: auth["password"].(string),
ServerAddress: auth["server_address"].(string),
}
}

- return types.AuthConfig{}
+ return registry.AuthConfig{}
}

// fromRegistryAuth extract the desired AuthConfiguration for the given image
-func fromRegistryAuth(image string, authConfigs map[string]types.AuthConfig) types.AuthConfig {
+func fromRegistryAuth(image string, authConfigs map[string]registry.AuthConfig) registry.AuthConfig {
// Remove normalized prefixes to simplify substring
// DevSkim: ignore DS137138
image = strings.Replace(strings.Replace(image, "http://", "", 1), "https://", "", 1)
@@ -625,12 +626,12 @@ func fromRegistryAuth(image string, authConfigs map[string]types.AuthConfig) typ
}
}

- return types.AuthConfig{}
+ return registry.AuthConfig{}
}

// retrieveAndMarshalAuth retrieves and marshals the service registry auth
func retrieveAndMarshalAuth(d *schema.ResourceData, meta interface{}, stageType string) []byte {
- var auth types.AuthConfig
+ var auth registry.AuthConfig
// when a service is updated/set for the first time the auth is set but empty
// this is why we need this additional check
if rawAuth, ok := d.GetOk("auth"); ok && len(rawAuth.([]interface{})) != 0 {
diff --git a/internal/provider/resource_docker_container_funcs.go b/internal/provider/resource_docker_container_funcs.go
index bde327f..61c8e2b 100644
--- a/internal/provider/resource_docker_container_funcs.go
+++ b/internal/provider/resource_docker_container_funcs.go
@@ -236,7 +236,7 @@ func resourceDockerContainerCreate(ctx context.Context, d *schema.ResourceData,
Privileged: d.Get("privileged").(bool),
PublishAllPorts: d.Get("publish_all_ports").(bool),
RestartPolicy: container.RestartPolicy{
- Name: d.Get("restart").(string),
+ Name: container.RestartPolicyMode(d.Get("restart").(string)),
MaximumRetryCount: d.Get("max_retry_count").(int),
},
Runtime: d.Get("runtime").(string),
diff --git a/internal/provider/resource_docker_image_funcs.go b/internal/provider/resource_docker_image_funcs.go
index 5c15e57..15370d8 100644
--- a/internal/provider/resource_docker_image_funcs.go
+++ b/internal/provider/resource_docker_image_funcs.go
@@ -15,6 +15,7 @@ import (

"github.com/docker/cli/cli/command/image/build"
"github.com/docker/docker/api/types"
+ "github.com/docker/docker/api/types/registry"
"github.com/docker/docker/api/types/versions"
"github.com/docker/docker/client"
"github.com/docker/docker/errdefs"
@@ -195,7 +196,7 @@ func fetchLocalImages(ctx context.Context, data *Data, client *client.Client) er
func pullImage(ctx context.Context, data *Data, client *client.Client, authConfig *AuthConfigs, image string, platform string) error {
pullOpts := parseImageOptions(image)

- auth := types.AuthConfig{}
+ auth := registry.AuthConfig{}
if authConfig, ok := authConfig.Configs[pullOpts.Registry]; ok {
auth = authConfig
}
diff --git a/internal/provider/resource_docker_container_funcs.go b/internal/provider/resource_docker_container_funcs.go
index 8c5409a..4a7e2e9 100644
--- a/internal/provider/resource_docker_container_funcs.go
+++ b/internal/provider/resource_docker_container_funcs.go
@@ -835,7 +835,7 @@ func resourceDockerContainerUpdate(ctx context.Context, d *schema.ResourceData,

updateConfig := container.UpdateConfig{
RestartPolicy: container.RestartPolicy{
- Name: d.Get("restart").(string),
+ Name: container.RestartPolicyMode(d.Get("restart").(string)),
MaximumRetryCount: d.Get("max_retry_count").(int),
},
Resources: container.Resources{
diff --git a/internal/provider/config.go b/internal/provider/config.go
index 7393751..5b2cabf 100644
--- a/internal/provider/config.go
+++ b/internal/provider/config.go
@@ -115,7 +115,7 @@ func (c *Config) NewClient() (*client.Client, error) {
return nil, err
}
if helper != nil {
- opts = append(opts, client.WithDialContext(helper.Dialer), client.WithHost(helper.Host))
+ opts = append(opts, client.WithHost(helper.Host), client.WithDialContext(helper.Dialer))
} else if c.Host != "" {
opts = append(opts, client.WithHost(c.Host))
}
Loading

0 comments on commit a4ba0e3

Please sign in to comment.