Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion test/infrastructure/vcsim/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ source vcsim.env
$ source vcsim.env

# Then you are ready to create a workload cluster
$ cat <your template> | envsubst | kubectl apply -f -
make envsubst
$ cat <your template> | hack/tools/bin/envsubst | kubectl apply -f -
```

#### Using govc with vcsim
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package v1alpha1

import (
"fmt"
"testing"

v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/yaml"
)

func TestVMOperatorDependencies_SetVCenterFromVCenterSimulator(t *testing.T) {
type fields struct {
TypeMeta v1.TypeMeta
ObjectMeta v1.ObjectMeta
Spec VMOperatorDependenciesSpec
Status VMOperatorDependenciesStatus
}
type args struct {
vCenterSimulator *VCenterSimulator
}
tests := []struct {
name string
fields fields
args args
}{
{
args: args{
vCenterSimulator: &VCenterSimulator{
Status: VCenterSimulatorStatus{
Host: "Host",
Username: "Username",
Password: "Password",
Thumbprint: "Thumbprint",
},
},
},
fields: fields{
TypeMeta: v1.TypeMeta{
Kind: "VMOperatorDependencies",
APIVersion: GroupVersion.String(),
},
ObjectMeta: v1.ObjectMeta{
Name: "name",
Namespace: "namespace",
},
Spec: VMOperatorDependenciesSpec{},
Status: VMOperatorDependenciesStatus{},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := &VMOperatorDependencies{
TypeMeta: tt.fields.TypeMeta,
ObjectMeta: tt.fields.ObjectMeta,
Spec: tt.fields.Spec,
Status: tt.fields.Status,
}
d.SetVCenterFromVCenterSimulator(tt.args.vCenterSimulator)

foo, _ := yaml.Marshal(d)
fmt.Println(string(foo))
})
}
}
19 changes: 19 additions & 0 deletions test/infrastructure/vcsim/controllers/backends/containers/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
Copyright 2025 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 containers implements backend for ControlPlaneEndpoint and VirtualMachines implemented using containers
// running on a host machine (inspired from CAPD).
package containers
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
Copyright 2025 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 backends

import (
"context"

ctrl "sigs.k8s.io/controller-runtime"

vcsimv1 "sigs.k8s.io/cluster-api-provider-vsphere/test/infrastructure/vcsim/api/v1alpha1"
)

type ControlPlaneEndpointReconciler interface {
ReconcileNormal(ctx context.Context, controlPlaneEndpoint *vcsimv1.ControlPlaneEndpoint) (ctrl.Result, error)
ReconcileDelete(ctx context.Context, controlPlaneEndpoint *vcsimv1.ControlPlaneEndpoint) (ctrl.Result, error)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
Copyright 2025 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 inmemory

import (
"context"

"github.com/pkg/errors"
"k8s.io/klog/v2"
inmemoryruntime "sigs.k8s.io/cluster-api/test/infrastructure/inmemory/pkg/runtime"
inmemoryserver "sigs.k8s.io/cluster-api/test/infrastructure/inmemory/pkg/server"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

vcsimv1 "sigs.k8s.io/cluster-api-provider-vsphere/test/infrastructure/vcsim/api/v1alpha1"
)

type ControlPlaneEndpointReconciler struct {
InMemoryManager inmemoryruntime.Manager
APIServerMux *inmemoryserver.WorkloadClustersMux
PodIP string
}

func (r *ControlPlaneEndpointReconciler) ReconcileNormal(ctx context.Context, controlPlaneEndpoint *vcsimv1.ControlPlaneEndpoint) (ctrl.Result, error) {
log := ctrl.LoggerFrom(ctx)
log.Info("Reconciling VCSim ControlPlaneEndpoint")

// Initialize a listener for the workload cluster.
// IMPORTANT: The fact that both the listener and the resourceGroup for a workload cluster have
// the same name is used as assumptions in other part of the implementation.
listenerName := klog.KObj(controlPlaneEndpoint).String()
listener, err := r.APIServerMux.InitWorkloadClusterListener(listenerName)
if err != nil {
return ctrl.Result{}, errors.Wrapf(err, "failed to init the listener for the control plane endpoint")
}

controlPlaneEndpoint.Status.Host = r.PodIP // NOTE: we are replacing the listener ip with the pod ip so it will be accessible from other pods as well
controlPlaneEndpoint.Status.Port = listener.Port()

return ctrl.Result{}, nil
}

func (r *ControlPlaneEndpointReconciler) ReconcileDelete(ctx context.Context, controlPlaneEndpoint *vcsimv1.ControlPlaneEndpoint) (ctrl.Result, error) {
log := ctrl.LoggerFrom(ctx)
log.Info("Reconciling delete VCSim ControlPlaneEndpoint")
listenerName := klog.KObj(controlPlaneEndpoint).String()

// Delete the resource group hosting all the cloud resources belonging the workload cluster;
if resourceGroup, err := r.APIServerMux.ResourceGroupByWorkloadCluster(listenerName); err == nil {
r.InMemoryManager.DeleteResourceGroup(resourceGroup)
}

// Delete the listener for the workload cluster;
if err := r.APIServerMux.DeleteWorkloadClusterListener(listenerName); err != nil {
return ctrl.Result{}, errors.Wrapf(err, "failed to delete the listener for the control plane endpoint")
}

controllerutil.RemoveFinalizer(controlPlaneEndpoint, vcsimv1.ControlPlaneEndpointFinalizer)

return ctrl.Result{}, nil
}
18 changes: 18 additions & 0 deletions test/infrastructure/vcsim/controllers/backends/inmemory/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
Copyright 2025 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 inmemory implements backend for ControlPlaneEndpoint and VirtualMachines implemented using CAPI inmemory test utils.
package inmemory
Loading
Loading