This repository has been archived by the owner on Jan 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 560
/
storageclass.go
94 lines (86 loc) · 2.32 KB
/
storageclass.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
package storageclass
import (
"context"
"encoding/json"
"log"
"os/exec"
"time"
"github.com/Azure/acs-engine/test/e2e/kubernetes/util"
"github.com/pkg/errors"
)
// StorageClass is used to parse data from kubectl get storageclass
type StorageClass struct {
Metadata Metadata `json:"metadata"`
Parameters Parameters `json:"parameters"`
}
// Metadata holds information like name, create time
type Metadata struct {
CreatedAt time.Time `json:"creationTimestamp"`
Name string `json:"name"`
}
// Parameters holds information like skuName
type Parameters struct {
SkuName string `json:"skuName"`
}
// CreateStorageClassFromFile will create a StorageClass from file with a name
func CreateStorageClassFromFile(filename, name string) (*StorageClass, error) {
cmd := exec.Command("kubectl", "apply", "-f", filename)
util.PrintCommand(cmd)
out, err := cmd.CombinedOutput()
if err != nil {
log.Printf("Error trying to create StorageClass %s:%s\n", name, string(out))
return nil, err
}
sc, err := Get(name)
if err != nil {
log.Printf("Error while trying to fetch StorageClass %s:%s\n", name, err)
return nil, err
}
return sc, nil
}
// Get will return a StorageClass with a given name and namespace
func Get(scName string) (*StorageClass, error) {
cmd := exec.Command("kubectl", "get", "storageclass", scName, "-o", "json")
util.PrintCommand(cmd)
out, err := cmd.CombinedOutput()
if err != nil {
return nil, err
}
sc := StorageClass{}
err = json.Unmarshal(out, &sc)
if err != nil {
log.Printf("Error unmarshalling StorageClass json:%s\n", err)
return nil, err
}
return &sc, nil
}
// WaitOnReady will block until StorageClass is available
func (sc *StorageClass) WaitOnReady(sleep, duration time.Duration) (bool, error) {
readyCh := make(chan bool, 1)
errCh := make(chan error)
ctx, cancel := context.WithTimeout(context.Background(), duration)
defer cancel()
go func() {
for {
select {
case <-ctx.Done():
errCh <- errors.Errorf("Timeout exceeded (%s) while waiting for StorageClass (%s) to become ready", duration.String(), sc.Metadata.Name)
default:
query, _ := Get(sc.Metadata.Name)
if query != nil {
readyCh <- true
} else {
time.Sleep(sleep)
}
}
}
}()
for {
select {
case err := <-errCh:
return false, err
case ready := <-readyCh:
return ready, nil
}
}
}