Skip to content

Commit

Permalink
Fix issues due to rebase.
Browse files Browse the repository at this point in the history
  • Loading branch information
Miles-Garnsey committed May 28, 2024
1 parent 460cf96 commit 5c3e445
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 57 deletions.
69 changes: 17 additions & 52 deletions cmd/kubectl-k8ssandra/register/register_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package register

import (
"context"
"errors"
"fmt"
"os"
"testing"
"time"
Expand All @@ -13,6 +11,7 @@ import (
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -40,21 +39,15 @@ func TestRegister(t *testing.T) {
}
}
return err1 == nil && err2 == nil
}, time.Second*60, time.Second*5)
}, time.Second*60, time.Second*2)

testDir, err := os.MkdirTemp("", "k8ssandra-operator-test-****")
require.NoError(err)
t.Cleanup(func() {
require.NoError(os.RemoveAll(testDir))
})

kc1, err := (*multiEnv)[0].GetKubeconfig()
require.NoError(err)
f1, err := os.Create(testDir + "/kubeconfig1")
require.NoError(err)
t.Cleanup(func() {
require.NoError(f1.Close())
})
kc1, err := (*multiEnv)[0].GetKubeconfig()
require.NoError(err)
_, err = f1.Write(kc1)
require.NoError(err)

Expand All @@ -80,52 +73,25 @@ func TestRegister(t *testing.T) {
Context: ctx,
DestinationName: "test-destination",
}
ctx := context.Background()

// Continue reconciliation
require.Eventually(func() bool {
if err := ex.RegisterCluster(); err != nil {
if errors.Is(err, NonRecoverableError{}) {
require.FailNow(fmt.Sprintf("Registration failed: %s", err.Error()))
}
if err.Error() == "no secret found for service account k8ssandra-operator" {
return true
}
return false
}
return true
}, time.Second*5, time.Millisecond*100)
res := ex.RegisterCluster()
return res == nil
}, time.Second*300, time.Second*1)

// This relies on a controller that is not running in the envtest.

desiredSaSecret := &corev1.Secret{}
require.NoError(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"),
"ca.crt": []byte("test-ca"),
}
require.NoError(client1.Patch(ctx, desiredSaSecret, patch))
sourceSecret := &corev1.Secret{}
// Ensure secret created.
require.Eventually(func() bool {
err := client1.Get(ctx, types.NamespacedName{Name: "k8ssandra-operator-secret", Namespace: "source-namespace"}, sourceSecret)
return err == nil
}, time.Second*60, time.Second*5)

desiredSa := &corev1.ServiceAccount{}
require.NoError(client1.Get(
context.Background(),
client.ObjectKey{Name: "k8ssandra-operator", Namespace: "source-namespace"},
desiredSa))

patch = client.MergeFrom(desiredSa.DeepCopy())
desiredSa.Secrets = []corev1.ObjectReference{
{
Name: "k8ssandra-operator-secret",
},
}
require.NoError(client1.Patch(ctx, desiredSa, patch))

// Continue reconciliation

require.Eventually(func() bool {
return ex.RegisterCluster() == nil
}, time.Second*3, time.Millisecond*100)

if err := configapi.AddToScheme(client2.Scheme()); err != nil {
require.NoError(err)
}
Expand All @@ -145,15 +111,14 @@ func TestRegister(t *testing.T) {
return false
}
return err == nil
}, time.Second*6, time.Millisecond*100)

}, time.Second*60, time.Second*2)
destKubeconfig := ClientConfigFromSecret(destSecret)
require.Equal(
desiredSaSecret.Data["ca.crt"],
sourceSecret.Data["ca.crt"],
destKubeconfig.Clusters["test-destination"].CertificateAuthorityData)

require.Equal(
string(desiredSaSecret.Data["token"]),
string(sourceSecret.Data["token"]),
destKubeconfig.AuthInfos["test-destination"].Token)
}

Expand Down
8 changes: 7 additions & 1 deletion cmd/kubectl-k8ssandra/register/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@ import (

var (
multiEnv *envtest.MultiK8sEnvironment
testDir string
err error
)

func TestMain(m *testing.M) {
testDir, err = os.MkdirTemp("", "k8ssandra-operator-test")
if err != nil {
panic(err.Error())
}
os.Exit(envtest.RunMultiKind(m, func(e *envtest.MultiK8sEnvironment) {
multiEnv = e
}, []int{1, 1}))
}, []int{1, 1}, testDir))
}
3 changes: 1 addition & 2 deletions internal/envtest/envtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,13 @@ func NewKindEnvironment(ctx context.Context, cluster KindManager) *Environment {
ctx, cancel := context.WithCancel(ctx)
env.Context = ctx
env.cancelManager = cancel
env.Client = *cluster.client
env.Kubeconfig = cluster.KubeconfigLocation.Name()
env.env.Config = cluster.RestConfig
env.KindCluster = cluster
return env
}

func (e *Environment) GetClient(namespace string) client.Client {
func (e *Environment) GetClientInNamespace(namespace string) client.Client {
c, err := kubernetes.GetClientInNamespace(e.env.Config, namespace)
if err != nil {
panic(err)
Expand Down
4 changes: 2 additions & 2 deletions internal/envtest/multi_envtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ func RunMulti(m *testing.M, setupFunc func(e *MultiK8sEnvironment), numClusters
return exitCode
}

func RunMultiKind(m *testing.M, setupFunc func(e *MultiK8sEnvironment), topology []int) (code int) {
func RunMultiKind(m *testing.M, setupFunc func(e *MultiK8sEnvironment), topology []int, testDir string) (code int) {
e := make(MultiK8sEnvironment, len(topology))
ctx := ctrl.SetupSignalHandler()
var wg sync.WaitGroup
for i := 0; i < len(topology); i++ {
cluster := KindManager{
ClusterName: "cluster" + strconv.Itoa(i),
KubeconfigLocation: os.NewFile(0, GetBuildDir()+"/cluster"+strconv.Itoa(i)),
KubeconfigLocation: os.NewFile(0, testDir+"/cluster"+strconv.Itoa(i)),
Nodes: topology[i],
}
wg.Add(1)
Expand Down

0 comments on commit 5c3e445

Please sign in to comment.