|
| 1 | +/* |
| 2 | +Copyright (c) 2022, Oracle and/or its affiliates. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package scope |
| 18 | + |
| 19 | +import ( |
| 20 | + "sync" |
| 21 | + |
| 22 | + "github.com/go-logr/logr" |
| 23 | + "github.com/oracle/cluster-api-provider-oci/cloud/services/compute" |
| 24 | + identityClient "github.com/oracle/cluster-api-provider-oci/cloud/services/identity" |
| 25 | + nlb "github.com/oracle/cluster-api-provider-oci/cloud/services/networkloadbalancer" |
| 26 | + "github.com/oracle/cluster-api-provider-oci/cloud/services/vcn" |
| 27 | + "github.com/oracle/oci-go-sdk/v63/common" |
| 28 | + "github.com/oracle/oci-go-sdk/v63/core" |
| 29 | + "github.com/oracle/oci-go-sdk/v63/identity" |
| 30 | + "github.com/oracle/oci-go-sdk/v63/networkloadbalancer" |
| 31 | + "github.com/pkg/errors" |
| 32 | + "k8s.io/klog/v2/klogr" |
| 33 | +) |
| 34 | + |
| 35 | +// OCIClients is the struct of all the needed OCI clients |
| 36 | +type OCIClients struct { |
| 37 | + ComputeClient compute.ComputeClient |
| 38 | + VCNClient vcn.Client |
| 39 | + LoadBalancerClient nlb.NetworkLoadBalancerClient |
| 40 | + IdentityClient identityClient.Client |
| 41 | +} |
| 42 | + |
| 43 | +// ClientProvider defines the regional clients |
| 44 | +type ClientProvider struct { |
| 45 | + *logr.Logger |
| 46 | + ociClients map[string]OCIClients |
| 47 | + ociClientsLock *sync.RWMutex |
| 48 | + ociAuthConfigProvider common.ConfigurationProvider |
| 49 | +} |
| 50 | + |
| 51 | +// NewClientProvider builds the ClientProvider with a client for the given region |
| 52 | +func NewClientProvider(ociAuthConfigProvider common.ConfigurationProvider) (*ClientProvider, error) { |
| 53 | + log := klogr.New() |
| 54 | + |
| 55 | + if ociAuthConfigProvider == nil { |
| 56 | + return nil, errors.New("ConfigurationProvider can not be nil") |
| 57 | + } |
| 58 | + |
| 59 | + provider := ClientProvider{ |
| 60 | + Logger: &log, |
| 61 | + ociAuthConfigProvider: ociAuthConfigProvider, |
| 62 | + ociClients: map[string]OCIClients{}, |
| 63 | + ociClientsLock: new(sync.RWMutex), |
| 64 | + } |
| 65 | + |
| 66 | + return &provider, nil |
| 67 | +} |
| 68 | + |
| 69 | +// GetOrBuildClient if the OCIClients exist for the region they are returned, if not clients will build them |
| 70 | +func (c *ClientProvider) GetOrBuildClient(region string) (OCIClients, error) { |
| 71 | + if len(region) <= 0 { |
| 72 | + return OCIClients{}, errors.New("ClientProvider.GetOrBuildClient region can not be empty") |
| 73 | + } |
| 74 | + |
| 75 | + c.ociClientsLock.RLock() |
| 76 | + clients, regionalClientsExists := c.ociClients[region] |
| 77 | + c.ociClientsLock.RUnlock() |
| 78 | + |
| 79 | + if regionalClientsExists { |
| 80 | + return clients, nil |
| 81 | + } |
| 82 | + |
| 83 | + c.ociClientsLock.Lock() |
| 84 | + defer c.ociClientsLock.Unlock() |
| 85 | + regionalClient, err := createClients(region, c.ociAuthConfigProvider, c.Logger) |
| 86 | + if err != nil { |
| 87 | + return regionalClient, err |
| 88 | + } |
| 89 | + c.ociClients[region] = regionalClient |
| 90 | + |
| 91 | + return regionalClient, nil |
| 92 | +} |
| 93 | + |
| 94 | +func createClients(region string, oCIAuthConfigProvider common.ConfigurationProvider, logger *logr.Logger) (OCIClients, error) { |
| 95 | + vcnClient, err := createVncClient(region, oCIAuthConfigProvider, logger) |
| 96 | + lbClient, err := createLbClient(region, oCIAuthConfigProvider, logger) |
| 97 | + identityClient, err := createIdentityClient(region, oCIAuthConfigProvider, logger) |
| 98 | + computeClient, err := createComputeClient(region, oCIAuthConfigProvider, logger) |
| 99 | + |
| 100 | + if err != nil { |
| 101 | + return OCIClients{}, err |
| 102 | + } |
| 103 | + |
| 104 | + return OCIClients{ |
| 105 | + VCNClient: vcnClient, |
| 106 | + LoadBalancerClient: lbClient, |
| 107 | + IdentityClient: identityClient, |
| 108 | + ComputeClient: computeClient, |
| 109 | + }, err |
| 110 | +} |
| 111 | + |
| 112 | +func createVncClient(region string, ociAuthConfigProvider common.ConfigurationProvider, logger *logr.Logger) (*core.VirtualNetworkClient, error) { |
| 113 | + vcnClient, err := core.NewVirtualNetworkClientWithConfigurationProvider(ociAuthConfigProvider) |
| 114 | + if err != nil { |
| 115 | + logger.Error(err, "unable to create OCI VCN Client") |
| 116 | + return nil, err |
| 117 | + } |
| 118 | + vcnClient.SetRegion(region) |
| 119 | + |
| 120 | + return &vcnClient, nil |
| 121 | +} |
| 122 | + |
| 123 | +func createLbClient(region string, ociAuthConfigProvider common.ConfigurationProvider, logger *logr.Logger) (*networkloadbalancer.NetworkLoadBalancerClient, error) { |
| 124 | + lbClient, err := networkloadbalancer.NewNetworkLoadBalancerClientWithConfigurationProvider(ociAuthConfigProvider) |
| 125 | + if err != nil { |
| 126 | + logger.Error(err, "unable to create OCI LB Client") |
| 127 | + return nil, err |
| 128 | + } |
| 129 | + lbClient.SetRegion(region) |
| 130 | + |
| 131 | + return &lbClient, nil |
| 132 | +} |
| 133 | + |
| 134 | +func createIdentityClient(region string, ociAuthConfigProvider common.ConfigurationProvider, logger *logr.Logger) (*identity.IdentityClient, error) { |
| 135 | + identityClient, err := identity.NewIdentityClientWithConfigurationProvider(ociAuthConfigProvider) |
| 136 | + if err != nil { |
| 137 | + logger.Error(err, "unable to create OCI Identity Client") |
| 138 | + return nil, err |
| 139 | + } |
| 140 | + identityClient.SetRegion(region) |
| 141 | + |
| 142 | + return &identityClient, nil |
| 143 | +} |
| 144 | + |
| 145 | +func createComputeClient(region string, ociAuthConfigProvider common.ConfigurationProvider, logger *logr.Logger) (*core.ComputeClient, error) { |
| 146 | + computeClient, err := core.NewComputeClientWithConfigurationProvider(ociAuthConfigProvider) |
| 147 | + if err != nil { |
| 148 | + logger.Error(err, "unable to create OCI Compute Client") |
| 149 | + return nil, err |
| 150 | + } |
| 151 | + computeClient.SetRegion(region) |
| 152 | + |
| 153 | + return &computeClient, nil |
| 154 | +} |
0 commit comments