forked from kiali/kiali
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
175 lines (154 loc) · 6.29 KB
/
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
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
package kubernetes
import (
"fmt"
"net"
"os"
"k8s.io/api/apps/v1beta1"
"k8s.io/api/core/v1"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
kube "k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
kialiConfig "github.com/kiali/kiali/config"
osv1 "github.com/openshift/api/project/v1"
)
const (
// These constants are tweaks to the k8s client I think once are set up they won't change so no need to put them on the config
// Default QPS and Burst are quite low and those are not designed for a backend that should perform several
// queries to build an inventory of entities from a k8s backend.
// Other k8s clients have increased these values to a similar values.
k8sQPS = 100
k8sBurst = 200
)
var (
emptyListOptions = meta_v1.ListOptions{}
emptyGetOptions = meta_v1.GetOptions{}
)
// IstioClientInterface for mocks (only mocked function are necessary here)
type IstioClientInterface interface {
GetAppDetails(namespace, app string) (AppDetails, error)
GetEndpoints(namespace string, serviceName string) (*v1.Endpoints, error)
GetDeployment(namespace string, deploymentName string) (*v1beta1.Deployment, error)
GetDeployments(namespace string, labelSelector string) ([]v1beta1.Deployment, error)
GetDestinationRule(namespace string, destinationrule string) (IstioObject, error)
GetDestinationRules(namespace string, serviceName string) ([]IstioObject, error)
GetGateway(namespace string, gateway string) (IstioObject, error)
GetGateways(namespace string) ([]IstioObject, error)
GetIstioDetails(namespace string, serviceName string) (*IstioDetails, error)
GetIstioRules(namespace string) (*IstioRules, error)
GetIstioRuleDetails(namespace string, istiorule string) (*IstioRuleDetails, error)
GetNamespaceAppsDetails(namespace string) (NamespaceApps, error)
GetNamespaces() ([]v1.Namespace, error)
GetPods(namespace, labelSelector string) ([]v1.Pod, error)
GetProjects() (*osv1.ProjectList, error)
GetService(namespace string, serviceName string) (*v1.Service, error)
GetServices(namespace string, selectorLabels map[string]string) ([]v1.Service, error)
GetServiceEntries(namespace string) ([]IstioObject, error)
GetServiceEntry(namespace string, serviceEntryName string) (IstioObject, error)
GetVirtualService(namespace string, virtualservice string) (IstioObject, error)
GetVirtualServices(namespace string, serviceName string) ([]IstioObject, error)
GetQuotaSpec(namespace string, quotaSpecName string) (IstioObject, error)
GetQuotaSpecs(namespace string) ([]IstioObject, error)
GetQuotaSpecBinding(namespace string, quotaSpecBindingName string) (IstioObject, error)
GetQuotaSpecBindings(namespace string) ([]IstioObject, error)
IsOpenShift() bool
}
// IstioClient is the client struct for Kubernetes and Istio APIs
// It hides the way it queries each API
type IstioClient struct {
IstioClientInterface
k8s *kube.Clientset
istioConfigApi *rest.RESTClient
istioNetworkingApi *rest.RESTClient
}
// ConfigClient return a client with the correct configuration
// Returns configuration if Kiali is in Cluster when InCluster is true
// Returns configuration if Kiali is not int Cluster when InCluster is false
// It returns an error on any problem
func ConfigClient() (*rest.Config, error) {
if kialiConfig.Get().InCluster {
return rest.InClusterConfig()
}
host, port := os.Getenv("KUBERNETES_SERVICE_HOST"), os.Getenv("KUBERNETES_SERVICE_PORT")
if len(host) == 0 || len(port) == 0 {
return nil, fmt.Errorf("unable to load in-cluster configuration, KUBERNETES_SERVICE_HOST and KUBERNETES_SERVICE_PORT must be defined")
}
return &rest.Config{
// TODO: switch to using cluster DNS.
Host: "http://" + net.JoinHostPort(host, port),
}, nil
}
// NewClient creates a new client to the Kubernetes and Istio APIs.
// It takes the assumption that Istio is deployed into the cluster.
// It hides the access to Kubernetes/Openshift credentials.
// It hides the low level use of the API of Kubernetes and Istio, it should be considered as an implementation detail.
// It returns an error on any problem.
func NewClient() (*IstioClient, error) {
client := IstioClient{}
config, err := ConfigClient()
if err != nil {
return nil, err
}
config.QPS = k8sQPS
config.Burst = k8sBurst
k8s, err := kube.NewForConfig(config)
if err != nil {
return nil, err
}
client.k8s = k8s
// Istio is a CRD extension of Kubernetes API, so any custom type should be registered here.
// KnownTypes registers the Istio objects we use, as soon as we get more info we will increase the number of types.
types := runtime.NewScheme()
schemeBuilder := runtime.NewSchemeBuilder(
func(scheme *runtime.Scheme) error {
for _, kind := range istioKnownTypes {
scheme.AddKnownTypes(*kind.groupVersion, kind.object, kind.collection)
}
meta_v1.AddToGroupVersion(scheme, istioConfigGroupVersion)
meta_v1.AddToGroupVersion(scheme, istioNetworkingGroupVersion)
return nil
})
err = schemeBuilder.AddToScheme(types)
if err != nil {
return nil, err
}
// Istio needs another type as it queries a different K8S API.
istioConfig := rest.Config{
Host: config.Host,
APIPath: "/apis",
ContentConfig: rest.ContentConfig{
GroupVersion: &istioConfigGroupVersion,
NegotiatedSerializer: serializer.DirectCodecFactory{CodecFactory: serializer.NewCodecFactory(types)},
ContentType: runtime.ContentTypeJSON,
},
BearerToken: config.BearerToken,
TLSClientConfig: config.TLSClientConfig,
QPS: config.QPS,
Burst: config.Burst,
}
istioConfigApi, err := rest.RESTClientFor(&istioConfig)
client.istioConfigApi = istioConfigApi
if err != nil {
return nil, err
}
istioNetworking := rest.Config{
Host: config.Host,
APIPath: "/apis",
ContentConfig: rest.ContentConfig{
GroupVersion: &istioNetworkingGroupVersion,
NegotiatedSerializer: serializer.DirectCodecFactory{CodecFactory: serializer.NewCodecFactory(types)},
ContentType: runtime.ContentTypeJSON,
},
BearerToken: config.BearerToken,
TLSClientConfig: config.TLSClientConfig,
QPS: config.QPS,
Burst: config.Burst,
}
istioNetworkingApi, err := rest.RESTClientFor(&istioNetworking)
client.istioNetworkingApi = istioNetworkingApi
if err != nil {
return nil, err
}
return &client, nil
}