From 6a335ebe8a86de08debf326369eb2e3130d06ef8 Mon Sep 17 00:00:00 2001 From: travisn Date: Wed, 5 Sep 2018 15:34:25 -0600 Subject: [PATCH] mon: refactor mon naming for use by other daemons Signed-off-by: travisn --- pkg/operator/ceph/cluster/mon/mon.go | 2 +- pkg/operator/ceph/cluster/mon/util.go | 36 +----------- pkg/operator/ceph/cluster/mon/util_test.go | 42 -------------- pkg/operator/k8sutil/name.go | 67 ++++++++++++++++++++++ pkg/operator/k8sutil/name_test.go | 48 ++++++++++++++++ 5 files changed, 117 insertions(+), 78 deletions(-) delete mode 100644 pkg/operator/ceph/cluster/mon/util_test.go create mode 100644 pkg/operator/k8sutil/name.go create mode 100644 pkg/operator/k8sutil/name_test.go diff --git a/pkg/operator/ceph/cluster/mon/mon.go b/pkg/operator/ceph/cluster/mon/mon.go index 97bdf8674..bfc7eef93 100644 --- a/pkg/operator/ceph/cluster/mon/mon.go +++ b/pkg/operator/ceph/cluster/mon/mon.go @@ -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)} } diff --git a/pkg/operator/ceph/cluster/mon/util.go b/pkg/operator/ceph/cluster/mon/util.go index d24eafaa1..1fd485388 100644 --- a/pkg/operator/ceph/cluster/mon/util.go +++ b/pkg/operator/ceph/cluster/mon/util.go @@ -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 - -} diff --git a/pkg/operator/ceph/cluster/mon/util_test.go b/pkg/operator/ceph/cluster/mon/util_test.go deleted file mode 100644 index 569eb909d..000000000 --- a/pkg/operator/ceph/cluster/mon/util_test.go +++ /dev/null @@ -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)) -} diff --git a/pkg/operator/k8sutil/name.go b/pkg/operator/k8sutil/name.go new file mode 100644 index 000000000..d2129e3e0 --- /dev/null +++ b/pkg/operator/k8sutil/name.go @@ -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 + +} diff --git a/pkg/operator/k8sutil/name_test.go b/pkg/operator/k8sutil/name_test.go new file mode 100644 index 000000000..bf2e457f4 --- /dev/null +++ b/pkg/operator/k8sutil/name_test.go @@ -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) +}