15
15
package oci
16
16
17
17
import (
18
- "errors "
18
+ "context "
19
19
"fmt"
20
+ "net"
20
21
"strings"
21
22
22
23
"github.com/golang/glog"
23
- "github.com/oracle/oci-cloud-controller-manager/pkg/oci/client "
24
- "github.com/oracle/oci-cloud-controller-manager/ pkg/oci/util "
24
+ "github.com/oracle/oci-go-sdk/core "
25
+ "github.com/pkg/errors "
25
26
26
27
api "k8s.io/api/core/v1"
27
- "k8s.io/apimachinery/pkg/types"
28
- "k8s.io/kubernetes/pkg/cloudprovider"
28
+ types "k8s.io/apimachinery/pkg/types"
29
+ cloudprovider "k8s.io/kubernetes/pkg/cloudprovider"
30
+
31
+ "github.com/oracle/oci-cloud-controller-manager/pkg/oci/client"
32
+ "github.com/oracle/oci-cloud-controller-manager/pkg/oci/util"
29
33
)
30
34
31
35
var _ cloudprovider.Instances = & CloudProvider {}
@@ -42,18 +46,48 @@ func mapInstanceNameToNodeName(displayName string) types.NodeName {
42
46
return types .NodeName (strings .ToLower (displayName ))
43
47
}
44
48
49
+ func extractNodeAddressesFromVNIC (vnic * core.Vnic ) ([]api.NodeAddress , error ) {
50
+ addresses := []api.NodeAddress {}
51
+ if vnic == nil {
52
+ return addresses , nil
53
+ }
54
+
55
+ if vnic .PrivateIp != nil && * vnic .PrivateIp != "" {
56
+ ip := net .ParseIP (* vnic .PrivateIp )
57
+ if ip == nil {
58
+ return nil , fmt .Errorf ("instance has invalid private address: %q" , * vnic .PrivateIp )
59
+ }
60
+ addresses = append (addresses , api.NodeAddress {Type : api .NodeInternalIP , Address : ip .String ()})
61
+ }
62
+
63
+ if vnic .PublicIp != nil && * vnic .PublicIp != "" {
64
+ ip := net .ParseIP (* vnic .PublicIp )
65
+ if ip == nil {
66
+ return nil , errors .Errorf ("instance has invalid public address: %q" , * vnic .PublicIp )
67
+ }
68
+ addresses = append (addresses , api.NodeAddress {Type : api .NodeExternalIP , Address : ip .String ()})
69
+ }
70
+
71
+ return addresses , nil
72
+ }
73
+
45
74
// NodeAddresses returns the addresses of the specified instance.
46
75
// TODO(roberthbailey): This currently is only used in such a way that it
47
76
// returns the address of the calling instance. We should do a rename to
48
77
// make this clearer.
49
78
func (cp * CloudProvider ) NodeAddresses (name types.NodeName ) ([]api.NodeAddress , error ) {
50
79
glog .V (4 ).Infof ("NodeAddresses(%q) called" , name )
51
80
52
- inst , err := cp .client .GetInstanceByNodeName (mapNodeNameToInstanceName (name ))
81
+ inst , err := cp .client .Compute (). GetInstanceByNodeName (context . TODO (), mapNodeNameToInstanceName (name ))
53
82
if err != nil {
54
- return nil , err
83
+ return []api. NodeAddress {}, errors . Wrap ( err , "GetInstanceByNodeName" )
55
84
}
56
- return cp .client .GetNodeAddressesForInstance (inst .ID )
85
+
86
+ vnic , err := cp .client .Compute ().GetPrimaryVNICForInstance (context .TODO (), * inst .Id )
87
+ if err != nil {
88
+ return []api.NodeAddress {}, errors .Wrap (err , "GetPrimaryVNICForInstance" )
89
+ }
90
+ return extractNodeAddressesFromVNIC (vnic )
57
91
}
58
92
59
93
// NodeAddressesByProviderID returns the addresses of the specified instance.
@@ -64,7 +98,11 @@ func (cp *CloudProvider) NodeAddresses(name types.NodeName) ([]api.NodeAddress,
64
98
func (cp * CloudProvider ) NodeAddressesByProviderID (providerID string ) ([]api.NodeAddress , error ) {
65
99
glog .V (4 ).Infof ("NodeAddressesByProviderID(%q) called" , providerID )
66
100
instanceID := util .MapProviderIDToInstanceID (providerID )
67
- return cp .client .GetNodeAddressesForInstance (instanceID )
101
+ vnic , err := cp .client .Compute ().GetPrimaryVNICForInstance (context .TODO (), instanceID )
102
+ if err != nil {
103
+ return []api.NodeAddress {}, errors .Wrap (err , "GetPrimaryVNICForInstance" )
104
+ }
105
+ return extractNodeAddressesFromVNIC (vnic )
68
106
}
69
107
70
108
// ExternalID returns the cloud provider ID of the node with the specified NodeName.
@@ -74,7 +112,7 @@ func (cp *CloudProvider) ExternalID(nodeName types.NodeName) (string, error) {
74
112
glog .V (4 ).Infof ("ExternalID(%q) called" , nodeName )
75
113
76
114
instName := mapNodeNameToInstanceName (nodeName )
77
- inst , err := cp .client .GetInstanceByNodeName (instName )
115
+ inst , err := cp .client .Compute (). GetInstanceByNodeName (context . TODO (), instName )
78
116
if client .IsNotFound (err ) {
79
117
glog .Infof ("Instance %q was not found. Unable to get ExternalID: %v" , instName , err )
80
118
return "" , cloudprovider .InstanceNotFound
@@ -84,8 +122,8 @@ func (cp *CloudProvider) ExternalID(nodeName types.NodeName) (string, error) {
84
122
return "" , err
85
123
}
86
124
87
- glog .V (4 ).Infof ("Got ExternalID %s for %s" , inst .ID , nodeName )
88
- return inst .ID , nil
125
+ glog .V (4 ).Infof ("Got ExternalID %s for %s" , * inst .Id , nodeName )
126
+ return * inst .Id , nil
89
127
}
90
128
91
129
// InstanceID returns the cloud provider ID of the node with the specified NodeName.
@@ -94,34 +132,34 @@ func (cp *CloudProvider) InstanceID(nodeName types.NodeName) (string, error) {
94
132
glog .V (4 ).Infof ("InstanceID(%q) called" , nodeName )
95
133
96
134
name := mapNodeNameToInstanceName (nodeName )
97
- inst , err := cp .client .GetInstanceByNodeName (name )
135
+ inst , err := cp .client .Compute (). GetInstanceByNodeName (context . TODO (), name )
98
136
if err != nil {
99
- return "" , fmt . Errorf ( "unable to fetch InstanceID for %q: %v" , name , err )
137
+ return "" , errors . Wrap ( err , "GetInstanceByNodeName" )
100
138
}
101
- return inst .ID , nil
139
+ return * inst .Id , nil
102
140
}
103
141
104
142
// InstanceType returns the type of the specified instance.
105
143
func (cp * CloudProvider ) InstanceType (name types.NodeName ) (string , error ) {
106
144
glog .V (4 ).Infof ("InstanceType(%q) called" , name )
107
145
108
- inst , err := cp .client .GetInstanceByNodeName (mapNodeNameToInstanceName (name ))
146
+ inst , err := cp .client .Compute (). GetInstanceByNodeName (context . TODO (), mapNodeNameToInstanceName (name ))
109
147
if err != nil {
110
- return "" , fmt . Errorf ( "getInstanceByNodeName failed for %q with %v" , name , err )
148
+ return "" , errors . Wrap ( err , "GetInstanceByNodeName" )
111
149
}
112
- return inst .Shape , nil
150
+ return * inst .Shape , nil
113
151
}
114
152
115
153
// InstanceTypeByProviderID returns the type of the specified instance.
116
154
func (cp * CloudProvider ) InstanceTypeByProviderID (providerID string ) (string , error ) {
117
155
glog .V (4 ).Infof ("InstanceTypeByProviderID(%q) called" , providerID )
118
156
119
157
instanceID := util .MapProviderIDToInstanceID (providerID )
120
- inst , err := cp .client .GetInstance (instanceID )
158
+ inst , err := cp .client .Compute (). GetInstance (context . TODO (), instanceID )
121
159
if err != nil {
122
- return "" , err
160
+ return "" , errors . Wrap ( err , "GetInstance" )
123
161
}
124
- return inst .Shape , nil
162
+ return * inst .Shape , nil
125
163
}
126
164
127
165
// AddSSHKeyToAllInstances adds an SSH public key as a legal identity for all instances
@@ -143,7 +181,7 @@ func (cp *CloudProvider) CurrentNodeName(hostname string) (types.NodeName, error
143
181
func (cp * CloudProvider ) InstanceExistsByProviderID (providerID string ) (bool , error ) {
144
182
glog .V (4 ).Infof ("InstanceExistsByProviderID(%q) called" , providerID )
145
183
instanceID := util .MapProviderIDToInstanceID (providerID )
146
- instance , err := cp .client .GetInstance (instanceID )
184
+ instance , err := cp .client .Compute (). GetInstance (context . TODO (), instanceID )
147
185
if client .IsNotFound (err ) {
148
186
return false , nil
149
187
}
0 commit comments