add cni networking with noop plugin as default

Signed-off-by: Rajat Chopra <rchopra@redhat.com>
This commit is contained in:
Rajat Chopra 2016-09-02 12:38:42 -07:00 committed by Mrunal Patel
parent ecb513e665
commit 4cf737bb7d
2 changed files with 43 additions and 1 deletions

View file

@ -148,6 +148,20 @@ func (s *Server) CreatePodSandbox(ctx context.Context, req *pb.CreatePodSandboxR
return nil, err 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 { if err := s.runtime.StartContainer(container); err != nil {
return nil, err 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) return nil, fmt.Errorf("specified sandbox not found: %s", *sbName)
} }
podInfraContainer := *sbName + "-infra"
for _, c := range sb.containers { 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 { 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) 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) cState := s.runtime.ContainerStatus(podInfraContainer)
created := cState.Created.Unix() 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{ return &pb.PodSandboxStatusResponse{
Status: &pb.PodSandboxStatus{ Status: &pb.PodSandboxStatus{
@ -255,6 +289,7 @@ func (s *Server) PodSandboxStatus(ctx context.Context, req *pb.PodSandboxStatusR
Network: sPtr(netNsPath), Network: sPtr(netNsPath),
}, },
}, },
Network: &pb.PodSandboxNetworkStatus{Ip: &ip},
}, },
}, nil }, nil
} }

View file

@ -6,6 +6,7 @@ import (
"github.com/mrunalp/ocid/oci" "github.com/mrunalp/ocid/oci"
"github.com/mrunalp/ocid/utils" "github.com/mrunalp/ocid/utils"
"github.com/rajatchopra/ocicni"
) )
const ( const (
@ -18,6 +19,7 @@ type Server struct {
runtime *oci.Runtime runtime *oci.Runtime
sandboxDir string sandboxDir string
state *serverState state *serverState
netPlugin ocicni.CNIPlugin
} }
// New creates a new Server with options provided // 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) sandboxes := make(map[string]*sandbox)
containers := make(map[string]*oci.Container) containers := make(map[string]*oci.Container)
netPlugin, err := ocicni.InitCNI("")
if err != nil {
return nil, err
}
return &Server{ return &Server{
runtime: r, runtime: r,
netPlugin: netPlugin,
sandboxDir: sandboxDir, sandboxDir: sandboxDir,
state: &serverState{ state: &serverState{
sandboxes: sandboxes, sandboxes: sandboxes,