From 32a2c35547bc17df64da75710e4c5dc4d682ba08 Mon Sep 17 00:00:00 2001 From: Matt Morrison Date: Thu, 31 May 2018 16:52:19 +1200 Subject: [PATCH] Use `Update()` instead of `Patch()` to update svc Fixes #23 --- kubernetes/resource_kubernetes_service.go | 32 ++++++++----------- .../resource_kubernetes_service_test.go | 1 + kubernetes/structures.go | 3 ++ 3 files changed, 17 insertions(+), 19 deletions(-) diff --git a/kubernetes/resource_kubernetes_service.go b/kubernetes/resource_kubernetes_service.go index 3aef35454f..b5531e6520 100644 --- a/kubernetes/resource_kubernetes_service.go +++ b/kubernetes/resource_kubernetes_service.go @@ -10,7 +10,6 @@ import ( api "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/errors" meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1" - pkgApi "k8s.io/apimachinery/pkg/types" kubernetes "k8s.io/client-go/kubernetes" ) @@ -235,35 +234,30 @@ func resourceKubernetesServiceRead(d *schema.ResourceData, meta interface{}) err func resourceKubernetesServiceUpdate(d *schema.ResourceData, meta interface{}) error { conn := meta.(*kubernetes.Clientset) - namespace, name, err := idParts(d.Id()) + namespace, _, err := idParts(d.Id()) if err != nil { return err } - ops := patchMetadata("metadata.0.", "/metadata/", d) - if d.HasChange("spec") { - 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...) + metadata := expandMetadata(d.Get("metadata").([]interface{})) + spec := expandServiceSpec(d.Get("spec").([]interface{})) + + if metadata.Namespace == "" { + metadata.Namespace = "default" } - data, err := ops.MarshalJSON() - if err != nil { - return fmt.Errorf("Failed to marshal update operations: %s", err) + + service := &api.Service{ + ObjectMeta: metadata, + Spec: spec, } - log.Printf("[INFO] Updating service %q: %v", name, string(data)) - out, err := conn.CoreV1().Services(namespace).Patch(name, pkgApi.JSONPatchType, data) + + out, err := conn.CoreV1().Services(namespace).Update(service) if err != nil { return fmt.Errorf("Failed to update service: %s", err) } log.Printf("[INFO] Submitted updated service: %#v", out) - d.SetId(buildId(out.ObjectMeta)) + d.SetId(buildId(out.ObjectMeta)) return resourceKubernetesServiceRead(d, meta) } diff --git a/kubernetes/resource_kubernetes_service_test.go b/kubernetes/resource_kubernetes_service_test.go index 1f449e199e..85f4e7e705 100644 --- a/kubernetes/resource_kubernetes_service_test.go +++ b/kubernetes/resource_kubernetes_service_test.go @@ -591,6 +591,7 @@ resource "kubernetes_service" "test" { } type = "LoadBalancer" + load_balancer_ip = "100.24.12.6" // New line added } } `, name, name) diff --git a/kubernetes/structures.go b/kubernetes/structures.go index 9375aba716..533b1a4101 100644 --- a/kubernetes/structures.go +++ b/kubernetes/structures.go @@ -50,6 +50,9 @@ func expandMetadata(in []interface{}) metav1.ObjectMeta { if v, ok := m["namespace"]; ok { meta.Namespace = v.(string) } + if v, ok := m["resource_version"]; ok { + meta.ResourceVersion = v.(string) + } return meta }