kube.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. // Copyright 2016 flannel authors
  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. package kube
  15. import (
  16. "encoding/json"
  17. "errors"
  18. "fmt"
  19. "io/ioutil"
  20. "net"
  21. "os"
  22. "time"
  23. "github.com/coreos/flannel/pkg/ip"
  24. "github.com/coreos/flannel/subnet"
  25. "github.com/golang/glog"
  26. "golang.org/x/net/context"
  27. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  28. "k8s.io/apimachinery/pkg/runtime"
  29. "k8s.io/apimachinery/pkg/types"
  30. "k8s.io/apimachinery/pkg/util/strategicpatch"
  31. "k8s.io/apimachinery/pkg/util/wait"
  32. "k8s.io/apimachinery/pkg/watch"
  33. clientset "k8s.io/client-go/kubernetes"
  34. listers "k8s.io/client-go/listers/core/v1"
  35. "k8s.io/client-go/pkg/api"
  36. "k8s.io/client-go/pkg/api/v1"
  37. "k8s.io/client-go/rest"
  38. "k8s.io/client-go/tools/cache"
  39. "k8s.io/client-go/tools/clientcmd"
  40. )
  41. var (
  42. ErrUnimplemented = errors.New("unimplemented")
  43. )
  44. const (
  45. resyncPeriod = 5 * time.Minute
  46. nodeControllerSyncTimeout = 10 * time.Minute
  47. subnetKubeManagedAnnotation = "flannel.alpha.coreos.com/kube-subnet-manager"
  48. backendDataAnnotation = "flannel.alpha.coreos.com/backend-data"
  49. backendTypeAnnotation = "flannel.alpha.coreos.com/backend-type"
  50. backendPublicIPAnnotation = "flannel.alpha.coreos.com/public-ip"
  51. netConfPath = "/etc/kube-flannel/net-conf.json"
  52. )
  53. type kubeSubnetManager struct {
  54. client clientset.Interface
  55. nodeName string
  56. nodeStore listers.NodeLister
  57. nodeController cache.Controller
  58. subnetConf *subnet.Config
  59. events chan subnet.Event
  60. }
  61. func NewSubnetManager(apiUrl, kubeconfig string) (subnet.Manager, error) {
  62. var cfg *rest.Config
  63. var err error
  64. // Use out of cluster config if the URL or kubeconfig have been specified. Otherwise use incluster config.
  65. if apiUrl != "" || kubeconfig != "" {
  66. cfg, err = clientcmd.BuildConfigFromFlags(apiUrl, kubeconfig)
  67. if err != nil {
  68. return nil, fmt.Errorf("unable to create k8s config: %v", err)
  69. }
  70. } else {
  71. cfg, err = rest.InClusterConfig()
  72. if err != nil {
  73. return nil, fmt.Errorf("unable to initialize inclusterconfig: %v", err)
  74. }
  75. }
  76. c, err := clientset.NewForConfig(cfg)
  77. if err != nil {
  78. return nil, fmt.Errorf("unable to initialize client: %v", err)
  79. }
  80. // The kube subnet mgr needs to know the k8s node name that it's running on so it can annotate it.
  81. // If we're running as a pod then the POD_NAME and POD_NAMESPACE will be populated and can be used to find the node
  82. // name. Otherwise, the environment variable NODE_NAME can be passed in.
  83. nodeName := os.Getenv("NODE_NAME")
  84. if nodeName == "" {
  85. podName := os.Getenv("POD_NAME")
  86. podNamespace := os.Getenv("POD_NAMESPACE")
  87. if podName == "" || podNamespace == "" {
  88. return nil, fmt.Errorf("env variables POD_NAME and POD_NAMESPACE must be set")
  89. }
  90. pod, err := c.Pods(podNamespace).Get(podName, metav1.GetOptions{})
  91. if err != nil {
  92. return nil, fmt.Errorf("error retrieving pod spec for '%s/%s': %v", podNamespace, podName, err)
  93. }
  94. nodeName = pod.Spec.NodeName
  95. if nodeName == "" {
  96. return nil, fmt.Errorf("node name not present in pod spec '%s/%s'", podNamespace, podName)
  97. }
  98. }
  99. netConf, err := ioutil.ReadFile(netConfPath)
  100. if err != nil {
  101. return nil, fmt.Errorf("failed to read net conf: %v", err)
  102. }
  103. sc, err := subnet.ParseConfig(string(netConf))
  104. if err != nil {
  105. return nil, fmt.Errorf("error parsing subnet config: %s", err)
  106. }
  107. sm, err := newKubeSubnetManager(c, sc, nodeName)
  108. if err != nil {
  109. return nil, fmt.Errorf("error creating network manager: %s", err)
  110. }
  111. go sm.Run(context.Background())
  112. glog.Infof("Waiting %s for node controller to sync", nodeControllerSyncTimeout)
  113. err = wait.Poll(time.Second, nodeControllerSyncTimeout, func() (bool, error) {
  114. return sm.nodeController.HasSynced(), nil
  115. })
  116. if err != nil {
  117. return nil, fmt.Errorf("error waiting for nodeController to sync state: %v", err)
  118. }
  119. glog.Infof("Node controller sync successful")
  120. return sm, nil
  121. }
  122. func newKubeSubnetManager(c clientset.Interface, sc *subnet.Config, nodeName string) (*kubeSubnetManager, error) {
  123. var ksm kubeSubnetManager
  124. ksm.client = c
  125. ksm.nodeName = nodeName
  126. ksm.subnetConf = sc
  127. ksm.events = make(chan subnet.Event, 5000)
  128. indexer, controller := cache.NewIndexerInformer(
  129. &cache.ListWatch{
  130. ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
  131. return ksm.client.CoreV1().Nodes().List(options)
  132. },
  133. WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
  134. return ksm.client.CoreV1().Nodes().Watch(options)
  135. },
  136. },
  137. &v1.Node{},
  138. resyncPeriod,
  139. cache.ResourceEventHandlerFuncs{
  140. AddFunc: func(obj interface{}) {
  141. ksm.handleAddLeaseEvent(subnet.EventAdded, obj)
  142. },
  143. UpdateFunc: ksm.handleUpdateLeaseEvent,
  144. DeleteFunc: func(obj interface{}) {
  145. ksm.handleAddLeaseEvent(subnet.EventRemoved, obj)
  146. },
  147. },
  148. cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc},
  149. )
  150. ksm.nodeController = controller
  151. ksm.nodeStore = listers.NewNodeLister(indexer)
  152. return &ksm, nil
  153. }
  154. func (ksm *kubeSubnetManager) handleAddLeaseEvent(et subnet.EventType, obj interface{}) {
  155. n := obj.(*v1.Node)
  156. if s, ok := n.Annotations[subnetKubeManagedAnnotation]; !ok || s != "true" {
  157. return
  158. }
  159. l, err := nodeToLease(*n)
  160. if err != nil {
  161. glog.Infof("Error turning node %q to lease: %v", n.ObjectMeta.Name, err)
  162. return
  163. }
  164. ksm.events <- subnet.Event{et, l}
  165. }
  166. func (ksm *kubeSubnetManager) handleUpdateLeaseEvent(oldObj, newObj interface{}) {
  167. o := oldObj.(*v1.Node)
  168. n := newObj.(*v1.Node)
  169. if s, ok := n.Annotations[subnetKubeManagedAnnotation]; !ok || s != "true" {
  170. return
  171. }
  172. if o.Annotations[backendDataAnnotation] == n.Annotations[backendDataAnnotation] &&
  173. o.Annotations[backendTypeAnnotation] == n.Annotations[backendTypeAnnotation] &&
  174. o.Annotations[backendPublicIPAnnotation] == n.Annotations[backendPublicIPAnnotation] {
  175. return // No change to lease
  176. }
  177. l, err := nodeToLease(*n)
  178. if err != nil {
  179. glog.Infof("Error turning node %q to lease: %v", n.ObjectMeta.Name, err)
  180. return
  181. }
  182. ksm.events <- subnet.Event{subnet.EventAdded, l}
  183. }
  184. func (ksm *kubeSubnetManager) GetNetworkConfig(ctx context.Context) (*subnet.Config, error) {
  185. return ksm.subnetConf, nil
  186. }
  187. func (ksm *kubeSubnetManager) AcquireLease(ctx context.Context, attrs *subnet.LeaseAttrs) (*subnet.Lease, error) {
  188. cachedNode, err := ksm.nodeStore.Get(ksm.nodeName)
  189. if err != nil {
  190. return nil, err
  191. }
  192. nobj, err := api.Scheme.DeepCopy(cachedNode)
  193. if err != nil {
  194. return nil, err
  195. }
  196. n := nobj.(*v1.Node)
  197. if n.Spec.PodCIDR == "" {
  198. return nil, fmt.Errorf("node %q pod cidr not assigned", ksm.nodeName)
  199. }
  200. bd, err := attrs.BackendData.MarshalJSON()
  201. if err != nil {
  202. return nil, err
  203. }
  204. _, cidr, err := net.ParseCIDR(n.Spec.PodCIDR)
  205. if err != nil {
  206. return nil, err
  207. }
  208. if n.Annotations[backendDataAnnotation] != string(bd) ||
  209. n.Annotations[backendTypeAnnotation] != attrs.BackendType ||
  210. n.Annotations[backendPublicIPAnnotation] != attrs.PublicIP.String() ||
  211. n.Annotations[subnetKubeManagedAnnotation] != "true" {
  212. n.Annotations[backendTypeAnnotation] = attrs.BackendType
  213. n.Annotations[backendDataAnnotation] = string(bd)
  214. n.Annotations[backendPublicIPAnnotation] = attrs.PublicIP.String()
  215. n.Annotations[subnetKubeManagedAnnotation] = "true"
  216. oldData, err := json.Marshal(cachedNode)
  217. if err != nil {
  218. return nil, err
  219. }
  220. newData, err := json.Marshal(n)
  221. if err != nil {
  222. return nil, err
  223. }
  224. patchBytes, err := strategicpatch.CreateTwoWayMergePatch(oldData, newData, v1.Node{})
  225. if err != nil {
  226. return nil, fmt.Errorf("failed to create patch for node %q: %v", ksm.nodeName, err)
  227. }
  228. _, err = ksm.client.CoreV1().Nodes().Patch(ksm.nodeName, types.StrategicMergePatchType, patchBytes, "status")
  229. if err != nil {
  230. return nil, err
  231. }
  232. }
  233. return &subnet.Lease{
  234. Subnet: ip.FromIPNet(cidr),
  235. Attrs: *attrs,
  236. Expiration: time.Now().Add(24 * time.Hour),
  237. }, nil
  238. }
  239. func (ksm *kubeSubnetManager) WatchLeases(ctx context.Context, cursor interface{}) (subnet.LeaseWatchResult, error) {
  240. select {
  241. case event := <-ksm.events:
  242. return subnet.LeaseWatchResult{
  243. Events: []subnet.Event{event},
  244. }, nil
  245. case <-ctx.Done():
  246. return subnet.LeaseWatchResult{}, nil
  247. }
  248. }
  249. func (ksm *kubeSubnetManager) Run(ctx context.Context) {
  250. glog.Infof("Starting kube subnet manager")
  251. ksm.nodeController.Run(ctx.Done())
  252. }
  253. func nodeToLease(n v1.Node) (l subnet.Lease, err error) {
  254. l.Attrs.PublicIP, err = ip.ParseIP4(n.Annotations[backendPublicIPAnnotation])
  255. if err != nil {
  256. return l, err
  257. }
  258. l.Attrs.BackendType = n.Annotations[backendTypeAnnotation]
  259. l.Attrs.BackendData = json.RawMessage(n.Annotations[backendDataAnnotation])
  260. _, cidr, err := net.ParseCIDR(n.Spec.PodCIDR)
  261. if err != nil {
  262. return l, err
  263. }
  264. l.Subnet = ip.FromIPNet(cidr)
  265. return l, nil
  266. }
  267. // unimplemented
  268. func (ksm *kubeSubnetManager) RenewLease(ctx context.Context, lease *subnet.Lease) error {
  269. return ErrUnimplemented
  270. }
  271. func (ksm *kubeSubnetManager) WatchLease(ctx context.Context, sn ip.IP4Net, cursor interface{}) (subnet.LeaseWatchResult, error) {
  272. return subnet.LeaseWatchResult{}, ErrUnimplemented
  273. }
  274. func (ksm *kubeSubnetManager) AddReservation(ctx context.Context, r *subnet.Reservation) error {
  275. return ErrUnimplemented
  276. }
  277. func (ksm *kubeSubnetManager) RemoveReservation(ctx context.Context, subnet ip.IP4Net) error {
  278. return ErrUnimplemented
  279. }
  280. func (ksm *kubeSubnetManager) ListReservations(ctx context.Context) ([]subnet.Reservation, error) {
  281. return nil, ErrUnimplemented
  282. }
  283. func (ksm *kubeSubnetManager) Name() string {
  284. return fmt.Sprintf("Kubernetes Subnet Manager - %s", ksm.nodeName)
  285. }