Skip to content

Commit

Permalink
kubernetes: Do kubernetes_pod diff-ing via CompareFunc (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Sep 20, 2015
1 parent 28f5900 commit 3a3463e
Showing 1 changed file with 43 additions and 5 deletions.
48 changes: 43 additions & 5 deletions builtin/providers/kubernetes/resource_pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package kubernetes

import (
"encoding/json"
"fmt"
"log"
"reflect"
"strings"

"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
"k8s.io/kubernetes/pkg/api"
client "k8s.io/kubernetes/pkg/client/unversioned"
"k8s.io/kubernetes/pkg/util/yaml"
Expand Down Expand Up @@ -38,8 +41,9 @@ func resourceKubernetesPod() *schema.Resource {
},

"spec": &schema.Schema{
Type: schema.TypeString,
Required: true,
Type: schema.TypeString,
Required: true,
CompareFunc: compareNonEmptyFields,
StateFunc: func(input interface{}) string {
s, err := normalizePodSpec(input.(string))
if err != nil {
Expand Down Expand Up @@ -162,7 +166,7 @@ func flattenPodSpec(spec api.PodSpec) (string, error) {
return "", err
}

return string(b), nil
return normalizePodSpec(string(b))
}

func normalizePodSpec(input string) (string, error) {
Expand All @@ -175,12 +179,46 @@ func normalizePodSpec(input string) (string, error) {
return "", err
}

// TODO: Add/ignore default structures, e.g. resources.limits.cpu = 100m

b, err := json.Marshal(spec)
if err != nil {
return "", err
}

return string(b), nil
}

func compareNonEmptyFields(o, n string) (*terraform.ResourceAttrDiff, error) {
oo := &api.PodSpec{}
json.Unmarshal([]byte(o), &oo)

no := &api.PodSpec{}
json.Unmarshal([]byte(n), &no)

err := stripUndefinedFieldsFromNew(oo, no)
if err != nil {
return nil, err
}

old_s, err := json.Marshal(oo)
if err != nil {
return nil, err
}
new_s, err := json.Marshal(no)
if err != nil {
return nil, err
}

if !reflect.DeepEqual(oo, no) {
return &terraform.ResourceAttrDiff{
Old: string(old_s),
New: string(new_s),
}, nil
}
return nil, nil

}

func stripUndefinedFieldsFromNew(o, n *api.PodSpec) error {
// TODO
return nil
}

0 comments on commit 3a3463e

Please sign in to comment.