/
nifcloud_zones.go
72 lines (59 loc) · 2.4 KB
/
nifcloud_zones.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package nifcloud
import (
"context"
"fmt"
"os"
"k8s.io/apimachinery/pkg/types"
cloudprovider "k8s.io/cloud-provider"
)
// GetZone returns the Zone containing the current failure zone and locality region that the program is running in
func (c *Cloud) GetZone(ctx context.Context) (cloudprovider.Zone, error) {
instanceID := os.Getenv("NODE_NAME")
if instanceID == "" {
return cloudprovider.Zone{}, fmt.Errorf("could not get instance id for this node. environment variable 'NODE_NAME' is empty")
}
instances, err := c.client.DescribeInstancesByInstanceID(ctx, []string{instanceID})
if err != nil {
return cloudprovider.Zone{}, fmt.Errorf("cloud not fetch instance info for %q: %w", instanceID, err)
}
if err := isSingleInstance(instances, instanceID); err != nil {
return cloudprovider.Zone{}, err
}
return cloudprovider.Zone{
FailureDomain: instances[0].Zone,
Region: c.region,
}, nil
}
// GetZoneByProviderID returns the Zone containing the current zone and locality region of the node specified by providerID
func (c *Cloud) GetZoneByProviderID(ctx context.Context, providerID string) (cloudprovider.Zone, error) {
instanceUniqueID, err := getInstanceUniqueIDFromProviderID(providerID)
if err != nil {
return cloudprovider.Zone{}, fmt.Errorf("unable to convert provider id %q: %v", providerID, err)
}
instances, err := c.client.DescribeInstancesByInstanceUniqueID(ctx, []string{instanceUniqueID})
if err != nil {
return cloudprovider.Zone{}, fmt.Errorf("cloud not fetch instance info for %q: %w", instanceUniqueID, err)
}
if err := isSingleInstance(instances, instanceUniqueID); err != nil {
return cloudprovider.Zone{}, err
}
return cloudprovider.Zone{
FailureDomain: instances[0].Zone,
Region: c.region,
}, nil
}
// GetZoneByNodeName returns the Zone containing the current zone and locality region of the node specified by node name
func (c *Cloud) GetZoneByNodeName(ctx context.Context, name types.NodeName) (cloudprovider.Zone, error) {
instanceID := string(name)
instances, err := c.client.DescribeInstancesByInstanceID(ctx, []string{instanceID})
if err != nil {
return cloudprovider.Zone{}, fmt.Errorf("cloud not fetch instance info for %q: %w", instanceID, err)
}
if err := isSingleInstance(instances, instanceID); err != nil {
return cloudprovider.Zone{}, err
}
return cloudprovider.Zone{
FailureDomain: instances[0].Zone,
Region: c.region,
}, nil
}