Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit a08ae87

Browse files
committed
pkg/*: Fix long cluster names causing pods to hand
This patch restricts the name of the cluster to less than 52 characters. This simpler approach is preferred so it can be documented. Operator log displays appropriate error for users. Fixes #1994
1 parent 65b572c commit a08ae87

5 files changed

Lines changed: 46 additions & 4 deletions

File tree

pkg/cluster/cluster.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ type Cluster struct {
8787

8888
func New(config Config, cl *api.EtcdCluster) *Cluster {
8989
lg := logrus.WithField("pkg", "cluster").WithField("cluster-name", cl.Name).WithField("cluster-namespace", cl.Namespace)
90+
if len(cl.Name) > k8sutil.MaxNameLength || len(cl.ClusterName) > k8sutil.MaxNameLength {
91+
return nil
92+
}
9093

9194
c := &Cluster{
9295
logger: lg,

pkg/cluster/cluster_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,19 @@ func TestUpdateEventUpdateLocalClusterObj(t *testing.T) {
5656
t.Errorf("expect version=%s, get=%s", newVersion, c.cluster.ResourceVersion)
5757
}
5858
}
59+
60+
func TestNewLongClusterName(t *testing.T) {
61+
clus := &api.EtcdCluster{
62+
TypeMeta: metav1.TypeMeta{
63+
APIVersion: api.SchemeGroupVersion.String(),
64+
},
65+
ObjectMeta: metav1.ObjectMeta{
66+
Name: "example-etcd-cluster123456789123456789123456789123456789123456",
67+
Namespace: metav1.NamespaceDefault,
68+
},
69+
}
70+
clus.SetClusterName("example-etcd-cluster123456789123456789123456789123456789123456")
71+
if c := New(Config{}, clus); c != nil {
72+
t.Errorf("expect c to be nil")
73+
}
74+
}

pkg/controller/controller.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ func (c *Controller) handleClusterEvent(event *Event) (bool, error) {
9292
}
9393

9494
nc := cluster.New(c.makeClusterConfig(), clus)
95-
95+
if nc == nil {
96+
return false, fmt.Errorf("cluster name cannot be more than %v characters long, please delete the CR\n", k8sutil.MaxNameLength)
97+
}
9698
c.clusters[getNamespacedName(clus)] = nc
9799

98100
clustersCreated.Inc()

pkg/controller/controller_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,24 @@ func TestHandleClusterEventNamespacedIgnored(t *testing.T) {
159159
t.Errorf("cluster should be ignored")
160160
}
161161
}
162+
163+
func TestHandleClusterEventWithLongClusterName(t *testing.T) {
164+
c := New(Config{})
165+
166+
clus := &api.EtcdCluster{
167+
ObjectMeta: metav1.ObjectMeta{
168+
Name: "example-etcd-cluster123456789123456789123456789123456789123456",
169+
},
170+
}
171+
e := &Event{
172+
Type: watch.Added,
173+
Object: clus,
174+
}
175+
if ignored, err := c.handleClusterEvent(e); !ignored {
176+
if err == nil {
177+
t.Errorf("err should not be nil")
178+
}
179+
} else {
180+
t.Errorf("cluster should not be ignored")
181+
}
182+
}

pkg/util/k8sutil/k8sutil.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ const (
5959

6060
randomSuffixLength = 10
6161
// k8s object name has a maximum length
62-
maxNameLength = 63 - randomSuffixLength - 1
62+
MaxNameLength = 63 - randomSuffixLength - 1
6363

6464
defaultBusyboxImage = "busybox:1.28.0-glibc"
6565

@@ -517,8 +517,8 @@ func mergeLabels(l1, l2 map[string]string) {
517517

518518
func UniqueMemberName(clusterName string) string {
519519
suffix := utilrand.String(randomSuffixLength)
520-
if len(clusterName) > maxNameLength {
521-
clusterName = clusterName[:maxNameLength]
520+
if len(clusterName) > MaxNameLength {
521+
clusterName = clusterName[:MaxNameLength]
522522
}
523523
return clusterName + "-" + suffix
524524
}

0 commit comments

Comments
 (0)