Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add vcsim server provider #2613

Merged
merged 1 commit into from
Feb 1, 2024
Merged
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
26 changes: 26 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,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 @@ -233,8 +237,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 @@ -275,13 +281,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 @@ -470,6 +488,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 @@
import (
"crypto/tls"
"fmt"
"net/url"
"os"
"os/exec"
"strings"
Expand All @@ -30,8 +31,10 @@

// 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 @@
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

Check warning on line 55 in internal/test/helpers/vcsim/builder.go

View check run for this annotation

Codecov / codecov/patch

internal/test/helpers/vcsim/builder.go#L53-L55

Added lines #L53 - L55 were not covered by tests
}

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

Check warning on line 61 in internal/test/helpers/vcsim/builder.go

View check run for this annotation

Codecov / codecov/patch

internal/test/helpers/vcsim/builder.go#L59-L61

Added lines #L59 - L61 were not covered by tests
}

// 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 @@

// 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
}

Check warning on line 76 in internal/test/helpers/vcsim/builder.go

View check run for this annotation

Codecov / codecov/patch

internal/test/helpers/vcsim/builder.go#L75-L76

Added lines #L75 - L76 were not covered by tests
}

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

Check warning on line 80 in internal/test/helpers/vcsim/builder.go

View check run for this annotation

Codecov / codecov/patch

internal/test/helpers/vcsim/builder.go#L80

Added line #L80 was not covered by tests
}

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 @@
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 @@

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)
}

Check warning on line 99 in pkg/util/supervisor.go

View check run for this annotation

Codecov / codecov/patch

pkg/util/supervisor.go#L91-L99

Added lines #L91 - L99 were not covered by tests
}
return nil, nil

Check warning on line 101 in pkg/util/supervisor.go

View check run for this annotation

Codecov / codecov/patch

pkg/util/supervisor.go#L101

Added line #L101 was not covered by tests
}

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

Check warning on line 110 in pkg/util/supervisor.go

View check run for this annotation

Codecov / codecov/patch

pkg/util/supervisor.go#L104-L110

Added lines #L104 - L110 were not covered by tests
}
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
Loading