Skip to content

Commit

Permalink
De-MC the repo.
Browse files Browse the repository at this point in the history
  • Loading branch information
Miles-Garnsey committed May 2, 2024
1 parent 0963337 commit 3a73b93
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 27 deletions.
12 changes: 6 additions & 6 deletions cmd/kubectl-k8ssandra/register/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ import (
"k8s.io/cli-runtime/pkg/genericclioptions"
)

var (
StaticNamespace = "mission-control"
)

var RegisterClusterCmd = &cobra.Command{
Use: "register [flags]",
Short: "register a data plane into the control plane.",
Expand All @@ -27,8 +23,10 @@ func Init(cmd *cobra.Command, streams genericclioptions.IOStreams) {
"path to destination cluster's kubeconfig file - defaults to KUBECONFIG then ~/.kube/config")
RegisterClusterCmd.Flags().String("source-context", "", "context name for source cluster")
RegisterClusterCmd.Flags().String("dest-context", "", "context name for destination cluster")
RegisterClusterCmd.Flags().String("serviceaccount-name", "mission-control", "serviceaccount name for destination cluster")
RegisterClusterCmd.Flags().String("destination-name", "remote-mission-control", "name for remote clientConfig and secret on destination cluster")
RegisterClusterCmd.Flags().String("source-namespace", "", "namespace containing service account for source cluster")
RegisterClusterCmd.Flags().String("dest-namespace", "", "namespace where secret and clientConfig will be created on destination cluster")
RegisterClusterCmd.Flags().String("serviceaccount-name", "k8ssandra-operator", "serviceaccount name for destination cluster")
RegisterClusterCmd.Flags().String("destination-name", "remote-k8ssandra-operator", "name for remote clientConfig and secret on destination cluster")
cmd.AddCommand(RegisterClusterCmd)
}

Expand Down Expand Up @@ -57,6 +55,8 @@ func NewRegistrationExecutorFromRegisterClusterCmd(cmd cobra.Command) *Registrat
DestKubeconfig: cmd.Flag("dest-kubeconfig").Value.String(),
SourceContext: cmd.Flag("source-context").Value.String(),
DestContext: cmd.Flag("dest-context").Value.String(),
SourceNamespace: cmd.Flag("source-namespace").Value.String(),
DestNamespace: cmd.Flag("dest-namespace").Value.String(),
ServiceAccount: cmd.Flag("serviceaccount-name").Value.String(),
Context: cmd.Context(),
DestinationName: cmd.Flag("destination-name").Value.String(),
Expand Down
24 changes: 13 additions & 11 deletions cmd/kubectl-k8ssandra/register/register_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ import (

func TestRegister(t *testing.T) {
require.New(t)
client1 := (*multiEnv)[0].GetClient("mission-control")
client2 := (*multiEnv)[1].GetClient("mission-control")
client1 := (*multiEnv)[0].GetClient("k8ssandra-operator")
client2 := (*multiEnv)[1].GetClient("k8ssandra-operator")

if err := client1.Create((*multiEnv)[0].Context, &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "mission-control"}}); err != nil {
if err := client1.Create((*multiEnv)[0].Context, &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "dest-namespace"}}); err != nil {
t.Fatal(err)
}

if err := client2.Create((*multiEnv)[1].Context, &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "mission-control"}}); err != nil {
if err := client2.Create((*multiEnv)[1].Context, &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "source-namespace"}}); err != nil {
t.Fatal(err)
}

Expand Down Expand Up @@ -77,7 +77,9 @@ func TestRegister(t *testing.T) {
DestKubeconfig: buildDir + "/kubeconfig2",
SourceContext: "default-context",
DestContext: "default-context",
ServiceAccount: "mission-control",
SourceNamespace: "source-namespace",
DestNamespace: "dest-namespace",
ServiceAccount: "k8ssandra-operator",
Context: context.TODO(),
DestinationName: "test-destination",
}
Expand All @@ -90,7 +92,7 @@ func TestRegister(t *testing.T) {
return true
case res.IsError():
t.Log(res.GetError())
if res.GetError().Error() == "no secret found for service account mission-control" {
if res.GetError().Error() == "no secret found for service account k8ssandra-operator" {
return true
}
}
Expand All @@ -100,7 +102,7 @@ func TestRegister(t *testing.T) {
// This relies on a controller that is not running in the envtest.

desiredSaSecret := &corev1.Secret{}
require.NoError(t, client1.Get(context.Background(), client.ObjectKey{Name: "mission-control-secret", Namespace: "mission-control"}, desiredSaSecret))
require.NoError(t, client1.Get(context.Background(), client.ObjectKey{Name: "k8ssandra-operator-secret", Namespace: "source-namespace"}, desiredSaSecret))
patch := client.MergeFrom(desiredSaSecret.DeepCopy())
desiredSaSecret.Data = map[string][]byte{
"token": []byte("test-token"),
Expand All @@ -111,13 +113,13 @@ func TestRegister(t *testing.T) {
desiredSa := &corev1.ServiceAccount{}
require.NoError(t, client1.Get(
context.Background(),
client.ObjectKey{Name: "mission-control", Namespace: "mission-control"},
client.ObjectKey{Name: "k8ssandra-operator", Namespace: "source-namespace"},
desiredSa))

patch = client.MergeFrom(desiredSa.DeepCopy())
desiredSa.Secrets = []corev1.ObjectReference{
{
Name: "mission-control-secret",
Name: "k8ssandra-operator-secret",
},
}
require.NoError(t, client1.Patch(ctx, desiredSa, patch))
Expand All @@ -142,14 +144,14 @@ func TestRegister(t *testing.T) {
destSecret := &corev1.Secret{}
require.Eventually(t, func() bool {
err = client2.Get(ctx,
client.ObjectKey{Name: "test-destination", Namespace: "mission-control"}, destSecret)
client.ObjectKey{Name: "test-destination", Namespace: "dest-namespace"}, destSecret)
if err != nil {
t.Log("didn't find dest secret")
return false
}
clientConfig := &configapi.ClientConfig{}
err = client2.Get(ctx,
client.ObjectKey{Name: "test-destination", Namespace: "mission-control"}, clientConfig)
client.ObjectKey{Name: "test-destination", Namespace: "dest-namespace"}, clientConfig)
if err != nil {
t.Log("didn't find dest client config")
return false
Expand Down
22 changes: 12 additions & 10 deletions cmd/kubectl-k8ssandra/register/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,29 @@ type RegistrationExecutor struct {
DestKubeconfig string
SourceContext string
DestContext string
SourceNamespace string
DestNamespace string
ServiceAccount string
Context context.Context
}

func getDefaultSecret(saName string) *corev1.Secret {
func getDefaultSecret(saName, saNamespace string) *corev1.Secret {
return &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: saName + "-secret",
Namespace: StaticNamespace,
Namespace: saNamespace,
Annotations: map[string]string{
"kubernetes.io/service-account.name": saName,
},
},
Type: corev1.SecretTypeServiceAccountToken,
}
}
func getDefaultServiceAccount(saName string) *corev1.ServiceAccount {
func getDefaultServiceAccount(saName, saNamespace string) *corev1.ServiceAccount {
return &corev1.ServiceAccount{
ObjectMeta: metav1.ObjectMeta{
Name: saName,
Namespace: StaticNamespace,
Namespace: saNamespace,
},
}

Expand All @@ -62,15 +64,15 @@ func (e *RegistrationExecutor) RegisterCluster() result.ReconcileResult {
}
// Get ServiceAccount
serviceAccount := &corev1.ServiceAccount{}
if err := srcClient.Get(e.Context, client.ObjectKey{Name: e.ServiceAccount, Namespace: StaticNamespace}, serviceAccount); err != nil {
if err := srcClient.Create(e.Context, getDefaultServiceAccount(e.ServiceAccount)); err != nil {
if err := srcClient.Get(e.Context, client.ObjectKey{Name: e.ServiceAccount, Namespace: e.SourceNamespace}, serviceAccount); err != nil {
if err := srcClient.Create(e.Context, getDefaultServiceAccount(e.ServiceAccount, e.SourceNamespace)); err != nil {
return result.Error(err)
}
return result.Error(err)
}
// Get a secret in this namespace which holds the service account token
secretsList := &corev1.SecretList{}
if err := srcClient.List(e.Context, secretsList, client.InNamespace(StaticNamespace)); err != nil {
if err := srcClient.List(e.Context, secretsList, client.InNamespace(e.SourceNamespace)); err != nil {
return result.Error(err)
}
var secret *corev1.Secret
Expand All @@ -81,7 +83,7 @@ func (e *RegistrationExecutor) RegisterCluster() result.ReconcileResult {
}
}
if secret == nil {
secret = getDefaultSecret(e.ServiceAccount)
secret = getDefaultSecret(e.ServiceAccount, e.SourceNamespace)
if err := srcClient.Create(e.Context, secret); err != nil {
return result.Error(err)
}
Expand All @@ -104,7 +106,7 @@ func (e *RegistrationExecutor) RegisterCluster() result.ReconcileResult {
destSecret := corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: e.DestinationName,
Namespace: StaticNamespace,
Namespace: e.DestNamespace,
},
Type: corev1.SecretTypeOpaque,
Data: map[string][]byte{
Expand All @@ -122,7 +124,7 @@ func (e *RegistrationExecutor) RegisterCluster() result.ReconcileResult {
destClientConfig := configapi.ClientConfig{
ObjectMeta: metav1.ObjectMeta{
Name: e.DestinationName,
Namespace: StaticNamespace,
Namespace: e.DestNamespace,
},
Spec: configapi.ClientConfigSpec{
KubeConfigSecret: corev1.LocalObjectReference{
Expand Down

0 comments on commit 3a73b93

Please sign in to comment.