From 24b6c697a7301625a01963ac1777548d2a6b3c0f Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Tue, 27 Feb 2018 13:19:13 +0000 Subject: [PATCH] resource/kubernetes_service: Update external_ips correctly on K8S 1.8+ --- kubernetes/resource_kubernetes_service.go | 9 ++++++++- kubernetes/structure_service_spec.go | 23 ++++++++++++++++------- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/kubernetes/resource_kubernetes_service.go b/kubernetes/resource_kubernetes_service.go index 9c189a6515..0bb8f7c21d 100644 --- a/kubernetes/resource_kubernetes_service.go +++ b/kubernetes/resource_kubernetes_service.go @@ -236,7 +236,14 @@ func resourceKubernetesServiceUpdate(d *schema.ResourceData, meta interface{}) e ops := patchMetadata("metadata.0.", "/metadata/", d) if d.HasChange("spec") { - diffOps := patchServiceSpec("spec.0.", "/spec/", d) + serverVersion, err := conn.ServerVersion() + if err != nil { + return err + } + diffOps, err := patchServiceSpec("spec.0.", "/spec/", d, serverVersion) + if err != nil { + return err + } ops = append(ops, diffOps...) } data, err := ops.MarshalJSON() diff --git a/kubernetes/structure_service_spec.go b/kubernetes/structure_service_spec.go index 0b5b312958..19fb0f1dca 100644 --- a/kubernetes/structure_service_spec.go +++ b/kubernetes/structure_service_spec.go @@ -1,8 +1,10 @@ package kubernetes import ( + gversion "github.com/hashicorp/go-version" "github.com/hashicorp/terraform/helper/schema" "k8s.io/apimachinery/pkg/util/intstr" + "k8s.io/apimachinery/pkg/version" "k8s.io/client-go/pkg/api/v1" ) @@ -141,7 +143,7 @@ func expandServiceSpec(l []interface{}) v1.ServiceSpec { // Patch Ops -func patchServiceSpec(keyPrefix, pathPrefix string, d *schema.ResourceData) PatchOperations { +func patchServiceSpec(keyPrefix, pathPrefix string, d *schema.ResourceData, v *version.Info) (PatchOperations, error) { ops := make([]PatchOperation, 0, 0) if d.HasChange(keyPrefix + "selector") { ops = append(ops, &ReplaceOperation{ @@ -180,11 +182,18 @@ func patchServiceSpec(keyPrefix, pathPrefix string, d *schema.ResourceData) Patc }) } if d.HasChange(keyPrefix + "external_ips") { - // If we haven't done this the deprecated field would have priority - ops = append(ops, &ReplaceOperation{ - Path: pathPrefix + "deprecatedPublicIPs", - Value: nil, - }) + k8sVersion, err := gversion.NewVersion(v.String()) + if err != nil { + return nil, err + } + v1_8_0, _ := gversion.NewVersion("1.8.0") + if k8sVersion.LessThan(v1_8_0) { + // If we haven't done this the deprecated field would have priority + ops = append(ops, &ReplaceOperation{ + Path: pathPrefix + "deprecatedPublicIPs", + Value: nil, + }) + } ops = append(ops, &ReplaceOperation{ Path: pathPrefix + "externalIPs", @@ -197,5 +206,5 @@ func patchServiceSpec(keyPrefix, pathPrefix string, d *schema.ResourceData) Patc Value: d.Get(keyPrefix + "external_name").(string), }) } - return ops + return ops, nil }