Skip to content

Commit

Permalink
vcsim: implement proxy to allow upgrade cases
Browse files Browse the repository at this point in the history
  • Loading branch information
chrischdi committed Jun 25, 2024
1 parent 4c45465 commit a576bc7
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 2 deletions.
2 changes: 1 addition & 1 deletion test/e2e/cluster_upgrade_runtimesdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
capi_e2e "sigs.k8s.io/cluster-api/test/e2e"
)

var _ = Describe("When upgrading a workload cluster using ClusterClass with RuntimeSDK [supervisor] [ClusterClass]", func() {
var _ = Describe("When upgrading a workload cluster using ClusterClass with RuntimeSDK [vcsim] [supervisor] [ClusterClass]", func() {
const specName = "k8s-upgrade-with-runtimesdk" // aligned to CAPI
Setup(specName, func(testSpecificSettingsGetter func() testSettings) {
capi_e2e.ClusterUpgradeWithRuntimeSDKSpec(ctx, func() capi_e2e.ClusterUpgradeWithRuntimeSDKSpecInput {
Expand Down
6 changes: 5 additions & 1 deletion test/e2e/e2e_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,11 @@ var _ = SynchronizedBeforeSuite(func() []byte {
Finder: vsphereFinder,
}))
}
bootstrapClusterProxy = framework.NewClusterProxy("bootstrap", kubeconfigPath, initScheme(), clusterProxyOptions...)
if testTarget == VCSimTestTarget {
bootstrapClusterProxy = vspherevcsim.NewClusterProxy("bootstrap", kubeconfigPath, initScheme(), clusterProxyOptions...)
} else {
bootstrapClusterProxy = framework.NewClusterProxy("bootstrap", kubeconfigPath, initScheme(), clusterProxyOptions...)
}

ipClaimLabels := map[string]string{}
for _, s := range strings.Split(ipClaimLabelsRaw, ";") {
Expand Down
178 changes: 178 additions & 0 deletions test/framework/vcsim/cluster_proxy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
/*
Copyright 2024 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Package vcsim provide helpers for vcsim controller.
package vcsim

import (
"context"
"fmt"
"net"
"net/url"
"strconv"
"time"

. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"sigs.k8s.io/cluster-api/test/framework"
inmemoryproxy "sigs.k8s.io/cluster-api/test/infrastructure/inmemory/pkg/server/proxy"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
)

const (
retryableOperationInterval = 3 * time.Second
// retryableOperationTimeout requires a higher value especially for self-hosted upgrades.
// Short unavailability of the Kube APIServer due to joining etcd members paired with unreachable conversion webhooks due to
// failed leader election and thus controller restarts lead to longer taking retries.
// The timeout occurs when listing machines in `GetControlPlaneMachinesByCluster`.
retryableOperationTimeout = 3 * time.Minute

initialCacheSyncTimeout = time.Minute

Check failure on line 48 in test/framework/vcsim/cluster_proxy.go

View workflow job for this annotation

GitHub Actions / lint (test)

const `initialCacheSyncTimeout` is unused (unused)
)

type vcSimManagementClusterProxy struct {
framework.ClusterProxy
}

// NewClusterProxy creates a ClusterProxy for usage with VCSim

Check failure on line 55 in test/framework/vcsim/cluster_proxy.go

View workflow job for this annotation

GitHub Actions / lint (test)

Comment should end in a period (godot)
func NewClusterProxy(name string, kubeconfigPath string, scheme *runtime.Scheme, options ...framework.Option) framework.ClusterProxy {
return &vcSimManagementClusterProxy{
ClusterProxy: framework.NewClusterProxy(name, kubeconfigPath, scheme, options...),
}
}

// GetWorkloadCluster returns ClusterProxy for the workload cluster.
func (p *vcSimManagementClusterProxy) GetWorkloadCluster(ctx context.Context, namespace, name string, options ...framework.Option) framework.ClusterProxy {
wlProxy := p.ClusterProxy.GetWorkloadCluster(ctx, namespace, name, options...)

// Get the vcSim pod information.
pods := corev1.PodList{}
Expect(p.GetClient().List(context.Background(), &pods, client.InNamespace("vcsim-system"))).To(Succeed())
Expect(pods.Items).To(HaveLen(1), "expecting to run vcsim with a single replica")
vcSimPod := pods.Items[0]

// Get the target port number from the restconfig of the workload cluster.
u, err := url.Parse(wlProxy.GetRESTConfig().Host)
Expect(err).ToNot(HaveOccurred())

port, err := strconv.Atoi(u.Port())
Expect(err).ToNot(HaveOccurred())

// Create a dialer which proxies through the kube-apiserver to the vcsim pod's port where the simulated kube-apiserver
// is running.
d, err := inmemoryproxy.NewDialer(inmemoryproxy.Proxy{
Kind: "pods",
Namespace: vcSimPod.GetNamespace(),
ResourceName: vcSimPod.GetName(),
Port: port,
KubeConfig: p.GetRESTConfig(),
})
Expect(err).ToNot(HaveOccurred())

dialFunc := func(ctx context.Context, _, _ string) (net.Conn, error) {
// Always use vcSimPodName as url to have a successful port forward.
return d.DialContext(ctx, "", vcSimPod.GetName())
}

return &vcSimWorkloadClusterProxy{
realProxy: wlProxy,
dialFunc: dialFunc,
}
}

type vcSimWorkloadClusterProxy struct {
realProxy framework.ClusterProxy
dialFunc func(ctx context.Context, _, _ string) (net.Conn, error)
}

func (p *vcSimWorkloadClusterProxy) GetName() string {
return p.realProxy.GetName()
}

func (p *vcSimWorkloadClusterProxy) GetKubeconfigPath() string {
return p.realProxy.GetKubeconfigPath()
}

func (p *vcSimWorkloadClusterProxy) GetScheme() *runtime.Scheme {
return p.realProxy.GetScheme()
}

func (p *vcSimWorkloadClusterProxy) GetClient() client.Client {
config := p.GetRESTConfig()

var c client.Client
var newClientErr error
err := wait.PollUntilContextTimeout(context.TODO(), retryableOperationInterval, retryableOperationTimeout, true, func(context.Context) (bool, error) {
c, newClientErr = client.New(config, client.Options{Scheme: p.realProxy.GetScheme()})
if newClientErr != nil {
return false, nil //nolint:nilerr
}
return true, nil
})
errorString := "Failed to get controller-runtime client"
Expect(newClientErr).ToNot(HaveOccurred(), errorString)
Expect(err).ToNot(HaveOccurred(), errorString)

return c
}

func (p *vcSimWorkloadClusterProxy) GetClientSet() *kubernetes.Clientset {
restConfig := p.GetRESTConfig()

cs, err := kubernetes.NewForConfig(restConfig)
Expect(err).ToNot(HaveOccurred(), "Failed to get client-go client")

return cs
}

func (p *vcSimWorkloadClusterProxy) GetRESTConfig() *rest.Config {
config := p.realProxy.GetRESTConfig()
config.Dial = p.dialFunc

return config
}

func (p *vcSimWorkloadClusterProxy) GetCache(ctx context.Context) cache.Cache {
return p.realProxy.GetCache(ctx)
}

func (p *vcSimWorkloadClusterProxy) GetLogCollector() framework.ClusterLogCollector {
// There are no logs in simulated clusters.
return nil
}

func (p *vcSimWorkloadClusterProxy) Apply(ctx context.Context, resources []byte, args ...string) error {

Check failure on line 162 in test/framework/vcsim/cluster_proxy.go

View workflow job for this annotation

GitHub Actions / lint (test)

unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
return fmt.Errorf("not supported")
}

func (p *vcSimWorkloadClusterProxy) GetWorkloadCluster(ctx context.Context, namespace, name string, options ...framework.Option) framework.ClusterProxy {

Check failure on line 166 in test/framework/vcsim/cluster_proxy.go

View workflow job for this annotation

GitHub Actions / lint (test)

unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
Expect(fmt.Errorf("simulated workload clusters can't have nested workload clusters"))

Check failure on line 167 in test/framework/vcsim/cluster_proxy.go

View workflow job for this annotation

GitHub Actions / lint (test)

ginkgo-linter: "Expect": missing assertion method. Expected "To()", "ToNot()" or "NotTo()" (ginkgolinter)
return nil
}

func (p *vcSimWorkloadClusterProxy) CollectWorkloadClusterLogs(ctx context.Context, namespace, name, outputPath string) {

Check failure on line 171 in test/framework/vcsim/cluster_proxy.go

View workflow job for this annotation

GitHub Actions / lint (test)

unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
// There are no logs in simulated clusters.
return

Check failure on line 173 in test/framework/vcsim/cluster_proxy.go

View workflow job for this annotation

GitHub Actions / lint (test)

S1023: redundant `return` statement (gosimple)
}

func (p *vcSimWorkloadClusterProxy) Dispose(ctx context.Context) {
p.realProxy.Dispose(ctx)
}
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,9 @@ func (r *vmBootstrapReconciler) reconcileBoostrapNode(ctx context.Context, clust
},
},
}
if machine.Spec.Version != nil {
node.Status.NodeInfo.KubeletVersion = *machine.Spec.Version
}
if util.IsControlPlaneMachine(machine) {
if node.Labels == nil {
node.Labels = map[string]string{}
Expand Down

0 comments on commit a576bc7

Please sign in to comment.