-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
155 lines (138 loc) · 3.95 KB
/
main.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
package main
import (
"context"
"fmt"
"os/signal"
"time"
v1 "github.com/heheh13/crd/custom/apis/heheh.com/v1"
"github.com/kr/pretty"
destroymentclientset "github.com/heheh13/crd/custom/client/clientset/versioned"
crdapi "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
crdclientset "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"log"
"os"
"path/filepath"
"k8s.io/client-go/tools/clientcmd"
)
func main() {
kubeconfigPath := filepath.Join(os.Getenv("HOME"), ".kube/config")
fmt.Print(kubeconfigPath)
config, err := clientcmd.BuildConfigFromFlags("", kubeconfigPath)
if err != nil {
log.Fatal("couldn't get kubernetes config %s", err)
}
log.Println("custom resource Destroymet Craeting.....")
crdClient, err := crdclientset.NewForConfig(config)
if err != nil {
log.Fatal("error while configuring crd client")
}
mycrd := &crdapi.CustomResourceDefinition{
ObjectMeta: metav1.ObjectMeta{
Name: "destroyments.heheh.com",
},
Spec: crdapi.CustomResourceDefinitionSpec{
Group: "heheh.com",
Names: crdapi.CustomResourceDefinitionNames{
Plural: "destroyments",
Singular: "destroyment",
ShortNames: []string{
"ds",
},
Kind: "Destroyment",
},
Scope: "Namespaced",
Versions: []crdapi.CustomResourceDefinitionVersion{
{
Name: "v1",
Served: true,
Storage: true,
Schema: &crdapi.CustomResourceValidation{
OpenAPIV3Schema: &crdapi.JSONSchemaProps{
Type: "object",
Properties: map[string]crdapi.JSONSchemaProps{
"spec": {
Type: "object",
Properties: map[string]crdapi.JSONSchemaProps{
"replicas": {
Type: "integer",
},
"container": {
Type: "object",
Properties: map[string]crdapi.JSONSchemaProps{
"image": {
Type: "string",
},
"port": {
Type: "integer",
},
},
},
},
},
},
},
},
},
},
},
}
_ = crdClient.ApiextensionsV1().CustomResourceDefinitions().Delete(context.TODO(), mycrd.Name, metav1.DeleteOptions{})
_, err = crdClient.ApiextensionsV1().CustomResourceDefinitions().Create(context.TODO(), mycrd, metav1.CreateOptions{})
if err != nil {
panic(err)
}
time.Sleep(5 * time.Second)
log.Println("custom resoucse 'destroyment' Created!")
//deleting destroyment?
defer func() {
log.Println("deleteing destroyment!")
if err = crdClient.ApiextensionsV1().CustomResourceDefinitions().Delete(context.TODO(), mycrd.Name, metav1.DeleteOptions{}); err != nil {
panic(err)
}
log.Println("destroyment deleted")
}()
log.Println("Press Ctrl+C to Create an instance of destroyment. . . .")
ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt)
<-ch ///taking input?
log.Println("creating destroyment ... ")
destroymentClient, err := destroymentclientset.NewForConfig(config)
destroyment := &v1.Destroyment{
ObjectMeta: metav1.ObjectMeta{
Name: "destroyment",
},
Spec: v1.DestroymentSpec{
Replicas: intptr(2),
Container: v1.ContainerSpec{
Image: "nginx",
Port: 8080,
},
},
Status: v1.DestroymentStatus{
Phase: "....ing",
},
}
ds, err := destroymentClient.HehehV1().Destroyments("default").Create(context.TODO(), destroyment, metav1.CreateOptions{})
if err != nil {
panic(err)
}
log.Println("destroyment created !!!!! ")
fmt.Println(ds)
pretty.Print(ds)
defer func() {
log.Println("deleting destroyment .......")
if err := destroymentClient.HehehV1().Destroyments("default").Delete(
context.TODO(), destroyment.Name, metav1.DeleteOptions{}); err != nil {
panic(err)
}
log.Println("destroyment deleted")
}()
log.Println("Press Ctrl+C to Delete the Custom Resource 'destroyment' and the instance of d3stroyment'. . . .")
ch = make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt)
<-ch
}
func intptr(i int32) *int32 {
return &i
}