-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
129 lines (112 loc) · 2.78 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
package cluster
import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"time"
yaml2 "github.com/ghodss/yaml"
"github.com/rancher/norman/types/convert"
"github.com/rancher/norman/types/values"
v1 "github.com/rancher/types/apis/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
v12 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/yaml"
)
type noopCloser struct {
io.Reader
}
func (noopCloser) Close() error {
return nil
}
func findNamespaceCreates(inputYAML string) ([]string, error) {
var namespaces []string
reader := yaml.NewDocumentDecoder(noopCloser{Reader: bytes.NewBufferString(inputYAML)})
for {
next, readErr := ioutil.ReadAll(reader)
if readErr != nil && readErr != io.ErrShortBuffer {
return nil, readErr
}
obj := &unstructured.Unstructured{}
next, err := yaml2.YAMLToJSON(next)
if err != nil {
return nil, err
}
err = json.Unmarshal(next, &obj.Object)
if err != nil {
return nil, err
}
if obj.IsList() {
obj.EachListItem(func(obj runtime.Object) error {
metadata, err := meta.Accessor(obj)
if err != nil {
return err
}
if obj.GetObjectKind().GroupVersionKind().Kind == "Namespace" && obj.GetObjectKind().GroupVersionKind().Version == "v1" {
namespaces = append(namespaces, metadata.GetName())
}
if metadata.GetNamespace() != "" {
namespaces = append(namespaces, metadata.GetNamespace())
}
return nil
})
} else if obj.GetKind() == "Namespace" && obj.GetAPIVersion() == "v1" {
namespaces = append(namespaces, obj.GetName())
if obj.GetNamespace() != "" {
namespaces = append(namespaces, obj.GetNamespace())
}
}
if readErr == nil {
break
}
}
uniq := map[string]bool{}
var newNamespaces []string
for _, ns := range namespaces {
if !uniq[ns] {
uniq[ns] = true
newNamespaces = append(newNamespaces, ns)
}
}
return newNamespaces, nil
}
func waitForNS(nsClient v1.NamespaceInterface, namespaces []string) {
for i := 0; i < 3; i++ {
allGood := true
for _, ns := range namespaces {
ns, err := nsClient.Get(ns, v12.GetOptions{})
if err != nil {
allGood = false
break
}
status := ns.Annotations["cattle.io/status"]
if status == "" {
allGood = false
break
}
nsMap := map[string]interface{}{}
err = json.Unmarshal([]byte(status), &nsMap)
if err != nil {
allGood = false
break
}
foundCond := false
conds := convert.ToMapSlice(values.GetValueN(nsMap, "Conditions"))
for _, cond := range conds {
if cond["Type"] == "InitialRolesPopulated" && cond["Status"] == "True" {
foundCond = true
}
}
if !foundCond {
allGood = false
}
}
if allGood {
break
} else {
time.Sleep(2 * time.Second)
}
}
}