github.com/verrazzano/verrazzano@v1.7.0/platform-operator/internal/operatorinit/versions.go (about)

     1  // Copyright (c) 2023, Oracle and/or its affiliates.
     2  // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     3  
     4  package operatorinit
     5  
     6  import (
     7  	"context"
     8  	"encoding/json"
     9  	"errors"
    10  	"github.com/verrazzano/verrazzano/pkg/bom"
    11  	"github.com/verrazzano/verrazzano/pkg/k8sutil"
    12  	vpoconst "github.com/verrazzano/verrazzano/platform-operator/constants"
    13  	"github.com/verrazzano/verrazzano/platform-operator/internal/config"
    14  	corev1 "k8s.io/api/core/v1"
    15  	apierrors "k8s.io/apimachinery/pkg/api/errors"
    16  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    17  )
    18  
    19  const (
    20  	configMapName    = "verrazzano-meta"
    21  	configMapDataKey = "verrazzano-versions"
    22  )
    23  
    24  func CreateVerrazzanoVersionsConfigMap(ctx context.Context) error {
    25  	client, err := k8sutil.GetCoreV1Func()
    26  	if err != nil {
    27  		return err
    28  	}
    29  
    30  	data, err := getVersionConfigMapData()
    31  	if err != nil {
    32  		return err
    33  	}
    34  	cmData := map[string]string{
    35  		configMapDataKey: data,
    36  	}
    37  	cm, err := client.ConfigMaps(vpoconst.VerrazzanoInstallNamespace).Get(ctx, configMapName, metav1.GetOptions{})
    38  	if err != nil && apierrors.IsNotFound(err) {
    39  		if apierrors.IsNotFound(err) {
    40  			_, err := client.ConfigMaps(vpoconst.VerrazzanoInstallNamespace).Create(ctx, &corev1.ConfigMap{
    41  				ObjectMeta: metav1.ObjectMeta{
    42  					Name: configMapName,
    43  				},
    44  				Data: cmData,
    45  			}, metav1.CreateOptions{})
    46  			return err
    47  		}
    48  		return err
    49  	}
    50  	cm.Data = cmData
    51  	_, err = client.ConfigMaps(vpoconst.VerrazzanoInstallNamespace).Update(ctx, cm, metav1.UpdateOptions{})
    52  	return err
    53  }
    54  
    55  func getVersionConfigMapData() (string, error) {
    56  	b, err := bom.NewBom(config.GetDefaultBOMFilePath())
    57  	if err != nil {
    58  		return "", err
    59  	}
    60  
    61  	comp, err := b.GetComponent(vpoconst.VerrazzanoPlatformOperatorHelmName)
    62  	if err != nil {
    63  		return "", err
    64  	}
    65  	var vpoTag = ""
    66  	for _, sc := range comp.SubComponents {
    67  		if sc.Name == vpoconst.VerrazzanoPlatformOperatorHelmName {
    68  			vpoTag = sc.Images[0].ImageTag
    69  		}
    70  	}
    71  	if vpoTag == "" {
    72  		return "", errors.New("failed to find image tag for Platform Operator")
    73  	}
    74  
    75  	versions := map[string]string{
    76  		b.GetVersion(): vpoTag,
    77  	}
    78  
    79  	data, err := json.Marshal(versions)
    80  	if err != nil {
    81  		return "", err
    82  	}
    83  	return string(data), nil
    84  }