diff --git a/pkg/cluster/cluster.go b/pkg/cluster/cluster.go index 7730334ee..2f8322a37 100644 --- a/pkg/cluster/cluster.go +++ b/pkg/cluster/cluster.go @@ -87,6 +87,9 @@ type Cluster struct { func New(config Config, cl *api.EtcdCluster) *Cluster { lg := logrus.WithField("pkg", "cluster").WithField("cluster-name", cl.Name).WithField("cluster-namespace", cl.Namespace) + if len(cl.Name) > k8sutil.MaxNameLength || len(cl.ClusterName) > k8sutil.MaxNameLength { + return nil + } c := &Cluster{ logger: lg, diff --git a/pkg/cluster/cluster_test.go b/pkg/cluster/cluster_test.go index c3d88d723..8c0786997 100644 --- a/pkg/cluster/cluster_test.go +++ b/pkg/cluster/cluster_test.go @@ -56,3 +56,19 @@ func TestUpdateEventUpdateLocalClusterObj(t *testing.T) { t.Errorf("expect version=%s, get=%s", newVersion, c.cluster.ResourceVersion) } } + +func TestNewLongClusterName(t *testing.T) { + clus := &api.EtcdCluster{ + TypeMeta: metav1.TypeMeta{ + APIVersion: api.SchemeGroupVersion.String(), + }, + ObjectMeta: metav1.ObjectMeta{ + Name: "example-etcd-cluster123456789123456789123456789123456789123456", + Namespace: metav1.NamespaceDefault, + }, + } + clus.SetClusterName("example-etcd-cluster123456789123456789123456789123456789123456") + if c := New(Config{}, clus); c != nil { + t.Errorf("expect c to be nil") + } +} diff --git a/pkg/controller/controller.go b/pkg/controller/controller.go index 2426cf6d0..8bdf1f49e 100644 --- a/pkg/controller/controller.go +++ b/pkg/controller/controller.go @@ -92,7 +92,9 @@ func (c *Controller) handleClusterEvent(event *Event) (bool, error) { } nc := cluster.New(c.makeClusterConfig(), clus) - + if nc == nil { + return false, fmt.Errorf("cluster name cannot be more than %v characters long, please delete the CR\n", k8sutil.MaxNameLength) + } c.clusters[getNamespacedName(clus)] = nc clustersCreated.Inc() diff --git a/pkg/controller/controller_test.go b/pkg/controller/controller_test.go index 43d2d4cec..90ba798f7 100644 --- a/pkg/controller/controller_test.go +++ b/pkg/controller/controller_test.go @@ -159,3 +159,24 @@ func TestHandleClusterEventNamespacedIgnored(t *testing.T) { t.Errorf("cluster should be ignored") } } + +func TestHandleClusterEventWithLongClusterName(t *testing.T) { + c := New(Config{}) + + clus := &api.EtcdCluster{ + ObjectMeta: metav1.ObjectMeta{ + Name: "example-etcd-cluster123456789123456789123456789123456789123456", + }, + } + e := &Event{ + Type: watch.Added, + Object: clus, + } + if ignored, err := c.handleClusterEvent(e); !ignored { + if err == nil { + t.Errorf("err should not be nil") + } + } else { + t.Errorf("cluster should not be ignored") + } +} diff --git a/pkg/util/k8sutil/k8sutil.go b/pkg/util/k8sutil/k8sutil.go index 39f00a088..4f7f4e73a 100644 --- a/pkg/util/k8sutil/k8sutil.go +++ b/pkg/util/k8sutil/k8sutil.go @@ -59,7 +59,7 @@ const ( randomSuffixLength = 10 // k8s object name has a maximum length - maxNameLength = 63 - randomSuffixLength - 1 + MaxNameLength = 63 - randomSuffixLength - 1 defaultBusyboxImage = "busybox:1.28.0-glibc" @@ -517,8 +517,8 @@ func mergeLabels(l1, l2 map[string]string) { func UniqueMemberName(clusterName string) string { suffix := utilrand.String(randomSuffixLength) - if len(clusterName) > maxNameLength { - clusterName = clusterName[:maxNameLength] + if len(clusterName) > MaxNameLength { + clusterName = clusterName[:MaxNameLength] } return clusterName + "-" + suffix }