forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
apply_template.go
156 lines (126 loc) · 4.13 KB
/
apply_template.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
package componentinstall
import (
"fmt"
"io/ioutil"
"path"
"path/filepath"
"time"
"github.com/golang/glog"
"github.com/openshift/origin/pkg/oc/clusterup/coreinstall/kubeapiserver"
"k8s.io/apimachinery/pkg/util/wait"
"github.com/openshift/origin/pkg/oc/clusterup/docker/dockerhelper"
"github.com/openshift/origin/pkg/oc/clusterup/docker/run"
"github.com/openshift/origin/pkg/oc/errors"
)
type Template struct {
Name string
Namespace string
PrivilegedSANames []string
NamespaceObj []byte
RBACTemplate []byte
InstallTemplate []byte
WaitCondition func() (bool, error)
}
func (t Template) MakeReady(cliImage, baseDir string, params map[string]string) Component {
return installReadyTemplate{
template: t,
image: cliImage,
baseDir: baseDir,
params: params,
}
}
const templateBashScript = `#!/bin/sh
set -e
set -x
ls -alh /
while read p; do
oc adm policy add-scc-to-user --config=/kubeconfig.kubeconfig privileged ${p}
done </privileged-sa-list.txt
ns=""
if [ -s /namespace-file ]; then
ns="--namespace=$(cat /namespace-file) "
fi
if [ -s /namespace.yaml ]; then
oc apply --config=/kubeconfig.kubeconfig -f /namespace.yaml
elif [ -s /namespace-file ]; then
oc create ns $(cat /namespace-file) --config=/kubeconfig.kubeconfig --dry-run -o yaml | oc apply --config=/kubeconfig.kubeconfig -f -
fi
if [ -s /rbac.yaml ]; then
oc process --local -o yaml --ignore-unknown-parameters --param-file=/param-file.txt -f /rbac.yaml | oc auth reconcile --config=/kubeconfig.kubeconfig -f -
fi
oc process --local -o yaml --ignore-unknown-parameters --param-file=/param-file.txt -f /install.yaml | oc apply ${ns} --config=/kubeconfig.kubeconfig -f -
`
type installReadyTemplate struct {
template Template
image string
baseDir string
params map[string]string
}
func (opt installReadyTemplate) Name() string {
return opt.template.Name
}
func (opt installReadyTemplate) Install(dockerClient dockerhelper.Interface) error {
imageRunHelper := run.NewRunHelper(dockerhelper.NewHelper(dockerClient)).New()
glog.Infof("Installing %q\n", opt.Name())
clusterAdminConfigBytes, err := ioutil.ReadFile(path.Join(opt.baseDir, kubeapiserver.KubeAPIServerDirName, "admin.kubeconfig"))
if err != nil {
return err
}
contentToCopy := map[string][]byte{
"kubeconfig.kubeconfig": clusterAdminConfigBytes,
"namespace.yaml": opt.template.NamespaceObj,
"rbac.yaml": opt.template.RBACTemplate,
"install.yaml": opt.template.InstallTemplate,
"param-file.txt": toParamFile(opt.params),
"namespace-file": []byte(opt.template.Namespace),
"privileged-sa-list.txt": toPrivilegedSAFile(opt.template.Namespace, opt.template.PrivilegedSANames),
"install.sh": []byte(templateBashScript),
}
var lastErr error
// do a very simple retry loop on failure. Three times, ten second gaps
wait.PollImmediate(10*time.Second, 30*time.Second, func() (bool, error) {
_, rc, err := imageRunHelper.Image(opt.image).
Privileged().
DiscardContainer().
Copy(contentToCopy).
SaveContainerLogs(opt.Name(), filepath.Join(opt.baseDir, "logs")).
HostNetwork().
HostPid().
Entrypoint("sh").
Command("-c", "echo '"+opt.Name()+"' && chmod 755 /install.sh && /install.sh").Run()
if err != nil {
lastErr = errors.NewError("failed to install %q: %v", opt.Name(), err).WithCause(err)
return false, nil
}
if rc != 0 {
lastErr = errors.NewError("failed to install %q: rc: %d", opt.Name(), rc)
return false, nil
}
lastErr = nil
return true, nil
})
if lastErr != nil {
return lastErr
}
if opt.template.WaitCondition == nil {
return nil
}
if err := wait.PollImmediate(time.Second, 5*time.Minute, opt.template.WaitCondition); err != nil {
return err
}
return nil
}
func toParamFile(params map[string]string) []byte {
output := ""
for k, v := range params {
output = output + fmt.Sprintf("%v=%q\n", k, v)
}
return []byte(output)
}
func toPrivilegedSAFile(namespace string, privilegedSANames []string) []byte {
output := ""
for _, v := range privilegedSANames {
output = output + fmt.Sprintf("system:serviceaccount:%v:%v\n", namespace, v)
}
return []byte(output)
}