Skip to content

Commit c440bf0

Browse files
committed
hotfix: also cleanup in user daemon if error occurs
1 parent 7757057 commit c440bf0

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

pkg/daemon/action/connect-fork.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ func (svr *Server) ConnectFork(req *rpc.ConnectRequest, resp rpc.Daemon_ConnectF
7979
}
8080

8181
config.Image = req.Image
82-
err = connect.DoConnect(sshCtx, true)
82+
err = connect.DoConnect(sshCtx, true, ctx.Done())
8383
if err != nil {
84-
log.Errorf("Do connect error: %v", err)
84+
log.Errorf("Failed to connect: %v", err)
8585
return err
8686
}
8787

@@ -122,6 +122,7 @@ func (svr *Server) redirectConnectForkToSudoDaemon(req *rpc.ConnectRequest, resp
122122
})
123123
defer func() {
124124
if err != nil {
125+
connect.Cleanup()
125126
sshCancel()
126127
}
127128
}()

pkg/daemon/action/connect.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ func (svr *Server) Connect(req *rpc.ConnectRequest, resp rpc.Daemon_ConnectServe
7979
})
8080
defer func() {
8181
if e != nil {
82+
svr.connect.Cleanup()
83+
svr.connect = nil
84+
svr.t = time.Time{}
8285
sshCancel()
8386
}
8487
}()
@@ -97,12 +100,9 @@ func (svr *Server) Connect(req *rpc.ConnectRequest, resp rpc.Daemon_ConnectServe
97100
}
98101

99102
config.Image = req.Image
100-
err = svr.connect.DoConnect(sshCtx, false)
103+
err = svr.connect.DoConnect(sshCtx, false, ctx.Done())
101104
if err != nil {
102105
log.Errorf("Failed to connect: %v", err)
103-
svr.connect.Cleanup()
104-
svr.connect = nil
105-
svr.t = time.Time{}
106106
return err
107107
}
108108
return nil
@@ -139,6 +139,7 @@ func (svr *Server) redirectToSudoDaemon(req *rpc.ConnectRequest, resp rpc.Daemon
139139
})
140140
defer func() {
141141
if e != nil {
142+
connect.Cleanup()
142143
sshCancel()
143144
}
144145
}()

pkg/handler/connect.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7+
"sync/atomic"
78

89
"net"
910
"net/url"
@@ -197,8 +198,16 @@ func (c *ConnectOptions) CreateRemoteInboundPod(ctx context.Context, workloads [
197198
return
198199
}
199200

200-
func (c *ConnectOptions) DoConnect(ctx context.Context, isLite bool) (err error) {
201+
func (c *ConnectOptions) DoConnect(ctx context.Context, isLite bool, stopChan <-chan struct{}) (err error) {
201202
c.ctx, c.cancel = context.WithCancel(ctx)
203+
var success atomic.Bool
204+
go func() {
205+
// if stop chan done before current function finished, means client ctrl+c to cancel operation
206+
<-stopChan
207+
if !success.Load() {
208+
c.cancel()
209+
}
210+
}()
202211

203212
log.Info("Starting connect")
204213
m := dhcp.NewDHCPManager(c.clientset.CoreV1().ConfigMaps(c.Namespace), c.Namespace)
@@ -268,6 +277,7 @@ func (c *ConnectOptions) DoConnect(ctx context.Context, isLite bool) (err error)
268277
log.Errorf("Configure DNS failed: %v", err)
269278
return
270279
}
280+
success.Store(true)
271281
log.Info("Configured DNS service")
272282
return
273283
}

pkg/util/route.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func ListService(ctx context.Context, lister v12.ServiceInterface, addRouteFunc
6363
}
6464
err = addRouteFunc(ips...)
6565
if err != nil {
66-
log.Errorf("Failed to add service IP: %s to route table: %v", ips, err)
66+
log.Errorf("Failed to add service IP to route table: %v", err)
6767
}
6868
if serviceList.Continue == "" {
6969
return nil
@@ -117,7 +117,7 @@ func ListPod(ctx context.Context, lister v12.PodInterface, addRouteFunc func(ipS
117117
}
118118
err = addRouteFunc(ips...)
119119
if err != nil {
120-
log.Errorf("Failed to add Pod IP: %v route table: %v", ips, err)
120+
log.Errorf("Failed to add Pod IP to route table: %v", err)
121121
}
122122
if podList.Continue == "" {
123123
return nil

0 commit comments

Comments
 (0)