Skip to content

Commit

Permalink
Merge pull request #2613 from fabriziopandini/vcsim-server
Browse files Browse the repository at this point in the history
 ✨ Add vcsim server provider
  • Loading branch information
k8s-ci-robot authored Feb 1, 2024
2 parents 75f53fd + 8245cd0 commit 298c373
Show file tree
Hide file tree
Showing 110 changed files with 13,788 additions and 206 deletions.
26 changes: 26 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,10 @@ STAGING_BUCKET ?= artifacts.k8s-staging-capi-vsphere.appspot.com
IMAGE_NAME ?= cluster-api-vsphere-controller
CONTROLLER_IMG ?= $(REGISTRY)/$(IMAGE_NAME)

# vcsim controller
VCSIM_IMAGE_NAME ?= cluster-api-vcsim-controller
VCSIM_CONTROLLER_IMG ?= $(REGISTRY)/$(VCSIM_IMAGE_NAME)

# It is set by Prow GIT_TAG, a git-based tag of the form vYYYYMMDD-hash, e.g., v20210120-v0.3.10-308-gc61521971

TAG ?= dev
Expand All @@ -238,8 +242,10 @@ MANIFEST_ROOT ?= ./config
CRD_ROOT ?= $(MANIFEST_ROOT)/default/crd/bases
SUPERVISOR_CRD_ROOT ?= $(MANIFEST_ROOT)/supervisor/crd
VMOP_CRD_ROOT ?= $(MANIFEST_ROOT)/deployments/integration-tests/crds
VCSIM_CRD_ROOT ?= test/infrastructure/vcsim/config/crd/bases
WEBHOOK_ROOT ?= $(MANIFEST_ROOT)/webhook
RBAC_ROOT ?= $(MANIFEST_ROOT)/rbac
VCSIM_RBAC_ROOT ?= test/infrastructure/vcsim/config/rbac
VERSION ?= $(shell cat clusterctl-settings.json | jq .config.nextVersion -r)
OVERRIDES_DIR := $(HOME)/.cluster-api/overrides/infrastructure-vsphere/$(VERSION)

Expand Down Expand Up @@ -280,13 +286,25 @@ generate-manifests: $(CONTROLLER_GEN) ## Generate manifests e.g. CRD, RBAC etc.
paths=github.com/vmware-tanzu/vm-operator/api/v1alpha1/... \
crd:crdVersions=v1 \
output:crd:dir=$(VMOP_CRD_ROOT)
# vcsim crds are used for tests.
$(CONTROLLER_GEN) \
paths=./test/infrastructure/vcsim/api/v1alpha1 \
crd:crdVersions=v1 \
output:crd:dir=$(VCSIM_CRD_ROOT)
$(CONTROLLER_GEN) \
paths=./test/infrastructure/vcsim/controllers/... \
output:rbac:dir=$(VCSIM_RBAC_ROOT) \
rbac:roleName=manager-role

.PHONY: generate-go-deepcopy
generate-go-deepcopy: $(CONTROLLER_GEN) ## Generate deepcopy go code for core
$(MAKE) clean-generated-deepcopy SRC_DIRS="./apis"
$(CONTROLLER_GEN) \
object:headerFile=./hack/boilerplate/boilerplate.generatego.txt \
paths=./apis/...
$(CONTROLLER_GEN) \
object:headerFile=./hack/boilerplate/boilerplate.generatego.txt \
paths=./test/infrastructure/vcsim/api/...

.PHONY: generate-modules
generate-modules: ## Run go mod tidy to ensure modules are up to date
Expand Down Expand Up @@ -476,6 +494,14 @@ docker-build: docker-pull-prerequisites ## Build the docker image for vsphere co
$(MAKE) set-manifest-pull-policy TARGET_RESOURCE="./config/base/manager_pull_policy.yaml"; \
fi

.PHONY: docker-build-vcsim
docker-build-vcsim: docker-pull-prerequisites ## Build the docker image for vcsim controller manager
DOCKER_BUILDKIT=1 docker build --platform linux/$(ARCH) --build-arg GOLANG_VERSION=$(GO_CONTAINER_IMAGE) --build-arg goproxy=$(GOPROXY) --build-arg ldflags="$(LDFLAGS)" . -t $(VCSIM_CONTROLLER_IMG)-$(ARCH):$(TAG)
@if [ "${DOCKER_BUILD_MODIFY_MANIFESTS}" = "true" ]; then \
$(MAKE) set-manifest-image MANIFEST_IMG=$(VCSIM_CONTROLLER_IMG)-$(ARCH) MANIFEST_TAG=$(TAG) TARGET_RESOURCE="./test/infrastructure/vcsim/config/default/manager_image_patch.yaml"; \
$(MAKE) set-manifest-pull-policy TARGET_RESOURCE="./test/infrastructure/vcsim/config/default/manager_pull_policy.yaml"; \
fi

## --------------------------------------
## Testing
## --------------------------------------
Expand Down
32 changes: 27 additions & 5 deletions internal/test/helpers/vcsim/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package vcsim
import (
"crypto/tls"
"fmt"
"net/url"
"os"
"os/exec"
"strings"
Expand All @@ -30,8 +31,10 @@ import (

// Builder helps in creating a vcsim simulator.
type Builder struct {
model *simulator.Model
operations []string
skipModelCreate bool
url *url.URL
model *simulator.Model
operations []string
}

// NewBuilder returns a new a Builder.
Expand All @@ -45,6 +48,19 @@ func (b *Builder) WithModel(model *simulator.Model) *Builder {
return b
}

// SkipModelCreate tells the builder to skip creating the model, because it is already created before passing it
// to WithModel.
func (b *Builder) SkipModelCreate() *Builder {
b.skipModelCreate = true
return b
}

// WithURL defines the url to be used for service listening.
func (b *Builder) WithURL(url *url.URL) *Builder {
b.url = url
return b
}

// WithOperations defines the operation that the Builder should executed on the newly created vcsim instance.
func (b *Builder) WithOperations(ops ...string) *Builder {
b.operations = append(b.operations, ops...)
Expand All @@ -53,9 +69,15 @@ func (b *Builder) WithOperations(ops ...string) *Builder {

// Build the vcsim instance.
func (b *Builder) Build() (*Simulator, error) {
err := b.model.Create()
if err != nil {
return nil, err
if !b.skipModelCreate {
err := b.model.Create()
if err != nil {
return nil, err
}
}

if b.url != nil {
b.model.Service.Listen = b.url
}

b.model.Service.TLS = new(tls.Config)
Expand Down
27 changes: 27 additions & 0 deletions pkg/util/supervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ limitations under the License.
package util

import (
"context"
"fmt"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

vmwarev1 "sigs.k8s.io/cluster-api-provider-vsphere/apis/vmware/v1beta1"
)

// SetControllerReferenceWithOverride sets owner as a Controller OwnerReference on controlled.
Expand Down Expand Up @@ -82,3 +86,26 @@ func referSameObject(a, b metav1.OwnerReference) bool {

return aGV.Group == bGV.Group && a.Kind == b.Kind && a.Name == b.Name
}

// GetOwnerVMWareMachine returns the vmwarev1.VSphereMachine owner for the passed object.
func GetOwnerVMWareMachine(ctx context.Context, c client.Client, obj metav1.ObjectMeta) (*vmwarev1.VSphereMachine, error) {
for _, ref := range obj.OwnerReferences {
gv, err := schema.ParseGroupVersion(ref.APIVersion)
if err != nil {
return nil, err
}
if ref.Kind == "VSphereMachine" && gv.Group == vmwarev1.GroupVersion.Group {
return getVMWareMachineByName(ctx, c, obj.Namespace, ref.Name)
}
}
return nil, nil
}

func getVMWareMachineByName(ctx context.Context, c client.Client, namespace, name string) (*vmwarev1.VSphereMachine, error) {
m := &vmwarev1.VSphereMachine{}
key := client.ObjectKey{Name: name, Namespace: namespace}
if err := c.Get(ctx, key, m); err != nil {
return nil, err
}
return m, nil
}
18 changes: 18 additions & 0 deletions test/framework/vmoperator/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
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 vmoperator contains utils to run tests with the vm-operator in standalone mode.
package vmoperator
Loading

0 comments on commit 298c373

Please sign in to comment.