コード例 #1
0
ファイル: cni.go プロジェクト: kubernetes/kubernetes
func getDefaultCNINetwork(pluginDir, binDir, vendorCNIDirPrefix string) (*cniNetwork, error) {
	if pluginDir == "" {
		pluginDir = DefaultNetDir
	}
	files, err := libcni.ConfFiles(pluginDir)
	switch {
	case err != nil:
		return nil, err
	case len(files) == 0:
		return nil, fmt.Errorf("No networks found in %s", pluginDir)
	}

	sort.Strings(files)
	for _, confFile := range files {
		conf, err := libcni.ConfFromFile(confFile)
		if err != nil {
			glog.Warningf("Error loading CNI config file %s: %v", confFile, err)
			continue
		}
		// Search for vendor-specific plugins as well as default plugins in the CNI codebase.
		vendorDir := vendorCNIDir(vendorCNIDirPrefix, conf.Network.Type)
		cninet := &libcni.CNIConfig{
			Path: []string{binDir, vendorDir},
		}
		network := &cniNetwork{name: conf.Network.Name, NetworkConfig: conf, CNIConfig: cninet}
		return network, nil
	}
	return nil, fmt.Errorf("No valid networks found in %s", pluginDir)
}
コード例 #2
0
func NewCNIExec(state *DockerPluginState) (*CNIExec, error) {
	if state.HostConfig.NetworkMode.IsContainer() ||
		state.HostConfig.NetworkMode.IsHost() ||
		state.HostConfig.NetworkMode.IsNone() {
		return &CNIExec{}, nil
	}

	c := &CNIExec{
		runtimeConf: libcni.RuntimeConf{
			ContainerID: state.ContainerID,
			NetNS:       fmt.Sprintf("/proc/%d/ns/net", state.State.Pid),
			IfName:      "eth0",
			Args: [][2]string{
				{"IgnoreUnknown", "1"},
				{"DOCKER", "true"},
			},
		},
		cninet: libcni.CNIConfig{
			Path: cniPath,
		},
	}

	dir := fmt.Sprintf(cniDir, state.HostConfig.NetworkMode.NetworkName())
	files, err := libcni.ConfFiles(dir)
	if err != nil {
		return nil, err
	}
	sort.Strings(files)

	os.Setenv("PATH", strings.Join(cniPath, ":"))

	for _, file := range files {
		netConf, err := libcni.ConfFromFile(file)
		if err != nil {
			return nil, err
		}
		c.confs = append(c.confs, netConf)
	}

	return c, nil
}