Skip to content

Commit 7150fb8

Browse files
Scale from and to 0 support for autoscaler for VPC cluster (#1523)
* Added support VPC autoscaler * Update controllers/ibmvpcmachinetemplate_controller_test.go Co-authored-by: Prajyot Parab <[email protected]> * Removed duplicate and updated error message --------- Co-authored-by: Prajyot Parab <[email protected]>
1 parent ea18e5b commit 7150fb8

14 files changed

+514
-2
lines changed

api/v1beta1/ibmvpc_conversion.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,10 @@ func Convert_v1beta2_IBMVPCMachineSpec_To_v1beta1_IBMVPCMachineSpec(in *infrav1b
196196
return autoConvert_v1beta2_IBMVPCMachineSpec_To_v1beta1_IBMVPCMachineSpec(in, out, s)
197197
}
198198

199+
func Convert_v1beta2_IBMVPCMachineTemplateStatus_To_v1beta1_IBMVPCMachineTemplateStatus(in *infrav1beta2.IBMVPCMachineTemplateStatus, out *IBMVPCMachineTemplateStatus, s apiconversion.Scope) error {
200+
return autoConvert_v1beta2_IBMVPCMachineTemplateStatus_To_v1beta1_IBMVPCMachineTemplateStatus(in, out, s)
201+
}
202+
199203
func Convert_Slice_Pointer_string_To_Slice_Pointer_v1beta2_IBMVPCResourceReference(in *[]*string, out *[]*infrav1beta2.IBMVPCResourceReference, _ apiconversion.Scope) error {
200204
for _, sshKey := range *in {
201205
*out = append(*out, &infrav1beta2.IBMVPCResourceReference{

api/v1beta1/ibmvpcmachinetemplate_types.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ type IBMVPCMachineTemplateResource struct {
3131
Spec IBMVPCMachineSpec `json:"spec"`
3232
}
3333

34+
// IBMVPCMachineTemplateStatus defines the observed state of IBMVPCMachineTemplate.
35+
type IBMVPCMachineTemplateStatus struct {
36+
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
37+
// Important: Run "make" to regenerate code after modifying this file
38+
}
39+
3440
// +kubebuilder:object:root=true
3541
// +kubebuilder:resource:path=ibmvpcmachinetemplates,scope=Namespaced,categories=cluster-api
3642

@@ -39,7 +45,8 @@ type IBMVPCMachineTemplate struct {
3945
metav1.TypeMeta `json:",inline"`
4046
metav1.ObjectMeta `json:"metadata,omitempty"`
4147

42-
Spec IBMVPCMachineTemplateSpec `json:"spec,omitempty"`
48+
Spec IBMVPCMachineTemplateSpec `json:"spec,omitempty"`
49+
Status IBMVPCMachineTemplateStatus `json:"status,omitempty"`
4350
}
4451

4552
//+kubebuilder:object:root=true

api/v1beta1/zz_generated.conversion.go

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/v1beta1/zz_generated.deepcopy.go

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/v1beta2/ibmvpcmachinetemplate_types.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package v1beta2
1818

1919
import (
20+
corev1 "k8s.io/api/core/v1"
2021
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2122
)
2223

@@ -31,6 +32,16 @@ type IBMVPCMachineTemplateResource struct {
3132
Spec IBMVPCMachineSpec `json:"spec"`
3233
}
3334

35+
// IBMVPCMachineTemplateStatus defines the observed state of IBMVPCMachineTemplate.
36+
type IBMVPCMachineTemplateStatus struct {
37+
// Capacity defines the resource capacity for this machine.
38+
// This value is used for autoscaling from zero operations as defined in:
39+
// https://github.com/kubernetes-sigs/cluster-api/blob/main/docs/proposals/20210310-opt-in-autoscaling-from-zero.md
40+
// +optional
41+
Capacity corev1.ResourceList `json:"capacity,omitempty"`
42+
}
43+
44+
//+kubebuilder:subresource:status
3445
// +kubebuilder:object:root=true
3546
// +kubebuilder:resource:path=ibmvpcmachinetemplates,scope=Namespaced,categories=cluster-api
3647
// +kubebuilder:storageversion
@@ -40,7 +51,8 @@ type IBMVPCMachineTemplate struct {
4051
metav1.TypeMeta `json:",inline"`
4152
metav1.ObjectMeta `json:"metadata,omitempty"`
4253

43-
Spec IBMVPCMachineTemplateSpec `json:"spec,omitempty"`
54+
Spec IBMVPCMachineTemplateSpec `json:"spec,omitempty"`
55+
Status IBMVPCMachineTemplateStatus `json:"status,omitempty"`
4456
}
4557

4658
//+kubebuilder:object:root=true

api/v1beta2/zz_generated.deepcopy.go

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/infrastructure.cluster.x-k8s.io_ibmvpcmachinetemplates.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ spec:
147147
required:
148148
- template
149149
type: object
150+
status:
151+
description: IBMVPCMachineTemplateStatus defines the observed state of
152+
IBMVPCMachineTemplate.
153+
type: object
150154
type: object
151155
served: true
152156
storage: false
@@ -296,6 +300,24 @@ spec:
296300
required:
297301
- template
298302
type: object
303+
status:
304+
description: IBMVPCMachineTemplateStatus defines the observed state of
305+
IBMVPCMachineTemplate.
306+
properties:
307+
capacity:
308+
additionalProperties:
309+
anyOf:
310+
- type: integer
311+
- type: string
312+
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
313+
x-kubernetes-int-or-string: true
314+
description: 'Capacity defines the resource capacity for this machine.
315+
This value is used for autoscaling from zero operations as defined
316+
in: https://github.com/kubernetes-sigs/cluster-api/blob/main/docs/proposals/20210310-opt-in-autoscaling-from-zero.md'
317+
type: object
318+
type: object
299319
type: object
300320
served: true
301321
storage: true
322+
subresources:
323+
status: {}

config/rbac/role.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,19 @@ rules:
169169
- get
170170
- patch
171171
- update
172+
- apiGroups:
173+
- infrastructure.cluster.x-k8s.io
174+
resources:
175+
- ibmvpcmachinetemplates
176+
verbs:
177+
- get
178+
- list
179+
- watch
180+
- apiGroups:
181+
- infrastructure.cluster.x-k8s.io
182+
resources:
183+
- ibmvpcmachinetemplates/status
184+
verbs:
185+
- get
186+
- patch
187+
- update
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
Copyright 2023 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package controllers
18+
19+
import (
20+
"context"
21+
"fmt"
22+
"reflect"
23+
24+
"github.com/IBM/vpc-go-sdk/vpcv1"
25+
26+
corev1 "k8s.io/api/core/v1"
27+
apierrors "k8s.io/apimachinery/pkg/api/errors"
28+
"k8s.io/apimachinery/pkg/api/resource"
29+
"k8s.io/apimachinery/pkg/runtime"
30+
31+
ctrl "sigs.k8s.io/controller-runtime"
32+
"sigs.k8s.io/controller-runtime/pkg/client"
33+
34+
"sigs.k8s.io/cluster-api/util/patch"
35+
36+
infrav1beta2 "sigs.k8s.io/cluster-api-provider-ibmcloud/api/v1beta2"
37+
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/vpc"
38+
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/endpoints"
39+
)
40+
41+
// IBMVPCMachineTemplateReconciler reconciles a IBMVPCMachineTemplate object.
42+
type IBMVPCMachineTemplateReconciler struct {
43+
client.Client
44+
Scheme *runtime.Scheme
45+
ServiceEndpoint []endpoints.ServiceEndpoint
46+
}
47+
48+
func (r *IBMVPCMachineTemplateReconciler) SetupWithManager(mgr ctrl.Manager) error {
49+
return ctrl.NewControllerManagedBy(mgr).
50+
For(&infrav1beta2.IBMVPCMachineTemplate{}).
51+
Complete(r)
52+
}
53+
54+
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=ibmvpcmachinetemplates,verbs=get;list;watch
55+
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=ibmvpcmachinetemplates/status,verbs=get;update;patch
56+
57+
func (r *IBMVPCMachineTemplateReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
58+
log := ctrl.LoggerFrom(ctx)
59+
log.Info("Reconciling IBMVPCMachineTemplate")
60+
61+
var machineTemplate infrav1beta2.IBMVPCMachineTemplate
62+
if err := r.Get(ctx, req.NamespacedName, &machineTemplate); err != nil {
63+
log.Error(err, "Unable to fetch ibmvpcmachinetemplate")
64+
return ctrl.Result{}, client.IgnoreNotFound(err)
65+
}
66+
67+
region := endpoints.CostructRegionFromZone(machineTemplate.Spec.Template.Spec.Zone)
68+
69+
// Fetch the service endpoint.
70+
svcEndpoint := endpoints.FetchVPCEndpoint(region, r.ServiceEndpoint)
71+
72+
vpcClient, err := vpc.NewService(svcEndpoint)
73+
if err != nil {
74+
return ctrl.Result{}, fmt.Errorf("failed to create IBM VPC client: %w", err)
75+
}
76+
77+
return r.reconcileNormal(ctx, vpcClient, machineTemplate)
78+
}
79+
80+
func (r *IBMVPCMachineTemplateReconciler) reconcileNormal(ctx context.Context, vpcClient vpc.Vpc, machineTemplate infrav1beta2.IBMVPCMachineTemplate) (ctrl.Result, error) {
81+
log := ctrl.LoggerFrom(ctx)
82+
helper, err := patch.NewHelper(&machineTemplate, r.Client)
83+
if err != nil {
84+
return ctrl.Result{}, fmt.Errorf("failed to init patch helper: %w", err)
85+
}
86+
87+
options := &vpcv1.GetInstanceProfileOptions{}
88+
options.SetName(machineTemplate.Spec.Template.Spec.Profile)
89+
profileDetails, _, err := vpcClient.GetInstanceProfile(options)
90+
if err != nil {
91+
return ctrl.Result{}, fmt.Errorf("failed to fetch profile Details: %w", err)
92+
}
93+
94+
if profileDetails == nil {
95+
return ctrl.Result{}, fmt.Errorf("failed to find profileDetails")
96+
}
97+
98+
log.V(3).Info("Profile Details:", "profileDetails", profileDetails)
99+
100+
capacity := make(corev1.ResourceList)
101+
memory := fmt.Sprintf("%vG", *profileDetails.Memory.(*vpcv1.InstanceProfileMemory).Value)
102+
cpu := fmt.Sprintf("%v", *profileDetails.VcpuCount.(*vpcv1.InstanceProfileVcpu).Value)
103+
capacity[corev1.ResourceCPU] = resource.MustParse(cpu)
104+
capacity[corev1.ResourceMemory] = resource.MustParse(memory)
105+
106+
log.V(3).Info("Calculated capacity for machine template", "capacity", capacity)
107+
if !reflect.DeepEqual(machineTemplate.Status.Capacity, capacity) {
108+
machineTemplate.Status.Capacity = capacity
109+
if err := helper.Patch(ctx, &machineTemplate); err != nil {
110+
if !apierrors.IsNotFound(err) {
111+
log.Error(err, "Failed to patch machineTemplate")
112+
return ctrl.Result{}, err
113+
}
114+
}
115+
}
116+
log.V(3).Info("Machine template status", "status", machineTemplate.Status)
117+
return ctrl.Result{}, nil
118+
}

0 commit comments

Comments
 (0)