Skip to content

Commit

Permalink
Merge pull request #4 from topfreegames/fix/token-creation
Browse files Browse the repository at this point in the history
fix: token creation in all flows
  • Loading branch information
csmartins authored Apr 6, 2023
2 parents 92c34bc + 135d0f0 commit 2755f3f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
8 changes: 6 additions & 2 deletions pkg/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{})
Expand Down
4 changes: 2 additions & 2 deletions pkg/util/k8sutil/k8sutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down
21 changes: 17 additions & 4 deletions pkg/util/k8sutil/k8sutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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)
}
}

0 comments on commit 2755f3f

Please sign in to comment.