forked from kiali/kiali
-
Notifications
You must be signed in to change notification settings - Fork 0
/
os_route_client.go
87 lines (78 loc) · 2.18 KB
/
os_route_client.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
package kubernetes
import (
"encoding/json"
"errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/client-go/rest"
)
// OSRouteClient is the client struct for OpenShift Routes API over Kubernetes
// It hides the way it queries each API
type OSRouteClient struct {
client *rest.RESTClient
}
// NewOSRouteClient creates a new client able to fetch OpenShift Routes API.
func NewOSRouteClient() (*OSRouteClient, error) {
config, err := rest.InClusterConfig()
if err != nil {
return nil, err
}
types := runtime.NewScheme()
schemeBuilder := runtime.NewSchemeBuilder(
func(scheme *runtime.Scheme) error {
return nil
})
err = schemeBuilder.AddToScheme(types)
if err != nil {
return nil, err
}
osConfig := rest.Config{
Host: config.Host,
APIPath: "/apis",
ContentConfig: rest.ContentConfig{
GroupVersion: &osRouteGroupVersion,
NegotiatedSerializer: serializer.DirectCodecFactory{CodecFactory: serializer.NewCodecFactory(types)},
ContentType: runtime.ContentTypeJSON,
},
BearerToken: config.BearerToken,
TLSClientConfig: config.TLSClientConfig,
QPS: k8sQPS,
Burst: k8sBurst,
}
client, err := rest.RESTClientFor(&osConfig)
if err != nil {
return nil, err
}
return &OSRouteClient{
client: client,
}, nil
}
// GetRoute returns an OpenShift route URL for the given name
func (in *OSRouteClient) GetRoute(namespace string, routeName string) (string, error) {
result, err := in.client.Get().Namespace(namespace).Resource("routes").SubResource(routeName).Do().Raw()
if err != nil {
return "", err
}
var obj interface{}
err = json.Unmarshal(result, &obj)
if err != nil {
return "", err
}
spec, ok := obj.(map[string]interface{})["spec"]
if !ok {
return "", errors.New("Missing spec in Route")
}
host, ok := spec.(map[string]interface{})["host"].(string)
if !ok {
return "", errors.New("Missing host in Route spec")
}
protocol := "http"
tls, ok := spec.(map[string]interface{})["tls"]
if ok {
tlsTermination, ok := tls.(map[string]interface{})["termination"].(string)
if ok && len(tlsTermination) > 0 {
protocol = "https"
}
}
return protocol + "://" + host, nil
}