forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtemplateservicebroker.go
116 lines (98 loc) · 4.49 KB
/
templateservicebroker.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
package openshift
import (
"encoding/base64"
"fmt"
"io/ioutil"
"path/filepath"
"time"
"github.com/golang/glog"
kapierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
kapi "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/kubectl/resource"
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
"github.com/openshift/origin/pkg/cmd/util/variable"
"github.com/openshift/origin/pkg/oc/bootstrap/docker/errors"
)
const (
tsbNamespace = "openshift-template-service-broker"
tsbRBACTemplateName = "template-service-broker-rbac"
tsbAPIServerTemplateName = "template-service-broker-apiserver"
tsbRegistrationTemplateName = "template-service-broker-registration"
)
// InstallTemplateServiceBroker checks whether the template service broker is installed and installs it if not already installed
func (h *Helper) InstallTemplateServiceBroker(f *clientcmd.Factory, imageFormat string, serverLogLevel int) error {
kubeClient, err := f.ClientSet()
if err != nil {
return errors.NewError("cannot obtain API clients").WithCause(err).WithDetails(h.OriginLog())
}
templateClient, err := f.OpenshiftInternalTemplateClient()
if err != nil {
return err
}
// create the namespace if needed. This is a reserved namespace, so you can't do it with the create project request
if _, err := kubeClient.Core().Namespaces().Create(&kapi.Namespace{ObjectMeta: metav1.ObjectMeta{Name: tsbNamespace}}); err != nil && !kapierrors.IsAlreadyExists(err) {
return errors.NewError("cannot create template service broker project").WithCause(err)
}
if err = instantiateTemplate(templateClient.Template(), clientcmd.ResourceMapper(f), nil, OpenshiftInfraNamespace, tsbRBACTemplateName, tsbNamespace, map[string]string{}, true); err != nil {
return errors.NewError("cannot instantiate template service broker permissions").WithCause(err)
}
// create the actual resources required
imageTemplate := variable.NewDefaultImageTemplate()
imageTemplate.Format = imageFormat
imageTemplate.Latest = false
params := map[string]string{
"IMAGE": imageTemplate.ExpandOrDie(""),
"LOGLEVEL": fmt.Sprint(serverLogLevel),
"NAMESPACE": tsbNamespace,
}
glog.V(2).Infof("instantiating template service broker template with parameters %v", params)
if err = instantiateTemplate(templateClient.Template(), clientcmd.ResourceMapper(f), nil, OpenshiftInfraNamespace, tsbAPIServerTemplateName, tsbNamespace, params, true); err != nil {
return errors.NewError("cannot instantiate template service broker resources").WithCause(err)
}
// Wait for the apiserver endpoint to become available
err = wait.Poll(1*time.Second, 10*time.Minute, func() (bool, error) {
glog.V(2).Infof("polling for template service broker api server endpoint availability")
ds, err := kubeClient.Extensions().DaemonSets(tsbNamespace).Get("apiserver", metav1.GetOptions{})
if err != nil {
return false, err
}
if ds.Status.NumberReady > 0 {
return true, nil
}
return false, nil
})
if err != nil {
return errors.NewError(fmt.Sprintf("failed to start the template service broker apiserver: %v", err))
}
return nil
}
// RegisterTemplateServiceBroker registers the TSB with the SC by creating the broker resource
func (h *Helper) RegisterTemplateServiceBroker(f *clientcmd.Factory, configDir string) error {
templateClient, err := f.OpenshiftInternalTemplateClient()
if err != nil {
return err
}
// Register the template broker with the service catalog
glog.V(2).Infof("registering the template broker with the service catalog")
// dynamic mapper is needed to support the broker resource which isn't part of the api.
dynamicMapper, dynamicTyper, err := f.UnstructuredObject()
dmapper := &resource.Mapper{
RESTMapper: dynamicMapper,
ObjectTyper: dynamicTyper,
ClientMapper: resource.ClientMapperFunc(f.UnstructuredClientForMapping),
}
serviceCABytes, err := ioutil.ReadFile(filepath.Join(configDir, "master", "service-signer.crt"))
serviceCAString := base64.StdEncoding.EncodeToString(serviceCABytes)
if err != nil {
return errors.NewError("unable to read service signer cert").WithCause(err)
}
if err = instantiateTemplate(templateClient.Template(), clientcmd.ResourceMapper(f), dmapper, OpenshiftInfraNamespace, tsbRegistrationTemplateName, tsbNamespace, map[string]string{
"TSB_NAMESPACE": tsbNamespace,
"CA_BUNDLE": serviceCAString,
}, true); err != nil {
return errors.NewError("cannot register the template service broker").WithCause(err)
}
return nil
}