Skip to content

Commit

Permalink
Use Update() instead of Patch() to update svc
Browse files Browse the repository at this point in the history
  • Loading branch information
sl1pm4t committed May 31, 2018
1 parent 84a6a12 commit c1e3a8c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 19 deletions.
32 changes: 13 additions & 19 deletions kubernetes/resource_kubernetes_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

func resourceKubernetesService() *schema.Resource {
Expand Down Expand Up @@ -228,35 +227,30 @@ func resourceKubernetesServiceRead(d *schema.ResourceData, meta interface{}) err
func resourceKubernetesServiceUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*kubernetesProvider).conn

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)
}

Expand Down
1 change: 1 addition & 0 deletions kubernetes/resource_kubernetes_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,7 @@ resource "kubernetes_service" "test" {
target_port = 81
}
type = "LoadBalancer"
load_balancer_ip = "100.24.12.6" // New line added
}
}`, name, name)
}
Expand Down
3 changes: 3 additions & 0 deletions kubernetes/structures.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,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
}
Expand Down

0 comments on commit c1e3a8c

Please sign in to comment.