Skip to content

Commit

Permalink
feat(kubernetes): make --prune respect --target
Browse files Browse the repository at this point in the history
  • Loading branch information
sh0rez committed Jan 6, 2020
1 parent 068ede9 commit 2f61573
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 33 deletions.
20 changes: 7 additions & 13 deletions pkg/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package kubernetes

import (
"fmt"
"regexp"
"strings"

"github.com/Masterminds/semver"
Expand Down Expand Up @@ -72,7 +73,7 @@ func New(c v1alpha1.Config) (*Kubernetes, error) {

// ApplyOpts allow set additional parameters for the apply operation
type ApplyOpts struct {
PruneOpts
*PruneOpts

// force allows to ignore checks and force the operation
Force bool
Expand Down Expand Up @@ -114,15 +115,15 @@ func (k *Kubernetes) Apply(state manifest.List, opts ApplyOpts) error {
return nil
}

if err := k.prune(state, opts.PruneOpts); err != nil {
if err := k.prune(state, *opts.PruneOpts); err != nil {
return errors.Wrap(err, "removing orphaned resources")
}
return nil
}

// DiffOpts allow to specify additional parameters for diff operations
type DiffOpts struct {
PruneOpts
*PruneOpts

// Use `diffstat(1)` to create a histogram of the changes instead
Summarize bool
Expand All @@ -140,7 +141,7 @@ func (k *Kubernetes) Diff(state manifest.List, opts DiffOpts) (*string, error) {

differs := []Differ{k.differs[strategy]}
if opts.PruneOpts.Prune {
differs = append(differs, k.diffOrphaned(opts.PruneOpts.AllKinds))
differs = append(differs, k.diffOrphaned(opts.PruneOpts.AllKinds, opts.PruneOpts.Targets))
}

diff, err := multiDiff(state, differs)
Expand Down Expand Up @@ -198,9 +199,9 @@ func diffParallel(diff Differ, state manifest.List, results chan (*string), errs
results <- r
}

func (k *Kubernetes) diffOrphaned(all bool) Differ {
func (k *Kubernetes) diffOrphaned(all bool, targets []*regexp.Regexp) Differ {
return func(state manifest.List) (*string, error) {
orphan, err := k.listOrphaned(state, all)
orphan, err := k.listOrphaned(state, all, targets)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -231,10 +232,3 @@ func (k *Kubernetes) diffOrphaned(all bool) Differ {
func (k *Kubernetes) Info() client.Info {
return k.info
}

func objectspec(m manifest.Manifest) string {
return fmt.Sprintf("%s/%s",
m.Kind(),
m.Metadata().Name(),
)
}
11 changes: 9 additions & 2 deletions pkg/kubernetes/prune.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package kubernetes

import (
"regexp"

"github.com/grafana/tanka/pkg/kubernetes/client"
"github.com/grafana/tanka/pkg/kubernetes/manifest"
"github.com/grafana/tanka/pkg/kubernetes/util"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -48,10 +51,13 @@ type PruneOpts struct {

// Skip verification and force deleting
Force bool

// Limit the working set to these objects
Targets []*regexp.Regexp
}

func (k *Kubernetes) prune(state manifest.List, opts PruneOpts) error {
orphan, err := k.listOrphaned(state, opts.AllKinds)
orphan, err := k.listOrphaned(state, opts.AllKinds, opts.Targets)
if err != nil {
return errors.Wrap(err, "listing orphaned objects")
}
Expand All @@ -66,7 +72,7 @@ func (k *Kubernetes) prune(state manifest.List, opts PruneOpts) error {

// listOrphaned returns all resources known to the cluster not present in
// Jsonnet
func (k *Kubernetes) listOrphaned(state manifest.List, all bool) (orphaned manifest.List, err error) {
func (k *Kubernetes) listOrphaned(state manifest.List, all bool, targets []*regexp.Regexp) (orphaned manifest.List, err error) {
if k.orphaned != nil {
return k.orphaned, nil
}
Expand Down Expand Up @@ -116,6 +122,7 @@ func (k *Kubernetes) listOrphaned(state manifest.List, all bool) (orphaned manif
return nil, lastErr
}

orphaned = util.FilterTargets(orphaned, targets)
return orphaned, nil
}

Expand Down
15 changes: 2 additions & 13 deletions pkg/kubernetes/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (

"github.com/pkg/errors"
"github.com/stretchr/objx"
funk "github.com/thoas/go-funk"

"github.com/grafana/tanka/pkg/kubernetes/manifest"
"github.com/grafana/tanka/pkg/kubernetes/util"
"github.com/grafana/tanka/pkg/spec/v1alpha1"
)

Expand Down Expand Up @@ -48,18 +48,7 @@ func Reconcile(raw map[string]interface{}, config v1alpha1.Config, targets []*re
}

// optionally filter the working set of objects
if len(targets) > 0 {
tmp := funk.Filter(out, func(i interface{}) bool {
p := objectspec(i.(manifest.Manifest))
for _, t := range targets {
if t.MatchString(p) {
return true
}
}
return false
}).([]manifest.Manifest)
out = manifest.List(tmp)
}
out = util.FilterTargets(out, targets)

// Stable output order
sort.SliceStable(out, func(i int, j int) bool {
Expand Down
29 changes: 29 additions & 0 deletions pkg/kubernetes/util/targetexp.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"fmt"
"regexp"
"strings"

"github.com/grafana/tanka/pkg/kubernetes/manifest"
funk "github.com/thoas/go-funk"
)

type ErrBadTargetExp struct {
Expand Down Expand Up @@ -38,3 +41,29 @@ func MustCompileTargetExps(strs ...string) (exps []*regexp.Regexp) {
}
return exps
}

// FilterTargets filters list to only include those matched by targets
func FilterTargets(list manifest.List, targets []*regexp.Regexp) manifest.List {
if len(targets) == 0 {
return list
}

tmp := funk.Filter(list, func(i interface{}) bool {
p := objectspec(i.(manifest.Manifest))
for _, t := range targets {
if t.MatchString(p) {
return true
}
}
return false
}).([]manifest.Manifest)

return manifest.List(tmp)
}

func objectspec(m manifest.Manifest) string {
return fmt.Sprintf("%s/%s",
m.Kind(),
m.Metadata().Name(),
)
}
8 changes: 6 additions & 2 deletions pkg/tanka/tanka.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@ func parseModifiers(mods []Modifier) *options {
mod(o)
}

// finish prune object
o.prune.Targets = o.targets
o.prune.Force = o.apply.Force
o.apply.PruneOpts = o.prune
o.diff.PruneOpts = o.prune

// add finished object to apply and diff (they need this info as well)
o.apply.PruneOpts = &o.prune
o.diff.PruneOpts = &o.prune

return o
}
Expand Down
4 changes: 1 addition & 3 deletions pkg/tanka/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ func Apply(baseDir string, mods ...Modifier) error {
return err
}

diff, err := kube.Diff(p.Resources, kubernetes.DiffOpts{
PruneOpts: opts.prune,
})
diff, err := kube.Diff(p.Resources, kubernetes.DiffOpts{})
if err != nil {
return errors.Wrap(err, "diffing")
}
Expand Down

0 comments on commit 2f61573

Please sign in to comment.