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

hotfix: also cleanup in user daemon if error occurs #471

Merged
merged 1 commit into from
Mar 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions pkg/daemon/action/connect-fork.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ func (svr *Server) ConnectFork(req *rpc.ConnectRequest, resp rpc.Daemon_ConnectF
}

config.Image = req.Image
err = connect.DoConnect(sshCtx, true)
err = connect.DoConnect(sshCtx, true, ctx.Done())
if err != nil {
log.Errorf("Do connect error: %v", err)
log.Errorf("Failed to connect: %v", err)
return err
}

Expand Down Expand Up @@ -122,6 +122,7 @@ func (svr *Server) redirectConnectForkToSudoDaemon(req *rpc.ConnectRequest, resp
})
defer func() {
if err != nil {
connect.Cleanup()
sshCancel()
}
}()
Expand Down
9 changes: 5 additions & 4 deletions pkg/daemon/action/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ func (svr *Server) Connect(req *rpc.ConnectRequest, resp rpc.Daemon_ConnectServe
})
defer func() {
if e != nil {
svr.connect.Cleanup()
svr.connect = nil
svr.t = time.Time{}
sshCancel()
}
}()
Expand All @@ -97,12 +100,9 @@ func (svr *Server) Connect(req *rpc.ConnectRequest, resp rpc.Daemon_ConnectServe
}

config.Image = req.Image
err = svr.connect.DoConnect(sshCtx, false)
err = svr.connect.DoConnect(sshCtx, false, ctx.Done())
if err != nil {
log.Errorf("Failed to connect: %v", err)
svr.connect.Cleanup()
svr.connect = nil
svr.t = time.Time{}
return err
}
return nil
Expand Down Expand Up @@ -139,6 +139,7 @@ func (svr *Server) redirectToSudoDaemon(req *rpc.ConnectRequest, resp rpc.Daemon
})
defer func() {
if e != nil {
connect.Cleanup()
sshCancel()
}
}()
Expand Down
12 changes: 11 additions & 1 deletion pkg/handler/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"sync/atomic"

"net"
"net/url"
Expand Down Expand Up @@ -197,8 +198,16 @@ func (c *ConnectOptions) CreateRemoteInboundPod(ctx context.Context, workloads [
return
}

func (c *ConnectOptions) DoConnect(ctx context.Context, isLite bool) (err error) {
func (c *ConnectOptions) DoConnect(ctx context.Context, isLite bool, stopChan <-chan struct{}) (err error) {
c.ctx, c.cancel = context.WithCancel(ctx)
var success atomic.Bool
go func() {
// if stop chan done before current function finished, means client ctrl+c to cancel operation
<-stopChan
if !success.Load() {
c.cancel()
}
}()

log.Info("Starting connect")
m := dhcp.NewDHCPManager(c.clientset.CoreV1().ConfigMaps(c.Namespace), c.Namespace)
Expand Down Expand Up @@ -268,6 +277,7 @@ func (c *ConnectOptions) DoConnect(ctx context.Context, isLite bool) (err error)
log.Errorf("Configure DNS failed: %v", err)
return
}
success.Store(true)
log.Info("Configured DNS service")
return
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/util/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func ListService(ctx context.Context, lister v12.ServiceInterface, addRouteFunc
}
err = addRouteFunc(ips...)
if err != nil {
log.Errorf("Failed to add service IP: %s to route table: %v", ips, err)
log.Errorf("Failed to add service IP to route table: %v", err)
}
if serviceList.Continue == "" {
return nil
Expand Down Expand Up @@ -117,7 +117,7 @@ func ListPod(ctx context.Context, lister v12.PodInterface, addRouteFunc func(ipS
}
err = addRouteFunc(ips...)
if err != nil {
log.Errorf("Failed to add Pod IP: %v route table: %v", ips, err)
log.Errorf("Failed to add Pod IP to route table: %v", err)
}
if podList.Continue == "" {
return nil
Expand Down
Loading