package cni import ( "fmt" "net" "time" "github.com/containernetworking/cni/pkg/skel" "github.com/containernetworking/cni/pkg/types" ) // dialTimeout bounds how long the plugin waits to connect to the agent // socket. kubelet has its own outer timeout for the whole CNI invocation, // but a tighter bound here gives a clearer error if the DaemonSet pod is // gone or starting up. const dialTimeout = 5 * time.Second // rpcSocket is overridable for tests. var rpcSocket = SocketPath // call issues one Request and returns the Response. It dials the agent // unix socket, encodes the request, and decodes a single response. The // connection is closed before returning. func call(req Request) (*Response, error) { conn, err := net.DialTimeout("unix", rpcSocket, dialTimeout) if err != nil { return nil, fmt.Errorf("dial flock-agent at %s: %w", rpcSocket, err) } defer conn.Close() if err := EncodeRequest(conn, req); err != nil { return nil, err } resp, err := DecodeResponse(conn) if err != nil { return nil, err } return &resp, nil } // fromArgs builds a Request from a CNI skel.CmdArgs invocation. func fromArgs(op Op, args *skel.CmdArgs) Request { return Request{ Op: op, ContainerID: args.ContainerID, Netns: args.Netns, IfName: args.IfName, Args: args.Args, Path: args.Path, StdinData: args.StdinData, } } // toCNIError converts an RPC Response.Error into a CNI types.Error, or nil. func toCNIError(stage string, resp *Response) error { if resp.Error == "" { return nil } return types.NewError(types.ErrInternal, "flock-"+stage, resp.Error) }