forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
openshiftclient.go
72 lines (65 loc) · 1.81 KB
/
openshiftclient.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
package server
import (
"errors"
"fmt"
"os"
kclient "github.com/GoogleCloudPlatform/kubernetes/pkg/client"
osclient "github.com/openshift/origin/pkg/client"
)
func NewUserOpenShiftClient(bearerToken string) (*osclient.Client, error) {
config, err := openShiftClientConfig()
if err != nil {
return nil, err
}
config.BearerToken = bearerToken
client, err := osclient.New(config)
if err != nil {
return nil, fmt.Errorf("error creating Origin client: %s", err)
}
return client, nil
}
func NewRegistryOpenShiftClient() (*osclient.Client, error) {
config, err := openShiftClientConfig()
if err != nil {
return nil, err
}
if !config.Insecure {
certData := os.Getenv("OPENSHIFT_CERT_DATA")
if len(certData) == 0 {
return nil, errors.New("OPENSHIFT_CERT_DATA is required")
}
certKeyData := os.Getenv("OPENSHIFT_KEY_DATA")
if len(certKeyData) == 0 {
return nil, errors.New("OPENSHIFT_KEY_DATA is required")
}
config.TLSClientConfig.CertData = []byte(certData)
config.TLSClientConfig.KeyData = []byte(certKeyData)
}
client, err := osclient.New(config)
if err != nil {
return nil, fmt.Errorf("error creating Origin client: %s", err)
}
return client, nil
}
func openShiftClientConfig() (*kclient.Config, error) {
openshiftAddr := os.Getenv("OPENSHIFT_MASTER")
if len(openshiftAddr) == 0 {
return nil, errors.New("OPENSHIFT_MASTER is required")
}
insecure := os.Getenv("OPENSHIFT_INSECURE") == "true"
var tlsClientConfig kclient.TLSClientConfig
if !insecure {
caData := os.Getenv("OPENSHIFT_CA_DATA")
if len(caData) == 0 {
return nil, errors.New("OPENSHIFT_CA_DATA is required")
}
tlsClientConfig = kclient.TLSClientConfig{
CAData: []byte(caData),
}
}
return &kclient.Config{
Host: openshiftAddr,
TLSClientConfig: tlsClientConfig,
Insecure: insecure,
}, nil
}