This repository has been archived by the owner on Mar 14, 2022. It is now read-only.
forked from istio/istio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kube.go
117 lines (98 loc) · 3.55 KB
/
kube.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
// Copyright 2018 Istio 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 common
import (
"k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
"k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/typed/apiextensions/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)
// Kube interface allows access to the Kubernetes API Service methods. It is mainly used for test/injection
// purposes
type Kube interface {
CustomResourceDefinitionInterface() (v1beta1.CustomResourceDefinitionInterface, error)
DynamicInterface(gv schema.GroupVersion, kind string, listKind string) (dynamic.Interface, error)
KubernetesInterface() (kubernetes.Interface, error)
}
type kube struct {
cfg *rest.Config
}
var _ Kube = &kube{}
// NewKube returns a new instance of Kube.
func NewKube(cfg *rest.Config) Kube {
return &kube{
cfg: cfg,
}
}
// CustomResourceDefinitionInterface returns a new instnace of v1beta1.CustomResourceDefinitionInterface.
func (k *kube) CustomResourceDefinitionInterface() (v1beta1.CustomResourceDefinitionInterface, error) {
c, err := clientset.NewForConfig(k.cfg)
if err != nil {
return nil, err
}
return c.ApiextensionsV1beta1().CustomResourceDefinitions(), nil
}
// DynamicInterface returns a new dynamic.Interface for the specified API Group/Version.
func (k *kube) DynamicInterface(gv schema.GroupVersion, kind, listKind string) (dynamic.Interface, error) {
configShallowCopy := *k.cfg
configShallowCopy.GroupVersion = &gv
configShallowCopy.APIPath = "/apis"
configShallowCopy.ContentType = runtime.ContentTypeJSON
scheme := runtime.NewScheme()
metav1.AddToGroupVersion(scheme, schema.GroupVersion{Version: "v1"})
err := addTypeToScheme(scheme, gv, kind, listKind)
if err != nil {
return nil, err
}
configShallowCopy.NegotiatedSerializer = serializer.DirectCodecFactory{
CodecFactory: serializer.NewCodecFactory(scheme),
}
return dynamic.NewClient(&configShallowCopy)
}
// KubernetesInterface returns a new kubernetes.Interface.
func (k *kube) KubernetesInterface() (kubernetes.Interface, error) {
return kubernetes.NewForConfig(k.cfg)
}
func addTypeToScheme(s *runtime.Scheme, gv schema.GroupVersion, kind, listKind string) error {
builder := runtime.NewSchemeBuilder(func(s *runtime.Scheme) error {
// Add the object itself
gvk := schema.GroupVersionKind{
Group: gv.Group,
Version: gv.Version,
Kind: kind,
}
o := &unstructured.Unstructured{}
o.SetAPIVersion(gv.Version)
o.SetKind(kind)
s.AddKnownTypeWithName(gvk, o)
// Add the collection object.
gvk = schema.GroupVersionKind{
Group: gv.Group,
Version: gv.Version,
Kind: listKind,
}
c := &unstructured.UnstructuredList{}
o.SetAPIVersion(gv.Group)
o.SetKind(listKind)
s.AddKnownTypeWithName(gvk, c)
return nil
})
return builder.AddToScheme(s)
}