Merge pull request #2096 from travisn/translate-mon-id

Refactor mon naming for use by other daemons
This commit is contained in:
Travis Nielsen authored and GitHub committed 2018-09-06 15:53:48 -06:00
commit 94a7b4ebda
5 files changed
+117 -78

No files matched your search

+1 -1
View File
@@ -228,7 +228,7 @@ func (c *Cluster) initMonConfig(size int) []*monConfig {
}
func newMonConfig(monID int) *monConfig {
daemonName := indexToName(monID)
daemonName := k8sutil.IndexToName(monID)
return &monConfig{ResourceName: resourceName(daemonName), DaemonName: daemonName, Port: int32(mondaemon.DefaultPort)}
}
+1 -35
View File
@@ -259,27 +259,11 @@ func extractKey(contents string) (string, error) {
return secret, nil
}
// convert an index to a mon name based on as few letters of the alphabet as possible
func indexToName(index int) string {
var result string
for {
i := index % maxPerChar
c := 'z' - maxPerChar + i + 1
result = fmt.Sprintf("%c%s", c, result)
if index < maxPerChar {
break
}
// subtract 1 since the character conversion is zero-based
index = (index / maxPerChar) - 1
}
return result
}
// convert the mon name to the numeric mon ID
func fullNameToIndex(name string) (int, error) {
prefix := appName + "-"
if strings.Index(name, prefix) != -1 && len(prefix) < len(name) {
return nameToIndex(name[len(prefix)+1:])
return k8sutil.NameToIndex(name[len(prefix)+1:])
}
// attempt to parse the legacy mon name
@@ -293,21 +277,3 @@ func fullNameToIndex(name string) (int, error) {
}
return id, nil
}
func nameToIndex(name string) (int, error) {
factor := 1
for i := 1; i < len(name); i++ {
factor *= maxPerChar
}
var result int
for _, c := range name {
charVal := int('z' - c + 1)
if charVal < 0 || charVal >= maxPerChar {
return -1, fmt.Errorf("invalid char '%c' in %s", c, name)
}
result += charVal * factor
factor /= maxPerChar
}
return result, nil
}
@@ -1,42 +0,0 @@
/*
Copyright 2018 The Rook Authors. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package mon
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestConvertMonID(t *testing.T) {
assert.Equal(t, "a", indexToName(0))
assert.Equal(t, "b", indexToName(1))
assert.Equal(t, "c", indexToName(2))
assert.Equal(t, "z", indexToName(25))
assert.Equal(t, "aa", indexToName(26))
assert.Equal(t, "ab", indexToName(27))
assert.Equal(t, "ac", indexToName(28))
assert.Equal(t, "az", indexToName(51))
assert.Equal(t, "ba", indexToName(52))
assert.Equal(t, "bb", indexToName(53))
assert.Equal(t, "bz", indexToName(77))
assert.Equal(t, "ca", indexToName(78))
assert.Equal(t, "za", indexToName(676))
assert.Equal(t, "aaa", indexToName(702))
assert.Equal(t, "aaz", indexToName(727))
assert.Equal(t, "aba", indexToName(728))
}
+67
View File
@@ -0,0 +1,67 @@
/*
Copyright 2018 The Rook Authors. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package k8sutil
import "fmt"
const (
maxPerChar = 26
)
// IndexToName converts an index to a daemon name based on as few letters of the alphabet as possible.
// For example:
// 0 -> a
// 1 -> b
// 25 -> z
// 26 -> aa
func IndexToName(index int) string {
var result string
for {
i := index % maxPerChar
c := 'z' - maxPerChar + i + 1
result = fmt.Sprintf("%c%s", c, result)
if index < maxPerChar {
break
}
// subtract 1 since the character conversion is zero-based
index = (index / maxPerChar) - 1
}
return result
}
// NameToIndex converts a daemon name to an index, which is the inverse of IndexToName
// For example:
// a -> 0
// b -> 1
func NameToIndex(name string) (int, error) {
factor := 1
for i := 1; i < len(name); i++ {
factor *= maxPerChar
}
var result int
for _, c := range name {
charVal := int(maxPerChar - ('z' - c))
if charVal < 1 || charVal > maxPerChar {
return -1, fmt.Errorf("invalid char '%c' (%d) in %s", c, charVal, name)
}
if factor == 1 {
// The least significant letter needs to be 0-based so we subtract 1
result += charVal - 1
} else {
result += charVal * factor
}
factor /= maxPerChar
}
return result, nil
}
+48
View File
@@ -0,0 +1,48 @@
/*
Copyright 2018 The Rook Authors. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package k8sutil
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestConvertDaemonID(t *testing.T) {
testConvertDaemonName(t, "a", 0)
testConvertDaemonName(t, "b", 1)
testConvertDaemonName(t, "c", 2)
testConvertDaemonName(t, "z", 25)
testConvertDaemonName(t, "aa", 26)
testConvertDaemonName(t, "ab", 27)
testConvertDaemonName(t, "ac", 28)
testConvertDaemonName(t, "az", 51)
testConvertDaemonName(t, "ba", 52)
testConvertDaemonName(t, "bb", 53)
testConvertDaemonName(t, "bz", 77)
testConvertDaemonName(t, "ca", 78)
testConvertDaemonName(t, "za", 676)
testConvertDaemonName(t, "aaa", 702)
testConvertDaemonName(t, "aaz", 727)
testConvertDaemonName(t, "aba", 728)
}
func testConvertDaemonName(t *testing.T, name string, index int) {
// test that the conversion is valid from int to string
assert.Equal(t, name, IndexToName(index))
// test that the inverse conversion is valid from string to int
i, err := NameToIndex(name)
assert.Nil(t, err)
assert.Equal(t, index, i)
}