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

delete objects based on label #483

Merged
merged 2 commits into from
Mar 24, 2017
Merged

delete objects based on label #483

merged 2 commits into from
Mar 24, 2017

Conversation

procrypt
Copy link
Contributor

@procrypt procrypt commented Mar 10, 2017

@kadel @containscafeine @surajssd @cdrage

This PR uses the SelectorFromSet() function which takes alabel or selector as an argument returns all the object that uses that label or selector. Once we get the object that uses a particular label we can further do the delete operation on them. This is similar to kubectl delete <object> --selector=<key>=<value>. Also the label has been modified from service to io.kompose.service.
Thanks @kadel for all the help :)
Review please.
Fixes #255

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Mar 10, 2017
@procrypt procrypt force-pushed the empty_vols branch 3 times, most recently from 9bf74fd to 9233c70 Compare March 10, 2017 10:24
@cdrage
Copy link
Member

cdrage commented Mar 10, 2017

Hi @procrypt

Just two things before reviewing!

  1. Please split the commits, one for updating vendoring, and the other the actual code you're modifying. Otherwise, it's really hard to review on GitHub / gif dif

  2. Please update your Git commit messages outlining what's changed / more descriptive rather than Fixes #255. You can do this with git commit --amend

@procrypt procrypt force-pushed the empty_vols branch 2 times, most recently from 6bf4a0f to 0aafb02 Compare March 10, 2017 16:01
@procrypt
Copy link
Contributor Author

Hey @cdrage I have split the commit into two different commit one for the code and other for vender.
Also I have elaborated what this PR is doing.
Ready for review :)

@cdrage
Copy link
Member

cdrage commented Mar 10, 2017

@procrypt

Commit titles shouldn't have underscores in them, and it looks like you haven't put your updated message into your commit, you need to git commit --amend, add then git push -f it. And it's vendor :)

@procrypt procrypt changed the title [WIP]delete_objects_based_on_label [WIP]delete objects based on label Mar 10, 2017
@procrypt procrypt force-pushed the empty_vols branch 3 times, most recently from 4fbb0d1 to 2fa5292 Compare March 10, 2017 16:44
@procrypt
Copy link
Contributor Author

@cdrage Done :D

@cdrage
Copy link
Member

cdrage commented Mar 13, 2017

I wasn't excepting the labels to appear when using kompose convert, I've typed up my concerns in the issue #255 !

Great code however, I don't see any problems with it and I've tested the PR 👍

@cdrage
Copy link
Member

cdrage commented Mar 13, 2017

I think we should get in the habit of commenting on our code, mind adding a few lines @procrypt to your implementation?

@procrypt procrypt force-pushed the empty_vols branch 2 times, most recently from 118f8cd to 3082ddd Compare March 13, 2017 20:30
@procrypt procrypt changed the title [WIP]delete objects based on label delete objects based on label Mar 13, 2017
@@ -633,37 +635,67 @@ func (k *Kubernetes) Undeploy(komposeObject kobject.KomposeObject, opt kobject.C
switch t := v.(type) {
case *extensions.Deployment:
//delete deployment
rpDeployment, err := kubectl.ReaperFor(extensions.Kind("Deployment"), client)
label := labels.SelectorFromSet(labels.Set(map[string]string{"io.kompose.service": t.Name}))
Copy link
Member

Choose a reason for hiding this comment

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

io.kompose.service is used in a lot of places. maybe we could use transformer.ConfigLabels to handle all the labels.
Or other option would be to create new function or constant that will hold that string

@procrypt procrypt force-pushed the empty_vols branch 2 times, most recently from d9ab7c9 to 2683c84 Compare March 14, 2017 17:54
@procrypt
Copy link
Contributor Author

@kadel Updated PR.

@kadel
Copy link
Member

kadel commented Mar 16, 2017

this will require new vendor update :-(

I think that easiest thing to do, will be remove 'update vendor' commit from your branch, do rebase, and than update vendor again ;-)

@procrypt procrypt force-pushed the empty_vols branch 2 times, most recently from aa95ee2 to e9cdb8b Compare March 16, 2017 18:07
@procrypt procrypt force-pushed the empty_vols branch 2 times, most recently from 5173b97 to 0e6df93 Compare March 16, 2017 20:34
@procrypt
Copy link
Contributor Author

@kadel Done all green now :D

@@ -705,37 +708,67 @@ func (k *Kubernetes) Undeploy(komposeObject kobject.KomposeObject, opt kobject.C
switch t := v.(type) {
case *extensions.Deployment:
//delete deployment
rpDeployment, err := kubectl.ReaperFor(extensions.Kind("Deployment"), client)
label := labels.SelectorFromSet(labels.Set(map[string]string{selector: t.Name}))
Copy link
Member

Choose a reason for hiding this comment

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

This and next line is same for every object. You can safely move them outside of this switch and for, than you won't have to repeat same code multiple times 😉

Copy link
Contributor Author

@procrypt procrypt Mar 20, 2017

Choose a reason for hiding this comment

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

@kadel If we put

label := labels.SelectorFromSet(labels.Set(map[string]string{selector: t.Name}))

outside switch and for how do we access the name of the object we want to delete created, here we are accessing it using t.Name.

Copy link
Member

@kadel kadel Mar 20, 2017

Choose a reason for hiding this comment

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

Than you can do it like this:
Keep it in for, but move it outside switch.

You can access object metadata fields by casting it to meta.Object.

// import "k8s.io/kubernetes/pkg/api/meta"
v.(meta.Object).GetName()

@@ -509,11 +514,20 @@ func (o *OpenShift) Undeploy(komposeObject kobject.KomposeObject, opt kobject.Co
switch t := v.(type) {
case *imageapi.ImageStream:
//delete imageStream
err = oclient.ImageStreams(namespace).Delete(t.Name)
label := labels.SelectorFromSet(labels.Set(map[string]string{selector: t.Name}))
Copy link
Member

Choose a reason for hiding this comment

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

same think as in kubernetes, you are repeating same lines

return err
}
log.Infof("Successfully deleted PersistentVolumeClaim: %s", t.Name)
}

case *extensions.Ingress:
Copy link
Member

@kadel kadel Mar 17, 2017

Choose a reason for hiding this comment

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

I think we should do same thing for Ingress and Pods also.

return err
}
log.Infof("Successfully deleted PersistentVolumeClaim: %s", t.Name)
}

case *routeapi.Route:
Copy link
Member

Choose a reason for hiding this comment

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

Don't forget about Pods and Routes ;-)

@@ -695,6 +697,7 @@ func (k *Kubernetes) Undeploy(komposeObject kobject.KomposeObject, opt kobject.C
if err != nil {
return errors.Wrap(err, "k.Transform failed")
}
selector := "io.kompose.service"
Copy link
Member

Choose a reason for hiding this comment

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

not to nitpick, but should this instead be a constant and at the top of the file? (outside the scope) ping @kadel

Copy link
Member

Choose a reason for hiding this comment

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

@cdrage you are right. I was thinking about it, but forgot to commend on it :-)

This should be even somewhere else (maybe const in transformer pkg?), so ConfigLabels function can use it, and we don't have same magic string in two places.

@procrypt procrypt force-pushed the empty_vols branch 2 times, most recently from c767d3b to a92dee9 Compare March 20, 2017 04:36
@kadel
Copy link
Member

kadel commented Mar 20, 2017

if nothing is deployed and call kompose down is called:

./kompose down
panic: runtime error: index out of range

goroutine 1 [running]:
github.com/kubernetes-incubator/kompose/pkg/transformer/kubernetes.(*Kubernetes).Undeploy(0xc4202cc180, 0xc42033e8a0, 0x1c625c5, 0x7, 0x100, 0x0, 0x0, 0x0, 0x0, 0x0, ...)
	/home/tomas/dev/go/src/github.com/kubernetes-incubator/kompose/pkg/transformer/kubernetes/kubernetes.go:740 +0x14d6
github.com/kubernetes-incubator/kompose/pkg/app.Down(0x100, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0xc42025fe50, 0x1, 0x1, ...)
	/home/tomas/dev/go/src/github.com/kubernetes-incubator/kompose/pkg/app/app.go:288 +0x2be
github.com/kubernetes-incubator/kompose/cmd.glob..func5(0x2a3b200, 0x2a69368, 0x0, 0x0)
	/home/tomas/dev/go/src/github.com/kubernetes-incubator/kompose/cmd/down.go:52 +0x4f
github.com/kubernetes-incubator/kompose/vendor/github.com/spf13/cobra.(*Command).execute(0x2a3b200, 0x2a69368, 0x0, 0x0, 0x2a3b200, 0x2a69368)
	/home/tomas/dev/go/src/github.com/kubernetes-incubator/kompose/vendor/github.com/spf13/cobra/command.go:603 +0x22b
github.com/kubernetes-incubator/kompose/vendor/github.com/spf13/cobra.(*Command).ExecuteC(0x2a3b420, 0xc420070058, 0x0, 0xc4204b9f48)
	/home/tomas/dev/go/src/github.com/kubernetes-incubator/kompose/vendor/github.com/spf13/cobra/command.go:689 +0x339
github.com/kubernetes-incubator/kompose/vendor/github.com/spf13/cobra.(*Command).Execute(0x2a3b420, 0x1, 0x1)
	/home/tomas/dev/go/src/github.com/kubernetes-incubator/kompose/vendor/github.com/spf13/cobra/command.go:648 +0x2b
github.com/kubernetes-incubator/kompose/cmd.Execute()
	/home/tomas/dev/go/src/github.com/kubernetes-incubator/kompose/cmd/root.go:92 +0x31
main.main()
	/home/tomas/dev/go/src/github.com/kubernetes-incubator/kompose/main.go:22 +0x20

@procrypt
Copy link
Contributor Author

procrypt commented Mar 20, 2017

@kadel Ah this is bad, I guess I know what the problem is, when we call kompose down the

client.Deployments(namespace).List()            returns  *extensions.DeploymentList
client.Services(namespace).List()               returns  *api.ServiceList
client.PersistentVolumeClaims(namespace).List() returns  *api.PersistentVolumeClaimList

and the struct

DeploymentList            contains  Items []Deployment
ServiceList               contains  Items []Service
PersistentVolumeClaimList contains  Items []PersistentVolumeClaim

and to get the Label we are accessing the first index of the each of slice of

Deployment              using  deployment.Items[0].Labels
Service                 using  svc.Items[0].Labels
PersistentVolumeClaim   using  pvc.Items[0].Labels

So this works when we do kompose up and then do kompose down because the application has been already deployed and kompose down get what needed to find the Label, but if we do kompose down and there is no application deployed the there is are objects, kompose tries to access the first element of the

DeploymentList 
ServiceList
PersistentVolumeClaimList

and since there is nothing like

deployment.Items[0].Labels
svc.Items[0].Labels
pvc.Items[0].Labels

It panics with runtime error: index out of range
Working to fix this.

@procrypt
Copy link
Contributor Author

procrypt commented Mar 20, 2017

@kadel Fixed kompose down

$ kompose down
FATA[0000] Error while deleting application: service `redis` not found

@procrypt procrypt force-pushed the empty_vols branch 4 times, most recently from 0985d02 to f51e41f Compare March 21, 2017 10:55
@procrypt
Copy link
Contributor Author

@kadel @cdrage Updated the PR Please Review

Copy link
Member

@kadel kadel left a comment

Choose a reason for hiding this comment

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

two last small changes, that it will be perfect ;-)

err = rpDeployment.Stop(namespace, t.Name, TIMEOUT*time.Second, nil)
if err != nil {
return err
komposeLabel := map[string]string{transformer.Selector: t.Name}
Copy link
Member

Choose a reason for hiding this comment

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

You can move this outside of this switch. That you wont have to repeat same line in every case.

if err != nil {
return err
komposeLabel := map[string]string{transformer.Selector: t.Name}
if len(deployment.Items) == 0 {
Copy link
Member

Choose a reason for hiding this comment

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

You can completely remove this this now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@kadel If we remove this line, there will be no output of kompose down is there is nothing deployed and you mentioned earlier that we want at least want to give an error in this situation.

Copy link
Member

Choose a reason for hiding this comment

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

That is OK. It is not an error if nothing is found.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But we should warn the user about it.

Copy link
Member

Choose a reason for hiding this comment

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

If there are no objects, that its fine. The were not created or already have been deleted. No need to inform user about that. Nothing happened.

This PR uses the "SelectorFromSet()" function which takes alabel or
selector as an argument returns all the object that uses that label or
selector. Once we get the object that uses a particular label we can
further do the delete operation on them. This is similar to "kubectl
delete <object> --selector=<key>=<value>". Also the label has been
modified from service to io.kompose.service.
@procrypt
Copy link
Contributor Author

@kadel Updated and removed the check 😉

@kadel kadel merged commit 3c3ae98 into kubernetes:master Mar 24, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants