Browse code

Make file references in config relative to config files

Jordan Liggitt authored on 2015/03/18 02:35:27
Showing 4 changed files
... ...
@@ -10,8 +10,74 @@ import (
10 10
 
11 11
 	"github.com/openshift/origin/pkg/client"
12 12
 	"github.com/openshift/origin/pkg/cmd/server/crypto"
13
+	cmdutil "github.com/openshift/origin/pkg/cmd/util"
13 14
 )
14 15
 
16
+func RelativizeMasterConfigPaths(config *MasterConfig, base string) error {
17
+	return cmdutil.RelativizePaths(GetMasterFileReferences(config), base)
18
+}
19
+
20
+func ResolveMasterConfigPaths(config *MasterConfig, base string) error {
21
+	return cmdutil.ResolvePaths(GetMasterFileReferences(config), base)
22
+}
23
+
24
+func GetMasterFileReferences(config *MasterConfig) []*string {
25
+	refs := []*string{}
26
+
27
+	refs = append(refs, &config.ServingInfo.ServerCert.CertFile)
28
+	refs = append(refs, &config.ServingInfo.ServerCert.KeyFile)
29
+	refs = append(refs, &config.ServingInfo.ClientCA)
30
+
31
+	refs = append(refs, &config.EtcdClientInfo.ClientCert.CertFile)
32
+	refs = append(refs, &config.EtcdClientInfo.ClientCert.KeyFile)
33
+	refs = append(refs, &config.EtcdClientInfo.CA)
34
+
35
+	if config.EtcdConfig != nil {
36
+		refs = append(refs, &config.EtcdConfig.ServingInfo.ServerCert.CertFile)
37
+		refs = append(refs, &config.EtcdConfig.ServingInfo.ServerCert.KeyFile)
38
+		refs = append(refs, &config.EtcdConfig.ServingInfo.ClientCA)
39
+		refs = append(refs, &config.EtcdConfig.StorageDir)
40
+	}
41
+
42
+	if config.OAuthConfig != nil {
43
+		refs = append(refs, &config.OAuthConfig.ProxyCA)
44
+	}
45
+
46
+	if config.AssetConfig != nil {
47
+		refs = append(refs, &config.AssetConfig.ServingInfo.ServerCert.CertFile)
48
+		refs = append(refs, &config.AssetConfig.ServingInfo.ServerCert.KeyFile)
49
+		refs = append(refs, &config.AssetConfig.ServingInfo.ClientCA)
50
+	}
51
+
52
+	refs = append(refs, &config.MasterClients.DeployerKubeConfig)
53
+	refs = append(refs, &config.MasterClients.OpenShiftLoopbackKubeConfig)
54
+	refs = append(refs, &config.MasterClients.KubernetesKubeConfig)
55
+
56
+	return refs
57
+}
58
+
59
+func RelativizeNodeConfigPaths(config *NodeConfig, base string) error {
60
+	return cmdutil.RelativizePaths(GetNodeFileReferences(config), base)
61
+}
62
+
63
+func ResolveNodeConfigPaths(config *NodeConfig, base string) error {
64
+	return cmdutil.ResolvePaths(GetNodeFileReferences(config), base)
65
+}
66
+
67
+func GetNodeFileReferences(config *NodeConfig) []*string {
68
+	refs := []*string{}
69
+
70
+	refs = append(refs, &config.ServingInfo.ServerCert.CertFile)
71
+	refs = append(refs, &config.ServingInfo.ServerCert.KeyFile)
72
+	refs = append(refs, &config.ServingInfo.ClientCA)
73
+
74
+	refs = append(refs, &config.MasterKubeConfig)
75
+
76
+	refs = append(refs, &config.VolumeDirectory)
77
+
78
+	return refs
79
+}
80
+
15 81
 func GetKubeClient(kubeConfigFile string) (*kclient.Client, *kclient.Config, error) {
16 82
 	loadingRules := &clientcmd.ClientConfigLoadingRules{CommandLinePath: kubeConfigFile}
17 83
 	loader := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, &clientcmd.ConfigOverrides{})
... ...
@@ -6,6 +6,8 @@ import (
6 6
 	"io/ioutil"
7 7
 	"net/http"
8 8
 	_ "net/http/pprof"
9
+	"os"
10
+	"path/filepath"
9 11
 	"strings"
10 12
 
11 13
 	"github.com/coreos/go-systemd/daemon"
... ...
@@ -174,6 +176,24 @@ func (o MasterOptions) RunMaster() error {
174 174
 	}
175 175
 
176 176
 	if o.WriteConfigOnly {
177
+		// Resolve relative to CWD
178
+		cwd, err := os.Getwd()
179
+		if err != nil {
180
+			return err
181
+		}
182
+		if err := configapi.ResolveMasterConfigPaths(masterConfig, cwd); err != nil {
183
+			return err
184
+		}
185
+
186
+		// Relativize to config file dir
187
+		base, err := cmdutil.MakeAbs(filepath.Dir(o.ConfigFile), cwd)
188
+		if err != nil {
189
+			return err
190
+		}
191
+		if err := configapi.RelativizeMasterConfigPaths(masterConfig, base); err != nil {
192
+			return err
193
+		}
194
+
177 195
 		content, err := WriteMaster(masterConfig)
178 196
 		if err != nil {
179 197
 			return err
... ...
@@ -249,6 +269,15 @@ func ReadMasterConfig(filename string) (*configapi.MasterConfig, error) {
249 249
 	if err := configapilatest.Codec.DecodeInto(data, config); err != nil {
250 250
 		return nil, err
251 251
 	}
252
+
253
+	base, err := cmdutil.MakeAbs(filepath.Dir(filename), "")
254
+	if err != nil {
255
+		return nil, err
256
+	}
257
+	if err := configapi.ResolveMasterConfigPaths(config, base); err != nil {
258
+		return nil, err
259
+	}
260
+
252 261
 	return config, nil
253 262
 }
254 263
 
... ...
@@ -6,6 +6,7 @@ import (
6 6
 	"io/ioutil"
7 7
 	"net/http"
8 8
 	_ "net/http/pprof"
9
+	"os"
9 10
 	"path"
10 11
 	"path/filepath"
11 12
 	"strings"
... ...
@@ -148,6 +149,24 @@ func (o NodeOptions) RunNode() error {
148 148
 	}
149 149
 
150 150
 	if o.WriteConfigOnly {
151
+		// Resolve relative to CWD
152
+		cwd, err := os.Getwd()
153
+		if err != nil {
154
+			return err
155
+		}
156
+		if err := configapi.ResolveNodeConfigPaths(nodeConfig, cwd); err != nil {
157
+			return err
158
+		}
159
+
160
+		// Relativize to config file dir
161
+		base, err := cmdutil.MakeAbs(filepath.Dir(o.ConfigFile), cwd)
162
+		if err != nil {
163
+			return err
164
+		}
165
+		if err := configapi.RelativizeNodeConfigPaths(nodeConfig, base); err != nil {
166
+			return err
167
+		}
168
+
151 169
 		content, err := WriteNode(nodeConfig)
152 170
 		if err != nil {
153 171
 			return err
... ...
@@ -239,6 +258,15 @@ func ReadNodeConfig(filename string) (*configapi.NodeConfig, error) {
239 239
 	if err := configapilatest.Codec.DecodeInto(data, config); err != nil {
240 240
 		return nil, err
241 241
 	}
242
+
243
+	base, err := cmdutil.MakeAbs(filepath.Dir(filename), "")
244
+	if err != nil {
245
+		return nil, err
246
+	}
247
+	if err := configapi.ResolveNodeConfigPaths(config, base); err != nil {
248
+		return nil, err
249
+	}
250
+
242 251
 	return config, nil
243 252
 }
244 253
 
245 254
new file mode 100644
... ...
@@ -0,0 +1,49 @@
0
+package util
1
+
2
+import (
3
+	"os"
4
+	"path/filepath"
5
+)
6
+
7
+func MakeAbs(path, base string) (string, error) {
8
+	if filepath.IsAbs(path) {
9
+		return path, nil
10
+	}
11
+	if len(base) == 0 {
12
+		cwd, err := os.Getwd()
13
+		if err != nil {
14
+			return "", err
15
+		}
16
+		base = cwd
17
+	}
18
+	return filepath.Join(base, path), nil
19
+}
20
+
21
+// ResolvePaths updates the given refs to be absolute paths, relative to the given base directory
22
+func ResolvePaths(refs []*string, base string) error {
23
+	for _, ref := range refs {
24
+		// Don't resolve empty paths
25
+		if len(*ref) > 0 {
26
+			// Don't resolve absolute paths
27
+			if !filepath.IsAbs(*ref) {
28
+				*ref = filepath.Join(base, *ref)
29
+			}
30
+		}
31
+	}
32
+	return nil
33
+}
34
+
35
+// RelativizePaths updates the given refs to be relative paths, relative to the given base directory
36
+func RelativizePaths(refs []*string, base string) error {
37
+	for _, ref := range refs {
38
+		// Don't relativize empty paths
39
+		if len(*ref) > 0 {
40
+			rel, err := filepath.Rel(base, *ref)
41
+			if err != nil {
42
+				return err
43
+			}
44
+			*ref = rel
45
+		}
46
+	}
47
+	return nil
48
+}