@@ -24,6 +24,7 @@ import (
24
24
corev1 "k8s.io/api/core/v1"
25
25
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26
26
"k8s.io/apimachinery/pkg/labels"
27
+ "k8s.io/apimachinery/pkg/types"
27
28
"k8s.io/apimachinery/pkg/watch"
28
29
"sigs.k8s.io/controller-runtime/pkg/client"
29
30
@@ -978,45 +979,56 @@ func (m *Monitor) finalizeWorkspaceContent(ctx context.Context, wso *workspaceOb
978
979
979
980
// deleteDanglingServices removes services for which there is no corresponding workspace pod anymore
980
981
func (m * Monitor ) deleteDanglingServices (ctx context.Context ) error {
981
- var endpoints corev1.EndpointsList
982
- err := m .manager .Clientset .List (ctx , & endpoints , workspaceObjectListOptions (m .manager .Config .Namespace ))
982
+ var services corev1.ServiceList
983
+ err := m .manager .Clientset .List (ctx , & services , workspaceObjectListOptions (m .manager .Config .Namespace ))
983
984
if err != nil {
984
985
return xerrors .Errorf ("deleteDanglingServices: %w" , err )
985
986
}
986
987
988
+ var zero int64 = 0
987
989
propagationPolicy := metav1 .DeletePropagationForeground
988
990
989
- for _ , e := range endpoints .Items {
991
+ for _ , service := range services .Items {
992
+ var endpoints corev1.Endpoints
993
+ err := m .manager .Clientset .Get (ctx , types.NamespacedName {Namespace : service .Namespace , Name : service .Name }, & endpoints )
994
+ if err != nil {
995
+ return xerrors .Errorf ("deleteDanglingServices: %w" , err )
996
+ }
997
+
990
998
hasReadyEndpoint := false
991
- for _ , s := range e .Subsets {
999
+ for _ , s := range endpoints .Subsets {
992
1000
hasReadyEndpoint = len (s .Addresses ) > 0
993
1001
}
994
1002
if hasReadyEndpoint {
995
1003
continue
996
1004
}
997
1005
998
- workspaceID , ok := e .Labels [wsk8s .WorkspaceIDLabel ]
1006
+ workspaceID , ok := endpoints .Labels [wsk8s .WorkspaceIDLabel ]
999
1007
if ! ok {
1000
- m .OnError (fmt .Errorf ("service endpoint %s does not have %s label" , e .Name , wsk8s .WorkspaceIDLabel ))
1008
+ m .OnError (fmt .Errorf ("service endpoint %s does not have %s label" , service .Name , wsk8s .WorkspaceIDLabel ))
1001
1009
continue
1002
1010
}
1003
- _ , err := m .manager .findWorkspacePod (ctx , workspaceID )
1011
+
1012
+ _ , err = m .manager .findWorkspacePod (ctx , workspaceID )
1004
1013
if ! isKubernetesObjNotFoundError (err ) {
1005
1014
continue
1006
1015
}
1007
1016
1008
1017
if m .manager .Config .DryRun {
1009
- log .WithFields (log .OWI ("" , "" , workspaceID )).WithField ("name" , e .Name ).Info ("should have deleted dangling service but this is a dry run" )
1018
+ log .WithFields (log .OWI ("" , "" , workspaceID )).WithField ("name" , service .Name ).Info ("should have deleted dangling service but this is a dry run" )
1010
1019
continue
1011
1020
}
1012
1021
1013
1022
// this relies on the Kubernetes convention that endpoints have the same name as their services
1014
- err = m .manager .Clientset .Delete (ctx , & e , & client.DeleteOptions {PropagationPolicy : & propagationPolicy })
1023
+ err = m .manager .Clientset .Delete (ctx , & service , & client.DeleteOptions {
1024
+ GracePeriodSeconds : & zero ,
1025
+ PropagationPolicy : & propagationPolicy ,
1026
+ })
1015
1027
if err != nil && ! isKubernetesObjNotFoundError (err ) {
1016
1028
m .OnError (xerrors .Errorf ("deleteDanglingServices: %w" , err ))
1017
1029
continue
1018
1030
}
1019
- log .WithFields (log .OWI ("" , "" , workspaceID )).WithField ("name" , e .Name ).Info ("deleted dangling service" )
1031
+ log .WithFields (log .OWI ("" , "" , workspaceID )).WithField ("name" , service .Name ).Info ("deleted dangling service" )
1020
1032
}
1021
1033
1022
1034
return nil
0 commit comments