forked from operator-framework/operator-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
110 lines (100 loc) · 3.86 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
// Copyright 2018 The Operator-SDK Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package k8sclient
import (
"fmt"
"net"
"os"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/discovery"
"k8s.io/client-go/discovery/cached"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)
var (
restMapper *discovery.DeferredDiscoveryRESTMapper
clientPool dynamic.ClientPool
)
// init initializes the restMapper and clientPool needed to create a resource client dynamically
func init() {
kubeClient, kubeConfig := mustNewKubeClientAndConfig()
cachedDiscoveryClient := cached.NewMemCacheClient(kubeClient.Discovery())
restMapper = discovery.NewDeferredDiscoveryRESTMapper(cachedDiscoveryClient, meta.InterfacesForUnstructured)
restMapper.Reset()
kubeConfig.ContentConfig = dynamic.ContentConfig()
clientPool = dynamic.NewClientPool(kubeConfig, restMapper, dynamic.LegacyAPIPathResolverFunc)
}
// GetResourceClient returns the dynamic client and pluralName for the resource specified by the apiVersion and kind
func GetResourceClient(apiVersion, kind, namespace string) (dynamic.ResourceInterface, string, error) {
gv, err := schema.ParseGroupVersion(apiVersion)
if err != nil {
return nil, "", fmt.Errorf("failed to parse apiVersion: %v", err)
}
gvk := schema.GroupVersionKind{
Group: gv.Group,
Version: gv.Version,
Kind: kind,
}
client, err := clientPool.ClientForGroupVersionKind(gvk)
if err != nil {
return nil, "", fmt.Errorf("failed to get client for GroupVersionKind(%s): %v", gvk.String(), err)
}
resource, err := apiResource(gvk, restMapper)
if err != nil {
return nil, "", fmt.Errorf("failed to get resource type: %v", err)
}
pluralName := resource.Name
resourceClient := client.Resource(resource, namespace)
return resourceClient, pluralName, nil
}
// apiResource consults the REST mapper to translate an <apiVersion, kind, namespace> tuple to a metav1.APIResource struct.
func apiResource(gvk schema.GroupVersionKind, restMapper *discovery.DeferredDiscoveryRESTMapper) (*metav1.APIResource, error) {
mapping, err := restMapper.RESTMapping(gvk.GroupKind(), gvk.Version)
if err != nil {
return nil, fmt.Errorf("failed to get the resource REST mapping for GroupVersionKind(%s): %v", gvk.String(), err)
}
resource := &metav1.APIResource{
Name: mapping.Resource,
Namespaced: mapping.Scope == meta.RESTScopeNamespace,
Kind: gvk.Kind,
}
return resource, nil
}
// mustNewKubeClientAndConfig returns the in-cluster config and kubernetes client
func mustNewKubeClientAndConfig() (kubernetes.Interface, *rest.Config) {
cfg, err := inClusterConfig()
if err != nil {
panic(err)
}
return kubernetes.NewForConfigOrDie(cfg), cfg
}
// inClusterConfig returns the in-cluster config accessible inside a pod
func inClusterConfig() (*rest.Config, error) {
// Work around https://github.com/kubernetes/kubernetes/issues/40973
// See https://github.com/coreos/etcd-operator/issues/731#issuecomment-283804819
if len(os.Getenv("KUBERNETES_SERVICE_HOST")) == 0 {
addrs, err := net.LookupHost("kubernetes.default.svc")
if err != nil {
panic(err)
}
os.Setenv("KUBERNETES_SERVICE_HOST", addrs[0])
}
if len(os.Getenv("KUBERNETES_SERVICE_PORT")) == 0 {
os.Setenv("KUBERNETES_SERVICE_PORT", "443")
}
return rest.InClusterConfig()
}