-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.go
146 lines (126 loc) · 4.68 KB
/
utils.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package common
import (
"fmt"
"strings"
"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud/meta"
"k8s.io/apimachinery/pkg/util/sets"
)
const (
// Volume ID Expected Format
// "projects/{projectName}/zones/{zoneName}/disks/{diskName}"
volIDZonalFmt = "projects/%s/zones/%s/disks/%s"
// "projects/{projectName}/regions/{regionName}/disks/{diskName}"
volIDRegionalFmt = "projects/%s/regions/%s/disks/%s"
volIDToplogyKey = 2
volIDToplogyValue = 3
volIDDiskNameValue = 5
volIDTotalElements = 6
// Snapshot ID
snapshotTotalElements = 5
snapshotTopologyKey = 2
// Node ID Expected Format
// "projects/{projectName}/zones/{zoneName}/disks/{diskName}"
nodeIDFmt = "projects/%s/zones/%s/instances/%s"
nodeIDZoneValue = 3
nodeIDNameValue = 5
nodeIDTotalElements = 6
regionalDeviceNameSuffix = "_regional"
)
func BytesToGb(bytes int64) int64 {
// TODO: Throw an error when div to 0
return bytes / (1024 * 1024 * 1024)
}
func GbToBytes(Gb int64) int64 {
// TODO: Check for overflow
return Gb * 1024 * 1024 * 1024
}
func VolumeIDToKey(id string) (*meta.Key, error) {
splitId := strings.Split(id, "/")
if len(splitId) != volIDTotalElements {
return nil, fmt.Errorf("failed to get id components. Expected projects/{project}/zones/{zone}/disks/{name}. Got: %s", id)
}
if splitId[volIDToplogyKey] == "zones" {
return meta.ZonalKey(splitId[volIDDiskNameValue], splitId[volIDToplogyValue]), nil
} else if splitId[volIDToplogyKey] == "regions" {
return meta.RegionalKey(splitId[volIDDiskNameValue], splitId[volIDToplogyValue]), nil
} else {
return nil, fmt.Errorf("could not get id components, expected either zones or regions, got: %v", splitId[volIDToplogyKey])
}
}
func KeyToVolumeID(volKey *meta.Key, project string) (string, error) {
switch volKey.Type() {
case meta.Zonal:
return fmt.Sprintf(volIDZonalFmt, project, volKey.Zone, volKey.Zone), nil
case meta.Regional:
return fmt.Sprintf(volIDZonalFmt, project, volKey.Region, volKey.Zone), nil
default:
return "", fmt.Errorf("volume key %v neither zonal nor regional", volKey.Name)
}
}
func GenerateUnderspecifiedVolumeID(diskName string, isZonal bool) string {
if isZonal {
return fmt.Sprintf(volIDZonalFmt, UnspecifiedValue, UnspecifiedValue, diskName)
}
return fmt.Sprintf(volIDRegionalFmt, UnspecifiedValue, UnspecifiedValue, diskName)
}
func SnapshotIDToKey(id string) (string, error) {
splitId := strings.Split(id, "/")
if len(splitId) != snapshotTotalElements {
return "", fmt.Errorf("failed to get id components. Expected projects/{project}/global/snapshot/{name}. Got: %s", id)
}
if splitId[snapshotTopologyKey] == "global" {
return splitId[snapshotTotalElements-1], nil
} else {
return "", fmt.Errorf("could not get id components, expected global, got: %v", splitId[snapshotTopologyKey])
}
}
func NodeIDToZoneAndName(id string) (string, string, error) {
splitId := strings.Split(id, "/")
if len(splitId) != nodeIDTotalElements {
return "", "", fmt.Errorf("failed to get id components. expected projects/{project}/zones/{zone}/instances/{name}. Got: %s", id)
}
return splitId[nodeIDZoneValue], splitId[nodeIDNameValue], nil
}
func GetRegionFromZones(zones []string) (string, error) {
regions := sets.String{}
if len(zones) < 1 {
return "", fmt.Errorf("no zones specified")
}
for _, zone := range zones {
// Zone expected format {locale}-{region}-{zone}
splitZone := strings.Split(zone, "-")
if len(splitZone) != 3 {
return "", fmt.Errorf("zone in unexpected format, expected: {locale}-{region}-{zone}, got: %v", zone)
}
regions.Insert(strings.Join(splitZone[0:2], "-"))
}
if regions.Len() != 1 {
return "", fmt.Errorf("multiple or no regions gotten from zones, got: %v", regions)
}
return regions.UnsortedList()[0], nil
}
func GetDeviceName(volKey *meta.Key) (string, error) {
switch volKey.Type() {
case meta.Zonal:
return volKey.Name, nil
case meta.Regional:
return volKey.Name + regionalDeviceNameSuffix, nil
default:
return "", fmt.Errorf("volume key %v neither zonal nor regional", volKey.Name)
}
}
func CreateNodeID(project, zone, name string) string {
return fmt.Sprintf(nodeIDFmt, project, zone, name)
}