github.com/ironcore-dev/gardener-extension-provider-ironcore@v0.3.2-0.20240314231816-8336447fb9a0/pkg/ironcore/helper/helper.go (about)

     1  // SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and IronCore contributors
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package helper
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"k8s.io/utils/ptr"
    10  
    11  	api "github.com/ironcore-dev/gardener-extension-provider-ironcore/pkg/apis/ironcore"
    12  	apiv1alpha1 "github.com/ironcore-dev/gardener-extension-provider-ironcore/pkg/apis/ironcore/v1alpha1"
    13  )
    14  
    15  // FindMachineImage takes a list of machine images and tries to find the first entry
    16  // whose name, version, architecture and zone matches with the given name, version, and zone. If no such entry is
    17  // found then an error will be returned.
    18  func FindMachineImage(machineImages []apiv1alpha1.MachineImage, name, version string, architecture *string) (*apiv1alpha1.MachineImage, error) {
    19  	for _, machineImage := range machineImages {
    20  		if machineImage.Name == name && machineImage.Version == version && ptr.Equal[string](architecture, machineImage.Architecture) {
    21  			return &machineImage, nil
    22  		}
    23  	}
    24  	return nil, fmt.Errorf("no machine image found with name %q and version %q", name, version)
    25  }
    26  
    27  // FindImageFromCloudProfile takes a list of machine images, and the desired image name and version. It tries
    28  // to find the image with the given name, architecture and version in the desired cloud profile. If it cannot be found then an error
    29  // is returned.
    30  func FindImageFromCloudProfile(cloudProfileConfig *api.CloudProfileConfig, imageName, imageVersion string, architecture *string) (string, error) {
    31  	if cloudProfileConfig != nil {
    32  		for _, machineImage := range cloudProfileConfig.MachineImages {
    33  			if machineImage.Name != imageName {
    34  				continue
    35  			}
    36  			for _, version := range machineImage.Versions {
    37  				if imageVersion == version.Version && ptr.Equal[string](architecture, version.Architecture) {
    38  					return version.Image, nil
    39  				}
    40  			}
    41  		}
    42  	}
    43  
    44  	return "", fmt.Errorf("could not find an image for name %q and in version %q", imageName, imageVersion)
    45  }