Skip to content

Commit

Permalink
Add logic to cleanup unix socket in ipam-daemon
Browse files Browse the repository at this point in the history
Signed-off-by: Yury Kulazhenkov <ykulazhenkov@nvidia.com>
  • Loading branch information
ykulazhenkov committed Jul 10, 2023
1 parent 6a24e5c commit 2f81247
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions cmd/ipam-daemon/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ package app

import (
"context"
"errors"
"fmt"
"net"
"os"
"path/filepath"
"strings"
"sync"

"github.com/go-logr/logr"
Expand Down Expand Up @@ -217,6 +220,10 @@ func initGRPCServer(opts *options.Options,
if err != nil {
return nil, nil, err
}
if err := cleanUNIXSocketIfRequired(opts); err != nil {
log.Error(err, "failed to clean socket path")
return nil, nil, err
}
listener, err := net.Listen(network, address)
if err != nil {
log.Error(err, "failed to start listener for GRPC server")
Expand All @@ -232,6 +239,31 @@ func initGRPCServer(opts *options.Options,
return grpcServer, listener, nil
}

func cleanUNIXSocketIfRequired(opts *options.Options) error {
socketPrefix := "unix://"
if !strings.HasPrefix(opts.BindAddress, socketPrefix) {
return nil
}
socketPath, _ := strings.CutPrefix(opts.BindAddress, socketPrefix)
info, err := os.Stat(socketPath)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return nil
}
return err
}
if info.Mode().Type() != os.ModeSocket {
return fmt.Errorf("socket bind path exist, but not a socket")
}
if err := os.Remove(socketPath); err != nil {
if errors.Is(err, os.ErrNotExist) {
return nil
}
return fmt.Errorf("failed to remove socket: %v", err)
}
return nil
}

func deployShimCNI(log logr.Logger, opts *options.Options) error {
// copy nv-ipam binary
if !opts.CNISkipBinFileCopy {
Expand Down

0 comments on commit 2f81247

Please sign in to comment.