Skip to content

Commit

Permalink
feat(kubernetes): Show support
Browse files Browse the repository at this point in the history
  • Loading branch information
sh0rez committed Jul 30, 2019
1 parent 51d448f commit 7c4bee8
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 28 deletions.
8 changes: 4 additions & 4 deletions cmd/tk/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,26 +86,26 @@ func main() {
os.Exit(1)
}

log.Fatalln(err)
log.Fatalln("Reading config:", err)
}
checkDeprecated()

if err := viper.Unmarshal(&config); err != nil {
log.Fatalln(err)
log.Fatalln("Parsing config:", err)
}

// Provider
var err error
prov, provName, err = setupProvider()
if err != nil {
log.Fatalln(err)
log.Fatalln("Setting up provider:", err)
}

rootCmd.AddCommand(providerCmd())

// Run!
if err := rootCmd.Execute(); err != nil {
log.Fatalln(rootCmd.Execute())
log.Fatalln("Ouch:", rootCmd.Execute())
}
}

Expand Down
18 changes: 9 additions & 9 deletions cmd/tk/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,26 +67,26 @@ func showCmd() *cobra.Command {
Use: "show",
Short: "[Requires Provider] print the jsonnet in the target state format",
}
cmd.RunE = func(cmd *cobra.Command, args []string) error {
cmd.Run = func(cmd *cobra.Command, args []string) {
var rawDict map[string]interface{}
raw, err := eval()
if err != nil {
return err
log.Fatalln("evaluating jsonnet:", err)
}
if err := json.Unmarshal([]byte(raw), &rawDict); err != nil {
return err
log.Fatalln("parsing jsonnet response", err)
}

state, err := prov.Show(rawDict)
state, err := prov.Format(rawDict)
if err != nil {
return err
log.Fatalln("invoking provider:", err)
}
out, err := json.Marshal(state)

pretty, err := prov.Show(state)
if err != nil {
return err
log.Fatalln("pretty printing state:", err)
}
fmt.Println(out)
return nil
fmt.Println(pretty)
}
return cmd
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@ require (
github.com/mitchellh/mapstructure v1.1.2
github.com/spf13/cobra v0.0.5
github.com/spf13/viper v1.4.0
github.com/stretchr/objx v0.2.0
golang.org/x/sys v0.0.0-20190616124812-15dcb6c0061f // indirect
golang.org/x/text v0.3.2 // indirect
gopkg.in/yaml.v2 v2.2.2
)
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
Expand Down Expand Up @@ -30,6 +31,7 @@ github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v1.2.1 h1:/s5zKNz0uPFCZ5hddgPdo2TK2TVrUNMn0OOX8/aZMTE=
github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
Expand All @@ -54,8 +56,10 @@ github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvW
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/magiconair/properties v1.8.0 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDePerRcY=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
Expand Down Expand Up @@ -105,6 +109,8 @@ github.com/spf13/viper v1.4.0 h1:yXHLWeravcrgGyFSyCgdYpXQ9dR9c/WED3pg1RhxqEU=
github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/yZzE=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.2.0 h1:Hbg2NidpLE8veEBkEZTL3CvlkUIVzuU9jDplZO54c48=
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
Expand Down Expand Up @@ -139,10 +145,15 @@ golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223 h1:DH4skfRX4EBpamg7iV4ZlCpblAHI6s6TDM39bFZumv8=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190616124812-15dcb6c0061f h1:25KHgbfyiSm6vwQLbM3zZIe1v9p/3ea4Rz+nnM5K/i4=
golang.org/x/sys v0.0.0-20190616124812-15dcb6c0061f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
Expand All @@ -152,6 +163,7 @@ google.golang.org/grpc v1.21.0/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ij
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=
gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74=
Expand Down
49 changes: 40 additions & 9 deletions pkg/provider/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,60 @@ package kubernetes
import (
"fmt"

"github.com/sh0rez/tanka/pkg/provider/util"
"github.com/spf13/cobra"
"github.com/stretchr/objx"
)

// Kubernetes provider bridges tanka to the Kubernetse orchestrator.
type Kubernetes struct {
apiServer string
namespace string
APIServer string
Namespace string
}

// Show receives the raw evaluated jsonnet as a marshaled json dict and
// Format 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) Show(raw map[string]interface{}) (state map[string]interface{}, err error) {
panic("not implemented")
func (k *Kubernetes) Format(raw map[string]interface{}) (state interface{}, err error) {
docs := flattenManifest(raw)
for _, d := range docs {
m := objx.New(d)
m.Set("metadata.namespace", k.Namespace)
}
return docs, nil
}

// flattenManifest traverses deeply nested kubernetes manifest and extracts them into a flat map.
func flattenManifest(deep map[string]interface{}) []map[string]interface{} {
flat := []map[string]interface{}{}

for n, d := range deep {
if n == "__ksonnet" {
continue
}
m := objx.New(d)
if m.Has("apiVersion") && m.Has("kind") {
flat = append(flat, m)
} else {
flat = append(flat, flattenManifest(m)...)
}
}
return flat
}

// Show receives the state and reformats it to YAML Documents
func (k *Kubernetes) Show(state interface{}) (string, error) {
return util.ShowYAMLDocs(state.([]map[string]interface{}))
}

// Apply receives a state object generated using `Show()` and may apply it to the target system
func (k *Kubernetes) Apply(desired map[string]interface{}) error {
// Apply receives a state object generated using `Format()` and may apply it to the target system
func (k *Kubernetes) Apply(desired interface{}) error {
panic("not implemented")
}

// State shall return the current state of the target system.
// It receives the desired state object generated using `Show()`.
// It receives the desired state object generated using `Format()`.
// This is used for diffing afterwards.
func (k *Kubernetes) State(desired map[string]interface{}) (real map[string]interface{}, err error) {
func (k *Kubernetes) State(desired interface{}) (real map[string]interface{}, err error) {
panic("not implemented")
}

Expand Down
16 changes: 10 additions & 6 deletions pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@ import "github.com/spf13/cobra"

// Provider describes methods for functionality more advanced than evaluating jsonnet
type Provider interface {
// Show receives the raw evaluated jsonnet as a marshaled json dict and
// Format receives the raw evaluated jsonnet as a marshaled json dict and
// shall return it reconciled as a state object of the target system
Show(raw map[string]interface{}) (state map[string]interface{}, err error)
Format(raw map[string]interface{}) (state interface{}, err error)

// Apply receives a state object generated using `Show()` and may apply it to the target system
Apply(desired map[string]interface{}) error
// Show receives the state object generated using `Format()`
// and may pretty-print it into the string.
Show(state interface{}) (string, error)

// Apply receives a state object generated using `Format()` and may apply it to the target system
Apply(desired interface{}) error

// State shall return the current state of the target system.
// It receives the desired state object generated using `Show()`.
// It receives the desired state object generated using `Format()`.
// This is used for diffing afterwards.
State(desired map[string]interface{}) (real map[string]interface{}, err error)
State(desired interface{}) (real map[string]interface{}, err error)

// Cmd shall return a command to be available under `tk provider <NAME>`
Cmd() *cobra.Command
Expand Down
34 changes: 34 additions & 0 deletions pkg/provider/util/print.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package util

import (
"bytes"
"encoding/json"

yaml "gopkg.in/yaml.v2"
)

// ShowJSON marshals the state into plain JSON
func ShowJSON(state interface{}) (string, error) {
out, err := json.MarshalIndent(state, "", " ")
return string(out), err
}

// ShowYAML marshals the state into a single YAML document.
func ShowYAML(state interface{}) (string, error) {
out, err := yaml.Marshal(state)
return string(out), err
}

// ShowYAMLDocs the state into multiple yaml documents
func ShowYAMLDocs(state []map[string]interface{}) (string, error) {
buf := bytes.Buffer{}
enc := yaml.NewEncoder(&buf)

for _, d := range state {
if err := enc.Encode(d); err != nil {
return "", err
}
}

return buf.String(), nil
}

0 comments on commit 7c4bee8

Please sign in to comment.