Skip to content

Commit bdf664c

Browse files
authored
Merge pull request #296 from IamPrvn/master
allow retrieval of instance fqdn using instanceID
2 parents 909bf68 + eee60e9 commit bdf664c

File tree

8 files changed

+406
-42
lines changed

8 files changed

+406
-42
lines changed

pkg/cloudprovider/providers/oci/instances.go

Lines changed: 30 additions & 15 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.
@@ -93,15 +110,13 @@ func (cp *CloudProvider) NodeAddresses(ctx context.Context, name types.NodeName)
93110
// in this method to obtain nodeaddresses.
94111
func (cp *CloudProvider) NodeAddressesByProviderID(ctx context.Context, providerID string) ([]api.NodeAddress, error) {
95112
cp.logger.With("instanceID", providerID).Debug("Getting node addresses by provider id")
113+
96114
instanceID, err := MapProviderIDToInstanceID(providerID)
97115
if err != nil {
98116
return nil, errors.Wrap(err, "MapProviderIDToInstanceID")
99117
}
100-
vnic, err := cp.client.Compute().GetPrimaryVNICForInstance(ctx, cp.config.CompartmentID, instanceID)
101-
if err != nil {
102-
return nil, errors.Wrap(err, "GetPrimaryVNICForInstance")
103-
}
104-
return extractNodeAddressesFromVNIC(vnic)
118+
return cp.extractNodeAddresses(ctx, instanceID)
119+
105120
}
106121

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

0 commit comments

Comments
 (0)