|
| 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 |
0 commit comments