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), "node.vexxhost.com/product": slug.Make(product.Name),
} }
if checkComputeNode() { if CheckComputeNode(nodeName) {
labels["node-role.openstack.org"] = "compute" labels["node-role.openstack.org"] = "compute"
} }
@ -91,26 +91,15 @@ func main() {
} }
} }
// checkComputeNode checks if the current node is a compute node. // checkComputeSubstring checks if the node name includes compute substring.
func checkComputeNode() bool { func CheckComputeNode(nodeName string) 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 {
compute_tag := []string{ compute_tag := []string{
"kvm", "kvm",
"compute", "compute",
} }
for _, sub := range compute_tag { for _, sub := range compute_tag {
if strings.Contains(label, sub) { if strings.Contains(nodeName, sub) {
return true return true
} }
} }

View File

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