-
Notifications
You must be signed in to change notification settings - Fork 28
/
kube-crd.go
126 lines (108 loc) · 3.22 KB
/
kube-crd.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
/*
Copyright 2016 Iguazio Systems Ltd.
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 main
import (
"fmt"
"github.com/yaronha/kube-crd/client"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
apiextcs "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/clientcmd"
"time"
"github.com/yaronha/kube-crd/crd"
)
// return rest config, if path not specified assume in cluster config
func GetClientConfig(kubeconfig string) (*rest.Config, error) {
if kubeconfig != "" {
return clientcmd.BuildConfigFromFlags("", kubeconfig)
}
return rest.InClusterConfig()
}
func main() {
kubeconf := "admin.conf" // Full path to Kube config
config, err := GetClientConfig(kubeconf)
if err != nil {
panic(err.Error())
}
// create clientset and create our CRD, this only need to run once
clientset, err := apiextcs.NewForConfig(config)
if err != nil {
panic(err.Error())
}
// note: if the CRD exist our CreateCRD function is set to exit without an error
err = crd.CreateCRD(clientset)
if err != nil {
panic(err)
}
// Wait for the CRD to be created before we use it (only needed if its a new one)
time.Sleep(3 * time.Second)
// Create a new clientset which include our CRD schema
crdcs, _, err := crd.NewClient(config)
if err != nil {
panic(err)
}
// Create a CRD client interface
crdclient := client.CrdClient(crdcs, "default")
// Create a new Example object and write to k8s
example := &crd.Example{
ObjectMeta: meta_v1.ObjectMeta{
Name: "example123",
Labels: map[string]string{"mylabel": "test"},
},
Spec: crd.ExampleSpec{
Foo: "example-text",
Bar: true,
},
Status: crd.ExampleStatus{
State: "created",
Message: "Created, not processed yet",
},
}
result, err := crdclient.Create(example)
if err == nil {
fmt.Printf("CREATED: %#v\n", result)
} else if apierrors.IsAlreadyExists(err) {
fmt.Printf("ALREADY EXISTS: %#v\n", result)
} else {
panic(err)
}
// List all Example objects
items, err := crdclient.List()
if err != nil {
panic(err)
}
fmt.Printf("List:\n%s\n", items)
// Watch for changes in Example objects and fire Add, Delete, Update callbacks (i.e. Controller)
_, controller := cache.NewInformer(
crdclient.NewListWatch(),
&crd.Example{},
time.Minute*10,
cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
fmt.Printf("add: %s \n", obj)
},
DeleteFunc: func(obj interface{}) {
fmt.Printf("delete: %s \n", obj)
},
UpdateFunc: func(oldObj, newObj interface{}) {
fmt.Printf("Update old: %s \n New: %s\n", oldObj, newObj)
},
},
)
stop := make(chan struct{})
go controller.Run(stop)
// Wait forever
select {}
}