Skip to content

Commit

Permalink
fix(kubernetes): sort during reconcile (grafana#33)
Browse files Browse the repository at this point in the history
Allows stable order in show and diff
  • Loading branch information
sh0rez authored Aug 9, 2019
1 parent 1e37b20 commit ab9c43a
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions pkg/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package kubernetes
import (
"bytes"
"fmt"
"sort"
"strings"

"github.com/Masterminds/semver"
Expand Down Expand Up @@ -41,6 +42,18 @@ func New(s v1alpha1.Spec) *Kubernetes {
// Manifest describes a single Kubernetes manifest
type Manifest map[string]interface{}

func (m Manifest) Kind() string {
return m["kind"].(string)
}

func (m Manifest) Name() string {
return m["metadata"].(map[string]interface{})["name"].(string)
}

func (m Manifest) Namespace() string {
return m["metadata"].(map[string]interface{})["namespace"].(string)
}

// Reconcile receives the raw evaluated jsonnet as a marshaled json dict and
// shall return it reconciled as a state object of the target system
func (k *Kubernetes) Reconcile(raw map[string]interface{}, objectspecs ...string) (state []Manifest, err error) {
Expand All @@ -67,6 +80,13 @@ func (k *Kubernetes) Reconcile(raw map[string]interface{}, objectspecs ...string
}).([]Manifest)
}

sort.SliceStable(out, func(i int, j int) bool {
if out[i].Kind() != out[j].Kind() {
return out[i].Kind() < out[j].Kind()
}
return out[i].Name() < out[j].Name()
})

return out, nil
}

Expand Down Expand Up @@ -114,7 +134,7 @@ func (k *Kubernetes) Diff(state []Manifest) (string, error) {

func objectspec(m Manifest) string {
return fmt.Sprintf("%s/%s",
m["kind"],
m["metadata"].(map[string]interface{})["name"],
m.Kind(),
m.Name(),
)
}

0 comments on commit ab9c43a

Please sign in to comment.