github.com/verrazzano/verrazzano@v1.7.0/cluster-operator/controllers/quickcreate/controller/oci/network/network.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 network
     5  
     6  import (
     7  	"context"
     8  	"errors"
     9  	vmcv1alpha1 "github.com/verrazzano/verrazzano/cluster-operator/apis/clusters/v1alpha1"
    10  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    11  	"k8s.io/apimachinery/pkg/runtime/schema"
    12  	"k8s.io/apimachinery/pkg/types"
    13  	clipkg "sigs.k8s.io/controller-runtime/pkg/client"
    14  )
    15  
    16  var (
    17  	GVKOCICluster = schema.GroupVersionKind{
    18  		Group:   "infrastructure.cluster.x-k8s.io",
    19  		Version: "v1beta2",
    20  		Kind:    "ocicluster",
    21  	}
    22  	GVKOCIManagedCluster = schema.GroupVersionKind{
    23  		Group:   "infrastructure.cluster.x-k8s.io",
    24  		Version: "v1beta2",
    25  		Kind:    "ocimanagedcluster",
    26  	}
    27  )
    28  
    29  func GetNetwork(ctx context.Context, cli clipkg.Client, o clipkg.Object, gvk schema.GroupVersionKind) (*vmcv1alpha1.Network, error) {
    30  	network := &vmcv1alpha1.Network{}
    31  	ociCluster := &unstructured.Unstructured{}
    32  	ociCluster.SetGroupVersionKind(gvk)
    33  	err := cli.Get(ctx, types.NamespacedName{
    34  		Namespace: o.GetNamespace(),
    35  		Name:      o.GetName(),
    36  	}, ociCluster)
    37  	if err != nil {
    38  		return nil, err
    39  	}
    40  	// Get VCN id and VCN subnets from the OCI Cluster resource
    41  	vcnID, found, err := unstructured.NestedString(ociCluster.Object, "spec", "networkSpec", "vcn", "id")
    42  	if err != nil || !found {
    43  		return nil, errors.New("waiting for VCN to be created")
    44  	}
    45  	network.VCN = vcnID
    46  	subnets, found, err := unstructured.NestedSlice(ociCluster.Object, "spec", "networkSpec", "vcn", "subnets")
    47  	if err != nil || !found {
    48  		return nil, errors.New("waiting for subnets to be created")
    49  	}
    50  
    51  	// For each subnet in the VCN subnet list, identify its role and populate the subnet id in the cluster state
    52  	for _, subnet := range subnets {
    53  		subnetObject, ok := subnet.(map[string]interface{})
    54  		if !ok {
    55  			return nil, errors.New("subnet is creating")
    56  		}
    57  
    58  		// Get nested subnet Role and id from the subnet object
    59  		subnetRole, found, err := unstructured.NestedString(subnetObject, "role")
    60  		if err != nil || !found {
    61  			return nil, errors.New("waiting for subnet role to be populated")
    62  		}
    63  		subnetID, found, err := unstructured.NestedString(subnetObject, "id")
    64  		if err != nil || !found {
    65  			return nil, errors.New("waiting for subnet id to be populated")
    66  		}
    67  
    68  		network.Subnets = append(network.Subnets, vmcv1alpha1.Subnet{
    69  			Role: vmcv1alpha1.SubnetRole(subnetRole),
    70  			ID:   subnetID,
    71  		})
    72  	}
    73  	// No network can be loaded currently
    74  	return network, nil
    75  }
    76  
    77  func GetLoadBalancerSubnet(network *vmcv1alpha1.Network) string {
    78  	if network == nil {
    79  		return ""
    80  	}
    81  	for _, subnet := range network.Subnets {
    82  		if subnet.Role == vmcv1alpha1.SubnetRoleServiceLB {
    83  			return subnet.ID
    84  		}
    85  	}
    86  	return ""
    87  }