Skip to content

Commit 2896e44

Browse files
prydieowainlewis
authored andcommitted
Move Config out of client pkg (#171)
1 parent d0d6558 commit 2896e44

11 files changed

+68
-75
lines changed

pkg/oci/ccm.go

+35-5
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@
1717
package oci
1818

1919
import (
20+
"context"
2021
"fmt"
2122
"io"
2223

2324
"time"
2425

2526
"github.com/golang/glog"
27+
"github.com/oracle/oci-go-sdk/common"
28+
"github.com/pkg/errors"
2629

2730
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
2831
wait "k8s.io/apimachinery/pkg/util/wait"
@@ -34,6 +37,7 @@ import (
3437
controller "k8s.io/kubernetes/pkg/controller"
3538

3639
"github.com/oracle/oci-cloud-controller-manager/pkg/oci/client"
40+
"github.com/oracle/oci-cloud-controller-manager/pkg/oci/instancemeta"
3741
"github.com/oracle/oci-cloud-controller-manager/pkg/oci/util"
3842
)
3943

@@ -54,35 +58,61 @@ type CloudProvider struct {
5458
kubeclient clientset.Interface
5559

5660
securityListManager securityListManager
57-
config *client.Config
61+
config *Config
5862
}
5963

6064
// Compile time check that CloudProvider implements the cloudprovider.Interface
6165
// interface.
6266
var _ cloudprovider.Interface = &CloudProvider{}
6367

6468
// NewCloudProvider creates a new oci.CloudProvider.
65-
func NewCloudProvider(cfg *client.Config) (cloudprovider.Interface, error) {
66-
c, err := client.New(cfg)
69+
func NewCloudProvider(config *Config) (cloudprovider.Interface, error) {
70+
c, err := client.New(common.NewRawConfigurationProvider(
71+
config.Auth.TenancyOCID,
72+
config.Auth.UserOCID,
73+
config.Auth.Region,
74+
config.Auth.Fingerprint,
75+
config.Auth.PrivateKey,
76+
&config.Auth.PrivateKeyPassphrase,
77+
))
6778
if err != nil {
6879
return nil, err
6980
}
7081

82+
if config.Auth.CompartmentOCID == "" {
83+
glog.Info("Compartment not supplied in config: attempting to infer from instance metadata")
84+
metadata, err := instancemeta.New().Get()
85+
if err != nil {
86+
return nil, err
87+
}
88+
config.Auth.CompartmentOCID = metadata.CompartmentOCID
89+
}
90+
91+
if config.VCNID == "" {
92+
glog.Infof("No vcn provided in cloud provider config. Falling back to looking up VCN via LB subnet.")
93+
subnet, err := c.Networking().GetSubnet(context.Background(), config.LoadBalancer.Subnet1)
94+
if err != nil {
95+
return nil, errors.Wrap(err, "get subnet for loadBalancer.subnet1")
96+
}
97+
config.VCNID = *subnet.VcnId
98+
}
99+
71100
return &CloudProvider{
72101
client: c,
73-
config: cfg,
102+
config: config,
74103
}, nil
75104
}
76105

77106
func init() {
78107
cloudprovider.RegisterCloudProvider(ProviderName(), func(config io.Reader) (cloudprovider.Interface, error) {
79-
cfg, err := client.ReadConfig(config)
108+
cfg, err := ReadConfig(config)
80109
if err != nil {
81110
return nil, err
82111
}
83112
if err = cfg.Validate(); err != nil {
84113
return nil, err
85114
}
115+
86116
return NewCloudProvider(cfg)
87117
})
88118
}

pkg/oci/client/client.go

+1-38
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package client
1616

1717
import (
18-
"context"
1918
"crypto/tls"
2019
"crypto/x509"
2120
"io/ioutil"
@@ -32,8 +31,6 @@ import (
3231
"github.com/oracle/oci-go-sdk/core"
3332
"github.com/oracle/oci-go-sdk/loadbalancer"
3433
"github.com/pkg/errors"
35-
36-
"github.com/oracle/oci-cloud-controller-manager/pkg/oci/instancemeta"
3734
)
3835

3936
// Interface of consumed OCI API functionality.
@@ -44,27 +41,15 @@ type Interface interface {
4441
}
4542

4643
type client struct {
47-
config *Config
48-
4944
compute *core.ComputeClient
5045
network *core.VirtualNetworkClient
5146
loadbalancer *loadbalancer.LoadBalancerClient
5247

53-
vcnID string
54-
5548
subnetCache cache.Store
5649
}
5750

5851
// New constructs an OCI API client.
59-
func New(config *Config) (Interface, error) {
60-
cp := common.NewRawConfigurationProvider(
61-
config.Auth.TenancyOCID,
62-
config.Auth.UserOCID,
63-
config.Auth.Region,
64-
config.Auth.Fingerprint,
65-
config.Auth.PrivateKey,
66-
&config.Auth.PrivateKeyPassphrase,
67-
)
52+
func New(cp common.ConfigurationProvider) (Interface, error) {
6853
compute, err := core.NewComputeClientWithConfigurationProvider(cp)
6954
if err != nil {
7055
return nil, errors.Wrap(err, "NewComputeClientWithConfigurationProvider")
@@ -96,35 +81,13 @@ func New(config *Config) (Interface, error) {
9681
}
9782

9883
c := &client{
99-
config: config,
100-
10184
compute: &compute,
10285
network: &network,
10386
loadbalancer: &lb,
10487

10588
subnetCache: cache.NewTTLStore(subnetCacheKeyFn, time.Duration(24)*time.Hour),
10689
}
10790

108-
if config.Auth.CompartmentOCID == "" {
109-
glog.Info("Compartment not supplied in config: attempting to infer from instance metadata")
110-
metadata, err := instancemeta.New().Get()
111-
if err != nil {
112-
return nil, err
113-
}
114-
config.Auth.CompartmentOCID = metadata.CompartmentOCID
115-
}
116-
117-
vcnID := config.VCNID
118-
if vcnID == "" {
119-
glog.Infof("No vcn provided in cloud provider config. Falling back to looking up VCN via LB subnet.")
120-
subnet, err := c.GetSubnet(context.Background(), config.LoadBalancer.Subnet1)
121-
if err != nil {
122-
return nil, errors.Wrap(err, "get subnet for loadBalancer.subnet1")
123-
}
124-
vcnID = *subnet.VcnId
125-
}
126-
c.vcnID = vcnID
127-
12891
return c, nil
12992
}
13093

pkg/oci/client/compute.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ type ComputeInterface interface {
2929
GetInstance(ctx context.Context, id string) (*core.Instance, error)
3030
// GetInstanceByNodeName gets the OCI instance corresponding to the given
3131
// Kubernetes node name.
32-
GetInstanceByNodeName(ctx context.Context, nodeName string) (*core.Instance, error)
32+
GetInstanceByNodeName(ctx context.Context, compartmentID, vcnID, nodeName string) (*core.Instance, error)
3333

34-
GetPrimaryVNICForInstance(ctx context.Context, instanceID string) (*core.Vnic, error)
34+
GetPrimaryVNICForInstance(ctx context.Context, compartmentID, instanceID string) (*core.Vnic, error)
3535
}
3636

3737
func (c *client) GetInstance(ctx context.Context, id string) (*core.Instance, error) {
@@ -47,14 +47,14 @@ func (c *client) GetInstance(ctx context.Context, id string) (*core.Instance, er
4747
return &resp.Instance, nil
4848
}
4949

50-
func (c *client) getInstanceByDisplayName(ctx context.Context, displayName string) (*core.Instance, error) {
50+
func (c *client) getInstanceByDisplayName(ctx context.Context, compartmentID, displayName string) (*core.Instance, error) {
5151
var (
5252
page *string
5353
instances []core.Instance
5454
)
5555
for {
5656
resp, err := c.compute.ListInstances(ctx, core.ListInstancesRequest{
57-
CompartmentId: &c.config.Auth.CompartmentOCID,
57+
CompartmentId: &compartmentID,
5858
DisplayName: &displayName,
5959
Page: page,
6060
})
@@ -91,12 +91,12 @@ func (c *client) listVNICAttachments(ctx context.Context, req core.ListVnicAttac
9191
return resp, nil
9292
}
9393

94-
func (c *client) GetPrimaryVNICForInstance(ctx context.Context, instanceID string) (*core.Vnic, error) {
94+
func (c *client) GetPrimaryVNICForInstance(ctx context.Context, compartmentID, instanceID string) (*core.Vnic, error) {
9595
var page *string
9696
for {
9797
resp, err := c.listVNICAttachments(ctx, core.ListVnicAttachmentsRequest{
9898
InstanceId: &instanceID,
99-
CompartmentId: &c.config.Auth.CompartmentOCID,
99+
CompartmentId: &compartmentID,
100100
Page: page,
101101
})
102102

@@ -129,9 +129,9 @@ func (c *client) GetPrimaryVNICForInstance(ctx context.Context, instanceID strin
129129
return nil, errors.WithStack(errNotFound)
130130
}
131131

132-
func (c *client) GetInstanceByNodeName(ctx context.Context, nodeName string) (*core.Instance, error) {
132+
func (c *client) GetInstanceByNodeName(ctx context.Context, compartmentID, vcnID, nodeName string) (*core.Instance, error) {
133133
// First try lookup by display name.
134-
instance, err := c.getInstanceByDisplayName(ctx, nodeName)
134+
instance, err := c.getInstanceByDisplayName(ctx, compartmentID, nodeName)
135135
if err == nil {
136136
return instance, nil
137137
}
@@ -143,7 +143,7 @@ func (c *client) GetInstanceByNodeName(ctx context.Context, nodeName string) (*c
143143
)
144144
for {
145145
resp, err := c.listVNICAttachments(ctx, core.ListVnicAttachmentsRequest{
146-
CompartmentId: &c.config.Auth.CompartmentOCID,
146+
CompartmentId: &compartmentID,
147147
Page: page,
148148
})
149149
if err != nil {
@@ -167,7 +167,7 @@ func (c *client) GetInstanceByNodeName(ctx context.Context, nodeName string) (*c
167167
if err != nil {
168168
return nil, err
169169
}
170-
if *subnet.VcnId != c.vcnID {
170+
if *subnet.VcnId != vcnID {
171171
continue
172172
}
173173

pkg/oci/client/load_balancer.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const workRequestPollInterval = 5 * time.Second
3030
type LoadBalancerInterface interface {
3131
CreateLoadBalancer(ctx context.Context, details loadbalancer.CreateLoadBalancerDetails) (string, error)
3232
GetLoadBalancer(ctx context.Context, id string) (*loadbalancer.LoadBalancer, error)
33-
GetLoadBalancerByName(ctx context.Context, name string) (*loadbalancer.LoadBalancer, error)
33+
GetLoadBalancerByName(ctx context.Context, compartmentID, name string) (*loadbalancer.LoadBalancer, error)
3434
DeleteLoadBalancer(ctx context.Context, id string) (string, error)
3535

3636
GetCertificateByName(ctx context.Context, lbID, name string) (*loadbalancer.Certificate, error)
@@ -60,11 +60,11 @@ func (c *client) GetLoadBalancer(ctx context.Context, id string) (*loadbalancer.
6060
return &resp.LoadBalancer, nil
6161
}
6262

63-
func (c *client) GetLoadBalancerByName(ctx context.Context, name string) (*loadbalancer.LoadBalancer, error) {
63+
func (c *client) GetLoadBalancerByName(ctx context.Context, compartmentID, name string) (*loadbalancer.LoadBalancer, error) {
6464
var page *string
6565
for {
6666
resp, err := c.loadbalancer.ListLoadBalancers(ctx, loadbalancer.ListLoadBalancersRequest{
67-
CompartmentId: &c.config.Auth.CompartmentOCID,
67+
CompartmentId: &compartmentID,
6868
DisplayName: &name,
6969
Page: page,
7070
})

pkg/oci/client/config.go renamed to pkg/oci/config.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package client
15+
package oci
1616

1717
import (
1818
"errors"

pkg/oci/client/config_test.go renamed to pkg/oci/config_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package client
15+
package oci
1616

1717
import (
1818
"strings"

pkg/oci/client/config_validate.go renamed to pkg/oci/config_validate.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package client
15+
package oci
1616

1717
import (
1818
"k8s.io/apimachinery/pkg/util/validation/field"

pkg/oci/client/config_validate_test.go renamed to pkg/oci/config_validate_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package client
15+
package oci
1616

1717
import (
1818
"reflect"

pkg/oci/instances.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,12 @@ func extractNodeAddressesFromVNIC(vnic *core.Vnic) ([]api.NodeAddress, error) {
7878
func (cp *CloudProvider) NodeAddresses(ctx context.Context, name types.NodeName) ([]api.NodeAddress, error) {
7979
glog.V(4).Infof("NodeAddresses(%q) called", name)
8080

81-
inst, err := cp.client.Compute().GetInstanceByNodeName(ctx, mapNodeNameToInstanceName(name))
81+
inst, err := cp.client.Compute().GetInstanceByNodeName(ctx, cp.config.Auth.CompartmentOCID, cp.config.VCNID, mapNodeNameToInstanceName(name))
8282
if err != nil {
8383
return nil, errors.Wrap(err, "GetInstanceByNodeName")
8484
}
8585

86-
vnic, err := cp.client.Compute().GetPrimaryVNICForInstance(ctx, *inst.Id)
86+
vnic, err := cp.client.Compute().GetPrimaryVNICForInstance(ctx, cp.config.Auth.CompartmentOCID, *inst.Id)
8787
if err != nil {
8888
return nil, errors.Wrap(err, "GetPrimaryVNICForInstance")
8989
}
@@ -98,7 +98,7 @@ func (cp *CloudProvider) NodeAddresses(ctx context.Context, name types.NodeName)
9898
func (cp *CloudProvider) NodeAddressesByProviderID(ctx context.Context, providerID string) ([]api.NodeAddress, error) {
9999
glog.V(4).Infof("NodeAddressesByProviderID(%q) called", providerID)
100100
instanceID := util.MapProviderIDToInstanceID(providerID)
101-
vnic, err := cp.client.Compute().GetPrimaryVNICForInstance(ctx, instanceID)
101+
vnic, err := cp.client.Compute().GetPrimaryVNICForInstance(ctx, cp.config.Auth.CompartmentOCID, instanceID)
102102
if err != nil {
103103
return nil, errors.Wrap(err, "GetPrimaryVNICForInstance")
104104
}
@@ -112,7 +112,7 @@ func (cp *CloudProvider) ExternalID(ctx context.Context, nodeName types.NodeName
112112
glog.V(4).Infof("ExternalID(%q) called", nodeName)
113113

114114
instName := mapNodeNameToInstanceName(nodeName)
115-
inst, err := cp.client.Compute().GetInstanceByNodeName(ctx, instName)
115+
inst, err := cp.client.Compute().GetInstanceByNodeName(ctx, cp.config.Auth.CompartmentOCID, cp.config.VCNID, instName)
116116
if client.IsNotFound(err) {
117117
glog.Infof("Instance %q was not found. Unable to get ExternalID: %v", instName, err)
118118
return "", cloudprovider.InstanceNotFound
@@ -131,7 +131,7 @@ func (cp *CloudProvider) InstanceID(ctx context.Context, nodeName types.NodeName
131131
glog.V(4).Infof("InstanceID(%q) called", nodeName)
132132

133133
name := mapNodeNameToInstanceName(nodeName)
134-
inst, err := cp.client.Compute().GetInstanceByNodeName(ctx, name)
134+
inst, err := cp.client.Compute().GetInstanceByNodeName(ctx, cp.config.Auth.CompartmentOCID, cp.config.VCNID, name)
135135
if err != nil {
136136
if client.IsNotFound(err) {
137137
return "", cloudprovider.InstanceNotFound
@@ -145,7 +145,7 @@ func (cp *CloudProvider) InstanceID(ctx context.Context, nodeName types.NodeName
145145
func (cp *CloudProvider) InstanceType(ctx context.Context, name types.NodeName) (string, error) {
146146
glog.V(4).Infof("InstanceType(%q) called", name)
147147

148-
inst, err := cp.client.Compute().GetInstanceByNodeName(ctx, mapNodeNameToInstanceName(name))
148+
inst, err := cp.client.Compute().GetInstanceByNodeName(ctx, cp.config.Auth.CompartmentOCID, cp.config.VCNID, mapNodeNameToInstanceName(name))
149149
if err != nil {
150150
return "", errors.Wrap(err, "GetInstanceByNodeName")
151151
}

0 commit comments

Comments
 (0)