Skip to content

Commit f1c7088

Browse files
add support for image and network name (#441)
Signed-off-by: Prajyot-Parab <[email protected]>
1 parent d46cff7 commit f1c7088

12 files changed

+197
-43
lines changed

api/v1alpha4/ibmpowervscluster_types.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ type IBMPowerVSClusterSpec struct {
3838
// ServiceInstanceID is the id of the power cloud instance where the vsi instance will get deployed
3939
ServiceInstanceID string `json:"serviceInstanceID"`
4040

41-
// Network is network ID used for the VSI
42-
NetworkID string `json:"networkID"`
41+
// Network is the reference to the Network to use for this cluster.
42+
Network IBMPowerVSResourceReference `json:"network"`
4343

4444
// ControlPlaneEndpoint represents the endpoint used to communicate with the control plane.
4545
// +optional

api/v1alpha4/ibmpowervsmachine_types.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,8 @@ type IBMPowerVSMachineSpec struct {
4141
// SSHKey is the name of the SSH key pair provided to the vsi for authenticating users
4242
SSHKey string `json:"sshKey,omitempty"`
4343

44-
// ImageID is the id of OS image which would be install on the instance.
45-
// Example: c57eef35-ea0b-45d7-b864-4b0d4893425b
46-
ImageID string `json:"imageID"`
44+
// Image is the reference to the Image from which to create the machine instance.
45+
Image IBMPowerVSResourceReference `json:"image"`
4746

4847
// SysType is the System type used to host the vsi
4948
SysType string `json:"sysType"`
@@ -57,14 +56,27 @@ type IBMPowerVSMachineSpec struct {
5756
// Memory is Amount of memory allocated (in GB)
5857
Memory string `json:"memory"`
5958

60-
// NetworkID is network ID used for the VSI
61-
NetworkID string `json:"networkID"`
59+
// Network is the reference to the Network to use for this instance.
60+
Network IBMPowerVSResourceReference `json:"network"`
6261

6362
// ProviderID is the unique identifier as specified by the cloud provider.
6463
// +optional
6564
ProviderID *string `json:"providerID,omitempty"`
6665
}
6766

67+
// IBMPowerVSResourceReference is a reference to a specific PowerVS resource by ID or Name
68+
// Only one of ID or Name may be specified. Specifying more than one will result in
69+
// a validation error.
70+
type IBMPowerVSResourceReference struct {
71+
// ID of resource
72+
// +optional
73+
ID *string `json:"id,omitempty"`
74+
75+
// Name of resource
76+
// +optional
77+
Name *string `json:"name,omitempty"`
78+
}
79+
6880
// IBMPowerVSMachineStatus defines the observed state of IBMPowerVSMachine
6981
type IBMPowerVSMachineStatus struct {
7082
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster

api/v1alpha4/zz_generated.deepcopy.go

Lines changed: 29 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cloud/scope/powervs_clients.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type IBMPowerVSClient struct {
3131
session *ibmpisession.IBMPISession
3232
InstanceClient *instance.IBMPIInstanceClient
3333
NetworkClient *instance.IBMPINetworkClient
34+
ImageClient *instance.IBMPIImageClient
3435
}
3536

3637
// NewIBMPowerVSClient creates and returns a IBM Power VS client
@@ -43,5 +44,6 @@ func NewIBMPowerVSClient(token, account, cloudInstanceID, region, zone string, d
4344

4445
client.InstanceClient = instance.NewIBMPIInstanceClient(client.session, cloudInstanceID)
4546
client.NetworkClient = instance.NewIBMPINetworkClient(client.session, cloudInstanceID)
47+
client.ImageClient = instance.NewIBMPIImageClient(client.session, cloudInstanceID)
4648
return client, nil
4749
}

cloud/scope/powervs_machine.go

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ import (
2525

2626
"github.com/go-logr/logr"
2727
"github.com/pkg/errors"
28+
utils "github.com/ppc64le-cloud/powervs-utils"
2829

29-
"github.com/ppc64le-cloud/powervs-utils"
30-
30+
"github.com/IBM-Cloud/power-go-client/ibmpisession"
31+
"github.com/IBM-Cloud/power-go-client/power/client/p_cloud_networks"
3132
"github.com/IBM-Cloud/power-go-client/power/client/p_cloud_p_vm_instances"
3233
"github.com/IBM-Cloud/power-go-client/power/models"
3334

@@ -157,13 +158,23 @@ func (m *PowerVSMachineScope) CreateMachine() (*models.PVMInstanceReference, err
157158
return nil, fmt.Errorf("failed to convert Processors(%s) to float64", s.Processors)
158159
}
159160

161+
imageID, err := getImageID(s.Image, m)
162+
if err != nil {
163+
return nil, fmt.Errorf("error getting image ID: %v", err)
164+
}
165+
166+
networkID, err := getNetworkID(s.Network, m)
167+
if err != nil {
168+
return nil, fmt.Errorf("error getting network ID: %v", err)
169+
}
170+
160171
params := &p_cloud_p_vm_instances.PcloudPvminstancesPostParams{
161172
Body: &models.PVMInstanceCreate{
162-
ImageID: &s.ImageID,
173+
ImageID: imageID,
163174
KeyPairName: s.SSHKey,
164175
Networks: []*models.PVMInstanceAddNetwork{
165176
{
166-
NetworkID: &s.NetworkID,
177+
NetworkID: networkID,
167178
//IPAddress: address,
168179
},
169180
},
@@ -216,3 +227,60 @@ func (m *PowerVSMachineScope) GetBootstrapData() (string, error) {
216227

217228
return base64.StdEncoding.EncodeToString(value), nil
218229
}
230+
231+
func getImageID(image v1alpha4.IBMPowerVSResourceReference, m *PowerVSMachineScope) (*string, error) {
232+
if image.ID != nil {
233+
return image.ID, nil
234+
} else if image.Name != nil {
235+
images, err := m.GetImages()
236+
if err != nil {
237+
m.Logger.Error(err, "failed to get images")
238+
return nil, err
239+
}
240+
for _, img := range images.Images {
241+
if *image.Name == *img.Name {
242+
m.Logger.Info("image found with ID", "Image", *image.Name, "ID", *img.ImageID)
243+
return img.ImageID, nil
244+
}
245+
}
246+
} else {
247+
return nil, fmt.Errorf("both ID and Name can't be nil")
248+
}
249+
return nil, fmt.Errorf("failed to find an image ID")
250+
}
251+
252+
func (m *PowerVSMachineScope) GetImages() (*models.Images, error) {
253+
return m.IBMPowerVSClient.ImageClient.GetAll(m.IBMPowerVSMachine.Spec.ServiceInstanceID)
254+
}
255+
256+
func getNetworkID(network v1alpha4.IBMPowerVSResourceReference, m *PowerVSMachineScope) (*string, error) {
257+
if network.ID != nil {
258+
return network.ID, nil
259+
} else if network.Name != nil {
260+
networks, err := m.GetNetworks()
261+
if err != nil {
262+
m.Logger.Error(err, "failed to get networks")
263+
return nil, err
264+
}
265+
for _, nw := range networks.Networks {
266+
if *network.Name == *nw.Name {
267+
m.Logger.Info("network found with ID", "Network", *network.Name, "ID", *nw.NetworkID)
268+
return nw.NetworkID, nil
269+
}
270+
}
271+
} else {
272+
return nil, fmt.Errorf("both ID and Name can't be nil")
273+
}
274+
275+
return nil, fmt.Errorf("failed to find a network ID")
276+
}
277+
278+
func (m *PowerVSMachineScope) GetNetworks() (*models.Networks, error) {
279+
params := p_cloud_networks.NewPcloudNetworksGetallParamsWithTimeout(TIMEOUT).WithCloudInstanceID(m.IBMPowerVSMachine.Spec.ServiceInstanceID)
280+
resp, err := m.IBMPowerVSClient.session.Power.PCloudNetworks.PcloudNetworksGetall(params, ibmpisession.NewAuth(m.IBMPowerVSClient.session, m.IBMPowerVSMachine.Spec.ServiceInstanceID))
281+
282+
if err != nil || resp.Payload == nil {
283+
return nil, err
284+
}
285+
return resp.Payload, nil
286+
}

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,23 @@ spec:
5151
- host
5252
- port
5353
type: object
54-
networkID:
55-
description: Network is network ID used for the VSI
56-
type: string
54+
network:
55+
description: Network is the reference to the Network to use for this
56+
cluster.
57+
properties:
58+
id:
59+
description: ID of resource
60+
type: string
61+
name:
62+
description: Name of resource
63+
type: string
64+
type: object
5765
serviceInstanceID:
5866
description: ServiceInstanceID is the id of the power cloud instance
5967
where the vsi instance will get deployed
6068
type: string
6169
required:
62-
- networkID
70+
- network
6371
- serviceInstanceID
6472
type: object
6573
status:

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

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,31 @@ spec:
5353
spec:
5454
description: IBMPowerVSMachineSpec defines the desired state of IBMPowerVSMachine
5555
properties:
56-
imageID:
57-
description: 'ImageID is the id of OS image which would be install
58-
on the instance. Example: c57eef35-ea0b-45d7-b864-4b0d4893425b'
59-
type: string
56+
image:
57+
description: Image is the reference to the Image from which to create
58+
the machine instance.
59+
properties:
60+
id:
61+
description: ID of resource
62+
type: string
63+
name:
64+
description: Name of resource
65+
type: string
66+
type: object
6067
memory:
6168
description: Memory is Amount of memory allocated (in GB)
6269
type: string
63-
networkID:
64-
description: NetworkID is network ID used for the VSI
65-
type: string
70+
network:
71+
description: Network is the reference to the Network to use for this
72+
instance.
73+
properties:
74+
id:
75+
description: ID of resource
76+
type: string
77+
name:
78+
description: Name of resource
79+
type: string
80+
type: object
6681
procType:
6782
description: 'ProcType is the processor type, e.g: dedicated, shared,
6883
capped'
@@ -86,9 +101,9 @@ spec:
86101
description: SysType is the System type used to host the vsi
87102
type: string
88103
required:
89-
- imageID
104+
- image
90105
- memory
91-
- networkID
106+
- network
92107
- procType
93108
- processors
94109
- serviceInstanceID

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

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,31 @@ spec:
4646
description: IBMPowerVSMachineSpec defines the desired state of
4747
IBMPowerVSMachine
4848
properties:
49-
imageID:
50-
description: 'ImageID is the id of OS image which would be
51-
install on the instance. Example: c57eef35-ea0b-45d7-b864-4b0d4893425b'
52-
type: string
49+
image:
50+
description: Image is the reference to the Image from which
51+
to create the machine instance.
52+
properties:
53+
id:
54+
description: ID of resource
55+
type: string
56+
name:
57+
description: Name of resource
58+
type: string
59+
type: object
5360
memory:
5461
description: Memory is Amount of memory allocated (in GB)
5562
type: string
56-
networkID:
57-
description: NetworkID is network ID used for the VSI
58-
type: string
63+
network:
64+
description: Network is the reference to the Network to use
65+
for this instance.
66+
properties:
67+
id:
68+
description: ID of resource
69+
type: string
70+
name:
71+
description: Name of resource
72+
type: string
73+
type: object
5974
procType:
6075
description: 'ProcType is the processor type, e.g: dedicated,
6176
shared, capped'
@@ -79,9 +94,9 @@ spec:
7994
description: SysType is the System type used to host the vsi
8095
type: string
8196
required:
82-
- imageID
97+
- image
8398
- memory
84-
- networkID
99+
- network
85100
- procType
86101
- processors
87102
- serviceInstanceID

docs/book/src/topics/powervs/creating-a-cluster.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@
101101
IBMPOWERVS_VIP="192.168.151.22" \
102102
IBMPOWERVS_VIP_EXTERNAL="158.175.162.22" \
103103
IBMPOWERVS_VIP_CIDR="29" \
104-
IBMPOWERVS_IMAGE_ID="505f57d8-1143-4a99-b67f-7e82d73342bf" \
104+
IBMPOWERVS_IMAGE_NAME="capibm-powervs-centos-8-1-22-4" \
105105
IBMPOWERVS_SERVICE_INSTANCE_ID="7845d372-d4e1-46b8-91fc-41051c984601" \
106-
IBMPOWERVS_NETWORK_ID="0ad342f5-f461-414a-a870-e2f2a2b7fa0c" \
106+
IBMPOWERVS_NETWORK_NAME="capi-test-3" \
107107
clusterctl generate cluster ibm-powervs-1 --kubernetes-version v1.22.4 \
108108
--target-namespace default \
109109
--control-plane-machine-count=3 \

pkg/authenticator.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package pkg
1818

1919
import (
2020
"fmt"
21+
2122
"github.com/IBM/go-sdk-core/v5/core"
2223
)
2324

0 commit comments

Comments
 (0)