forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.go
189 lines (162 loc) · 4.83 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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package latest
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"path"
"reflect"
"github.com/ghodss/yaml"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/sets"
kyaml "k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/apiserver/pkg/apis/apiserver"
configapi "github.com/openshift/origin/pkg/cmd/server/apis/config"
)
func ReadSessionSecrets(filename string) (*configapi.SessionSecrets, error) {
config := &configapi.SessionSecrets{}
if err := ReadYAMLFileInto(filename, config); err != nil {
return nil, err
}
return config, nil
}
func ReadMasterConfig(filename string) (*configapi.MasterConfig, error) {
config := &configapi.MasterConfig{}
if err := ReadYAMLFileInto(filename, config); err != nil {
return nil, err
}
return config, nil
}
func ReadAndResolveMasterConfig(filename string) (*configapi.MasterConfig, error) {
masterConfig, err := ReadMasterConfig(filename)
if err != nil {
return nil, err
}
if err := configapi.ResolveMasterConfigPaths(masterConfig, path.Dir(filename)); err != nil {
return nil, err
}
return masterConfig, nil
}
func ReadNodeConfig(filename string) (*configapi.NodeConfig, error) {
config := &configapi.NodeConfig{}
if err := ReadYAMLFileInto(filename, config); err != nil {
return nil, err
}
return config, nil
}
func ReadAndResolveNodeConfig(filename string) (*configapi.NodeConfig, error) {
nodeConfig, err := ReadNodeConfig(filename)
if err != nil {
return nil, err
}
if err := configapi.ResolveNodeConfigPaths(nodeConfig, path.Dir(filename)); err != nil {
return nil, err
}
return nodeConfig, nil
}
// TODO: Remove this when a YAML serializer is available from upstream
func WriteYAML(obj runtime.Object) ([]byte, error) {
json, err := runtime.Encode(Codec, obj)
if err != nil {
return nil, err
}
content, err := yaml.JSONToYAML(json)
if err != nil {
return nil, err
}
return content, err
}
func ReadYAML(reader io.Reader) (runtime.Object, error) {
if reader == nil || reflect.ValueOf(reader).IsNil() {
return nil, nil
}
data, err := ioutil.ReadAll(reader)
if err != nil {
return nil, err
}
jsonData, err := kyaml.ToJSON(data)
if err != nil {
return nil, err
}
return runtime.Decode(Codec, jsonData)
}
// TODO: Remove this when a YAML serializer is available from upstream
func ReadYAMLInto(data []byte, obj runtime.Object) error {
data, err := kyaml.ToJSON(data)
if err != nil {
return err
}
err = runtime.DecodeInto(Codec, data, obj)
return captureSurroundingJSONForError("error reading config: ", data, err)
}
func ReadYAMLFileInto(filename string, obj runtime.Object) error {
data, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
err = ReadYAMLInto(data, obj)
if err != nil {
return fmt.Errorf("could not load config file %q due to an error: %v", filename, err)
}
return nil
}
// TODO: we ultimately want a better decoder for JSON that allows us exact line numbers and better
// surrounding text description. This should be removed / replaced when that happens.
func captureSurroundingJSONForError(prefix string, data []byte, err error) error {
if syntaxErr, ok := err.(*json.SyntaxError); err != nil && ok {
offset := syntaxErr.Offset
begin := offset - 20
if begin < 0 {
begin = 0
}
end := offset + 20
if end > int64(len(data)) {
end = int64(len(data))
}
return fmt.Errorf("%s%v (found near '%s')", prefix, err, string(data[begin:end]))
}
if err != nil {
return fmt.Errorf("%s%v", prefix, err)
}
return err
}
// IsAdmissionPluginActivated returns true if the admission plugin is activated using configapi.DefaultAdmissionConfig
// otherwise it returns a default value
func IsAdmissionPluginActivated(reader io.Reader, defaultValue bool) (bool, error) {
obj, err := ReadYAML(reader)
if err != nil {
return false, err
}
if obj == nil {
return defaultValue, nil
}
activationConfig, ok := obj.(*configapi.DefaultAdmissionConfig)
if !ok {
// if we failed the cast, then we've got a config object specified for this admission plugin
// that means that this must be enabled and all additional validation is up to the
// admission plugin itself
return true, nil
}
return !activationConfig.Disable, nil
}
func ConvertOpenshiftAdmissionConfigToKubeAdmissionConfig(in map[string]configapi.AdmissionPluginConfig) (*apiserver.AdmissionConfiguration, error) {
ret := &apiserver.AdmissionConfiguration{}
for _, pluginName := range sets.StringKeySet(in).List() {
openshiftConfig := in[pluginName]
kubeConfig := apiserver.AdmissionPluginConfiguration{
Name: pluginName,
Path: openshiftConfig.Location,
}
if openshiftConfig.Configuration != nil {
configBytes, err := runtime.Encode(Codec, openshiftConfig.Configuration)
if err != nil {
return nil, err
}
kubeConfig.Configuration = &runtime.Unknown{
Raw: configBytes,
}
}
ret.Plugins = append(ret.Plugins, kubeConfig)
}
return ret, nil
}