Skip to content

Commit

Permalink
Fix e2e TMC fixture to support several VW URLs
Browse files Browse the repository at this point in the history
Signed-off-by: David Festal <dfestal@redhat.com>
  • Loading branch information
davidfestal committed Mar 29, 2023
1 parent 2c895bd commit 9403375
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 71 deletions.
9 changes: 9 additions & 0 deletions test/e2e/framework/kcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ type RunningServer interface {
BaseConfig(t *testing.T) *rest.Config
RootShardSystemMasterBaseConfig(t *testing.T) *rest.Config
ShardSystemMasterBaseConfig(t *testing.T, shard string) *rest.Config
ShardNames() []string
Artifact(t *testing.T, producer func() (runtime.Object, error))
ClientCAUserConfig(t *testing.T, config *rest.Config, name string, groups ...string) *rest.Config
CADirectory() string
Expand Down Expand Up @@ -853,6 +854,10 @@ func (c *kcpServer) ShardSystemMasterBaseConfig(t *testing.T, shard string) *res
return c.RootShardSystemMasterBaseConfig(t)
}

func (c *kcpServer) ShardNames() []string {
return []string{corev1alpha1.RootShard}
}

// RawConfig exposes a copy of the client config for this server.
func (c *kcpServer) RawConfig() (clientcmdapi.Config, error) {
c.lock.Lock()
Expand Down Expand Up @@ -1117,6 +1122,10 @@ func (s *unmanagedKCPServer) ShardSystemMasterBaseConfig(t *testing.T, shard str
return wrappedCfg
}

func (s *unmanagedKCPServer) ShardNames() []string {
return sets.StringKeySet(s.shardCfgs).List()
}

func (s *unmanagedKCPServer) Artifact(t *testing.T, producer func() (runtime.Object, error)) {
t.Helper()
artifact(t, s, producer)
Expand Down
55 changes: 20 additions & 35 deletions test/e2e/framework/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,42 +310,27 @@ func (sf *syncerFixture) CreateSyncTargetAndApplyToDownstream(t *testing.T) *app
ctx, cancelFunc := context.WithCancel(context.Background())
t.Cleanup(cancelFunc)

rawConfig, err := sf.upstreamServer.RawConfig()
require.NoError(t, err)

kcpClusterClient, err := kcpclientset.NewForConfig(syncerConfig.UpstreamConfig)
require.NoError(t, err)
var virtualWorkspaceURL string
var syncTargetClusterName logicalcluster.Name
Eventually(t, func() (success bool, reason string) {
syncTarget, err := kcpClusterClient.Cluster(syncerConfig.SyncTargetPath).WorkloadV1alpha1().SyncTargets().Get(ctx, syncerConfig.SyncTargetName, metav1.GetOptions{})
require.NoError(t, err)
if len(syncTarget.Status.VirtualWorkspaces) != 1 {
return false, ""
}
virtualWorkspaceURL = syncTarget.Status.VirtualWorkspaces[0].SyncerURL
syncTargetClusterName = logicalcluster.From(syncTarget)
return true, "Virtual workspace URL is available"
}, wait.ForeverTestTimeout, 100*time.Millisecond, "Syncer Virtual Workspace URL not available")

virtualWorkspaceRawConfig := rawConfig.DeepCopy()
virtualWorkspaceRawConfig.Clusters["syncer"] = rawConfig.Clusters["base"].DeepCopy()
virtualWorkspaceRawConfig.Clusters["syncer"].Server = virtualWorkspaceURL
virtualWorkspaceRawConfig.Contexts["syncer"] = rawConfig.Contexts["base"].DeepCopy()
virtualWorkspaceRawConfig.Contexts["syncer"].Cluster = "syncer"
virtualWorkspaceRawConfig.Clusters["upsyncer"] = rawConfig.Clusters["base"].DeepCopy()
virtualWorkspaceRawConfig.Clusters["upsyncer"].Server = strings.Replace(virtualWorkspaceURL, "/services/syncer/", "/services/upsyncer/", 1)
virtualWorkspaceRawConfig.Contexts["upsyncer"] = rawConfig.Contexts["base"].DeepCopy()
virtualWorkspaceRawConfig.Contexts["upsyncer"].Cluster = "upsyncer"
syncerVWConfig, err := clientcmd.NewNonInteractiveClientConfig(*virtualWorkspaceRawConfig, "syncer", nil, nil).ClientConfig()
require.NoError(t, err)
syncerVWConfig = rest.AddUserAgent(rest.CopyConfig(syncerVWConfig), t.Name())
require.NoError(t, err)
upsyncerVWConfig, err := clientcmd.NewNonInteractiveClientConfig(*virtualWorkspaceRawConfig, "upsyncer", nil, nil).ClientConfig()
require.NoError(t, err)
upsyncerVWConfig = rest.AddUserAgent(rest.CopyConfig(upsyncerVWConfig), t.Name())
syncTarget, err := kcpClusterClient.Cluster(syncerConfig.SyncTargetPath).WorkloadV1alpha1().SyncTargets().Get(ctx, syncerConfig.SyncTargetName, metav1.GetOptions{})
require.NoError(t, err)

syncTargetClusterName = logicalcluster.From(syncTarget)

getVWURLs := func(toURL func(workloadv1alpha1.VirtualWorkspace) string) func() []string {
return func() []string {
syncTarget, err := kcpClusterClient.Cluster(syncerConfig.SyncTargetPath).WorkloadV1alpha1().SyncTargets().Get(ctx, syncerConfig.SyncTargetName, metav1.GetOptions{})
require.NoError(t, err)

var urls []string
for _, vw := range syncTarget.Status.VirtualWorkspaces {
urls = append(urls, toURL(vw))
}
return urls
}
}

return &appliedSyncerFixture{
syncerFixture: *sf,

Expand All @@ -356,8 +341,8 @@ func (sf *syncerFixture) CreateSyncTargetAndApplyToDownstream(t *testing.T) *app
DownstreamKubeClient: downstreamKubeClient,
DownstreamKubeconfigPath: downstreamKubeconfigPath,

SyncerVirtualWorkspaceConfig: syncerVWConfig,
UpsyncerVirtualWorkspaceConfig: upsyncerVWConfig,
GetSyncerVirtualWorkspaceURLs: getVWURLs(func(vw workloadv1alpha1.VirtualWorkspace) string { return vw.SyncerURL }),
GetUpsyncerVirtualWorkspaceURLs: getVWURLs(func(vw workloadv1alpha1.VirtualWorkspace) string { return vw.UpsyncerURL }),
}
}

Expand Down Expand Up @@ -571,8 +556,8 @@ type appliedSyncerFixture struct {
DownstreamKubeClient kubernetesclient.Interface
DownstreamKubeconfigPath string

SyncerVirtualWorkspaceConfig *rest.Config
UpsyncerVirtualWorkspaceConfig *rest.Config
GetSyncerVirtualWorkspaceURLs func() []string
GetUpsyncerVirtualWorkspaceURLs func() []string

stopHeartBeat context.CancelFunc
stopSyncerTunnel context.CancelFunc
Expand Down
11 changes: 10 additions & 1 deletion test/e2e/reconciler/scheduling/upsynced_scheduling_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/rest"

"github.com/kcp-dev/kcp/sdk/apis/third_party/conditions/util/conditions"
workloadv1alpha1 "github.com/kcp-dev/kcp/sdk/apis/workload/v1alpha1"
Expand Down Expand Up @@ -124,7 +125,15 @@ func TestUpsyncedScheduling(t *testing.T) {
}

// Create a client that uses the upsyncer URL
upsyncerKCPClient, err := kcpkubernetesclientset.NewForConfig(syncerFixture.UpsyncerVirtualWorkspaceConfig)
upsyncerVirtualWorkspaceConfig := rest.CopyConfig(upstreamConfig)
var upsyncerVirtualWorkspaceURL string
framework.Eventually(t, func() (found bool, message string) {
upsyncerVirtualWorkspaceURL, found, err = framework.VirtualWorkspaceURL(ctx, upstreamKcpClient, userWs, syncerFixture.GetUpsyncerVirtualWorkspaceURLs())
require.NoError(t, err)
return found, "Upsyncer virtual workspace URL not found"
}, wait.ForeverTestTimeout, time.Millisecond*100, "Upsyncer virtual workspace URL not found")
upsyncerVirtualWorkspaceConfig.Host = upsyncerVirtualWorkspaceURL
upsyncerKCPClient, err := kcpkubernetesclientset.NewForConfig(upsyncerVirtualWorkspaceConfig)
require.NoError(t, err)

_, err = upsyncerKCPClient.Cluster(userWsName.Path()).CoreV1().Pods(upstreamNamespace.Name).Create(ctx, &pod, metav1.CreateOptions{})
Expand Down
11 changes: 10 additions & 1 deletion test/e2e/syncer/tunnels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"

"github.com/kcp-dev/kcp/pkg/syncer/shared"
workloadv1alpha1 "github.com/kcp-dev/kcp/sdk/apis/workload/v1alpha1"
Expand Down Expand Up @@ -377,7 +378,15 @@ func TestSyncerTunnelFilter(t *testing.T) {
require.NoError(t, err)

// Create a pod on the upstream namespace that looks like the downstream pod being upsynced.
upsyncedClient, err := kcpkubernetesclientset.NewForConfig(syncerFixture.UpsyncerVirtualWorkspaceConfig)
upsyncerVirtualWorkspaceConfig := rest.CopyConfig(kcpServer.BaseConfig(t))
var upsyncerVirtualWorkspaceURL string
framework.Eventually(t, func() (found bool, message string) {
upsyncerVirtualWorkspaceURL, found, err = framework.VirtualWorkspaceURL(ctx, kcpClient, userWs, syncerFixture.GetUpsyncerVirtualWorkspaceURLs())
require.NoError(t, err)
return found, "Upsyncer virtual workspace URL not found"
}, wait.ForeverTestTimeout, time.Millisecond*100, "Upsyncer virtual workspace URL not found")
upsyncerVirtualWorkspaceConfig.Host = upsyncerVirtualWorkspaceURL
upsyncedClient, err := kcpkubernetesclientset.NewForConfig(upsyncerVirtualWorkspaceConfig)
require.NoError(t, err)

upsyncedPod, err := upsyncedClient.CoreV1().Pods().Cluster(userWsName.Path()).Namespace(upstreamNs.Name).Create(ctx, &corev1.Pod{
Expand Down
Loading

0 comments on commit 9403375

Please sign in to comment.