Skip to content

Commit

Permalink
feat(cli): limit output to specific objects only (grafana#30)
Browse files Browse the repository at this point in the history
Allows to limit the processed objects using an `objectspec`.

Format: --target (-t) <kind>/<name>
Example: tk show --target=deployment/nginx

Can be used multiple times to use multiple objects
  • Loading branch information
sh0rez authored Aug 8, 2019
1 parent 9e2d927 commit caf205a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
20 changes: 17 additions & 3 deletions cmd/tk/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,20 @@ import (
"github.com/grafana/tanka/pkg/cmp"
"github.com/posener/complete"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"golang.org/x/crypto/ssh/terminal"
)

type workflowFlagVars struct {
targets []string
}

func workflowFlags(fs *pflag.FlagSet) *workflowFlagVars {
v := workflowFlagVars{}
fs.StringSliceVarP(&v.targets, "target", "t", nil, "only use the specified objects (Format: <type>/<name>)")
return &v
}

func applyCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "apply [directory]",
Expand All @@ -21,13 +32,14 @@ func applyCmd() *cobra.Command {
"args": "baseDir",
},
}
vars := workflowFlags(cmd.Flags())
cmd.Run = func(cmd *cobra.Command, args []string) {
raw, err := evalDict(args[0])
if err != nil {
log.Fatalln("Evaluating jsonnet:", err)
}

desired, err := kube.Reconcile(raw)
desired, err := kube.Reconcile(raw, vars.targets...)
if err != nil {
log.Fatalln("Reconciling:", err)
}
Expand All @@ -52,6 +64,7 @@ func diffCmd() *cobra.Command {
"flags/diff-strategy": "diffStrategy",
},
}
vars := workflowFlags(cmd.Flags())
cmd.Run = func(cmd *cobra.Command, args []string) {
raw, err := evalDict(args[0])
if err != nil {
Expand All @@ -62,7 +75,7 @@ func diffCmd() *cobra.Command {
kube.Spec.DiffStrategy = cmd.Flag("diff-strategy").Value.String()
}

desired, err := kube.Reconcile(raw)
desired, err := kube.Reconcile(raw, vars.targets...)
if err != nil {
log.Fatalln("Reconciling:", err)
}
Expand Down Expand Up @@ -93,13 +106,14 @@ func showCmd() *cobra.Command {
"args": "baseDir",
},
}
vars := workflowFlags(cmd.Flags())
cmd.Run = func(cmd *cobra.Command, args []string) {
raw, err := evalDict(args[0])
if err != nil {
log.Fatalln("Evaluating jsonnet:", err)
}

state, err := kube.Reconcile(raw)
state, err := kube.Reconcile(raw, vars.targets...)
if err != nil {
log.Fatalln("Reconciling:", err)
}
Expand Down
25 changes: 24 additions & 1 deletion pkg/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package kubernetes

import (
"bytes"
"fmt"
"strings"

"github.com/Masterminds/semver"
"github.com/pkg/errors"
"github.com/stretchr/objx"
funk "github.com/thoas/go-funk"
yaml "gopkg.in/yaml.v2"

"github.com/grafana/tanka/pkg/config/v1alpha1"
Expand Down Expand Up @@ -40,7 +43,7 @@ type Manifest map[string]interface{}

// 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{}) (state []Manifest, err error) {
func (k *Kubernetes) Reconcile(raw map[string]interface{}, objectspecs ...string) (state []Manifest, err error) {
docs, err := walkJSON(raw, "")
out := make([]Manifest, 0, len(docs))
if err != nil {
Expand All @@ -51,6 +54,19 @@ func (k *Kubernetes) Reconcile(raw map[string]interface{}) (state []Manifest, er
m.Set("metadata.namespace", k.Spec.Namespace)
out = append(out, Manifest(m))
}

if len(objectspecs) > 0 {
out = funk.Filter(out, func(i interface{}) bool {
p := objectspec(i.(Manifest))
for _, o := range objectspecs {
if strings.EqualFold(p, o) {
return true
}
}
return false
}).([]Manifest)
}

return out, nil
}

Expand Down Expand Up @@ -95,3 +111,10 @@ func (k *Kubernetes) Diff(state []Manifest) (string, error) {

return k.differs[k.Spec.DiffStrategy](yaml)
}

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

0 comments on commit caf205a

Please sign in to comment.