Skip to content

Commit

Permalink
Merge pull request #3161 from chrischdi/pr-vspheremachintetpl-autoscaler
Browse files Browse the repository at this point in the history
✨ supervisor: implement support for autoscale to/from zero
  • Loading branch information
k8s-ci-robot authored Aug 26, 2024
2 parents 46b9b9c + ce106be commit 1b7c2dc
Show file tree
Hide file tree
Showing 18 changed files with 596 additions and 23 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ generate-e2e-templates-main: $(KUSTOMIZE) ## Generate test templates for the mai
"$(KUSTOMIZE)" --load-restrictor LoadRestrictionsNone build "$(E2E_SUPERVISOR_TEMPLATE_DIR)/main/clusterclass-runtimesdk" > "$(E2E_SUPERVISOR_TEMPLATE_DIR)/main/clusterclass-quick-start-supervisor-runtimesdk.yaml"
cp "$(RELEASE_DIR)/main/cluster-template-topology-supervisor.yaml" "$(E2E_SUPERVISOR_TEMPLATE_DIR)/main/topology/cluster-template-topology-supervisor.yaml"
"$(KUSTOMIZE)" --load-restrictor LoadRestrictionsNone build "$(E2E_SUPERVISOR_TEMPLATE_DIR)/main/topology" > "$(E2E_SUPERVISOR_TEMPLATE_DIR)/main/cluster-template-topology-supervisor.yaml"
"$(KUSTOMIZE)" --load-restrictor LoadRestrictionsNone build "$(E2E_SUPERVISOR_TEMPLATE_DIR)/main/topology-autoscaler" > "$(E2E_SUPERVISOR_TEMPLATE_DIR)/main/cluster-template-topology-autoscaler-supervisor.yaml"
"$(KUSTOMIZE)" --load-restrictor LoadRestrictionsNone build "$(E2E_SUPERVISOR_TEMPLATE_DIR)/main/topology-runtimesdk" > "$(E2E_SUPERVISOR_TEMPLATE_DIR)/main/cluster-template-topology-runtimesdk-supervisor.yaml"
"$(KUSTOMIZE)" --load-restrictor LoadRestrictionsNone build "$(E2E_SUPERVISOR_TEMPLATE_DIR)/main/conformance" > "$(E2E_SUPERVISOR_TEMPLATE_DIR)/main/cluster-template-conformance-supervisor.yaml"
"$(KUSTOMIZE)" --load-restrictor LoadRestrictionsNone build "$(E2E_SUPERVISOR_TEMPLATE_DIR)/main/fast-rollout" > "$(E2E_SUPERVISOR_TEMPLATE_DIR)/main/cluster-template-fast-rollout-supervisor.yaml"
Expand Down
22 changes: 21 additions & 1 deletion apis/vmware/v1beta1/vspheremachinetemplate_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,44 @@ limitations under the License.
package v1beta1

import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const (
// VSphereResourceCPU defines Resource type CPU for VSphereMachines.
VSphereResourceCPU corev1.ResourceName = "cpu"

// VSphereResourceMemory defines Resource type memory for VSphereMachines.
VSphereResourceMemory corev1.ResourceName = "memory"
)

// VSphereMachineTemplateSpec defines the desired state of VSphereMachineTemplate.
type VSphereMachineTemplateSpec struct {
Template VSphereMachineTemplateResource `json:"template"`
}

// VSphereMachineTemplateStatus defines the observed state of VSphereMachineTemplate.
type VSphereMachineTemplateStatus struct {
// Capacity defines the resource capacity for this VSphereMachineTemplate.
// This value is used for autoscaling from zero operations as defined in:
// https://github.com/kubernetes-sigs/cluster-api/blob/main/docs/proposals/20210310-opt-in-autoscaling-from-zero.md
// +optional
Capacity corev1.ResourceList `json:"capacity,omitempty"`
}

// +kubebuilder:object:root=true
// +kubebuilder:resource:path=vspheremachinetemplates,scope=Namespaced,categories=cluster-api
// +kubebuilder:storageversion
// +kubebuilder:subresource:status

// VSphereMachineTemplate is the Schema for the vspheremachinetemplates API.
type VSphereMachineTemplate struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec VSphereMachineTemplateSpec `json:"spec,omitempty"`
Spec VSphereMachineTemplateSpec `json:"spec,omitempty"`
Status VSphereMachineTemplateStatus `json:"status,omitempty"`
}

// +kubebuilder:object:root=true
Expand Down
23 changes: 23 additions & 0 deletions apis/vmware/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,14 @@ rules:
- get
- list
- watch
- apiGroups:
- vmoperator.vmware.com
resources:
- virtualmachineclasses
verbs:
- get
- list
- watch
- apiGroups:
- vmoperator.vmware.com
resources:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,25 @@ spec:
required:
- template
type: object
status:
description: VSphereMachineTemplateStatus defines the observed state of
VSphereMachineTemplate.
properties:
capacity:
additionalProperties:
anyOf:
- type: integer
- type: string
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
description: |-
Capacity defines the resource capacity for this VSphereMachineTemplate.
This value is used for autoscaling from zero operations as defined in:
https://github.com/kubernetes-sigs/cluster-api/blob/main/docs/proposals/20210310-opt-in-autoscaling-from-zero.md
type: object
type: object
type: object
served: true
storage: true
subresources:
status: {}
5 changes: 5 additions & 0 deletions controllers/vmware/test/controllers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import (
infrav1 "sigs.k8s.io/cluster-api-provider-vsphere/apis/v1beta1"
vmwarev1 "sigs.k8s.io/cluster-api-provider-vsphere/apis/vmware/v1beta1"
"sigs.k8s.io/cluster-api-provider-vsphere/controllers"
"sigs.k8s.io/cluster-api-provider-vsphere/controllers/vmware"
vmwarewebhooks "sigs.k8s.io/cluster-api-provider-vsphere/internal/webhooks/vmware"
"sigs.k8s.io/cluster-api-provider-vsphere/pkg/constants"
capvcontext "sigs.k8s.io/cluster-api-provider-vsphere/pkg/context"
Expand Down Expand Up @@ -259,6 +260,10 @@ func getManager(cfg *rest.Config, networkProvider string, withWebhooks bool) man
return err
}
}
if err := vmware.AddVSphereMachineTemplateControllerToManager(ctx, controllerCtx, mgr, controllerOpts); err != nil {
return err
}

return controllers.AddMachineControllerToManager(ctx, controllerCtx, mgr, true, controllerOpts)
}

Expand Down
121 changes: 121 additions & 0 deletions controllers/vmware/vspheremachinetemplate_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
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 vmware

import (
"context"

"github.com/pkg/errors"
vmoprv1 "github.com/vmware-tanzu/vm-operator/api/v1alpha2"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
"sigs.k8s.io/cluster-api/util/patch"
"sigs.k8s.io/cluster-api/util/predicates"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

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

// +kubebuilder:rbac:groups=vmware.infrastructure.cluster.x-k8s.io,resources=vspheremachinetemplates,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=vmware.infrastructure.cluster.x-k8s.io,resources=vspheremachinetemplates/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=vmoperator.vmware.com,resources=virtualmachineclasses,verbs=get;list;watch

// AddVSphereMachineTemplateControllerToManager adds the machine template controller to the provided
// manager.
func AddVSphereMachineTemplateControllerToManager(ctx context.Context, controllerManagerContext *capvcontext.ControllerManagerContext, mgr manager.Manager, options controller.Options) error {
r := &vSphereMachineTemplateReconciler{
Client: controllerManagerContext.Client,
}

return ctrl.NewControllerManagedBy(mgr).
For(&vmwarev1.VSphereMachineTemplate{}).
WithOptions(options).
Watches(
&vmoprv1.VirtualMachineClass{},
handler.EnqueueRequestsFromMapFunc(r.enqueueVirtualMachineClassToVSphereMachineTemplateRequests),
).
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), controllerManagerContext.WatchFilterValue)).
Complete(r)
}

type vSphereMachineTemplateReconciler struct {
Client client.Client
}

func (r *vSphereMachineTemplateReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Result, reterr error) {
// Fetch VSphereMachineTemplate object
vSphereMachineTemplate := &vmwarev1.VSphereMachineTemplate{}
if err := r.Client.Get(ctx, req.NamespacedName, vSphereMachineTemplate); err != nil {
if apierrors.IsNotFound(err) {
return reconcile.Result{}, nil
}
return reconcile.Result{}, err
}

// Fetch the VirtualMachineClass
vmClass := &vmoprv1.VirtualMachineClass{}
if err := r.Client.Get(ctx, client.ObjectKey{Namespace: req.Namespace, Name: vSphereMachineTemplate.Spec.Template.Spec.ClassName}, vmClass); err != nil {
return reconcile.Result{}, errors.Wrapf(err, "failed to get VirtualMachineClass %q for VSphereMachineTemplate", vSphereMachineTemplate.Spec.Template.Spec.ClassName)
}

patchHelper, err := patch.NewHelper(vSphereMachineTemplate, r.Client)
if err != nil {
return reconcile.Result{}, err
}

if vSphereMachineTemplate.Status.Capacity == nil {
vSphereMachineTemplate.Status.Capacity = corev1.ResourceList{}
}
if vmClass.Spec.Hardware.Cpus > 0 {
vSphereMachineTemplate.Status.Capacity[vmwarev1.VSphereResourceCPU] = *resource.NewQuantity(vmClass.Spec.Hardware.Cpus, resource.DecimalSI)
}
if !vmClass.Spec.Hardware.Memory.IsZero() {
vSphereMachineTemplate.Status.Capacity[vmwarev1.VSphereResourceMemory] = vmClass.Spec.Hardware.Memory
}

return reconcile.Result{}, patchHelper.Patch(ctx, vSphereMachineTemplate)
}

// enqueueVirtualMachineClassToVSphereMachineTemplateRequests returns a list of VSphereMachineTemplate reconcile requests
// that use a specific VirtualMachineClass.
func (r *vSphereMachineTemplateReconciler) enqueueVirtualMachineClassToVSphereMachineTemplateRequests(ctx context.Context, virtualMachineClass client.Object) []reconcile.Request {
requests := []reconcile.Request{}

vSphereMachineTemplates := &vmwarev1.VSphereMachineTemplateList{}
if err := r.Client.List(ctx, vSphereMachineTemplates, client.InNamespace(virtualMachineClass.GetNamespace())); err != nil {
return nil
}

for _, vSphereMachineTemplate := range vSphereMachineTemplates.Items {
if vSphereMachineTemplate.Spec.Template.Spec.ClassName != virtualMachineClass.GetName() {
continue
}

requests = append(requests, reconcile.Request{
NamespacedName: client.ObjectKey{Namespace: vSphereMachineTemplate.Namespace, Name: vSphereMachineTemplate.Name},
})
}

return requests
}
Loading

0 comments on commit 1b7c2dc

Please sign in to comment.