Skip to content

Commit

Permalink
implement pona cmdAdd
Browse files Browse the repository at this point in the history
Signed-off-by: walnuts1018 <[email protected]>
  • Loading branch information
walnuts1018 committed Aug 29, 2024
1 parent a88ebdc commit 8f59337
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
22 changes: 19 additions & 3 deletions cmd/pona/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (

"github.com/containernetworking/cni/pkg/skel"
"github.com/containernetworking/cni/pkg/types"
cni100 "github.com/containernetworking/cni/pkg/types/100"

"github.com/containernetworking/cni/pkg/version"
"github.com/cybozu-go/pona"
"github.com/cybozu-go/pona/pkg/cni"
Expand Down Expand Up @@ -36,12 +38,26 @@ func cmdAdd(args *skel.CmdArgs) error {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
defer cancel()

resp, err := client.Add(ctx, cni)
resp, err := client.Add(ctx, cniArgs)
if err != nil {
return convertError(err)
}

result, err := cni100.NewResult(resp.Result)
if err != nil {
return types.NewError(types.ErrDecodingFailure, "failed to unmarshal result", err.Error())
}

return types.PrintResult(result, conf.CNIVersion)
}

func cmdDel(args *skel.CmdArgs) error
func cmdDel(args *skel.CmdArgs) error {
return nil
}

func cmdCheck(args *skel.CmdArgs) error
func cmdCheck(args *skel.CmdArgs) error {
return nil
}

func main() {
skel.PluginMainFuncs(skel.CNIFuncs{Add: cmdAdd, Del: cmdDel, Check: cmdCheck, GC: nil, Status: nil}, version.PluginSupports("0.3.1", "0.4.0", "1.0.0"), fmt.Sprintf("coil %s", pona.Version))
Expand Down
17 changes: 17 additions & 0 deletions cmd/pona/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/resolver"
"google.golang.org/grpc/status"
)

// PluginEnvArgs represents CNI_ARG
Expand Down Expand Up @@ -59,3 +60,19 @@ func connect(sockPath string) (*grpc.ClientConn, error) {
}
return conn, nil
}

// convertError turns err returned from gRPC library into CNI's types.Error
func convertError(err error) error {
st := status.Convert(err)
details := st.Details()
if len(details) != 1 {
return types.NewError(types.ErrInternal, st.Message(), err.Error())
}

cniErr, ok := details[0].(*cnirpc.CNIError)
if !ok {
types.NewError(types.ErrInternal, st.Message(), err.Error())
}

return types.NewError(uint(cniErr.Code), cniErr.Msg, cniErr.Details)
}

0 comments on commit 8f59337

Please sign in to comment.