github.com/mirantis/virtlet@v1.5.2-0.20191204181327-1659b8a48e9b/pkg/cni/configuration.go (about)

     1  /*
     2  Copyright 2016 Mirantis
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  
    16  Based on code from Kubernetes project, pkg/kubelet/network/cni/cni.go
    17  
    18  Copyright 2014 The Kubernetes Authors.
    19  
    20  Licensed under the Apache License, Version 2.0 (the "License");
    21  you may not use this file except in compliance with the License.
    22  You may obtain a copy of the License at
    23  
    24      http://www.apache.org/licenses/LICENSE-2.0
    25  
    26  Unless required by applicable law or agreed to in writing, software
    27  distributed under the License is distributed on an "AS IS" BASIS,
    28  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    29  See the License for the specific language governing permissions and
    30  limitations under the License.
    31  */
    32  
    33  package cni
    34  
    35  import (
    36  	"fmt"
    37  	"sort"
    38  	"strings"
    39  
    40  	"github.com/containernetworking/cni/libcni"
    41  	"github.com/golang/glog"
    42  )
    43  
    44  // ReadConfiguration iterates over CNI config files in a directory
    45  // and returns first configuration with non empty Plugins list.
    46  func ReadConfiguration(configDir string) (*libcni.NetworkConfigList, error) {
    47  	files, err := libcni.ConfFiles(configDir, []string{".conf", ".conflist", ".json"})
    48  	switch {
    49  	case err != nil:
    50  		return nil, err
    51  	case len(files) == 0:
    52  		return nil, fmt.Errorf("No networks found in %s", configDir)
    53  	}
    54  
    55  	sort.Strings(files)
    56  	for _, confFile := range files {
    57  		var confList *libcni.NetworkConfigList
    58  		if strings.HasSuffix(confFile, ".conflist") {
    59  			confList, err = libcni.ConfListFromFile(confFile)
    60  			if err != nil {
    61  				glog.Warningf("Error loading CNI config list file %s: %v", confFile, err)
    62  				continue
    63  			}
    64  		} else {
    65  			conf, err := libcni.ConfFromFile(confFile)
    66  			if err != nil {
    67  				glog.Warningf("Error loading CNI config file %s: %v", confFile, err)
    68  				continue
    69  			}
    70  			// Ensure the config has a "type" so we know what plugin to run.
    71  			// Also catches the case where somebody put a conflist into a conf file.
    72  			if conf.Network.Type == "" {
    73  				glog.Warningf("Error loading CNI config file %s: no 'type'; perhaps this is a .conflist?", confFile)
    74  				continue
    75  			}
    76  
    77  			confList, err = libcni.ConfListFromConf(conf)
    78  			if err != nil {
    79  				glog.Warningf("Error converting CNI config file %s to list: %v", confFile, err)
    80  				continue
    81  			}
    82  		}
    83  		if len(confList.Plugins) == 0 {
    84  			glog.Warningf("CNI config list %s has no networks, skipping", confFile)
    85  			continue
    86  		}
    87  		// TODO: vendor dir handling (see pkg/kubelet/network/cni/cni.go)
    88  		return confList, err
    89  	}
    90  	return nil, fmt.Errorf("No valid networks found in %s", configDir)
    91  }