Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deletions (third attempt) #123

Closed
wants to merge 6 commits into from
Closed

Deletions (third attempt) #123

wants to merge 6 commits into from

Conversation

malcolmholmes
Copy link
Collaborator

@sh0rez pointed out that kubectl supports --prune which does the heavy lifting for us.

This PR replaces #117 (which was itself a second try!) and implements #88, using --prune. Because of that, it has called the feature "prune" to follow kubectl.

This fix is much more in line with what Tanka has been so far - just passing data around rather than doing heavy lifting.

cmd/tk/workflow.go Outdated Show resolved Hide resolved
@@ -10,17 +11,17 @@ import (
)

// Apply applies the given yaml to the cluster
func (k Kubectl) Apply(data manifest.List, opts ApplyOpts) error {
func (k Kubectl) Apply(labels []string, data manifest.List, opts ApplyOpts) error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The labels are not required if not running with prune, right? If so, then please move them into ApplyOpts

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not quite. The labels are needed in Apply in order to label them so that prune can work. So they are only not needed if we have injectLabels set to false.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the k *Kubernetes contained the environment name (or baseDir), this would reduce the need to pass it around and remove this extra parameter.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, add the whole Environment type in there, not just the spec :D

@@ -70,16 +72,22 @@ func New(s v1alpha1.Spec) (*Kubernetes, error) {
type ApplyOpts client.ApplyOpts

// Apply receives a state object generated using `Reconcile()` and may apply it to the target system
func (k *Kubernetes) Apply(state manifest.List, opts ApplyOpts) error {
func (k *Kubernetes) Apply(baseDir string, state manifest.List, opts ApplyOpts) error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pkg/kubernetes should not be concerned with directories and such ... that's the concern of pkg/tanka.

For calculating the environment label ... the environments name is available in k *Kubernetes.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll check that. The label needs to be k8s compatible, so must exclude slashes, hence this label has slashes replaced with dots.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see the environment preset in k *Kubernetes, but... we could put it there.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, it only includes the spec. I’m fine with adding the whole env in there

Comment on lines 84 to 87
if opts.Prune {
message = `Applying to and pruning namespace '%s' of cluster '%s' at '%s' using context '%s'.`
} else {
message = `Applying to namespace '%s' of cluster '%s' at '%s' using context '%s'.`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No if else dance please, just default to the one without prune and reassign in case

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

huh? Explain? This may be a Golang thing I've not heard.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

message := `Applying to namespace '%s' of cluster '%s' at '%s' using context '%s'.`
if opts.Prune {
  message = `Applying to and pruning namespace '%s' of cluster '%s' at '%s' using context '%s'.`
}

@@ -90,7 +98,10 @@ func (k *Kubernetes) Apply(state manifest.List, opts ApplyOpts) error {
return err
}
}
return k.ctl.Apply(state, client.ApplyOpts(opts))

environmentLabel := TankaEnvironmentLabel + "=" + getEnvironmentLabel(baseDir)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use fmt.Sprintf please

@@ -133,6 +133,11 @@ func (m Metadata) Labels() map[string]interface{} {
return m["labels"].(map[string]interface{})
}

// SetLabels sets the labels of a manifest
func (m Metadata) SetLabels(labels map[string]interface{}) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for that, a map[string]interface{} is a pointer type so you can just assign values on the return value of Labels()

@@ -14,23 +14,33 @@ import (
"github.com/grafana/tanka/pkg/spec/v1alpha1"
)

// TankaEnvironmentLabel is the label applied to all resources. Used to identify resources that are
// eligible for deletion, if not present in the generated set of manifests.
const TankaEnvironmentLabel = "tanka/environment"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use the prefix tanka.dev please

@@ -85,3 +85,10 @@ func WithApplyAutoApprove(b bool) Modifier {
opts.apply.AutoApprove = b
}
}

// WithPrune invokes `kubectl apply` with the `--prune` flag
func WithPrune(b bool) Modifier {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WithApplyPrune

@sh0rez sh0rez mentioned this pull request Dec 11, 2019
@stale
Copy link

stale bot commented Jan 4, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Jan 4, 2020
@stale stale bot closed this Jan 11, 2020
@sh0rez sh0rez deleted the deletions_mk3 branch March 11, 2020 19:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants