forked from rancher/local-path-provisioner
-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
40 lines (36 loc) · 926 Bytes
/
util.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
v1 "k8s.io/api/core/v1"
"sigs.k8s.io/yaml"
)
func loadFile(filepath string) (string, error) {
f, err := os.Open(filepath)
if err != nil {
return "", err
}
defer f.Close()
helperPodYaml, err := ioutil.ReadAll(f)
if err != nil {
return "", err
}
return string(helperPodYaml), nil
}
func loadHelperPodFile(helperPodYaml string) (*v1.Pod, error) {
helperPodJSON, err := yaml.YAMLToJSON([]byte(helperPodYaml))
if err != nil {
return nil, fmt.Errorf("invalid YAMLToJSON the helper pod with helperPodYaml: %v", helperPodYaml)
}
p := v1.Pod{}
err = json.Unmarshal(helperPodJSON, &p)
if err != nil {
return nil, fmt.Errorf("invalid unmarshal the helper pod with helperPodJson: %v", string(helperPodJSON))
}
if len(p.Spec.Containers) == 0 {
return nil, fmt.Errorf("helper pod template does not specify any container")
}
return &p, nil
}