Skip to content

Commit

Permalink
Merge pull request #69 from sl1pm4t/deploy-strategy
Browse files Browse the repository at this point in the history
Fix “expected pointer, but got nil” error when Deployment strategy = Recreate
  • Loading branch information
sl1pm4t authored Oct 16, 2018
2 parents 130d154 + 322b824 commit ae3d43e
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 11 deletions.
4 changes: 2 additions & 2 deletions kubernetes/api_versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,12 @@ func (kp *kubernetesProvider) serverSupportsResourceAPIVersion(rname string, gro
func Convert(item, out interface{}) error {
bytes, err := json.Marshal(item)
if err != nil {
return err
return fmt.Errorf("failed to convert API object - Marshal failed: %s", err)
}

err = json.Unmarshal(bytes, out)
if err != nil {
return err
return fmt.Errorf("failed to convert API object - Unmarshal failed: %s", err)
}

return nil
Expand Down
10 changes: 5 additions & 5 deletions kubernetes/resource_kubernetes_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func resourceKubernetesDeployment() *schema.Resource {
Type: schema.TypeList,
Optional: true,
Computed: true,
Description: "Update strategy. One of RollingUpdate, Destroy. Defaults to RollingDate",
Description: "Update strategy. One of RollingUpdate, Recreate. Defaults to RollingDate",
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Expand All @@ -118,15 +118,15 @@ func resourceKubernetesDeployment() *schema.Resource {
Schema: map[string]*schema.Schema{
"max_surge": {
Type: schema.TypeString,
Description: "max surge",
Description: "The maximum number of pods that can be scheduled above the desired number of pods. Value can be an absolute number (ex: 5) or a percentage of desired pods (ex: 10%). This can not be 0 if MaxUnavailable is 0. Absolute number is calculated from percentage by rounding up. Defaults to 25%. Example: when this is set to 30%, the new RC can be scaled up immediately when the rolling update starts, such that the total number of old and new pods do not exceed 130% of desired pods. Once old pods have been killed, new RC can be scaled up further, ensuring that total number of pods running at any time during the update is atmost 130% of desired pods.",
Optional: true,
Default: 1,
Default: "25%",
},
"max_unavailable": {
Type: schema.TypeString,
Description: "max unavailable",
Description: "The maximum number of pods that can be unavailable during the update. Value can be an absolute number (ex: 5) or a percentage of desired pods (ex: 10%). Absolute number is calculated from percentage by rounding down. This can not be 0 if MaxSurge is 0. Defaults to 25%. Example: when this is set to 30%, the old RC can be scaled down to 70% of desired pods immediately when the rolling update starts. Once new pods are ready, old RC can be scaled down further, followed by scaling up the new RC, ensuring that the total number of pods available at all times during the update is at least 70% of desired pods.",
Optional: true,
Default: 1,
Default: "25%",
},
},
},
Expand Down
170 changes: 170 additions & 0 deletions kubernetes/resource_kubernetes_deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,66 @@ func TestAccKubernetesDeployment_noTopLevelLabels(t *testing.T) {
})
}

func TestAccKubernetesDeployment_strategy(t *testing.T) {
t.Parallel()

var conf appsv1.Deployment
name := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
IDRefreshName: "kubernetes_deployment.test",
Providers: testAccProviders,
CheckDestroy: testAccCheckKubernetesDeploymentDestroy,
Steps: []resource.TestStep{
{
Config: testAccKubernetesDeploymentWithStrategy(name, "Recreate"),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckKubernetesDeploymentExists("kubernetes_deployment.test", &conf),
resource.TestCheckResourceAttr("kubernetes_deployment.test", "spec.0.strategy.0.type", "Recreate"),
resource.TestCheckResourceAttr("kubernetes_deployment.test", "spec.0.strategy.0.rolling_update.#", "0"),
),
},
{
Config: testAccKubernetesDeploymentWithStrategy(name, "RollingUpdate"),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckKubernetesDeploymentExists("kubernetes_deployment.test", &conf),
resource.TestCheckResourceAttr("kubernetes_deployment.test", "spec.0.strategy.0.type", "RollingUpdate"),
resource.TestCheckResourceAttr("kubernetes_deployment.test", "spec.0.strategy.0.rolling_update.#", "1"),
),
},
{
Config: testAccKubernetesDeploymentWithStrategy(name, "Recreate"),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckKubernetesDeploymentExists("kubernetes_deployment.test", &conf),
resource.TestCheckResourceAttr("kubernetes_deployment.test", "spec.0.strategy.0.type", "Recreate"),
resource.TestCheckResourceAttr("kubernetes_deployment.test", "spec.0.strategy.0.rolling_update.#", "0"),
),
},
{
Config: testAccKubernetesDeploymentWithRollingUpdateStrategy(name),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckKubernetesDeploymentExists("kubernetes_deployment.test", &conf),
resource.TestCheckResourceAttr("kubernetes_deployment.test", "spec.0.strategy.0.type", "RollingUpdate"),
resource.TestCheckResourceAttr("kubernetes_deployment.test", "spec.0.strategy.0.rolling_update.#", "1"),
resource.TestCheckResourceAttr("kubernetes_deployment.test", "spec.0.strategy.0.rolling_update.0.max_surge", "50%"),
resource.TestCheckResourceAttr("kubernetes_deployment.test", "spec.0.strategy.0.rolling_update.0.max_surge", "50%"),
),
},
{
Config: testAccKubernetesDeploymentWithRollingUpdateStrategy2(name),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckKubernetesDeploymentExists("kubernetes_deployment.test", &conf),
resource.TestCheckResourceAttr("kubernetes_deployment.test", "spec.0.strategy.0.type", "RollingUpdate"),
resource.TestCheckResourceAttr("kubernetes_deployment.test", "spec.0.strategy.0.rolling_update.#", "1"),
resource.TestCheckResourceAttr("kubernetes_deployment.test", "spec.0.strategy.0.rolling_update.0.max_surge", "25%"),
resource.TestCheckResourceAttr("kubernetes_deployment.test", "spec.0.strategy.0.rolling_update.0.max_surge", "25%"),
),
},
},
})
}

func pause() resource.TestCheckFunc {
return func(s *terraform.State) error {
time.Sleep(1 * time.Minute)
Expand Down Expand Up @@ -554,3 +614,113 @@ resource "kubernetes_deployment" "test" {
}
`, depName, imageName)
}

func testAccKubernetesDeploymentWithStrategy(depName, strategy string) string {
return fmt.Sprintf(`
resource "kubernetes_deployment" "test" {
metadata {
name = "%s"
}
spec {
selector {
foo = "bar"
}
strategy {
type = "%s"
}
template {
metadata {
labels {
foo = "bar"
}
}
spec {
container {
image = "alpine"
name = "containername"
}
}
}
}
}
`, depName, strategy)
}

func testAccKubernetesDeploymentWithRollingUpdateStrategy(depName string) string {
return fmt.Sprintf(`
resource "kubernetes_deployment" "test" {
metadata {
name = "%s"
}
spec {
selector {
foo = "bar"
}
strategy {
type = "RollingUpdate"
rolling_update {
max_surge = "50%%"
max_unavailable = "50%%"
}
}
template {
metadata {
labels {
foo = "bar"
}
}
spec {
container {
image = "alpine"
name = "containername"
}
}
}
}
}
`, depName)
}

func testAccKubernetesDeploymentWithRollingUpdateStrategy2(depName string) string {
return fmt.Sprintf(`
resource "kubernetes_deployment" "test" {
metadata {
name = "%s"
}
spec {
selector {
foo = "bar"
}
strategy {
type = "RollingUpdate"
rolling_update {}
}
template {
metadata {
labels {
foo = "bar"
}
}
spec {
container {
image = "alpine"
name = "containername"
}
}
}
}
}
`, depName)
}
9 changes: 5 additions & 4 deletions kubernetes/structures_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,12 @@ func expandDeploymentSpec(deployment []interface{}) (appsv1.DeploymentSpec, erro

obj.MinReadySeconds = int32(in["min_ready_seconds"].(int))
obj.Paused = in["paused"].(bool)
obj.Replicas = ptrToInt32(int32(in["replicas"].(int)))
obj.Strategy = expandDeploymentStrategy(in["strategy"].([]interface{}))

if in["progress_deadline_seconds"].(int) > 0 {
obj.ProgressDeadlineSeconds = ptrToInt32(int32(in["progress_deadline_seconds"].(int)))
}
obj.Replicas = ptrToInt32(int32(in["replicas"].(int)))

if in["revision_history_limit"] != nil {
obj.RevisionHistoryLimit = ptrToInt32(int32(in["revision_history_limit"].(int)))
Expand All @@ -92,7 +94,6 @@ func expandDeploymentSpec(deployment []interface{}) (appsv1.DeploymentSpec, erro
obj.Selector = &metav1.LabelSelector{
MatchLabels: expandStringMap(in["selector"].(map[string]interface{})),
}
obj.Strategy = expandDeploymentStrategy(in["strategy"].([]interface{}))

for _, v := range in["template"].([]interface{}) {
template := v.(map[string]interface{})
Expand All @@ -116,7 +117,7 @@ func expandDeploymentStrategy(p []interface{}) appsv1.DeploymentStrategy {
if v, ok := in["type"]; ok {
obj.Type = appsv1.DeploymentStrategyType(v.(string))
}
if v, ok := in["rolling_update"]; ok {
if v, ok := in["rolling_update"]; ok && obj.Type == appsv1.RollingUpdateDeploymentStrategyType {
obj.RollingUpdate = expandRollingUpdateDeployment(v.([]interface{}))
}
return obj
Expand All @@ -125,7 +126,7 @@ func expandDeploymentStrategy(p []interface{}) appsv1.DeploymentStrategy {
func expandRollingUpdateDeployment(p []interface{}) *appsv1.RollingUpdateDeployment {
obj := appsv1.RollingUpdateDeployment{}
if len(p) == 0 || p[0] == nil {
return &obj
return nil
}
in := p[0].(map[string]interface{})

Expand Down

0 comments on commit ae3d43e

Please sign in to comment.