nicgrayson / terraform-provider-marathon

a Terraform (http://terraform.io) provider for interacting with Marathon (https://mesosphere.github.io/marathon/)
MIT License
59 stars 24 forks source link

Error when building plan against existing state #60

Open rakai93 opened 7 years ago

rakai93 commented 7 years ago

When I run terraform (including the marathon provider plugin) plan against an existing state, the following error occurs:

Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.

marathon_app.my_app: Refreshing state... (ID: /teststra...egy-core)
panic: empty structure "ports." has entry "ports.0"

goroutine 647 [running]:
github.com/hashicorp/terraform/terraform.(*InstanceState).MergeDiff(0xc420221d60, 0xc42030bae0, 0x0)
    /opt/gopath/src/github.com/hashicorp/terraform/terraform/state.go:1713 +0x5a8
github.com/hashicorp/terraform/terraform.(*EvalDiff).Eval(0xc420b06000, 0x740d2c0, 0xc420344620, 0x2, 0x2, 0x4c582eb, 0x4)
    /opt/gopath/src/github.com/hashicorp/terraform/terraform/eval_diff.go:176 +0x481
github.com/hashicorp/terraform/terraform.EvalRaw(0x73dae60, 0xc420b06000, 0x740d2c0, 0xc420344620, 0x48b8d80, 0xc420221d60, 0x0, 0x0)
    /opt/gopath/src/github.com/hashicorp/terraform/terraform/eval.go:53 +0x175
github.com/hashicorp/terraform/terraform.(*EvalSequence).Eval(0xc4205be4a0, 0x740d2c0, 0xc420344620, 0x2, 0x2, 0x4c582eb, 0x4)
    /opt/gopath/src/github.com/hashicorp/terraform/terraform/eval_sequence.go:14 +0xc1
github.com/hashicorp/terraform/terraform.EvalRaw(0x73db420, 0xc4205be4a0, 0x740d2c0, 0xc420344620, 0x42d9bc0, 0xc420b1c5aa, 0x4050660, 0xc420b1c650)
    /opt/gopath/src/github.com/hashicorp/terraform/terraform/eval.go:53 +0x175
github.com/hashicorp/terraform/terraform.Eval(0x73db420, 0xc4205be4a0, 0x740d2c0, 0xc420344620, 0xc4205be4a0, 0x73db420, 0xc4205be4a0, 0x200000003)
    /opt/gopath/src/github.com/hashicorp/terraform/terraform/eval.go:34 +0x4d
github.com/hashicorp/terraform/terraform.(*Graph).walk.func1(0x4a97120, 0xc420af40c8, 0x0, 0x0)
    /opt/gopath/src/github.com/hashicorp/terraform/terraform/graph.go:126 +0xd4d
github.com/hashicorp/terraform/dag.(*Walker).walkVertex(0xc420266690, 0x4a97120, 0xc420af40c8, 0xc420afb240)
    /opt/gopath/src/github.com/hashicorp/terraform/dag/walk.go:387 +0x392
created by github.com/hashicorp/terraform/dag.(*Walker).Update
    /opt/gopath/src/github.com/hashicorp/terraform/dag/walk.go:310 +0x9ca

!!!!!!!!!!!!!!!!!!!!!!!!!!! TERRAFORM CRASH !!!!!!!!!!!!!!!!!!!!!!!!!!!!

Terraform crashed! This is always indicative of a bug within Terraform.
A crash log has been placed at "crash.log" relative to your current
working directory. It would be immensely helpful if you could please
report the crash with Terraform[1] so that we can fix this.

When reporting bugs, please include your terraform version. That
information is available on the first line of crash.log. You can also
get it by running 'terraform --version' on the command line.

[1]: https://github.com/hashicorp/terraform/issues

!!!!!!!!!!!!!!!!!!!!!!!!!!! TERRAFORM CRASH !!!!!!!!!!!!!!!!!!!!!!!!!!!!

The related resource:

resource "marathon_app" "my_app" {
  app_id = "/my/app"
  cmd = "/do/something"

  cpus = 1
  mem = 128
  instances = 1

  constraints {
    constraint {
      attribute = "role"
      operation = "LIKE"
      parameter = "some_role"
    }
  }

  container {
    docker {
      image = "${var.docker_registry}/some/image:latest"
      force_pull_image = true
      network = "BRIDGE"
      port_mappings {
        port_mapping {
          container_port = 5000
          host_port = 0
          protocol = "tcp"
        }
      }
    }
  }

  env {
    SOME = "ENV"
  }

  health_checks {
    health_check {
      grace_period_seconds = 60
      interval_seconds = 60
      timeout_seconds = 20
      max_consecutive_failures = 3
      path = "/api/v1/status"
      port_index = 0
      protocol = "HTTP"
    }
  }

  upgrade_strategy {
    minimum_health_capacity = 1
    maximum_over_capacity = 0
  }

  labels {
    HAPROXY_GROUP = "external"
    HAPROXY_0_VHOST = "my-app.my-domain"
  }
}

State file:

{
    "version": 3,
    "terraform_version": "0.9.1",
    "serial": 7,
    "lineage": "<uuid>",
    "modules": [
        {
            "path": [
                "root"
            ],
            "outputs": {},
            "resources": {
                "marathon_app.my_app": {
                    "type": "marathon_app",
                    "depends_on": [],
                    "primary": {
                        "id": "/my/app",
                        "attributes": {
                            "accepted_resource_roles.#": "0",
                            "app_id": "/my/app",
                            "args.#": "0",
                            "backoff_factor": "1.15",
                            "backoff_seconds": "1",
                            "cmd": "/do/something",
                            "constraints.#": "1",
                            "constraints.0.constraint.#": "1",
                            "constraints.0.constraint.0.attribute": "role",
                            "constraints.0.constraint.0.operation": "LIKE",
                            "constraints.0.constraint.0.parameter": "some_role",
                            "container.#": "1",
                            "container.0.docker.#": "1",
                            "container.0.docker.0.force_pull_image": "true",
                            "container.0.docker.0.image": "docker-registry.my-domain/some/image:latest",
                            "container.0.docker.0.network": "BRIDGE",
                            "container.0.docker.0.parameters.#": "0",
                            "container.0.docker.0.port_mappings.#": "1",
                            "container.0.docker.0.port_mappings.0.port_mapping.#": "1",
                            "container.0.docker.0.port_mappings.0.port_mapping.0.container_port": "5000",
                            "container.0.docker.0.port_mappings.0.port_mapping.0.host_port": "0",
                            "container.0.docker.0.port_mappings.0.port_mapping.0.protocol": "tcp",
                            "container.0.docker.0.port_mappings.0.port_mapping.0.service_port": "0",
                            "container.0.docker.0.privileged": "false",
                            "container.0.type": "DOCKER",
                            "container.0.volumes.#": "0",
                            "cpus": "1",
                            "dependencies.#": "0",
                            "env.%": "1",
                            "env.SOME": "ENV",
                            "fetch.#": "0",
                            "health_checks.#": "1",
                            "health_checks.0.health_check.#": "1",
                            "health_checks.0.health_check.0.command.#": "0",
                            "health_checks.0.health_check.0.grace_period_seconds": "60",
                            "health_checks.0.health_check.0.interval_seconds": "60",
                            "health_checks.0.health_check.0.max_consecutive_failures": "3",
                            "health_checks.0.health_check.0.path": "/api/v1/status",
                            "health_checks.0.health_check.0.port_index": "0",
                            "health_checks.0.health_check.0.protocol": "HTTP",
                            "health_checks.0.health_check.0.timeout_seconds": "20",
                            "id": "/teststrategy/teststrategy-core",
                            "instances": "1",
                            "labels.%": "2",
                            "labels.HAPROXY_0_VHOST": "my-app.my-domain",
                            "labels.HAPROXY_GROUP": "external",
                            "mem": "128",
                            "ports.#": "1",
                            "ports.0": "0",
                            "require_ports": "false",
                            "upgrade_strategy.#": "1",
                            "upgrade_strategy.0.maximum_over_capacity": "0",
                            "upgrade_strategy.0.minimum_health_capacity": "1",
                            "uris.#": "0",
                            "version": "2017-06-20T14:08:40.761Z"
                        },
                        "meta": {},
                        "tainted": false
                    },
                    "deposed": [],
                    "provider": ""
                }
            },
            "depends_on": []
        }
    ]
}

I open the issue here instead of the terraform repository as I assume this bug is issued by the marathon plugin. It seems like the port definitions generated by this plugin is not correctly interpreted later once the state is read.

Versions:

nicgrayson commented 7 years ago

@rakai93 I always add ports = [0] when doing a docker port mapping

rakai93 commented 7 years ago

@nicgrayson Thx, that worked for me, although this seems more like a workaround.