-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move NewPvcName func to k8sutil package
Signed-off-by: Clément Nussbaumer <clement.nussbaumer@postfinance.ch>
- Loading branch information
1 parent
45c822c
commit 39dfe4e
Showing
6 changed files
with
108 additions
and
108 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,30 @@ | ||
package k8sutil | ||
|
||
import "fmt" | ||
|
||
const nameSuffix = "-pvcmigrate" | ||
|
||
// if the length after adding the suffix is more than 63 characters, we need to reduce that to fit within k8s limits | ||
// pruning from the end runs the risk of dropping the '0'/'1'/etc of a statefulset's PVC name | ||
// pruning from the front runs the risk of making a-replica-... and b-replica-... collide | ||
// so this removes characters from the middle of the string | ||
func NewPvcName(originalName string) string { | ||
candidate := originalName + nameSuffix | ||
if len(candidate) <= 253 { | ||
return candidate | ||
} | ||
|
||
// remove characters from the middle of the string to reduce the total length to 63 characters | ||
newCandidate := candidate[0:100] + candidate[len(candidate)-153:] | ||
return newCandidate | ||
} | ||
|
||
// NewPrefixedName returns a name prefixed by prefix and with length that is no longer than 63 | ||
// chars | ||
func NewPrefixedName(prefix, original string) string { | ||
newName := fmt.Sprintf("%s-%s", prefix, original) | ||
if len(newName) > 63 { | ||
newName = newName[0:31] + newName[len(newName)-32:] | ||
} | ||
return newName | ||
} |
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,67 @@ | ||
package k8sutil | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_newPvcName(t *testing.T) { | ||
tests := []struct { | ||
originalName string | ||
want string | ||
}{ | ||
{ | ||
originalName: "abc", | ||
want: "abc-pvcmigrate", | ||
}, | ||
{ | ||
originalName: "very very very 253longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglong name test with a suffix that might be the only unique part of it 0", | ||
want: "very very very 253longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglongloonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglong name test with a suffix that might be the only unique part of it 0-pvcmigrate", | ||
}, | ||
{ | ||
originalName: "0 very very very 253longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglong name test with a prefix that might be the only unique part of it", | ||
want: "0 very very very 253longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglongglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglong name test with a prefix that might be the only unique part of it-pvcmigrate", | ||
}, | ||
{ | ||
originalName: "253 character (after suffix) 253longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglong name is untouched paddin", | ||
want: "253 character (after suffix) 253longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglong name is untouched paddin-pvcmigrate", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.originalName, func(t *testing.T) { | ||
req := require.New(t) | ||
got := NewPvcName(tt.originalName) | ||
req.Equal(tt.want, got) | ||
}) | ||
} | ||
} | ||
|
||
func TestNewPrefixedName(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
originalName string | ||
prefix string | ||
want string | ||
}{ | ||
{ | ||
name: "when name is < 63 chars expect new name to be prefixed", | ||
originalName: "abc", | ||
prefix: "pvcmigrate", | ||
want: "pvcmigrate-abc", | ||
}, | ||
{ | ||
name: "when name is > 63 chars expect new name to be prefixed and 63 chars long", | ||
originalName: "this label will exceed its allowed length and than be truncated", | ||
prefix: "pvcmigrate", | ||
want: "pvcmigrate-this label will excewed length and than be truncated", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.originalName, func(t *testing.T) { | ||
req := require.New(t) | ||
got := NewPrefixedName(tt.prefix, tt.originalName) | ||
req.Equal(tt.want, got) | ||
}) | ||
} | ||
} |
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