forked from openebs-archive/maya
/
stats.go
159 lines (147 loc) · 4.82 KB
/
stats.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
147
148
149
150
151
152
153
154
155
156
157
158
159
package command
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"os"
"time"
)
// Volume is a command implementation struct
type Volume struct {
Spec struct {
AccessModes interface{} `json:"AccessModes"`
Capacity interface{} `json:"Capacity"`
ClaimRef interface{} `json:"ClaimRef"`
OpenEBS struct {
VolumeID string `json:"volumeID"`
} `json:"OpenEBS"`
PersistentVolumeReclaimPolicy string `json:"PersistentVolumeReclaimPolicy"`
StorageClassName string `json:"StorageClassName"`
} `json:"Spec"`
Status struct {
Message string `json:"Message"`
Phase string `json:"Phase"`
Reason string `json:"Reason"`
} `json:"Status"`
Metadata struct {
Annotations interface{} `json:"annotations"`
CreationTimestamp interface{} `json:"creationTimestamp"`
Name string `json:"name"`
} `json:"metadata"`
}
// Annotations describes volume struct
type Annotations struct {
TargetPortal string `json:"vsm.openebs.io/targetportals"`
ClusterIP string `json:"vsm.openebs.io/cluster-ips"`
Iqn string `json:"vsm.openebs.io/iqn"`
ReplicaCount string `json:"vsm.openebs.io/replica-count"`
ControllerStatus string `json:"vsm.openebs.io/controller-status"`
ReplicaStatus string `json:"vsm.openebs.io/replica-status"`
VolSize string `json:"vsm.openebs.io/volume-size"`
ControllerIP string `json:"vsm.openebs.io/controller-ips"`
// VolAddr string `json:"vsm.openebs.io/replica-ips"`
Replicas string `json:"vsm.openebs.io/replica-ips"`
}
const (
timeout = 5 * time.Second
)
// getVolDetails gets response in json format of a volume from m-apiserver
func GetVolDetails(volName string, obj interface{}) error {
addr := os.Getenv("MAPI_ADDR")
if addr == "" {
err := errors.New("MAPI_ADDR environment variable not set")
fmt.Println(err)
return err
}
url := addr + "/latest/volumes/info/" + volName
client := &http.Client{
Timeout: timeout,
}
resp, err := client.Get(url)
if resp != nil {
if resp.StatusCode == 500 {
fmt.Printf("Volume: %s not found at M_API server\n", volName)
return errors.New("Internal Server Error")
} else if resp.StatusCode == 503 {
fmt.Println("M_API server not reachable")
return errors.New("Service Unavailable")
} else if resp.StatusCode == 404 {
fmt.Printf("Volume: %s not found at M_API server\n", volName)
return errors.New("Page Not Found")
}
} else {
fmt.Println("M_API server not reachable")
return err
}
if err != nil {
fmt.Println(err)
return err
}
defer resp.Body.Close()
return json.NewDecoder(resp.Body).Decode(obj)
}
// GetVolAnnotations gets annotations of volume
func GetVolAnnotations(volName string) (*Annotations, error) {
var volume Volume
var annotations Annotations
err := GetVolDetails(volName, &volume)
if err != nil || volume.Metadata.Annotations == nil {
if volume.Status.Reason == "pending" {
fmt.Println("VOLUME status Unknown to M_API server")
}
return nil, err
}
for key, value := range volume.Metadata.Annotations.(map[string]interface{}) {
switch key {
case "vsm.openebs.io/volume-size":
annotations.VolSize = value.(string)
// case "fe.jiva.volume.openebs.io/ip":
// annotations.VolAddr = value.(string)
case "vsm.openebs.io/iqn":
annotations.Iqn = value.(string)
case "vsm.openebs.io/replica-count":
annotations.ReplicaCount = value.(string)
case "vsm.openebs.io/cluster-ips":
annotations.ClusterIP = value.(string)
case "vsm.openebs.io/replica-ips":
annotations.Replicas = value.(string)
case "vsm.openebs.io/targetportals":
annotations.TargetPortal = value.(string)
case "vsm.openebs.io/controller-status":
annotations.ControllerStatus = value.(string)
case "vsm.openebs.io/replica-status":
annotations.ReplicaStatus = value.(string)
case "vsm.openebs.io/controller-ips":
annotations.ControllerIP = value.(string)
}
}
return &annotations, nil
}
func GetVolumeSpec(volName string) (*Annotations, error) {
var volume Volume
var annotations Annotations
for key, value := range volume.Metadata.Annotations.(map[string]interface{}) {
switch key {
case "vsm.openebs.io/volume-size":
annotations.VolSize = value.(string)
// case "fe.jiva.volume.openebs.io/ip":
// annotations.VolAddr = value.(string)
case "vsm.openebs.io/iqn":
annotations.Iqn = value.(string)
case "vsm.openebs.io/replica-count":
annotations.ReplicaCount = value.(string)
case "vsm.openebs.io/cluster-ips":
annotations.ClusterIP = value.(string)
case "vsm.openebs.io/replica-ips":
annotations.Replicas = value.(string)
case "vsm.openebs.io/targetportals":
annotations.TargetPortal = value.(string)
case "vsm.openebs.io/controller-status":
annotations.ControllerStatus = value.(string)
case "vsm.openebs.io/controller-ips":
annotations.ControllerIP = value.(string)
}
}
return &annotations, nil
}