forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
router_install.go
177 lines (159 loc) · 5.51 KB
/
router_install.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
package router
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
goruntime "runtime"
"github.com/golang/glog"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
kapi "k8s.io/kubernetes/pkg/apis/core"
kclientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
"github.com/openshift/origin/pkg/cmd/server/admin"
configapi "github.com/openshift/origin/pkg/cmd/server/apis/config"
configapilatest "github.com/openshift/origin/pkg/cmd/server/apis/config/latest"
"github.com/openshift/origin/pkg/oc/clusteradd/componentinstall"
"github.com/openshift/origin/pkg/oc/clusterup/coreinstall/kubeapiserver"
"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"
)
const (
DefaultNamespace = "default"
RouterServiceAccountName = "router"
RouterServiceName = "router"
)
type RouterComponentOptions struct {
InstallContext componentinstall.Context
}
func (c *RouterComponentOptions) Name() string {
return "openshift-router"
}
func (c *RouterComponentOptions) Install(dockerClient dockerhelper.Interface) error {
kubeAdminClient, err := kclientset.NewForConfig(c.InstallContext.ClusterAdminClientConfig())
if err != nil {
return err
}
_, err = kubeAdminClient.Core().Services(DefaultNamespace).Get(RouterServiceName, metav1.GetOptions{})
if err == nil {
glog.V(3).Infof("The %q service is already present, skipping installation", RouterServiceName)
// Router service already exists, nothing to do
return nil
}
if !apierrors.IsNotFound(err) {
return errors.NewError("error retrieving router service").WithCause(err)
}
componentName := "install-router"
imageRunHelper := run.NewRunHelper(dockerhelper.NewHelper(dockerClient)).New()
// Create service account for router
routerSA := &kapi.ServiceAccount{}
routerSA.Name = RouterServiceAccountName
_, err = kubeAdminClient.Core().ServiceAccounts(DefaultNamespace).Create(routerSA)
if err != nil {
return errors.NewError("cannot create router service account").WithCause(err)
}
if err := componentinstall.AddPrivilegedUser(c.InstallContext.ClusterAdminClientConfig(), DefaultNamespace, RouterServiceAccountName); err != nil {
return err
}
masterConfig, err := getMasterConfig(c.InstallContext.BaseDir())
if err != nil {
return err
}
masterConfigDir := path.Join(c.InstallContext.BaseDir(), kubeapiserver.KubeAPIServerDirName)
// Create router cert
cmdOutput := &bytes.Buffer{}
createCertOptions := &admin.CreateServerCertOptions{
SignerCertOptions: &admin.SignerCertOptions{
CertFile: filepath.Join(masterConfigDir, "ca.crt"),
KeyFile: filepath.Join(masterConfigDir, "ca.key"),
SerialFile: filepath.Join(masterConfigDir, "ca.serial.txt"),
},
Overwrite: true,
Hostnames: []string{
masterConfig.RoutingConfig.Subdomain,
// This will ensure that routes using edge termination and the default
// certs will use certs valid for their arbitrary subdomain names.
"*." + masterConfig.RoutingConfig.Subdomain,
},
CertFile: filepath.Join(masterConfigDir, "router.crt"),
KeyFile: filepath.Join(masterConfigDir, "router.key"),
Output: cmdOutput,
}
_, err = createCertOptions.CreateServerCert()
if err != nil {
return errors.NewError("cannot create router cert").WithCause(err)
}
err = catFiles(filepath.Join(masterConfigDir, "router.pem"),
filepath.Join(masterConfigDir, "router.crt"),
filepath.Join(masterConfigDir, "router.key"),
filepath.Join(masterConfigDir, "ca.crt"))
if err != nil {
return errors.NewError("cannot create aggregate router cert").WithCause(err)
}
routerCertPath := masterConfigDir + "/router.pem"
flags := []string{
"adm", "router",
"--host-ports=true",
fmt.Sprintf("--loglevel=%d", c.InstallContext.ComponentLogLevel()),
"--config=" + masterConfigDir + "/admin.kubeconfig",
fmt.Sprintf("--host-network=%v", !portForwarding()),
fmt.Sprintf("--images=%s", c.InstallContext.ImageFormat()),
fmt.Sprintf("--default-cert=%s", routerCertPath),
}
_, rc, err := imageRunHelper.Image(c.InstallContext.ClientImage()).
Privileged().
DiscardContainer().
HostNetwork().
SaveContainerLogs(componentName, filepath.Join(c.InstallContext.BaseDir(), "logs")).
Bind(masterConfigDir + ":" + masterConfigDir).
Entrypoint("oc").
Command(flags...).Run()
if rc != 0 {
return errors.NewError("could not run %q: rc=%d", componentName, rc)
}
return nil
}
// catFiles concatenates multiple source files into a single destination file
func catFiles(dest string, src ...string) error {
out, err := os.Create(dest)
if err != nil {
return err
}
defer out.Close()
for _, f := range src {
in, oerr := os.Open(f)
if oerr != nil {
return err
}
_, err = io.Copy(out, in)
in.Close()
if err != nil {
return err
}
}
return nil
}
func getMasterConfig(basedir string) (*configapi.MasterConfig, error) {
configBytes, err := ioutil.ReadFile(path.Join(basedir, kubeapiserver.KubeAPIServerDirName, "master-config.yaml"))
if err != nil {
return nil, err
}
configObj, err := runtime.Decode(configapilatest.Codec, configBytes)
if err != nil {
return nil, err
}
masterConfig, ok := configObj.(*configapi.MasterConfig)
if !ok {
return nil, fmt.Errorf("the %#v is not MasterConfig", configObj)
}
return masterConfig, nil
}
func portForwarding() bool {
// true if running on Mac, with no DOCKER_HOST defined
return goruntime.GOOS == "darwin" && len(os.Getenv("DOCKER_HOST")) == 0
}