Skip to content

Commit

Permalink
Added flag to select ProviderID format for Machine (#580)
Browse files Browse the repository at this point in the history
* Added flag to select ProviderID format for Machine

* Added generated files
  • Loading branch information
Karthik-K-N authored Mar 2, 2022
1 parent bcb0b1e commit 78b9d0e
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 68 deletions.
2 changes: 2 additions & 0 deletions api/v1alpha4/zz_generated.conversion.go

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

6 changes: 6 additions & 0 deletions api/v1beta1/ibmpowervsmachine_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ type IBMPowerVSMachineStatus struct {
// Conditions defines current service state of the IBMPowerVSMachine.
// +optional
Conditions clusterv1.Conditions `json:"conditions,omitempty"`

// Region specifies the Power VS Service instance region
Region *string `json:"region,omitempty"`

// Zone specifies the Power VS Service instance zone
Zone *string `json:"zone,omitempty"`
}

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

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

44 changes: 38 additions & 6 deletions cloud/scope/powervs_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@ import (
"strconv"
"strings"

"k8s.io/utils/pointer"

"github.com/go-logr/logr"
"github.com/pkg/errors"
utils "github.com/ppc64le-cloud/powervs-utils"
powerVSUtils "github.com/ppc64le-cloud/powervs-utils"

"github.com/IBM-Cloud/power-go-client/ibmpisession"
"github.com/IBM-Cloud/power-go-client/power/client/p_cloud_p_vm_instances"
Expand All @@ -38,6 +36,8 @@ import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog/v2/klogr"
"k8s.io/utils/pointer"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/options"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/util/patch"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -144,11 +144,13 @@ func NewPowerVSMachineScope(params PowerVSMachineScopeParams) (scope *PowerVSMac
return
}

region, err := utils.GetRegion(*res.RegionID)
region, err := powerVSUtils.GetRegion(*res.RegionID)
if err != nil {
err = errors.Wrap(err, "failed to get region")
return
}
scope.SetRegion(region)
scope.SetZone(*res.RegionID)

options := powervs.ServiceOptions{
IBMPIOptions: &ibmpisession.IBMPIOptions{
Expand Down Expand Up @@ -397,6 +399,36 @@ func (m *PowerVSMachineScope) GetInstanceState() v1beta1.PowerVSInstanceState {
return m.IBMPowerVSMachine.Status.InstanceState
}

func (m *PowerVSMachineScope) SetProviderID() {
m.IBMPowerVSMachine.Spec.ProviderID = pointer.StringPtr(fmt.Sprintf("ibmpowervs://%s/%s", m.Machine.Spec.ClusterName, m.IBMPowerVSMachine.Name))
func (m *PowerVSMachineScope) SetRegion(region string) {
m.IBMPowerVSMachine.Status.Region = &region
}

func (m *PowerVSMachineScope) GetRegion() string {
if m.IBMPowerVSMachine.Status.Region == nil {
return ""
}
return *m.IBMPowerVSMachine.Status.Region
}

func (m *PowerVSMachineScope) SetZone(zone string) {
m.IBMPowerVSMachine.Status.Zone = &zone
}

func (m *PowerVSMachineScope) GetZone() string {
if m.IBMPowerVSMachine.Status.Zone == nil {
return ""
}
return *m.IBMPowerVSMachine.Status.Zone
}

// SetProviderID will set the provider id for the machine
func (m *PowerVSMachineScope) SetProviderID(id *string) {
// Based on the ProviderIDFormat version the providerID format will be decided
if options.PowerVSProviderIDFormat == options.PowerVSProviderIDFormatV2 {
if id != nil {
m.IBMPowerVSMachine.Spec.ProviderID = pointer.StringPtr(fmt.Sprintf("ibmpowervs://%s/%s/%s/%s", m.GetRegion(), m.GetZone(), m.IBMPowerVSMachine.Spec.ServiceInstanceID, *id))
}
} else {
m.IBMPowerVSMachine.Spec.ProviderID = pointer.StringPtr(fmt.Sprintf("ibmpowervs://%s/%s", m.Machine.Spec.ClusterName, m.IBMPowerVSMachine.Name))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -205,37 +205,9 @@ spec:
resource that holds the details for provisioning the Image for a
Cluster.
properties:
apiVersion:
description: API version of the referent.
type: string
fieldPath:
description: 'If referring to a piece of an object instead of
an entire object, this string should contain a valid JSON/Go
field access statement, such as desiredState.manifest.containers[2].
For example, if the object reference is to a container within
a pod, this would take on a value like: "spec.containers{name}"
(where "name" refers to the name of the container that triggered
the event) or if no container name is specified "spec.containers[2]"
(container with index 2 in this pod). This syntax is chosen
only to have some well-defined way of referencing a part of
an object. TODO: this design is not final and this field is
subject to change in the future.'
type: string
kind:
description: 'Kind of the referent. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
type: string
name:
description: 'Name of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names'
type: string
namespace:
description: 'Namespace of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/'
type: string
resourceVersion:
description: 'Specific resourceVersion to which this reference
is made, if any. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#concurrency-control-and-consistency'
type: string
uid:
description: 'UID of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#uids'
description: 'Name of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names
TODO: Add other useful fields. apiVersion, kind, uid?'
type: string
type: object
memory:
Expand Down Expand Up @@ -390,6 +362,12 @@ spec:
ready:
description: Ready is true when the provider resource is ready.
type: boolean
region:
description: Region specifies the Power VS Service instance region
type: string
zone:
description: Zone specifies the Power VS Service instance zone
type: string
type: object
type: object
served: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,38 +162,9 @@ spec:
resource that holds the details for provisioning the Image
for a Cluster.
properties:
apiVersion:
description: API version of the referent.
type: string
fieldPath:
description: 'If referring to a piece of an object instead
of an entire object, this string should contain a valid
JSON/Go field access statement, such as desiredState.manifest.containers[2].
For example, if the object reference is to a container
within a pod, this would take on a value like: "spec.containers{name}"
(where "name" refers to the name of the container that
triggered the event) or if no container name is specified
"spec.containers[2]" (container with index 2 in this
pod). This syntax is chosen only to have some well-defined
way of referencing a part of an object. TODO: this design
is not final and this field is subject to change in
the future.'
type: string
kind:
description: 'Kind of the referent. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
type: string
name:
description: 'Name of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names'
type: string
namespace:
description: 'Namespace of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/'
type: string
resourceVersion:
description: 'Specific resourceVersion to which this reference
is made, if any. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#concurrency-control-and-consistency'
type: string
uid:
description: 'UID of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#uids'
description: 'Name of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names
TODO: Add other useful fields. apiVersion, kind, uid?'
type: string
type: object
memory:
Expand Down
2 changes: 1 addition & 1 deletion controllers/ibmpowervsmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ func (r *IBMPowerVSMachineReconciler) reconcileNormal(ctx context.Context, machi
if err != nil {
return ctrl.Result{}, err
}
machineScope.SetProviderID(instance.PvmInstanceID)
machineScope.SetInstanceID(instance.PvmInstanceID)
machineScope.SetAddresses(instance)
machineScope.SetHealth(instance.Health)
Expand All @@ -227,7 +228,6 @@ func (r *IBMPowerVSMachineReconciler) reconcileNormal(ctx context.Context, machi
}
machineScope.Info(*ins.PvmInstanceID)
}
machineScope.SetProviderID()

// Requeue after 2 minute if machine is not ready to update status of the machine properly
if !machineScope.IsReady() {
Expand Down
27 changes: 27 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package main

import (
"flag"
"fmt"
"os"
"time"

Expand All @@ -35,6 +36,7 @@ import (
infrastructurev1alpha4 "sigs.k8s.io/cluster-api-provider-ibmcloud/api/v1alpha4"
infrastructurev1beta1 "sigs.k8s.io/cluster-api-provider-ibmcloud/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-ibmcloud/controllers"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/options"
// +kubebuilder:scaffold:imports
)

Expand Down Expand Up @@ -68,6 +70,11 @@ func main() {

ctrl.SetLogger(klogr.New())

if err := validateFlags(); err != nil {
setupLog.Error(err, "Flag validation failure")
os.Exit(1)
}

if watchNamespace != "" {
setupLog.Info("Watching cluster-api objects only in namespace for reconciliation", "namespace", watchNamespace)
}
Expand Down Expand Up @@ -210,4 +217,24 @@ func initFlags(fs *pflag.FlagSet) {
10*time.Minute,
"The minimum interval at which watched resources are reconciled.",
)

fs.StringVar(
&options.PowerVSProviderIDFormat,
"powervs-provider-id-fmt",
options.PowerVSProviderIDFormatV1,
"ProviderID format is used set the Provider ID format for Machine",
)
}

func validateFlags() error {
switch options.PowerVSProviderIDFormat {
case options.PowerVSProviderIDFormatV1:
setupLog.Info("Using v1 version of ProviderID format")
case options.PowerVSProviderIDFormatV2:
setupLog.Info("Using v2 version of ProviderID format")
default:
errStr := fmt.Errorf("Invalid value for flag powervs-provider-id-fmt: %s, Supported values: v1, v2 ", options.PowerVSProviderIDFormat)
return errStr
}
return nil
}
26 changes: 26 additions & 0 deletions pkg/options/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
Copyright 2022 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 options

// PowerVSProviderIDFormat is used to identify the Provider ID format for Machine
var PowerVSProviderIDFormat string

// PowerVSProviderIDFormatV2 will set provider id to machine as ibmpowervs://<region>/<zone>/<service_instance_id>/<powervs_machine_id>
const PowerVSProviderIDFormatV2 = "v2"

// PowerVSProviderIDFormatV1 will set provider id to machine as ibmpowervs://<cluster_name>/<vm_hostname>
const PowerVSProviderIDFormatV1 = "v1"

0 comments on commit 78b9d0e

Please sign in to comment.