Skip to content

Commit

Permalink
resource/kubernetes_service: Update external_ips correctly on K8S 1.8+
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Feb 27, 2018
1 parent b88dd22 commit 24b6c69
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
9 changes: 8 additions & 1 deletion kubernetes/resource_kubernetes_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
23 changes: 16 additions & 7 deletions kubernetes/structure_service_spec.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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",
Expand All @@ -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
}

0 comments on commit 24b6c69

Please sign in to comment.