Fix getting node name

Change-Id: Id34db7684a132a3e5eb108b4d5e925714e3b1fec
This commit is contained in:
okozachenko 2020-07-23 20:00:54 +03:00
parent 9f265beb2b
commit 977f62ba56
2 changed files with 11 additions and 22 deletions

19
main.go
View File

@ -64,7 +64,7 @@ func main() {
"node.vexxhost.com/product": slug.Make(product.Name),
}
if checkComputeNode() {
if CheckComputeNode(nodeName) {
labels["node-role.openstack.org"] = "compute"
}
@ -91,26 +91,15 @@ func main() {
}
}
// checkComputeNode checks if the current node is a compute node.
func checkComputeNode() bool {
hostname, err := os.Hostname()
if err != nil {
log.Fatal(err.Error())
}
// Now there is only one check logic for now using hostname
return CheckComputeSubstring(hostname)
}
// checkComputeSubstring checks if the string includes compute substring.
func CheckComputeSubstring(label string) bool {
// checkComputeSubstring checks if the node name includes compute substring.
func CheckComputeNode(nodeName string) bool {
compute_tag := []string{
"kvm",
"compute",
}
for _, sub := range compute_tag {
if strings.Contains(label, sub) {
if strings.Contains(nodeName, sub) {
return true
}
}

View File

@ -6,28 +6,28 @@ import (
"github.com/stretchr/testify/assert"
)
func assertCheckComputeSubstring(t *testing.T, label string, expected bool) {
res := CheckComputeSubstring(label)
func assertCheckComputeNode(t *testing.T, label string, expected bool) {
res := CheckComputeNode(label)
assert.Equal(t, expected, res)
}
func TestCheckComputeSubstringWithkvmHostname(t *testing.T) {
hostname := string("kvm10.specterops.iad1.vexxhost.net")
expected := bool(true)
assertCheckComputeSubstring(t, hostname, expected)
assertCheckComputeNode(t, hostname, expected)
}
func TestCheckComputeSubstringWithcomputeHostname(t *testing.T) {
func TestCheckComputeNodeWithcomputeHostname(t *testing.T) {
hostname := string("compute.specterops.iad1.vexxhost.net")
expected := bool(true)
assertCheckComputeSubstring(t, hostname, expected)
assertCheckComputeNode(t, hostname, expected)
}
func TestCheckComputeSubstringWithNoneComputeHostname(t *testing.T) {
func TestCheckComputeNodeWithNoneComputeHostname(t *testing.T) {
hostname := string("ctl1.specterops.iad1.vexxhost.net")
expected := bool(false)
assertCheckComputeSubstring(t, hostname, expected)
assertCheckComputeNode(t, hostname, expected)
}