Skip to content

Commit b05cb3c

Browse files
committed
PowerVS: Use PowerVS Stock Catalogue Images
Signed-off-by: Ashwin Hendre <112116232+AshwinHIBM@users.noreply.github.com>
1 parent 1785dc2 commit b05cb3c

3 files changed

Lines changed: 99 additions & 10 deletions

File tree

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package powervs
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/IBM-Cloud/power-go-client/clients/instance"
8+
"github.com/IBM-Cloud/power-go-client/ibmpisession"
9+
"github.com/IBM/go-sdk-core/v5/core"
10+
"github.com/sirupsen/logrus"
11+
12+
powervsconfig "github.com/openshift/installer/pkg/asset/installconfig/powervs"
13+
)
14+
15+
// GetBootImageFromWorkspace retrieves a boot image from the PowerVS workspace.
16+
// If an active image is found in the workspace, it returns that image name.
17+
// If no images are found, it returns a default name in the format "rhcos-{clusterID}".
18+
func GetBootImageFromWorkspace(ctx context.Context, serviceInstanceGUID string, zone string, clusterID string) (string, error) {
19+
// Create a new PowerVS client
20+
client, err := powervsconfig.NewClient()
21+
if err != nil {
22+
return "", fmt.Errorf("failed to create PowerVS client: %w", err)
23+
}
24+
25+
// Create authenticator
26+
authenticator := &core.IamAuthenticator{
27+
ApiKey: client.GetAPIKey(),
28+
}
29+
30+
// Create PI session
31+
piSession, err := ibmpisession.NewIBMPISession(&ibmpisession.IBMPIOptions{
32+
Authenticator: authenticator,
33+
UserAccount: client.BXCli.User.Account,
34+
Zone: zone,
35+
Debug: false,
36+
})
37+
if err != nil {
38+
return "", fmt.Errorf("failed to create PowerVS session: %w", err)
39+
}
40+
41+
// Create image client
42+
imageClient := instance.NewIBMPIImageClient(ctx, piSession, serviceInstanceGUID)
43+
if imageClient == nil {
44+
return "", fmt.Errorf("failed to create PowerVS image client")
45+
}
46+
47+
// Get all images from the workspace
48+
images, err := imageClient.GetAll()
49+
if err != nil {
50+
return "", fmt.Errorf("failed to list images from PowerVS workspace: %w", err)
51+
}
52+
53+
// If no images found in workspace, use default naming pattern
54+
if images == nil || len(images.Images) == 0 {
55+
defaultImage := fmt.Sprintf("rhcos-%s", clusterID)
56+
logrus.Infof("No images found in PowerVS workspace, using default image name: %s", defaultImage)
57+
return defaultImage, nil
58+
}
59+
60+
// Find the first active image
61+
for _, image := range images.Images {
62+
if image.State != nil && *image.State == "active" && image.Name != nil {
63+
logrus.Infof("Selected PowerVS boot image from workspace: %s", *image.Name)
64+
return *image.Name, nil
65+
}
66+
}
67+
68+
// If no active images found, use default naming pattern
69+
defaultImage := fmt.Sprintf("rhcos-%s", clusterID)
70+
logrus.Infof("No active images found in PowerVS workspace, using default image name: %s", defaultImage)
71+
return defaultImage, nil
72+
}
73+
74+
// Made with Bob

pkg/asset/machines/powervs/machinesets.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
package powervs
33

44
import (
5+
"context"
56
"fmt"
67

8+
"github.com/sirupsen/logrus"
79
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
810
"k8s.io/apimachinery/pkg/runtime"
911

@@ -25,7 +27,14 @@ func MachineSets(clusterID string, config *types.InstallConfig, pool *types.Mach
2527
platform := config.Platform.PowerVS
2628
mpool := pool.Platform.PowerVS
2729
var network string
28-
image := fmt.Sprintf("rhcos-%s", clusterID)
30+
31+
// Get the boot image from the PowerVS workspace, fallback to default if error occurs
32+
image, err := GetBootImageFromWorkspace(context.TODO(), config.PowerVS.ServiceInstanceGUID, config.PowerVS.Zone, clusterID)
33+
if err != nil {
34+
// Fallback to default image naming pattern
35+
image = fmt.Sprintf("rhcos-%s", clusterID)
36+
logrus.Warnf("Failed to get boot image from PowerVS workspace, using default: %s (error: %v)", image, err)
37+
}
2938

3039
total := int32(0)
3140
if pool.Replicas != nil {

pkg/asset/machines/powervs/powervsmachines.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
package powervs
33

44
import (
5+
"context"
56
"fmt"
67

8+
"github.com/sirupsen/logrus"
79
v1 "k8s.io/api/core/v1"
810
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
911
"k8s.io/utils/ptr"
@@ -37,10 +39,16 @@ func GenerateMachines(clusterID string, ic *types.InstallConfig, pool *types.Mac
3739
powerVSMachine *capibm.IBMPowerVSMachine
3840
dataSecret string
3941
machine *capi.Machine
42+
err error
4043
)
4144

42-
// Note: This will be created later
43-
image = fmt.Sprintf("rhcos-%s", clusterID)
45+
// Get the boot image from the PowerVS workspace, fallback to default if error occurs
46+
image, err = GetBootImageFromWorkspace(context.TODO(), ic.PowerVS.ServiceInstanceGUID, ic.PowerVS.Zone, clusterID)
47+
if err != nil {
48+
// Fallback to default image naming pattern
49+
image = fmt.Sprintf("rhcos-%s", clusterID)
50+
logrus.Warnf("Failed to get boot image from PowerVS workspace, using default: %s (error: %v)", image, err)
51+
}
4452

4553
if ic.PowerVS.ServiceInstanceGUID == "" {
4654
serviceName := fmt.Sprintf("%s-power-iaas", clusterID)
@@ -112,13 +120,11 @@ func GenerateMachine(ic *types.InstallConfig, service capibm.IBMPowerVSResourceR
112120
ServiceInstanceID: ic.PowerVS.ServiceInstanceGUID,
113121
ServiceInstance: &service,
114122
SSHKey: "",
115-
ImageRef: &v1.LocalObjectReference{
116-
Name: image,
117-
},
118-
SystemType: mpool.SysType,
119-
ProcessorType: capibm.PowerVSProcessorType(mpool.ProcType),
120-
Processors: mpool.Processors,
121-
MemoryGiB: mpool.MemoryGiB,
123+
Image: &capibm.IBMPowerVSResourceReference{Name: &image},
124+
SystemType: mpool.SysType,
125+
ProcessorType: capibm.PowerVSProcessorType(mpool.ProcType),
126+
Processors: mpool.Processors,
127+
MemoryGiB: mpool.MemoryGiB,
122128
},
123129
}
124130
utils.SetMachineOSStreamLabels(machine, ic)

0 commit comments

Comments
 (0)