|
| 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 | +} |
0 commit comments