This repository has been archived by the owner on Mar 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
kubernetes.go
74 lines (65 loc) · 1.89 KB
/
kubernetes.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
package handler
import (
"k8s.io/apimachinery/pkg/api/meta"
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/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/restmapper"
"k8s.io/client-go/tools/clientcmd"
)
type KubernetesClient interface {
Apply(obj runtime.Object, namespace string) error
}
type kubernetesClient struct {
dynamic.Interface
meta.RESTMapper
}
func NewKubernetesClient(kubeconfig string) (*kubernetesClient, error) {
config, err := buildKubernetesConfig(kubeconfig)
if err != nil {
return nil, err
}
intf, err := dynamic.NewForConfig(config)
if err != nil {
return nil, err
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}
groupResources, err := restmapper.GetAPIGroupResources(clientset.Discovery())
if err != nil {
return nil, err
}
return &kubernetesClient{
Interface: intf,
RESTMapper: restmapper.NewDiscoveryRESTMapper(groupResources),
}, nil
}
func buildKubernetesConfig(kubeconfig string) (config *rest.Config, err error) {
if kubeconfig != "" {
return clientcmd.BuildConfigFromFlags("", kubeconfig)
}
return rest.InClusterConfig()
}
func (k *kubernetesClient) Apply(obj runtime.Object, namespace string) error {
switch obj := obj.(type) {
case *unstructured.Unstructured:
gvk := obj.GroupVersionKind()
gk := schema.GroupKind{Group: gvk.Group, Kind: gvk.Kind}
mapping, err := k.RESTMapper.RESTMapping(gk, gvk.Version)
if err != nil {
return err
}
if _, err := k.Interface.Resource(mapping.Resource).Namespace(namespace).Create(obj, metav1.CreateOptions{}); err != nil {
return err
}
case *unstructured.UnstructuredList:
return obj.EachListItem(func(o runtime.Object) error { return k.Apply(o, namespace) })
}
return nil
}