Skip to content

Commit abe4e06

Browse files
Merge pull request #18838 from deads2k/up-12-containers
Automatic merge from submit-queue (batch tested with PRs 18836, 18865, 18466, 18869, 18838). use containers to install cluster up components use containers to install cluster up components /assign @mfojtik
2 parents 6b7a3b6 + e37ae26 commit abe4e06

File tree

6 files changed

+429
-208
lines changed

6 files changed

+429
-208
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package componentinstall
2+
3+
import (
4+
"time"
5+
6+
"github.com/golang/glog"
7+
8+
"k8s.io/apimachinery/pkg/util/wait"
9+
10+
"github.com/openshift/origin/pkg/oc/bootstrap/docker/dockerhelper"
11+
"github.com/openshift/origin/pkg/oc/bootstrap/docker/run"
12+
"github.com/openshift/origin/pkg/oc/errors"
13+
)
14+
15+
type List struct {
16+
ComponentName string
17+
18+
Image string
19+
20+
Namespace string
21+
KubeConfig []byte
22+
List []byte
23+
24+
WaitCondition func() (bool, error)
25+
}
26+
27+
const listBashScript = `#!/bin/sh
28+
set -e
29+
set -x
30+
31+
ls -alh /
32+
33+
ns=""
34+
if [ -s /namespace-file ]; then
35+
ns="--namespace=$(cat /namespace-file) "
36+
fi
37+
38+
oc apply ${ns} --config=/kubeconfig.kubeconfig -f /list.yaml
39+
`
40+
41+
func (opt List) Name() string {
42+
return opt.ComponentName
43+
}
44+
45+
func (opt List) Install(dockerClient dockerhelper.Interface) error {
46+
imageRunHelper := run.NewRunHelper(dockerhelper.NewHelper(dockerClient)).New()
47+
48+
glog.Infof("Installing %q", opt.Name())
49+
50+
contentToCopy := map[string][]byte{
51+
"kubeconfig.kubeconfig": opt.KubeConfig,
52+
"list.yaml": opt.List,
53+
"apply.sh": []byte(listBashScript),
54+
"namespace-file": []byte(opt.Namespace),
55+
}
56+
57+
var lastErr error
58+
// do a very simple retry loop on failure. Three times, ten second gaps
59+
wait.PollImmediate(10*time.Second, 30*time.Second, func() (bool, error) {
60+
_, rc, err := imageRunHelper.Image(opt.Image).
61+
Privileged().
62+
DiscardContainer().
63+
Copy(contentToCopy).
64+
HostNetwork().
65+
HostPid().
66+
Entrypoint("sh").
67+
Command("-c", "chmod 755 /apply.sh && /apply.sh").Run()
68+
if err != nil {
69+
lastErr = errors.NewError("failed to install %q: %v", opt.Name(), err).WithCause(err)
70+
return false, nil
71+
}
72+
if rc != 0 {
73+
lastErr = errors.NewError("failed to install %q: rc: %d", opt.Name(), rc)
74+
return false, nil
75+
}
76+
lastErr = nil
77+
return true, nil
78+
})
79+
if lastErr != nil {
80+
return lastErr
81+
}
82+
83+
if opt.WaitCondition == nil {
84+
return nil
85+
}
86+
87+
if err := wait.PollImmediate(time.Second, 5*time.Minute, opt.WaitCondition); err != nil {
88+
return err
89+
}
90+
91+
return nil
92+
}
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
package componentinstall
2+
3+
import (
4+
"fmt"
5+
"time"
6+
7+
"github.com/golang/glog"
8+
9+
"k8s.io/apimachinery/pkg/util/wait"
10+
11+
"github.com/openshift/origin/pkg/oc/bootstrap/docker/dockerhelper"
12+
"github.com/openshift/origin/pkg/oc/bootstrap/docker/run"
13+
"github.com/openshift/origin/pkg/oc/errors"
14+
)
15+
16+
type Template struct {
17+
Name string
18+
19+
Namespace string
20+
PrivilegedSANames []string
21+
NamespaceObj []byte
22+
RBACTemplate []byte
23+
InstallTemplate []byte
24+
25+
WaitCondition func() (bool, error)
26+
}
27+
28+
func (t Template) MakeReady(image string, kubeconfig []byte, params map[string]string) Component {
29+
return installReadyTemplate{
30+
template: t,
31+
image: image,
32+
kubeconfig: kubeconfig,
33+
params: params,
34+
}
35+
}
36+
37+
const templateBashScript = `#!/bin/sh
38+
set -e
39+
set -x
40+
41+
ls -alh /
42+
43+
while read p; do
44+
oc adm policy add-scc-to-user --config=/kubeconfig.kubeconfig privileged ${p}
45+
done </privileged-sa-list.txt
46+
47+
ns=""
48+
if [ -s /namespace-file ]; then
49+
ns="--namespace=$(cat /namespace-file) "
50+
fi
51+
52+
if [ -s /namespace.yaml ]; then
53+
oc apply --config=/kubeconfig.kubeconfig -f /namespace.yaml
54+
fi
55+
56+
if [ -s /rbac.yaml ]; then
57+
oc process --local -o yaml --ignore-unknown-parameters --param-file=/param-file.txt -f /rbac.yaml | oc auth reconcile --config=/kubeconfig.kubeconfig -f -
58+
fi
59+
60+
oc process --local -o yaml --ignore-unknown-parameters --param-file=/param-file.txt -f /install.yaml | oc apply ${ns} --config=/kubeconfig.kubeconfig -f -
61+
`
62+
63+
type installReadyTemplate struct {
64+
template Template
65+
image string
66+
kubeconfig []byte
67+
params map[string]string
68+
}
69+
70+
func (opt installReadyTemplate) Name() string {
71+
return opt.template.Name
72+
}
73+
74+
func (opt installReadyTemplate) Install(dockerClient dockerhelper.Interface) error {
75+
imageRunHelper := run.NewRunHelper(dockerhelper.NewHelper(dockerClient)).New()
76+
77+
glog.Infof("Installing %q\n", opt.Name())
78+
79+
contentToCopy := map[string][]byte{
80+
"kubeconfig.kubeconfig": opt.kubeconfig,
81+
"namespace.yaml": opt.template.NamespaceObj,
82+
"rbac.yaml": opt.template.RBACTemplate,
83+
"install.yaml": opt.template.InstallTemplate,
84+
"param-file.txt": toParamFile(opt.params),
85+
"namespace-file": []byte(opt.template.Namespace),
86+
"privileged-sa-list.txt": toPrivilegedSAFile(opt.template.Namespace, opt.template.PrivilegedSANames),
87+
"install.sh": []byte(templateBashScript),
88+
}
89+
90+
var lastErr error
91+
// do a very simple retry loop on failure. Three times, ten second gaps
92+
wait.PollImmediate(10*time.Second, 30*time.Second, func() (bool, error) {
93+
_, rc, err := imageRunHelper.Image(opt.image).
94+
Privileged().
95+
Copy(contentToCopy).
96+
HostNetwork().
97+
HostPid().
98+
Entrypoint("sh").
99+
Command("-c", "echo '"+opt.template.Name+"' && chmod 755 /install.sh && /install.sh").Run()
100+
if err != nil {
101+
lastErr = errors.NewError("failed to install %q: %v", opt.Name(), err).WithCause(err)
102+
return false, nil
103+
}
104+
if rc != 0 {
105+
lastErr = errors.NewError("failed to install %q: rc: %d", opt.Name(), rc)
106+
return false, nil
107+
}
108+
109+
lastErr = nil
110+
return true, nil
111+
})
112+
if lastErr != nil {
113+
return lastErr
114+
}
115+
116+
if opt.template.WaitCondition == nil {
117+
return nil
118+
}
119+
120+
if err := wait.PollImmediate(time.Second, 5*time.Minute, opt.template.WaitCondition); err != nil {
121+
return err
122+
}
123+
124+
return nil
125+
}
126+
127+
func toParamFile(params map[string]string) []byte {
128+
output := ""
129+
130+
for k, v := range params {
131+
output = output + fmt.Sprintf("%v=%v\n", k, v)
132+
}
133+
return []byte(output)
134+
}
135+
136+
func toPrivilegedSAFile(namespace string, privilegedSANames []string) []byte {
137+
output := ""
138+
139+
for _, v := range privilegedSANames {
140+
output = output + fmt.Sprintf("system:serviceaccount:%v:%v\n", namespace, v)
141+
}
142+
return []byte(output)
143+
}
144+
145+
func InstallTemplates(templates []Template, image string, kubeconfig []byte, params map[string]string, dockerClient dockerhelper.Interface) error {
146+
components := []Component{}
147+
for _, template := range templates {
148+
components = append(components, template.MakeReady(image, kubeconfig, params))
149+
}
150+
151+
return InstallComponents(components, dockerClient)
152+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package componentinstall
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
"sync"
7+
8+
utilerrors "k8s.io/apimachinery/pkg/util/errors"
9+
10+
"github.com/golang/glog"
11+
"github.com/openshift/origin/pkg/oc/bootstrap/docker/dockerhelper"
12+
)
13+
14+
type Component interface {
15+
Name() string
16+
Install(dockerClient dockerhelper.Interface) error
17+
}
18+
19+
func InstallComponents(components []Component, dockerClient dockerhelper.Interface) error {
20+
componentNames := []string{}
21+
errorCh := make(chan error, len(components))
22+
waitGroupOne := sync.WaitGroup{}
23+
for i := range components {
24+
component := components[i]
25+
componentNames = append(componentNames, fmt.Sprintf("%q", component.Name()))
26+
glog.V(4).Infof("Installing %q...", component.Name())
27+
waitGroupOne.Add(1)
28+
29+
go func() {
30+
defer waitGroupOne.Done()
31+
32+
err := component.Install(dockerClient)
33+
if err != nil {
34+
glog.Errorf("Failed to install %q: %v", component.Name(), err)
35+
errorCh <- err
36+
}
37+
38+
}()
39+
}
40+
waitGroupOne.Wait()
41+
glog.Infof("Finished installing %v", strings.Join(componentNames, " "))
42+
close(errorCh)
43+
44+
errs := []error{}
45+
for err := range errorCh {
46+
errs = append(errs, err)
47+
}
48+
49+
return utilerrors.NewAggregate(errs)
50+
}

0 commit comments

Comments
 (0)