<commit_before>/*
Copyright 2016 Mirantis

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cni

import (
	"fmt"

	"github.com/containernetworking/cni/libcni"
	cnicurrent "github.com/containernetworking/cni/pkg/types/current"
	"github.com/davecgh/go-spew/spew"
	"github.com/golang/glog"

	"github.com/Mirantis/virtlet/pkg/utils"
)

// CNIClient provides an interface to CNI
type CNIClient interface {
	// AddSandboxToNetwork adds a pod sandbox to the CNI network
	AddSandboxToNetwork(podId, podName, podNs string) (*cnicurrent.Result, error)
	// RemoveSandboxFromNetwork removes a pod sandbox from the CNI network
	RemoveSandboxFromNetwork(podId, podName, podNs string) error
	// GetDummyNetwork creates a dummy network using CNI plugin.
	// It's used for making a dummy gateway for Calico CNI plugin
	GetDummyNetwork() (*cnicurrent.Result, string, error)
}

type Client struct {
	cniConfig     *libcni.CNIConfig
	netConfigList *libcni.NetworkConfigList
}

var _ CNIClient = &Client{}

func NewClient(pluginsDir, configsDir string) (*Client, error) {
	netConfigList, err := ReadConfiguration(configsDir)
	glog.V(3).Infof("CNI config: name: %q type: %q", netConfigList.Plugins[0].Network.Name, netConfigList.Plugins[0].Network.Type)
	if err != nil {
		return nil, fmt.Errorf("failed to read CNI configuration: %v", err)
	}

	return &Client{
		cniConfig:     &libcni.CNIConfig{Path: []string{pluginsDir}},
		netConfigList: netConfigList,
	}, nil
}

func (c *Client) cniRuntimeConf(podId, podName, podNs string) *libcni.RuntimeConf {
	r := &libcni.RuntimeConf{
		ContainerID: podId,
		NetNS:       PodNetNSPath(podId),
		IfName:      "virtlet-eth0",<commit_msg>tapmanager panic when ReadConfiguration fail<commit_after>/*
Copyright 2016 Mirantis

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cni

import (
	"fmt"

	"github.com/containernetworking/cni/libcni"
	cnicurrent "github.com/containernetworking/cni/pkg/types/current"
	"github.com/davecgh/go-spew/spew"
	"github.com/golang/glog"

	"github.com/Mirantis/virtlet/pkg/utils"
)

// CNIClient provides an interface to CNI
type CNIClient interface {
	// AddSandboxToNetwork adds a pod sandbox to the CNI network
	AddSandboxToNetwork(podId, podName, podNs string) (*cnicurrent.Result, error)
	// RemoveSandboxFromNetwork removes a pod sandbox from the CNI network
	RemoveSandboxFromNetwork(podId, podName, podNs string) error
	// GetDummyNetwork creates a dummy network using CNI plugin.
	// It's used for making a dummy gateway for Calico CNI plugin
	GetDummyNetwork() (*cnicurrent.Result, string, error)
}

type Client struct {
	cniConfig     *libcni.CNIConfig
	netConfigList *libcni.NetworkConfigList
}

var _ CNIClient = &Client{}

func NewClient(pluginsDir, configsDir string) (*Client, error) {
	netConfigList, err := ReadConfiguration(configsDir)
	if err != nil {
		return nil, fmt.Errorf("failed to read CNI configuration %q: %v", configsDir, err)
	}
	glog.V(3).Infof("CNI config: name: %q type: %q", netConfigList.Plugins[0].Network.Name, netConfigList.Plugins[0].Network.Type)

	return &Client{
		cniConfig:     &libcni.CNIConfig{Path: []string{pluginsDir}},
		netConfigList: netConfigList,
	}, nil
}

func (c *Client) cniRuntimeConf(podId, podName, podNs string) *libcni.RuntimeConf {
	r := &libcni.RuntimeConf{
		ContainerID: podId,
		NetNS:       PodNetNSPath(podId),
		IfName:      "virtlet-eth0",
{'fraction_non_alphanumeric': 0.09640221402214022, 'fraction_numerical': 0.005535055350553505, 'mean_word_length': 4.759628154050465, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '64262', 'n_tokens_mistral': 1456, 'n_tokens_neox': 1384, 'n_words': 437}