github.com/verrazzano/verrazzano@v1.7.0/cluster-operator/controllers/quickcreate/controller/ocne/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 ocne
     5  
     6  import (
     7  	"context"
     8  	"errors"
     9  	"fmt"
    10  	corev1 "k8s.io/api/core/v1"
    11  	"k8s.io/apimachinery/pkg/types"
    12  	clipkg "sigs.k8s.io/controller-runtime/pkg/client"
    13  	"sigs.k8s.io/yaml"
    14  )
    15  
    16  type (
    17  	VersionDefaults struct {
    18  		Release         string `json:"Release"`
    19  		ContainerImages struct {
    20  			Calico         string `json:"calico"`
    21  			CoreDNS        string `json:"coredns"`
    22  			ETCD           string `json:"etcd"`
    23  			TigeraOperator string `json:"tigera-operator" yaml:"tigera-operator"`
    24  		} `json:"container-images" yaml:"container-images"`
    25  		KubernetesVersion string `json:"-"`
    26  	}
    27  )
    28  
    29  const (
    30  	ocneConfigMapName      = "ocne-metadata"
    31  	ocneConfigMapNamespace = "verrazzano-capi"
    32  )
    33  
    34  func GetVersionDefaults(ctx context.Context, cli clipkg.Client, ocneVersion string) (*VersionDefaults, error) {
    35  	versions, err := getVersionMapping(ctx, cli)
    36  	if err != nil {
    37  		return nil, err
    38  	}
    39  	for k, v := range versions {
    40  		if ocneVersion == v.Release {
    41  			v.KubernetesVersion = k
    42  			return v, nil
    43  		}
    44  	}
    45  	return nil, fmt.Errorf("no verion mapping found for OCNE version %s", ocneVersion)
    46  }
    47  
    48  func getVersionMapping(ctx context.Context, cli clipkg.Client) (map[string]*VersionDefaults, error) {
    49  	cm := &corev1.ConfigMap{}
    50  	err := cli.Get(ctx, types.NamespacedName{
    51  		Namespace: ocneConfigMapNamespace,
    52  		Name:      ocneConfigMapName,
    53  	}, cm)
    54  	if err != nil {
    55  		return nil, err
    56  	}
    57  	if cm.Data == nil {
    58  		return nil, errors.New("no OCNE Metadata")
    59  	}
    60  	mapping := cm.Data["mapping"]
    61  	if len(mapping) < 1 {
    62  		return nil, errors.New("no OCNE version mapping found")
    63  	}
    64  	versions := map[string]*VersionDefaults{}
    65  	if err := yaml.Unmarshal([]byte(mapping), &versions); err != nil {
    66  		return nil, err
    67  	}
    68  	if len(versions) < 1 {
    69  		return nil, errors.New("no OCNE kubernetes versions found")
    70  	}
    71  	return versions, nil
    72  }