44
55A generic CNI library to provide APIs for CNI plugin interactions. The library provides APIs to:
66
7+ - Load CNI network config from different sources
78- Setup networks for container namespace
89- Remove networks from container namespace
910- Query status of CNI network plugin initialization
@@ -18,11 +19,17 @@ func main() {
1819 defaultIfName := "eth0"
1920 // Initialize library
2021 l = gocni.New(gocni.WithMinNetworkCount(2),
21- gocni.WithLoNetwork(),
2222 gocni.WithPluginConfDir("/etc/mycni/net.d"),
2323 gocni.WithPluginDir([]string{"/opt/mycni/bin", "/opt/cni/bin"}),
2424 gocni.WithDefaultIfName(defaultIfName))
25-
25+
26+ // Load the cni configuration
27+ err:= l.Load(gocni.WithLoNetwork(),gocni.WithDefaultConf())
28+ if err != nil{
29+ log.Errorf("failed to load cni configuration: %v", err)
30+ return
31+ }
32+
2633 // Setup network for namespace.
2734 labels := map[string]string{
2835 "K8S_POD_NAMESPACE": "namespace1",
@@ -31,16 +38,10 @@ func main() {
3138 }
3239 result, err := l.Setup(id, netns, gocni.WithLabels(labels))
3340 if err != nil {
34- return nil, fmt.Errorf("failed to setup network for namespace %q: %v", id, err)
41+ log.Errorf("failed to setup network for namespace %q: %v",id, err)
42+ return
3543 }
36- defer func() {
37- if retErr != nil {
38- // Teardown network if an error is returned.
39- if err := l.Remove(id, netns, gocni.WithLabels(labels)); err != nil {
40- fmt.Errorf("Failed to destroy network for namespace %q", id)
41- }
42- }
43- }()
44+
4445 // Get IP of the default interface
4546 IP := result.Interfaces[defaultIfName].IPConfigs[0].IP.String()
4647 fmt.Printf("IP of the default interface %s:%s", defaultIfName, IP)
0 commit comments