Skip to content

Commit 94de293

Browse files
James SharpeDavid Conde
authored andcommitted
fix: Handle shortening cluster names for clusters named by their FQDN. (jenkins-x#3758)
Closes jenkins-x#3755 Signed-off-by: James Sharpe <[email protected]>
1 parent b66dd09 commit 94de293

File tree

3 files changed

+20
-28
lines changed

3 files changed

+20
-28
lines changed

pkg/kube/cluster/cluster.go

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package cluster
33
import (
44
"strings"
55

6-
"github.com/jenkins-x/jx/pkg/kube"
76
"github.com/pkg/errors"
87
"k8s.io/client-go/tools/clientcmd/api"
8+
9+
"github.com/jenkins-x/jx/pkg/kube"
910
)
1011

1112
// Name gets the cluster name from the current context
@@ -43,24 +44,7 @@ func ShortName(kuber kube.Kuber) (string, error) {
4344
if err != nil {
4445
return "", errors.Wrap(err, "retrieving the cluster name")
4546
}
46-
return ShortClusterName(clusterName), nil
47-
}
48-
49-
// ShortClusterName shrinks the cluster name
50-
func ShortClusterName(clusterName string) string {
51-
return ShortNameN(clusterName, 16)
52-
}
53-
54-
// ShortNameN shrinks the name to a max length
55-
func ShortNameN(clusterName string, maxLen int) string {
56-
shortClusterName := clusterName
57-
if len(clusterName) > maxLen {
58-
shortClusterName = clusterName[0:maxLen]
59-
}
60-
if strings.HasSuffix(shortClusterName, "_") || strings.HasSuffix(shortClusterName, "-") {
61-
shortClusterName = shortClusterName[0 : len(shortClusterName)-1]
62-
}
63-
return shortClusterName
47+
return kube.ToValidNameTruncated(clusterName, 16), nil
6448
}
6549

6650
// SimplifiedClusterName get the simplified cluster name from the long-winded context cluster name that gets generated

pkg/kube/cluster/cluster_test.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package cluster_test
22

33
import (
4-
"github.com/jenkins-x/jx/pkg/kube/vault"
54
"testing"
65

7-
"github.com/jenkins-x/jx/pkg/kube/cluster"
8-
kube_test "github.com/jenkins-x/jx/pkg/kube/mocks"
96
. "github.com/petergtz/pegomock"
107
"github.com/stretchr/testify/assert"
118
"k8s.io/client-go/tools/clientcmd/api"
9+
10+
"github.com/jenkins-x/jx/pkg/kube/cluster"
11+
kube_test "github.com/jenkins-x/jx/pkg/kube/mocks"
12+
"github.com/jenkins-x/jx/pkg/kube/vault"
1213
)
1314

1415
func TestGetSimplifiedClusterName(t *testing.T) {
@@ -41,6 +42,9 @@ func TestShortClusterName(t *testing.T) {
4142
},
4243
}
4344
When(kuber.LoadConfig()).ThenReturn(&config, nil, nil)
45+
46+
clusterName, err = cluster.ShortName(kuber)
47+
4448
assert.NoError(t, err)
4549
assert.Equal(t, "short-cluster-na", clusterName)
4650

@@ -51,6 +55,9 @@ func TestShortClusterName(t *testing.T) {
5155
},
5256
}
5357
When(kuber.LoadConfig()).ThenReturn(&config, nil, nil)
58+
59+
clusterName, err = cluster.ShortName(kuber)
60+
5461
assert.NoError(t, err)
5562
assert.Equal(t, "short-cluster-na", clusterName)
5663
}

pkg/kube/vault/vault.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@ import (
55

66
"github.com/banzaicloud/bank-vaults/operator/pkg/apis/vault/v1alpha1"
77
"github.com/banzaicloud/bank-vaults/operator/pkg/client/clientset/versioned"
8+
"github.com/pkg/errors"
9+
v1 "k8s.io/api/core/v1"
10+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11+
"k8s.io/client-go/kubernetes"
12+
813
"github.com/jenkins-x/jx/pkg/kube"
914
"github.com/jenkins-x/jx/pkg/kube/cluster"
1015
"github.com/jenkins-x/jx/pkg/kube/serviceaccount"
1116
"github.com/jenkins-x/jx/pkg/kube/services"
1217
"github.com/jenkins-x/jx/pkg/vault"
13-
"github.com/pkg/errors"
14-
v1 "k8s.io/api/core/v1"
15-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
16-
"k8s.io/client-go/kubernetes"
1718
)
1819

1920
const (
@@ -137,9 +138,9 @@ func SystemVaultName(kuber kube.Kuber) (string, error) {
137138

138139
// SystemVaultNameForCluster returns the system vault name from a given cluster name
139140
func SystemVaultNameForCluster(clusterName string) string {
140-
shortClusterName := cluster.ShortClusterName(clusterName)
141+
shortClusterName := kube.ToValidNameTruncated(clusterName, 16)
141142
fullName := fmt.Sprintf("%s-%s", vault.SystemVaultNamePrefix, shortClusterName)
142-
return cluster.ShortNameN(fullName, 22)
143+
return kube.ToValidNameTruncated(fullName, 22)
143144
}
144145

145146
// CreateGKEVault creates a new vault backed by GCP KMS and storage

0 commit comments

Comments
 (0)