-
Notifications
You must be signed in to change notification settings - Fork 752
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
Conversation
9bf74fd
to
9233c70
Compare
Hi @procrypt Just two things before reviewing!
|
6bf4a0f
to
0aafb02
Compare
Hey @cdrage I have split the commit into two different commit one for the code and other for vender. |
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 |
4fbb0d1
to
2fa5292
Compare
@cdrage Done :D |
I wasn't excepting the labels to appear when using Great code however, I don't see any problems with it and I've tested the PR 👍 |
I think we should get in the habit of commenting on our code, mind adding a few lines @procrypt to your implementation? |
118f8cd
to
3082ddd
Compare
@@ -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})) |
There was a problem hiding this comment.
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
d9ab7c9
to
2683c84
Compare
@kadel Updated PR. |
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 ;-) |
aa95ee2
to
e9cdb8b
Compare
5173b97
to
0e6df93
Compare
@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})) |
There was a problem hiding this comment.
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 😉
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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})) |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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" |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
c767d3b
to
a92dee9
Compare
if nothing is deployed and call
|
@kadel Ah this is bad, I guess I know what the problem is, when we call 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 Deployment using deployment.Items[0].Labels
Service using svc.Items[0].Labels
PersistentVolumeClaim using pvc.Items[0].Labels So this works when we do DeploymentList
ServiceList
PersistentVolumeClaimList and since there is nothing like deployment.Items[0].Labels
svc.Items[0].Labels
pvc.Items[0].Labels It panics with |
@kadel Fixed $ kompose down
FATA[0000] Error while deleting application: service `redis` not found |
0985d02
to
f51e41f
Compare
There was a problem hiding this 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} |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
@kadel Updated and removed the check 😉 |
@kadel @containscafeine @surajssd @cdrage
This PR uses the
SelectorFromSet()
function which takes alabel or selector
as an argument returns all the object that uses thatlabel or selector
. Once we get the object that uses a particular label we can further do the delete operation on them. This is similar tokubectl delete <object> --selector=<key>=<value>
. Also the label has been modified fromservice
toio.kompose.service
.Thanks @kadel for all the help :)
Review please.
Fixes #255