diff --git a/pkg/cluster/cluster.go b/pkg/cluster/cluster.go index 8de195dfa..13742fd63 100644 --- a/pkg/cluster/cluster.go +++ b/pkg/cluster/cluster.go @@ -30,7 +30,6 @@ import ( "github.com/coreos/etcd-operator/pkg/util/k8sutil" "github.com/coreos/etcd-operator/pkg/util/retryutil" - "github.com/pborman/uuid" "github.com/sirupsen/logrus" v1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" @@ -404,7 +403,12 @@ func (c *Cluster) isPodPVEnabled() bool { } func (c *Cluster) createPod(ctx context.Context, members etcdutil.MemberSet, m *etcdutil.Member, state string) error { - pod, err := k8sutil.NewEtcdPod(ctx, c.config.KubeCli, m, members.PeerURLPairs(), c.cluster.Name, c.cluster.Namespace, state, uuid.New(), c.cluster.Spec, c.cluster.AsOwner()) + token, err := k8sutil.CreateToken(c.cluster.Spec) + if err != nil { + return err + } + + pod, err := k8sutil.NewEtcdPod(ctx, c.config.KubeCli, m, members.PeerURLPairs(), c.cluster.Name, c.cluster.Namespace, state, token, c.cluster.Spec, c.cluster.AsOwner()) if c.isPodPVEnabled() { pvc := k8sutil.NewEtcdPodPVC(m, *c.cluster.Spec.Pod.PersistentVolumeClaimSpec, c.cluster.Name, c.cluster.Namespace, c.cluster.AsOwner()) _, err := c.config.KubeCli.CoreV1().PersistentVolumeClaims(c.cluster.Namespace).Create(ctx, pvc, metav1.CreateOptions{}) diff --git a/pkg/util/k8sutil/k8sutil.go b/pkg/util/k8sutil/k8sutil.go index 4459ca717..6c3055875 100644 --- a/pkg/util/k8sutil/k8sutil.go +++ b/pkg/util/k8sutil/k8sutil.go @@ -313,7 +313,7 @@ func addOwnerRefToObject(o metav1.Object, r metav1.OwnerReference) { o.SetOwnerReferences(append(o.GetOwnerReferences(), r)) } -func createToken(clusterSpec api.ClusterSpec) (string, error) { +func CreateToken(clusterSpec api.ClusterSpec) (string, error) { if clusterSpec.ClusteringMode == "discovery" { if clusterSpec.ClusterToken == "" { return "", ErrDiscoveryTokenNotProvided @@ -328,7 +328,7 @@ func createToken(clusterSpec api.ClusterSpec) (string, error) { // NewSeedMemberPod returns a Pod manifest for a seed member. // It's special that it has new token, and might need recovery init containers func NewSeedMemberPod(ctx context.Context, kubecli kubernetes.Interface, clusterName, clusterNamespace string, ms etcdutil.MemberSet, m *etcdutil.Member, cs api.ClusterSpec, owner metav1.OwnerReference, backupURL *url.URL) (*v1.Pod, error) { - token, err := createToken(cs) + token, err := CreateToken(cs) if err != nil { return nil, err } diff --git a/pkg/util/k8sutil/k8sutils_test.go b/pkg/util/k8sutil/k8sutils_test.go index 1eacf7296..405fb4398 100644 --- a/pkg/util/k8sutil/k8sutils_test.go +++ b/pkg/util/k8sutil/k8sutils_test.go @@ -169,7 +169,7 @@ func TestCreateTokenLocalCluster(t *testing.T) { ClusterToken: "testtoken", } - token, _ := createToken(*clusterSpec) + token, _ := CreateToken(*clusterSpec) if token == "testtoken" { t.Errorf("token should be a randon uuid, instead got %s", token) @@ -182,7 +182,7 @@ func TestCreateTokenDiscoveryClusterNoTokenSent(t *testing.T) { ClusteringMode: "discovery", } - _, err := createToken(*clusterSpec) + _, err := CreateToken(*clusterSpec) if err == nil { t.Errorf("Expected an error to be thrown when discovery mode on and no token is set") @@ -196,7 +196,7 @@ func TestCreateTokenDiscoveryClusterTokenEmpty(t *testing.T) { ClusterToken: "", } - _, err := createToken(*clusterSpec) + _, err := CreateToken(*clusterSpec) if err == nil { t.Errorf("Expected an error to be thrown when discovery mode on and no token is set") @@ -210,9 +210,22 @@ func TestCreateTokenDistributedCluster(t *testing.T) { ClusterToken: "testtoken", } - token, _ := createToken(*clusterSpec) + token, _ := CreateToken(*clusterSpec) if token != "testtoken" { t.Errorf("expected token=%s, got=%s", clusterSpec.ClusterToken, token) } } + +func TestCreateTokenNoMode(t *testing.T) { + clusterSpec := &api.ClusterSpec{ + Size: 1, + ClusterToken: "testtoken", + } + + token, _ := CreateToken(*clusterSpec) + + if token == "testtoken" { + t.Errorf("expected random uiid token, got=%s", clusterSpec.ClusterToken) + } +} \ No newline at end of file