Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix get external address for the authentication service
- Loading branch information
Showing
3 changed files
with
72 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package discovery | ||
|
||
import ( | ||
"fmt" | ||
|
||
v1 "k8s.io/api/core/v1" | ||
"k8s.io/klog" | ||
) | ||
|
||
var preferOrder = []v1.NodeAddressType{ | ||
v1.NodeExternalDNS, | ||
v1.NodeExternalIP, | ||
v1.NodeInternalDNS, | ||
v1.NodeInternalIP, | ||
v1.NodeHostName, | ||
} | ||
|
||
// return an address from a Node pool | ||
func GetAddressFromNodeList(nodes []v1.Node) (string, error) { | ||
for _, addrType := range preferOrder { | ||
for _, node := range nodes { | ||
if addr, err := getAddressByType(&node, addrType); err != nil { | ||
klog.V(4).Info(err.Error()) | ||
continue | ||
} else { | ||
klog.V(4).Infof("found address %v with type %v", addr, addrType) | ||
return addr, nil | ||
} | ||
} | ||
} | ||
return "", fmt.Errorf("no address found") | ||
} | ||
|
||
// return an address for a Node | ||
func GetAddress(node *v1.Node) (string, error) { | ||
return GetAddressFromNodeList([]v1.Node{ | ||
*node, | ||
}) | ||
} | ||
|
||
func getAddressByType(node *v1.Node, addrType v1.NodeAddressType) (string, error) { | ||
for _, addr := range node.Status.Addresses { | ||
if addr.Type == addrType { | ||
return addr.Address, nil | ||
} | ||
} | ||
return "", fmt.Errorf("no address with type %v found in node %v", addrType, node.Name) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters