-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathk8apply.go
165 lines (141 loc) · 4.14 KB
/
k8apply.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
package k8apply
import (
"os"
"path/filepath"
"strings"
"time"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/cli-runtime/pkg/resource"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/client-go/restmapper"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
)
var (
restConfig *rest.Config
clientset *kubernetes.Clientset
decoder runtime.Decoder
)
func init() {
applyChan = make(chan ApplyWithChan)
go consumeApplies()
}
// InitKubeClient initializes kubernetes client with CLUSTER_TYPE choose
func InitKubeClient() error {
var kubeConfig *string
var err error
if os.Getenv("KUBECD_CLUSTER_TYPE") == "OUT_OF_CLUSTER" {
if kubeconfigFromEnv := os.Getenv("KUBECONFIG"); kubeconfigFromEnv != "" {
kubeConfig = &kubeconfigFromEnv
} else {
kubeConfigWithHomeDir := filepath.Join(homedir.HomeDir(), ".kube", "config")
kubeConfig = &kubeConfigWithHomeDir
}
restConfig, err = clientcmd.BuildConfigFromFlags("", *kubeConfig)
if err != nil {
return err
}
} else {
restConfig, err = rest.InClusterConfig()
if err != nil {
return err
}
}
clientset, err = kubernetes.NewForConfig(restConfig)
if err != nil {
return err
}
decoder = scheme.Codecs.UniversalDeserializer()
return nil
}
// decodeObj decodes raw yaml or json files to runtime.Object
func decodeObj(data []byte) (runtime.Object, error) {
obj, _, err := decoder.Decode(data, nil, nil)
return obj, err
}
type ApplyWithChan struct {
Data []byte
C chan error
}
// applyChan uses for queue mechanism, all kubernetes manifests must apply with this chan because of kubernetes rate limit
var applyChan chan ApplyWithChan
// lastApply uses for detecting rate limit diff
var lastApply = time.Now()
// AddToApplyQueue adds raw manifest to applyChan
func AddToApplyQueue(data []byte, c chan error) {
applyChan <- ApplyWithChan{
Data: data,
C: c,
}
}
// consumeApplies consumes applyChan
func consumeApplies() {
for d := range applyChan {
diff := time.Since(lastApply)
if diff < time.Millisecond*2000 {
time.Sleep(time.Millisecond * 2000)
}
d.C <- Apply(d.Data)
}
}
// Apply like kubectl apply, tries to create new object, if it exists tries to replace
func Apply(data []byte) error {
lastApply = time.Now()
decodedObj, err := decodeObj(data)
if err != nil {
return err
}
_, err = applyObject(clientset, *restConfig, decodedObj)
if err != nil {
return err
}
return nil
}
// applyObject applies with runtime.Object, tries to create, if it exists tries to replace
func applyObject(kubeClientset kubernetes.Interface, restConfig rest.Config, obj runtime.Object) (runtime.Object, error) {
groupResources, err := restmapper.GetAPIGroupResources(kubeClientset.Discovery())
if err != nil {
return nil, err
}
rm := restmapper.NewDiscoveryRESTMapper(groupResources)
groupVersionKind := obj.GetObjectKind().GroupVersionKind()
groupKind := schema.GroupKind{Group: groupVersionKind.Group, Kind: groupVersionKind.Kind}
mapping, err := rm.RESTMapping(groupKind, groupVersionKind.Version)
if err != nil {
return nil, err
}
restClient, err := newRestClient(restConfig, mapping.GroupVersionKind.GroupVersion())
if err != nil {
return nil, err
}
name, err := meta.NewAccessor().Name(obj)
if err != nil {
return nil, err
}
namespace, err := meta.NewAccessor().Namespace(obj)
if err != nil || namespace == "" {
namespace = "default"
}
restHelper := resource.NewHelper(restClient, mapping)
// _, err = restHelper.DeleteWithOptions(namespace, name, &metav1.DeleteOptions{})
_, err = restHelper.Create(namespace, true, obj)
if err != nil && strings.Contains(err.Error(), "already exists") {
return restHelper.Replace(namespace, name, true, obj)
}
return obj, err
}
// newRestClient
func newRestClient(restConfig rest.Config, gv schema.GroupVersion) (rest.Interface, error) {
restConfig.ContentConfig = resource.UnstructuredPlusDefaultContentConfig()
restConfig.GroupVersion = &gv
if len(gv.Group) == 0 {
restConfig.APIPath = "/api"
} else {
restConfig.APIPath = "/apis"
}
return rest.RESTClientFor(&restConfig)
}