forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configuration.go
153 lines (125 loc) · 3.19 KB
/
configuration.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
package configuration
import (
"bytes"
"errors"
"io"
"io/ioutil"
"os"
"reflect"
"strings"
"time"
"gopkg.in/yaml.v2"
"github.com/docker/distribution/configuration"
)
var (
// CurrentVersion is the most recent Version that can be parsed.
CurrentVersion = configuration.MajorMinorVersion(1, 0)
ErrUnsupportedVersion = errors.New("Unsupported openshift configuration version")
)
type openshiftConfig struct {
Openshift Configuration
}
type Configuration struct {
Version configuration.Version `yaml:"version"`
Metrics Metrics `yaml:"metrics"`
Requests Requests `yaml:"requests"`
}
type Metrics struct {
Enabled bool `yaml:"enabled"`
Secret string `yaml:"secret"`
}
type Requests struct {
Read RequestsLimits `yaml:"read"`
Write RequestsLimits `yaml:"write"`
}
type RequestsLimits struct {
MaxRunning int `yaml:"maxrunning"`
MaxInQueue int `yaml:"maxinqueue"`
MaxWaitInQueue time.Duration `yaml:"maxwaitinqueue"`
}
type versionInfo struct {
Openshift struct {
Version *configuration.Version
}
}
// Parse parses an input configuration and returns docker configuration structure and
// openshift specific configuration.
// Environment variables may be used to override configuration parameters.
func Parse(rd io.Reader) (*configuration.Configuration, *Configuration, error) {
in, err := ioutil.ReadAll(rd)
if err != nil {
return nil, nil, err
}
// We don't want to change the version from the environment variables.
os.Unsetenv("REGISTRY_OPENSHIFT_VERSION")
openshiftEnv, err := popEnv("REGISTRY_OPENSHIFT_")
if err != nil {
return nil, nil, err
}
dockerConfig, err := configuration.Parse(bytes.NewBuffer(in))
if err != nil {
return nil, nil, err
}
dockerEnv, err := popEnv("REGISTRY_")
if err != nil {
return nil, nil, err
}
if err := pushEnv(openshiftEnv); err != nil {
return nil, nil, err
}
config := openshiftConfig{}
vInfo := &versionInfo{}
if err := yaml.Unmarshal(in, &vInfo); err != nil {
return nil, nil, err
}
if vInfo.Openshift.Version != nil {
if *vInfo.Openshift.Version != CurrentVersion {
return nil, nil, ErrUnsupportedVersion
}
} else {
return dockerConfig, &config.Openshift, nil
}
p := configuration.NewParser("registry", []configuration.VersionedParseInfo{
{
Version: dockerConfig.Version,
ParseAs: reflect.TypeOf(config),
ConversionFunc: func(c interface{}) (interface{}, error) {
return c, nil
},
},
})
if err = p.Parse(in, &config); err != nil {
return nil, nil, err
}
if err := pushEnv(dockerEnv); err != nil {
return nil, nil, err
}
return dockerConfig, &config.Openshift, nil
}
type envVar struct {
name string
value string
}
func popEnv(prefix string) ([]envVar, error) {
var envVars []envVar
for _, env := range os.Environ() {
if !strings.HasPrefix(env, prefix) {
continue
}
envParts := strings.SplitN(env, "=", 2)
err := os.Unsetenv(envParts[0])
if err != nil {
return nil, err
}
envVars = append(envVars, envVar{envParts[0], envParts[1]})
}
return envVars, nil
}
func pushEnv(environ []envVar) error {
for _, env := range environ {
if err := os.Setenv(env.name, env.value); err != nil {
return err
}
}
return nil
}