github.com/verrazzano/verrazzano@v1.7.0/cluster-operator/controllers/quickcreate/ociocne/properties.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 ociocne
     5  
     6  import (
     7  	"context"
     8  	"fmt"
     9  	vmcv1alpha1 "github.com/verrazzano/verrazzano/cluster-operator/apis/clusters/v1alpha1"
    10  	"github.com/verrazzano/verrazzano/cluster-operator/controllers/quickcreate/controller/oci"
    11  	ocicommon "github.com/verrazzano/verrazzano/cluster-operator/controllers/quickcreate/controller/oci/common"
    12  	ocinetwork "github.com/verrazzano/verrazzano/cluster-operator/controllers/quickcreate/controller/oci/network"
    13  	"github.com/verrazzano/verrazzano/cluster-operator/controllers/quickcreate/controller/ocne"
    14  	"github.com/verrazzano/verrazzano/pkg/k8sutil"
    15  	corev1 "k8s.io/api/core/v1"
    16  	"k8s.io/apimachinery/pkg/types"
    17  	clipkg "sigs.k8s.io/controller-runtime/pkg/client"
    18  )
    19  
    20  type (
    21  	//Properties contains all the properties for rendering OCI OCNE Cluster templates.
    22  	Properties struct {
    23  		ocicommon.Values
    24  		*ocne.VersionDefaults
    25  		vmcv1alpha1.OCIOCNEClusterSpec
    26  		LoadBalancerSubnet string
    27  		ProviderID         string
    28  		DockerConfigJSON   string
    29  	}
    30  )
    31  
    32  // NewProperties creates a new properties object based on the quick create resource.
    33  func NewProperties(ctx context.Context, cli clipkg.Client, loader oci.CredentialsLoader, ociClientGetter func(creds *oci.Credentials) (oci.Client, error), q *vmcv1alpha1.OCNEOCIQuickCreate) (*Properties, error) {
    34  	// Get the OCNE Versions
    35  	versions, err := ocne.GetVersionDefaults(ctx, cli, q.Spec.OCNE.Version)
    36  	if err != nil {
    37  		return nil, err
    38  	}
    39  	// Try to load the credentials, if allowed
    40  	creds, err := loader.GetCredentialsIfAllowed(ctx, cli, q.Spec.IdentityRef.AsNamespacedName(), q.Namespace)
    41  	if err != nil {
    42  		return nil, err
    43  	}
    44  	props := &Properties{
    45  		Values: ocicommon.Values{
    46  			Name:            q.Name,
    47  			Namespace:       q.Namespace,
    48  			Credentials:     creds,
    49  			Network:         q.Spec.OCI.Network,
    50  			OCIClientGetter: ociClientGetter,
    51  		},
    52  		VersionDefaults:    versions,
    53  		OCIOCNEClusterSpec: q.Spec,
    54  		ProviderID:         oci.ProviderID,
    55  	}
    56  	if err := props.SetCommonValues(ctx, cli, q, ocinetwork.GVKOCICluster); err != nil {
    57  		return nil, err
    58  	}
    59  	if props.HasImagePullSecret() {
    60  		if err := props.SetDockerConfigJSON(ctx, cli); err != nil {
    61  			return nil, err
    62  		}
    63  	}
    64  	// Set LoadBalancerSubnet for simple lookup. Will be empty string if the network has not created yet.
    65  	props.LoadBalancerSubnet = ocinetwork.GetLoadBalancerSubnet(props.Network)
    66  	return props, nil
    67  }
    68  
    69  func (p *Properties) ApplyTemplate(cli clipkg.Client, templates ...[]byte) error {
    70  	applier := k8sutil.NewYAMLApplier(cli, "")
    71  	for _, tmpl := range templates {
    72  		if err := applier.ApplyBT(tmpl, p); err != nil {
    73  			return err
    74  		}
    75  	}
    76  	return nil
    77  }
    78  
    79  func (p *Properties) IsControlPlaneOnly() bool {
    80  	return len(p.OCI.Workers) < 1
    81  }
    82  
    83  func (p *Properties) HasImagePullSecret() bool {
    84  	return p.PrivateRegistry != nil && len(p.PrivateRegistry.CredentialsSecret.Name) > 0
    85  }
    86  
    87  func (p *Properties) SetDockerConfigJSON(ctx context.Context, cli clipkg.Client) error {
    88  	secret := &corev1.Secret{}
    89  	if err := cli.Get(ctx, types.NamespacedName{
    90  		Namespace: p.PrivateRegistry.CredentialsSecret.Namespace,
    91  		Name:      p.PrivateRegistry.CredentialsSecret.Name,
    92  	}, secret); err != nil {
    93  		return err
    94  	}
    95  	if secret.Data == nil {
    96  		return fmt.Errorf("failed to load private registry credentials from secret %s/%s", p.PrivateRegistry.CredentialsSecret.Namespace, p.PrivateRegistry.CredentialsSecret.Name)
    97  	}
    98  	dockerConfigJSON, ok := secret.Data[".dockerconfigjson"]
    99  	if !ok {
   100  		return fmt.Errorf("no private registry credentials found in secret %s/%s", p.PrivateRegistry.CredentialsSecret.Namespace, p.PrivateRegistry.CredentialsSecret.Name)
   101  	}
   102  	p.DockerConfigJSON = string(dockerConfigJSON)
   103  	return nil
   104  }