-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for clusters in multiple regions
Adding `ClientProvider` allows clusters to use different `OCIClients` groups to interact with the regional APIs.
- Loading branch information
Showing
20 changed files
with
690 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
/* | ||
Copyright (c) 2021, 2022 Oracle and/or its affiliates. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package scope | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/go-logr/logr" | ||
"github.com/oracle/cluster-api-provider-oci/cloud/config" | ||
"github.com/oracle/cluster-api-provider-oci/cloud/ociutil" | ||
"github.com/oracle/cluster-api-provider-oci/cloud/services/compute" | ||
"github.com/oracle/cluster-api-provider-oci/cloud/services/vcn" | ||
"github.com/oracle/oci-go-sdk/v63/common" | ||
"github.com/oracle/oci-go-sdk/v63/core" | ||
"github.com/oracle/oci-go-sdk/v63/identity" | ||
"github.com/oracle/oci-go-sdk/v63/networkloadbalancer" | ||
"github.com/pkg/errors" | ||
"k8s.io/klog/v2/klogr" | ||
) | ||
|
||
// OCIClients is the struct of all the needed OCI clients | ||
type OCIClients struct { | ||
ComputeClient compute.ComputeClient | ||
VCNClient vcn.Client | ||
LoadBalancerClient *networkloadbalancer.NetworkLoadBalancerClient | ||
IdentityClient *identity.IdentityClient | ||
} | ||
|
||
// ClientProvider defines the regional clients | ||
type ClientProvider struct { | ||
*logr.Logger | ||
ociClients map[string]OCIClients | ||
ociClientsLock *sync.RWMutex | ||
ociAuthConfigProvider common.ConfigurationProvider | ||
authConfig *config.AuthConfig | ||
} | ||
|
||
// NewClientProvider builds the ClientProvider with a client for the given region | ||
func NewClientProvider(authConfig *config.AuthConfig) (*ClientProvider, error) { | ||
if err := ociutil.ValidateAuthConfig(authConfig); err != nil { | ||
return nil, errors.Wrapf(err, "NewClientProvider authConfig is invalid") | ||
} | ||
|
||
log := klogr.New() | ||
|
||
ociAuthConfigProvider, err := config.NewConfigurationProvider(authConfig) | ||
if err != nil { | ||
log.Error(err, "authentication provider could not be initialised") | ||
return nil, err | ||
} | ||
|
||
provider := ClientProvider{ | ||
Logger: &log, | ||
ociAuthConfigProvider: ociAuthConfigProvider, | ||
ociClients: map[string]OCIClients{}, | ||
ociClientsLock: new(sync.RWMutex), | ||
authConfig: authConfig, | ||
} | ||
_, err = provider.GetOrBuildClient(authConfig.Region) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &provider, nil | ||
} | ||
|
||
// GetOrBuildClient if the OCIClients exist for the region they are returned, if not clients will build them | ||
func (c *ClientProvider) GetOrBuildClient(region string) (OCIClients, error) { | ||
if len(region) <= 0 { | ||
return OCIClients{}, errors.New("ClientProvider.GetOrBuildClient region can not be empty") | ||
} | ||
|
||
c.ociClientsLock.RLock() | ||
clients, regionalClientsExists := c.ociClients[region] | ||
c.ociClientsLock.RUnlock() | ||
|
||
if regionalClientsExists { | ||
return clients, nil | ||
} | ||
|
||
regionalAuth := c.authConfig | ||
regionalAuth.Region = region | ||
ociAuthConfigProvider, err := config.NewConfigurationProvider(regionalAuth) | ||
if err != nil { | ||
return OCIClients{}, err | ||
} | ||
|
||
c.ociClientsLock.Lock() | ||
defer c.ociClientsLock.Unlock() | ||
regionalClient, err := createClients(ociAuthConfigProvider, c.Logger) | ||
if err != nil { | ||
return regionalClient, err | ||
} | ||
c.ociClients[region] = regionalClient | ||
|
||
return regionalClient, nil | ||
} | ||
|
||
func createClients(oCIAuthConfigProvider common.ConfigurationProvider, logger *logr.Logger) (OCIClients, error) { | ||
vcnClient, err := createVncClient(oCIAuthConfigProvider, logger) | ||
lbClient, err := createLbClient(oCIAuthConfigProvider, logger) | ||
identityClient, err := createIdentityClient(oCIAuthConfigProvider, logger) | ||
computeClient, err := createComputeClient(oCIAuthConfigProvider, logger) | ||
|
||
if err != nil { | ||
return OCIClients{}, err | ||
} | ||
|
||
return OCIClients{ | ||
VCNClient: vcnClient, | ||
LoadBalancerClient: lbClient, | ||
IdentityClient: identityClient, | ||
ComputeClient: computeClient, | ||
}, err | ||
} | ||
|
||
func createVncClient(ociAuthConfigProvider common.ConfigurationProvider, logger *logr.Logger) (*core.VirtualNetworkClient, error) { | ||
vcnClient, err := core.NewVirtualNetworkClientWithConfigurationProvider(ociAuthConfigProvider) | ||
if err != nil { | ||
logger.Error(err, "unable to create OCI VCN Client") | ||
return nil, err | ||
} | ||
|
||
return &vcnClient, nil | ||
} | ||
|
||
func createLbClient(ociAuthConfigProvider common.ConfigurationProvider, logger *logr.Logger) (*networkloadbalancer.NetworkLoadBalancerClient, error) { | ||
lbClient, err := networkloadbalancer.NewNetworkLoadBalancerClientWithConfigurationProvider(ociAuthConfigProvider) | ||
if err != nil { | ||
logger.Error(err, "unable to create OCI LB Client") | ||
return nil, err | ||
} | ||
|
||
return &lbClient, nil | ||
} | ||
|
||
func createIdentityClient(ociAuthConfigProvider common.ConfigurationProvider, logger *logr.Logger) (*identity.IdentityClient, error) { | ||
identityClient, err := identity.NewIdentityClientWithConfigurationProvider(ociAuthConfigProvider) | ||
if err != nil { | ||
logger.Error(err, "unable to create OCI Identity Client") | ||
return nil, err | ||
} | ||
|
||
return &identityClient, nil | ||
} | ||
|
||
func createComputeClient(ociAuthConfigProvider common.ConfigurationProvider, logger *logr.Logger) (*core.ComputeClient, error) { | ||
computeClient, err := core.NewComputeClientWithConfigurationProvider(ociAuthConfigProvider) | ||
if err != nil { | ||
logger.Error(err, "unable to create OCI Compute Client") | ||
return nil, err | ||
} | ||
|
||
return &computeClient, nil | ||
} |
Oops, something went wrong.