generated from cybozu-go/neco-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* change directory structure Signed-off-by: gotti <18141824+gotti@users.noreply.github.com> * fix Dockerfiles Signed-off-by: gotti <18141824+gotti@users.noreply.github.com> * add protobuf Signed-off-by: gotti <18141824+gotti@users.noreply.github.com> * [WIP] add ponad Signed-off-by: walnuts1018 <r.juglans.1018@gmail.com> * [WIP] implementing ponad Signed-off-by: gotti <18141824+gotti@users.noreply.github.com> * [WIP] ponad Add Signed-off-by: walnuts1018 <r.juglans.1018@gmail.com> * [WIP] implementing ponad Signed-off-by: gotti <18141824+gotti@users.noreply.github.com> * implemented collectDestinationsForEgress Signed-off-by: walnuts1018 <r.juglans.1018@gmail.com> * add pkg/cni Signed-off-by: gotti <18141824+gotti@users.noreply.github.com> * [WIP] Add Signed-off-by: walnuts1018 <r.juglans.1018@gmail.com> * [WIP] add nat client Signed-off-by: gotti <18141824+gotti@users.noreply.github.com> * [WIP] nc Signed-off-by: walnuts1018 <r.juglans.1018@gmail.com> * [WIP] implement updateroutes Signed-off-by: gotti <18141824+gotti@users.noreply.github.com> * [WIP] UpdateRoutes Signed-off-by: walnuts1018 <r.juglans.1018@gmail.com> * implemented nat client Signed-off-by: gotti <18141824+gotti@users.noreply.github.com> * implement Add Signed-off-by: walnuts1018 <r.juglans.1018@gmail.com> * [WIP] add pona cni Signed-off-by: walnuts1018 <r.juglans.1018@gmail.com> * add netip utility test Signed-off-by: walnuts1018 <r.juglans.1018@gmail.com> * [WIP] implementing cni add Signed-off-by: gotti <18141824+gotti@users.noreply.github.com> * implement pona cmdAdd Signed-off-by: walnuts1018 <r.juglans.1018@gmail.com> * implement pona & create pona-installer Signed-off-by: walnuts1018 <r.juglans.1018@gmail.com> * [WIP] pona-installer Signed-off-by: gotti <18141824+gotti@users.noreply.github.com> * implement installer Signed-off-by: walnuts1018 <r.juglans.1018@gmail.com> * use netns Signed-off-by: walnuts1018 <r.juglans.1018@gmail.com> * [WIP] ponad debugging Signed-off-by: gotti <18141824+gotti@users.noreply.github.com> * fix netiputil Signed-off-by: walnuts1018 <r.juglans.1018@gmail.com> * coil -> pona Signed-off-by: walnuts1018 <r.juglans.1018@gmail.com> * add version Signed-off-by: walnuts1018 <r.juglans.1018@gmail.com> * check-generate Signed-off-by: walnuts1018 <r.juglans.1018@gmail.com> * update protoc-gen-go Signed-off-by: walnuts1018 <r.juglans.1018@gmail.com> * change error Signed-off-by: walnuts1018 <r.juglans.1018@gmail.com> * rm unused env Signed-off-by: walnuts1018 <r.juglans.1018@gmail.com> * rm unused conf Signed-off-by: walnuts1018 <r.juglans.1018@gmail.com> * add addthrow error Signed-off-by: walnuts1018 <r.juglans.1018@gmail.com> * fix throw route failure Signed-off-by: gotti <18141824+gotti@users.noreply.github.com> * fix localip config Signed-off-by: gotti <18141824+gotti@users.noreply.github.com> --------- Signed-off-by: gotti <18141824+gotti@users.noreply.github.com> Signed-off-by: walnuts1018 <r.juglans.1018@gmail.com> Co-authored-by: gotti <18141824+gotti@users.noreply.github.com> Co-authored-by: walnuts1018 <r.juglans.1018@gmail.com>
- Loading branch information
1 parent
90cd1ba
commit adb5bea
Showing
37 changed files
with
2,356 additions
and
191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,3 +25,6 @@ go.work | |
*.swp | ||
*.swo | ||
*~ | ||
|
||
# protobuf downloaded files | ||
include |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
func installPona(ponaPath, cniBinDir string) error { | ||
f, err := os.Open(ponaPath) | ||
if err != nil { | ||
return fmt.Errorf("failed to read pona %w", err) | ||
} | ||
if err := os.MkdirAll(cniBinDir, 0755); err != nil { | ||
return fmt.Errorf("failed to MkdirAll: %w", err) | ||
} | ||
|
||
g, err := os.CreateTemp(cniBinDir, ".tmp") | ||
if err != nil { | ||
return fmt.Errorf("failed to CreateTemp: %w", err) | ||
} | ||
defer func() { | ||
g.Close() | ||
os.Remove(g.Name()) | ||
}() | ||
|
||
if _, err := io.Copy(g, f); err != nil { | ||
return fmt.Errorf("failed to io.Copy: %w", err) | ||
} | ||
|
||
if err := g.Chmod(0755); err != nil { | ||
return fmt.Errorf("failed to chmod: %w", err) | ||
} | ||
|
||
if err := g.Sync(); err != nil { | ||
return fmt.Errorf("failed to Sync: %w", err) | ||
} | ||
|
||
if err := os.Rename(g.Name(), filepath.Join(cniBinDir, "pona")); err != nil { | ||
return fmt.Errorf("failed to rename: %w", err) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package main | ||
|
||
import ( | ||
"log/slog" | ||
"os" | ||
|
||
"github.com/caarlos0/env/v10" | ||
_ "github.com/joho/godotenv/autoload" | ||
) | ||
|
||
type Config struct { | ||
CniEtcDir string `env:"CNI_ETC_DIR" envDefault:"/host/etc/cni/net.d"` | ||
CniBinDir string `env:"CNI_BIN_DIR" envDefault:"/host/opt/cni/bin"` | ||
PonaPath string `env:"CNI_PATH" envDefault:"/pona"` | ||
} | ||
|
||
func main() { | ||
var cfg Config | ||
if err := env.Parse(&cfg); err != nil { | ||
slog.Error("failed to parse config", slog.Any("error", err)) | ||
os.Exit(1) | ||
} | ||
|
||
if err := installPona(cfg.PonaPath, cfg.CniBinDir); err != nil { | ||
slog.Error("failed to install pona", | ||
slog.Any("error", err), | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"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" | ||
"github.com/cybozu-go/pona/pkg/cnirpc" | ||
) | ||
|
||
func cmdAdd(args *skel.CmdArgs) error { | ||
conf, err := cni.ParseConfig(args.StdinData) | ||
if err != nil { | ||
return types.NewError(types.ErrDecodingFailure, "failed to parse config from stdin data", err.Error()) | ||
} | ||
if conf.PrevResult == nil { | ||
return types.NewError(types.ErrInternal, "ponad must be called as chained plugin", "") | ||
} | ||
|
||
cniArgs, err := makeCNIArgs(args) | ||
if err != nil { | ||
return types.NewError(types.ErrInvalidNetworkConfig, "failed to transform args to RPC arg", err.Error()) | ||
} | ||
|
||
conn, err := connect(conf.Socket) | ||
if err != nil { | ||
return types.NewError(types.ErrTryAgainLater, "failed to connect to socket", err.Error()) | ||
} | ||
|
||
client := cnirpc.NewCNIClient(conn) | ||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute) | ||
defer cancel() | ||
|
||
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 { | ||
return nil | ||
} | ||
|
||
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", "1.1.0"), fmt.Sprintf("pona %s", pona.Version)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net" | ||
|
||
"github.com/containernetworking/cni/pkg/skel" | ||
"github.com/containernetworking/cni/pkg/types" | ||
"github.com/cybozu-go/pona/internal/constants" | ||
"github.com/cybozu-go/pona/pkg/cnirpc" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/credentials/insecure" | ||
"google.golang.org/grpc/resolver" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
// PluginEnvArgs represents CNI_ARG | ||
type PluginEnvArgs struct { | ||
types.CommonArgs | ||
K8S_POD_NAMESPACE types.UnmarshallableString | ||
K8S_POD_NAME types.UnmarshallableString | ||
K8S_POD_INFRA_CONTAINER_ID types.UnmarshallableString | ||
} | ||
|
||
// Map returns a map[string]string | ||
func (e PluginEnvArgs) Map() map[string]string { | ||
return map[string]string{ | ||
constants.PodNamespaceKey: string(e.K8S_POD_NAMESPACE), | ||
constants.PodNameKey: string(e.K8S_POD_NAME), | ||
constants.PodContainerKey: string(e.K8S_POD_INFRA_CONTAINER_ID), | ||
} | ||
} | ||
|
||
func makeCNIArgs(args *skel.CmdArgs) (*cnirpc.CNIArgs, error) { | ||
a := &PluginEnvArgs{} | ||
if err := types.LoadArgs(args.Args, a); err != nil { | ||
return nil, fmt.Errorf("failed to load args: %w", err) | ||
} | ||
return &cnirpc.CNIArgs{ | ||
ContainerId: args.ContainerID, | ||
Netns: args.Netns, | ||
Ifname: args.IfName, | ||
Args: a.Map(), | ||
Path: args.Path, | ||
StdinData: args.StdinData, | ||
}, nil | ||
} | ||
|
||
func connect(sockPath string) (*grpc.ClientConn, error) { | ||
dialer := &net.Dialer{} | ||
dialFunc := func(ctx context.Context, a string) (net.Conn, error) { | ||
return dialer.DialContext(ctx, "unix", a) | ||
} | ||
resolver.SetDefaultScheme("passthrough") | ||
|
||
conn, err := grpc.NewClient(sockPath, grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithContextDialer(dialFunc)) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to connect to %s: %w", sockPath, err) | ||
} | ||
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) | ||
} |
Oops, something went wrong.