Skip to content

Commit eee60e9

Browse files
committed
refactoring and adding tests
1 parent c312190 commit eee60e9

File tree

2 files changed

+372
-69
lines changed

2 files changed

+372
-69
lines changed

pkg/cloudprovider/providers/oci/instances.go

Lines changed: 29 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,10 @@ import (
2121
"strings"
2222

2323
"github.com/oracle/oci-cloud-controller-manager/pkg/oci/client"
24-
"github.com/oracle/oci-go-sdk/core"
2524
"github.com/pkg/errors"
2625
api "k8s.io/api/core/v1"
27-
types "k8s.io/apimachinery/pkg/types"
28-
cloudprovider "k8s.io/kubernetes/pkg/cloudprovider"
26+
"k8s.io/apimachinery/pkg/types"
27+
"k8s.io/kubernetes/pkg/cloudprovider"
2928
)
3029

3130
var _ cloudprovider.Instances = &CloudProvider{}
@@ -42,8 +41,13 @@ func mapInstanceNameToNodeName(displayName string) types.NodeName {
4241
return types.NodeName(strings.ToLower(displayName))
4342
}
4443

45-
func extractNodeAddressesFromVNIC(vnic *core.Vnic) ([]api.NodeAddress, error) {
44+
func (cp *CloudProvider) extractNodeAddresses(ctx context.Context, instanceID string) ([]api.NodeAddress, error) {
4645
addresses := []api.NodeAddress{}
46+
vnic, err := cp.client.Compute().GetPrimaryVNICForInstance(ctx, cp.config.CompartmentID, instanceID)
47+
if err != nil {
48+
return nil, errors.Wrap(err, "GetPrimaryVNICForInstance")
49+
}
50+
4751
if vnic == nil {
4852
return addresses, nil
4953
}
@@ -64,6 +68,24 @@ func extractNodeAddressesFromVNIC(vnic *core.Vnic) ([]api.NodeAddress, error) {
6468
addresses = append(addresses, api.NodeAddress{Type: api.NodeExternalIP, Address: ip.String()})
6569
}
6670

71+
if vnic.HostnameLabel != nil && *vnic.HostnameLabel != "" {
72+
subnet, err := cp.client.Networking().GetSubnet(ctx, *vnic.SubnetId)
73+
if err != nil {
74+
return nil, errors.Wrap(err, "GetSubnetForInstance")
75+
}
76+
if subnet != nil && subnet.DnsLabel != nil && *subnet.DnsLabel != "" {
77+
vcn, err := cp.client.Networking().GetVcn(ctx, *subnet.VcnId)
78+
if err != nil {
79+
return nil, errors.Wrap(err, "GetVcnForInstance")
80+
}
81+
if vcn != nil && vcn.DnsLabel != nil && *vcn.DnsLabel != "" {
82+
fqdn := strings.Join([]string{*vnic.HostnameLabel, *subnet.DnsLabel, *vcn.DnsLabel, "oraclevcn.com"}, ".")
83+
addresses = append(addresses, api.NodeAddress{Type: api.NodeHostName, Address: fqdn})
84+
addresses = append(addresses, api.NodeAddress{Type: api.NodeInternalDNS, Address: fqdn})
85+
}
86+
}
87+
}
88+
6789
return addresses, nil
6890
}
6991

@@ -78,12 +100,7 @@ func (cp *CloudProvider) NodeAddresses(ctx context.Context, name types.NodeName)
78100
if err != nil {
79101
return nil, errors.Wrap(err, "GetInstanceByNodeName")
80102
}
81-
82-
vnic, err := cp.client.Compute().GetPrimaryVNICForInstance(ctx, cp.config.CompartmentID, *inst.Id)
83-
if err != nil {
84-
return nil, errors.Wrap(err, "GetPrimaryVNICForInstance")
85-
}
86-
return extractNodeAddressesFromVNIC(vnic)
103+
return cp.extractNodeAddresses(ctx, *inst.Id)
87104
}
88105

89106
// NodeAddressesByProviderID returns the addresses of the specified instance.
@@ -92,44 +109,14 @@ func (cp *CloudProvider) NodeAddresses(ctx context.Context, name types.NodeName)
92109
// nodeaddresses are being queried. i.e. local metadata services cannot be used
93110
// in this method to obtain nodeaddresses.
94111
func (cp *CloudProvider) NodeAddressesByProviderID(ctx context.Context, providerID string) ([]api.NodeAddress, error) {
95-
addresses := []api.NodeAddress{}
96112
cp.logger.With("instanceID", providerID).Debug("Getting node addresses by provider id")
113+
97114
instanceID, err := MapProviderIDToInstanceID(providerID)
98115
if err != nil {
99116
return nil, errors.Wrap(err, "MapProviderIDToInstanceID")
100117
}
101-
vnic, err := cp.client.Compute().GetPrimaryVNICForInstance(ctx, cp.config.CompartmentID, instanceID)
102-
if err != nil {
103-
return nil, errors.Wrap(err, "GetPrimaryVNICForInstance")
104-
}
105-
vnicAddresses, err := extractNodeAddressesFromVNIC(vnic)
106-
if err != nil {
107-
return nil, err
108-
}
109-
addresses = append(addresses, vnicAddresses...)
118+
return cp.extractNodeAddresses(ctx, instanceID)
110119

111-
if vnic != nil {
112-
hostname := vnic.HostnameLabel
113-
if hostname != nil && *hostname != "" {
114-
subnet, err := cp.client.Networking().GetSubnet(ctx, *vnic.SubnetId)
115-
if err != nil {
116-
return nil, errors.Wrap(err, "GetSubnetForInstance")
117-
}
118-
if subnet != nil && *subnet.DnsLabel != "" {
119-
vcn, err := cp.client.Networking().GetVcn(ctx, *subnet.VcnId)
120-
if err != nil {
121-
return nil, errors.Wrap(err, "GetVcnForInstance")
122-
}
123-
if vcn != nil && *vcn.DnsLabel != "" {
124-
fqdn := strings.Join([]string{*hostname, *subnet.DnsLabel, *vcn.DnsLabel, "oraclevcn.com"}, ".")
125-
addresses = append(addresses, api.NodeAddress{Type: api.NodeHostName, Address: fqdn})
126-
addresses = append(addresses, api.NodeAddress{Type: api.NodeInternalDNS, Address: fqdn})
127-
}
128-
}
129-
}
130-
}
131-
132-
return addresses, nil
133120
}
134121

135122
// InstanceID returns the cloud provider ID of the node with the specified NodeName.

0 commit comments

Comments
 (0)