github.com/elfadel/cilium@v1.6.12/pkg/health/server/node.go (about)

     1  // Copyright 2018-2019 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 server
    16  
    17  import (
    18  	"github.com/cilium/cilium/api/v1/models"
    19  )
    20  
    21  type healthNode struct {
    22  	*models.NodeElement
    23  }
    24  
    25  // NewHealthNode creates a new node structure based on the specified model.
    26  func NewHealthNode(elem *models.NodeElement) healthNode {
    27  	return healthNode{
    28  		NodeElement: elem,
    29  	}
    30  }
    31  
    32  // PrimaryIP returns the primary IP address of the node.
    33  func (n *healthNode) PrimaryIP() string {
    34  	if n.NodeElement.PrimaryAddress.IPV4.Enabled {
    35  		return n.NodeElement.PrimaryAddress.IPV4.IP
    36  	}
    37  	return n.NodeElement.PrimaryAddress.IPV6.IP
    38  }
    39  
    40  // HealthIP returns the IP address of the health endpoint for the node.
    41  func (n *healthNode) HealthIP() string {
    42  	if n.NodeElement.HealthEndpointAddress == nil {
    43  		return ""
    44  	}
    45  	if n.NodeElement.HealthEndpointAddress.IPV4.Enabled {
    46  		return n.NodeElement.HealthEndpointAddress.IPV4.IP
    47  	}
    48  	return n.NodeElement.HealthEndpointAddress.IPV6.IP
    49  }
    50  
    51  // Addresses returns a map of the node's addresses -> "primary" bool
    52  func (n *healthNode) Addresses() map[*models.NodeAddressingElement]bool {
    53  	addresses := map[*models.NodeAddressingElement]bool{}
    54  	if n.NodeElement.PrimaryAddress != nil {
    55  		addr := n.NodeElement.PrimaryAddress
    56  		addresses[addr.IPV4] = addr.IPV4.Enabled
    57  		addresses[addr.IPV6] = addr.IPV6.Enabled
    58  	}
    59  	if n.NodeElement.HealthEndpointAddress != nil {
    60  		addresses[n.NodeElement.HealthEndpointAddress.IPV4] = false
    61  		addresses[n.NodeElement.HealthEndpointAddress.IPV6] = false
    62  	}
    63  	for _, elem := range n.NodeElement.SecondaryAddresses {
    64  		addresses[elem] = false
    65  	}
    66  	return addresses
    67  }