...

Source file src/k8s.io/kubernetes/pkg/proxy/node.go

Documentation: k8s.io/kubernetes/pkg/proxy

     1  /*
     2  Copyright 2022 The Kubernetes Authors.
     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  
    17  package proxy
    18  
    19  import (
    20  	"reflect"
    21  	"sync"
    22  
    23  	v1 "k8s.io/api/core/v1"
    24  	"k8s.io/klog/v2"
    25  	"k8s.io/kubernetes/pkg/proxy/config"
    26  	"k8s.io/kubernetes/pkg/proxy/healthcheck"
    27  )
    28  
    29  // NodePodCIDRHandler handles the life cycle of kube-proxy based on the node PodCIDR assigned
    30  // Implements the config.NodeHandler interface
    31  // https://issues.k8s.io/111321
    32  type NodePodCIDRHandler struct {
    33  	mu       sync.Mutex
    34  	podCIDRs []string
    35  }
    36  
    37  func NewNodePodCIDRHandler(podCIDRs []string) *NodePodCIDRHandler {
    38  	return &NodePodCIDRHandler{
    39  		podCIDRs: podCIDRs,
    40  	}
    41  }
    42  
    43  var _ config.NodeHandler = &NodePodCIDRHandler{}
    44  
    45  // OnNodeAdd is a handler for Node creates.
    46  func (n *NodePodCIDRHandler) OnNodeAdd(node *v1.Node) {
    47  	n.mu.Lock()
    48  	defer n.mu.Unlock()
    49  
    50  	podCIDRs := node.Spec.PodCIDRs
    51  	// initialize podCIDRs
    52  	if len(n.podCIDRs) == 0 && len(podCIDRs) > 0 {
    53  		klog.InfoS("Setting current PodCIDRs", "podCIDRs", podCIDRs)
    54  		n.podCIDRs = podCIDRs
    55  		return
    56  	}
    57  	if !reflect.DeepEqual(n.podCIDRs, podCIDRs) {
    58  		klog.ErrorS(nil, "Using NodeCIDR LocalDetector mode, current PodCIDRs are different than previous PodCIDRs, restarting",
    59  			"node", klog.KObj(node), "newPodCIDRs", podCIDRs, "oldPodCIDRs", n.podCIDRs)
    60  		klog.FlushAndExit(klog.ExitFlushTimeout, 1)
    61  	}
    62  }
    63  
    64  // OnNodeUpdate is a handler for Node updates.
    65  func (n *NodePodCIDRHandler) OnNodeUpdate(_, node *v1.Node) {
    66  	n.mu.Lock()
    67  	defer n.mu.Unlock()
    68  	podCIDRs := node.Spec.PodCIDRs
    69  	// initialize podCIDRs
    70  	if len(n.podCIDRs) == 0 && len(podCIDRs) > 0 {
    71  		klog.InfoS("Setting current PodCIDRs", "podCIDRs", podCIDRs)
    72  		n.podCIDRs = podCIDRs
    73  		return
    74  	}
    75  	if !reflect.DeepEqual(n.podCIDRs, podCIDRs) {
    76  		klog.ErrorS(nil, "Using NodeCIDR LocalDetector mode, current PodCIDRs are different than previous PodCIDRs, restarting",
    77  			"node", klog.KObj(node), "newPodCIDRs", podCIDRs, "oldPODCIDRs", n.podCIDRs)
    78  		klog.FlushAndExit(klog.ExitFlushTimeout, 1)
    79  	}
    80  }
    81  
    82  // OnNodeDelete is a handler for Node deletes.
    83  func (n *NodePodCIDRHandler) OnNodeDelete(node *v1.Node) {
    84  	klog.ErrorS(nil, "Current Node is being deleted", "node", klog.KObj(node))
    85  }
    86  
    87  // OnNodeSynced is a handler for Node syncs.
    88  func (n *NodePodCIDRHandler) OnNodeSynced() {}
    89  
    90  // NodeEligibleHandler handles the life cycle of the Node's eligibility, as
    91  // determined by the health server for directing load balancer traffic.
    92  type NodeEligibleHandler struct {
    93  	HealthServer *healthcheck.ProxierHealthServer
    94  }
    95  
    96  var _ config.NodeHandler = &NodeEligibleHandler{}
    97  
    98  // OnNodeAdd is a handler for Node creates.
    99  func (n *NodeEligibleHandler) OnNodeAdd(node *v1.Node) { n.HealthServer.SyncNode(node) }
   100  
   101  // OnNodeUpdate is a handler for Node updates.
   102  func (n *NodeEligibleHandler) OnNodeUpdate(_, node *v1.Node) { n.HealthServer.SyncNode(node) }
   103  
   104  // OnNodeDelete is a handler for Node deletes.
   105  func (n *NodeEligibleHandler) OnNodeDelete(node *v1.Node) { n.HealthServer.SyncNode(node) }
   106  
   107  // OnNodeSynced is a handler for Node syncs.
   108  func (n *NodeEligibleHandler) OnNodeSynced() {}
   109  

View as plain text