github.com/looshlee/cilium@v1.6.12/plugins/cilium-docker/main.go (about) 1 // Copyright 2016-2017 Authors of Cilium 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package main 16 17 import ( 18 "os" 19 "path/filepath" 20 21 "github.com/cilium/cilium/common" 22 "github.com/cilium/cilium/pkg/logging" 23 "github.com/cilium/cilium/pkg/logging/logfields" 24 "github.com/cilium/cilium/plugins/cilium-docker/driver" 25 26 "github.com/sirupsen/logrus" 27 "github.com/spf13/cobra" 28 ) 29 30 var ( 31 log = logging.DefaultLogger.WithField(logfields.LogSubsys, "cilium-docker") 32 pluginPath string 33 driverSock string 34 debug bool 35 ciliumAPI string 36 ) 37 38 // RootCmd represents the base command when called without any subcommands 39 var RootCmd = &cobra.Command{ 40 Use: "cilium-docker", 41 Short: "Cilium plugin for Docker (libnetwork)", 42 Long: `Cilium plugin for Docker (libnetwork) 43 44 Docker plugin implementing the networking and IPAM API. 45 46 The plugin handles requests from the local Docker runtime to provide 47 network connectivity and IP address management for containers that are 48 connected to a Docker network of type "cilium".`, 49 Example: ` docker network create my_network --ipam-driver cilium --driver cilium 50 docker run --net my_network hello-world 51 `, 52 Run: func(cmd *cobra.Command, args []string) { 53 common.RequireRootPrivilege("cilium-docker") 54 55 createPluginSock() 56 57 if d, err := driver.NewDriver(ciliumAPI); err != nil { 58 log.WithError(err).Fatal("Unable to create cilium-net driver") 59 } else { 60 log.WithField(logfields.Path, driverSock).Info("Listening for events from Docker") 61 if err := d.Listen(driverSock); err != nil { 62 log.Fatal(err) 63 } 64 } 65 }, 66 } 67 68 func main() { 69 if err := RootCmd.Execute(); err != nil { 70 log.Fatal(err) 71 os.Exit(-1) 72 } 73 } 74 75 func init() { 76 cobra.OnInitialize(initConfig) 77 flags := RootCmd.PersistentFlags() 78 flags.BoolVarP(&debug, "debug", "D", false, "Enable debug messages") 79 flags.StringVar(&ciliumAPI, "cilium-api", "", "URI to server-side API") 80 flags.StringVar(&pluginPath, "docker-plugins", "/run/docker/plugins", 81 "Path to Docker plugins directory") 82 } 83 84 func initConfig() { 85 if debug { 86 log.Logger.SetLevel(logrus.DebugLevel) 87 } else { 88 log.Logger.SetLevel(logrus.InfoLevel) 89 } 90 } 91 92 func createPluginSock() { 93 driverSock = filepath.Join(pluginPath, "cilium.sock") 94 95 if err := os.MkdirAll(pluginPath, 0755); err != nil && !os.IsExist(err) { 96 log.WithError(err).Fatal("Could not create net plugin path directory") 97 } 98 99 if _, err := os.Stat(driverSock); err == nil { 100 log.WithField(logfields.Path, driverSock).Debug("socket file already exists, unlinking the old file handle.") 101 os.RemoveAll(driverSock) 102 } 103 }