forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
70 lines (61 loc) · 1.73 KB
/
helpers.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
package util
import (
"io/ioutil"
"os"
"regexp"
"strings"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/sets"
kyaml "k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/kubernetes/pkg/api/legacyscheme"
imageapi "github.com/openshift/origin/pkg/image/apis/image"
templateapi "github.com/openshift/origin/pkg/template/apis/template"
)
const additionalAllowedRegistriesEnvVar = "ADDITIONAL_ALLOWED_REGISTRIES"
func GetTemplateFixture(filename string) (*templateapi.Template, error) {
data, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
jsonData, err := kyaml.ToJSON(data)
if err != nil {
return nil, err
}
obj, err := runtime.Decode(legacyscheme.Codecs.UniversalDecoder(), jsonData)
if err != nil {
return nil, err
}
return obj.(*templateapi.Template), nil
}
func GetImageFixture(filename string) (*imageapi.Image, error) {
data, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
jsonData, err := kyaml.ToJSON(data)
if err != nil {
return nil, err
}
obj, err := runtime.Decode(legacyscheme.Codecs.UniversalDecoder(), jsonData)
if err != nil {
return nil, err
}
return obj.(*imageapi.Image), nil
}
func SetAdditionalAllowedRegistries(hostPortGlobs ...string) {
os.Setenv(additionalAllowedRegistriesEnvVar, strings.Join(hostPortGlobs, ","))
}
func AddAdditionalAllowedRegistries(hostPortGlobs ...string) {
regs := GetAdditionalAllowedRegistries()
regs.Insert(hostPortGlobs...)
SetAdditionalAllowedRegistries(regs.List()...)
}
func GetAdditionalAllowedRegistries() sets.String {
regs := sets.NewString()
for _, r := range regexp.MustCompile(`[[:space:],]+`).Split(os.Getenv(additionalAllowedRegistriesEnvVar), -1) {
if len(r) > 0 {
regs.Insert(r)
}
}
return regs
}