Skip to content

Commit 29396f9

Browse files
author
Murali Reddy
committed
add option '--hostname-override' to deal with cases where kubelet is launched
with --hostname-override flag Fixes #23
1 parent 4b3d1a3 commit 29396f9

File tree

8 files changed

+21
-32
lines changed

8 files changed

+21
-32
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ Alternatively you can download the prebuilt binary from https://github.com/cloud
4848
--cluster-asn ASN number under which cluster nodes will run iBGP
4949
--peer-asn ASN number of the BGP peer to which cluster nodes will advertise cluster ip and node's pod cidr
5050
--peer-router The ip address of the external router to which all nodes will peer and advertise the cluster ip and pod cidr's
51+
--hostname-override If non-empty, this string will be used as identification of node name instead of the actual hostname.
5152
```
5253

5354
### Try Kube-router with cluster installers

app/controllers/network_policy_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,7 @@ func NewNetworkPolicyController(clientset *kubernetes.Clientset, config *options
770770

771771
npc.syncPeriod = config.IPTablesSyncPeriod
772772

773-
node, err := utils.GetNodeObject(clientset)
773+
node, err := utils.GetNodeObject(clientset, config.HostnameOverride)
774774
if err != nil {
775775
panic(err.Error())
776776
}

app/controllers/network_routes_controller.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type NetworkRoutingController struct {
3535
asnNumber uint32
3636
peerAsnNumber uint32
3737
clusterCIDR string
38+
hostnameOverride string
3839
}
3940
var(
4041
activeNodes = make(map[string]bool)
@@ -49,7 +50,7 @@ func (nrc *NetworkRoutingController) Run(stopCh <-chan struct{}, wg *sync.WaitGr
4950
cidrlen, _ := cidr.Mask.Size()
5051
oldCidr := cidr.IP.String() + "/" + strconv.Itoa(cidrlen)
5152

52-
currentCidr, err := utils.GetPodCidrFromNodeSpec(nrc.clientset)
53+
currentCidr, err := utils.GetPodCidrFromNodeSpec(nrc.clientset, nrc.hostnameOverride)
5354
if err != nil {
5455
glog.Errorf("Failed to get pod CIDR from node spec: %s", err.Error())
5556
}
@@ -160,7 +161,7 @@ func (nrc *NetworkRoutingController) watchBgpUpdates() {
160161

161162
func (nrc *NetworkRoutingController) advertiseRoute() error {
162163

163-
cidr, err := utils.GetPodCidrFromNodeSpec(nrc.clientset)
164+
cidr, err := utils.GetPodCidrFromNodeSpec(nrc.clientset, nrc.hostnameOverride)
164165
if err != nil {
165166
return err
166167
}
@@ -357,8 +358,8 @@ func NewNetworkRoutingController(clientset *kubernetes.Clientset, kubeRouterConf
357358
nrc.peerAsnNumber = uint32(asn)
358359
}
359360

360-
361-
node, err := utils.GetNodeObject(clientset)
361+
nrc.hostnameOverride = kubeRouterConfig.HostnameOverride
362+
node, err := utils.GetNodeObject(clientset, nrc.hostnameOverride)
362363
if err != nil {
363364
panic(err.Error())
364365
}

app/controllers/network_services_controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -528,14 +528,14 @@ func NewNetworkServicesController(clientset *kubernetes.Clientset, config *optio
528528
}
529529

530530
if config.RunRouter {
531-
cidr, err := utils.GetPodCidrFromNodeSpec(nsc.client)
531+
cidr, err := utils.GetPodCidrFromNodeSpec(nsc.client, config.HostnameOverride)
532532
if err != nil {
533533
return nil, fmt.Errorf("Failed to get pod CIDR details from Node.spec: %s", err.Error())
534534
}
535535
nsc.podCidr = cidr
536536
}
537537

538-
node, err := utils.GetNodeObject(clientset)
538+
node, err := utils.GetNodeObject(clientset, config.HostnameOverride)
539539
if err != nil {
540540
panic(err.Error())
541541
}

app/options/options.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type KubeRouterConfig struct {
2323
PeerRouter string
2424
ClusterAsn string
2525
PeerAsn string
26+
HostnameOverride string
2627
}
2728

2829
func NewKubeRouterConfig() *KubeRouterConfig {
@@ -54,4 +55,5 @@ func (s *KubeRouterConfig) AddFlags(fs *pflag.FlagSet) {
5455
fs.StringVar(&s.PeerRouter, "peer-router", s.PeerRouter, "The ip address of the external router to which all nodes will peer and advertise the cluster ip and pod cidr's")
5556
fs.StringVar(&s.ClusterAsn, "cluster-asn", s.ClusterAsn, "ASN number under which cluster nodes will run iBGP")
5657
fs.StringVar(&s.PeerAsn, "peer-asn", s.PeerAsn, "ASN number of the BGP peer to which cluster nodes will advertise cluster ip and node's pod cidr")
58+
fs.StringVar(&s.HostnameOverride, "hostname-override", s.HostnameOverride, "If non-empty, will use this string as identification instead of the actual hostname.")
5759
}

utils/fqdn.go

Lines changed: 0 additions & 16 deletions
This file was deleted.

utils/node.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1010
)
1111

12-
func GetNodeObject(clientset *kubernetes.Clientset) (*apiv1.Node, error) {
12+
func GetNodeObject(clientset *kubernetes.Clientset, hostnameOverride string) (*apiv1.Node, error) {
1313

1414
// assuming kube-router is running as pod, first check env NODE_NAME
1515
nodeName := os.Getenv("NODE_NAME")
@@ -27,12 +27,13 @@ func GetNodeObject(clientset *kubernetes.Clientset) (*apiv1.Node, error) {
2727
return node, nil
2828
}
2929

30-
// if env NODE_NAME is not set then check if node is registerd by FQDN
31-
fqdnHostName := GetFqdn()
32-
node, err = clientset.Core().Nodes().Get(fqdnHostName, metav1.GetOptions{})
33-
if err == nil {
34-
return node, nil
30+
// if env NODE_NAME is not set and node is not registered with hostname, then use host name override
31+
if hostnameOverride != "" {
32+
node, err = clientset.Core().Nodes().Get(hostnameOverride, metav1.GetOptions{})
33+
if err == nil {
34+
return node, nil
35+
}
3536
}
3637

37-
return nil, fmt.Errorf("Failed to identify the node by NODE_NAME, hostname or FQDN name of the host")
38+
return nil, fmt.Errorf("Failed to identify the node by NODE_NAME, hostname or --hostname-override")
3839
}

utils/pod_cidr.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ func InsertPodCidrInCniSpec(cniConfFilePath string, cidr string) error {
4545
return nil
4646
}
4747

48-
func GetPodCidrFromNodeSpec(clientset *kubernetes.Clientset) (string, error) {
49-
node, err := GetNodeObject(clientset)
48+
func GetPodCidrFromNodeSpec(clientset *kubernetes.Clientset, hostnameOverride string) (string, error) {
49+
node, err := GetNodeObject(clientset, hostnameOverride)
5050
if err != nil {
5151
return "", fmt.Errorf("Failed to get pod CIDR allocated for the node due to: " + err.Error())
5252
}

0 commit comments

Comments
 (0)