diff --git a/src/k8s/pkg/k8sd/api/cluster_tokens.go b/src/k8s/pkg/k8sd/api/cluster_tokens.go index a03bc7371..81b78fb8b 100644 --- a/src/k8s/pkg/k8sd/api/cluster_tokens.go +++ b/src/k8s/pkg/k8sd/api/cluster_tokens.go @@ -31,7 +31,7 @@ func postClusterJoinTokens(m *microcluster.MicroCluster, s *state.State, r *http if req.Worker { token, err = createWorkerToken(s) } else { - token, err = m.NewJoinToken(hostname) + token, err = getOrCreateJoinToken(m, hostname) } if err != nil { return response.InternalError(fmt.Errorf("failed to create token: %w", err)) @@ -40,6 +40,28 @@ func postClusterJoinTokens(m *microcluster.MicroCluster, s *state.State, r *http return response.SyncResponse(true, &apiv1.TokensResponse{EncodedToken: token}) } +func getOrCreateJoinToken(m *microcluster.MicroCluster, tokenName string) (string, error) { + // grab token if it exists and return it + records, err := m.ListJoinTokens() + if err != nil { + fmt.Println("Failed to get existing tokens. Trying to create a new token.") + } else { + for _, record := range records { + if record.Name == tokenName { + return record.Token, nil + } + } + fmt.Println("No token exists yet. Creating a new token.") + } + + // if token does not exist, create a new one + token, err := m.NewJoinToken(tokenName) + if err != nil { + return "", fmt.Errorf("failed to generate a new microcluster join token: %w", err) + } + return token, nil +} + func createWorkerToken(s *state.State) (string, error) { var token string if err := s.Database.Transaction(s.Context, func(ctx context.Context, tx *sql.Tx) error {