From 4cf737bb7d640c3e2b9a7e8621e4417f4560b014 Mon Sep 17 00:00:00 2001 From: Rajat Chopra Date: Fri, 2 Sep 2016 12:38:42 -0700 Subject: [PATCH] add cni networking with noop plugin as default Signed-off-by: Rajat Chopra --- server/runtime.go | 37 ++++++++++++++++++++++++++++++++++++- server/server.go | 7 +++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/server/runtime.go b/server/runtime.go index 8809c9e8..0a61cd2f 100644 --- a/server/runtime.go +++ b/server/runtime.go @@ -148,6 +148,20 @@ func (s *Server) CreatePodSandbox(ctx context.Context, req *pb.CreatePodSandboxR return nil, err } + if err := s.runtime.UpdateStatus(container); err != nil { + return nil, err + } + + // Setup the network + podNamespace := "" + netnsPath, err := container.NetNsPath() + if err != nil { + return nil, err + } + if err := s.netPlugin.SetUpPod(netnsPath, podNamespace, name, containerName); err != nil { + return nil, fmt.Errorf("failed to create network for container %s in sandbox %s: %v", containerName, name, err) + } + if err := s.runtime.StartContainer(container); err != nil { return nil, err } @@ -173,7 +187,18 @@ func (s *Server) StopPodSandbox(ctx context.Context, req *pb.StopPodSandboxReque return nil, fmt.Errorf("specified sandbox not found: %s", *sbName) } + podInfraContainer := *sbName + "-infra" for _, c := range sb.containers { + if podInfraContainer == c.Name() { + podNamespace := "" + netnsPath, err := c.NetNsPath() + if err != nil { + return nil, err + } + if err := s.netPlugin.TearDownPod(netnsPath, podNamespace, *sbName, podInfraContainer); err != nil { + return nil, fmt.Errorf("failed to destroy network for container %s in sandbox %s: %v", c.Name(), *sbName, err) + } + } if err := s.runtime.StopContainer(c); err != nil { return nil, fmt.Errorf("failed to stop container %s in sandbox %s: %v", c.Name(), *sbName, err) } @@ -244,7 +269,16 @@ func (s *Server) PodSandboxStatus(ctx context.Context, req *pb.PodSandboxStatusR cState := s.runtime.ContainerStatus(podInfraContainer) created := cState.Created.Unix() - netNsPath := fmt.Sprintf("/proc/%d/ns/net", cState.Pid) + netNsPath, err := podInfraContainer.NetNsPath() + if err != nil { + return nil, err + } + podNamespace := "" + ip, err := s.netPlugin.GetContainerNetworkStatus(netNsPath, podNamespace, *sbName, podInfraContainerName) + if err != nil { + // ignore the error on network status + ip = "" + } return &pb.PodSandboxStatusResponse{ Status: &pb.PodSandboxStatus{ @@ -255,6 +289,7 @@ func (s *Server) PodSandboxStatus(ctx context.Context, req *pb.PodSandboxStatusR Network: sPtr(netNsPath), }, }, + Network: &pb.PodSandboxNetworkStatus{Ip: &ip}, }, }, nil } diff --git a/server/server.go b/server/server.go index ee557de7..3c1f6abb 100644 --- a/server/server.go +++ b/server/server.go @@ -6,6 +6,7 @@ import ( "github.com/mrunalp/ocid/oci" "github.com/mrunalp/ocid/utils" + "github.com/rajatchopra/ocicni" ) const ( @@ -18,6 +19,7 @@ type Server struct { runtime *oci.Runtime sandboxDir string state *serverState + netPlugin ocicni.CNIPlugin } // New creates a new Server with options provided @@ -40,8 +42,13 @@ func New(runtimePath, sandboxDir, containerDir string) (*Server, error) { } sandboxes := make(map[string]*sandbox) containers := make(map[string]*oci.Container) + netPlugin, err := ocicni.InitCNI("") + if err != nil { + return nil, err + } return &Server{ runtime: r, + netPlugin: netPlugin, sandboxDir: sandboxDir, state: &serverState{ sandboxes: sandboxes,