-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
148 lines (124 loc) · 3.74 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
/*
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"
"time"
"github.com/tricky42/sf_envs/client"
"github.com/tricky42/sf_envs/crd"
"flag"
apiextcs "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
apierrors "k8s.io/apimachinery/pkg/api/errors"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/clientcmd"
)
// return rest config, if path not specified assume in cluster config
func GetClientConfig(kubeconfig string) (*rest.Config, error) {
if kubeconfig != "" {
fmt.Printf("kubeconfig: %s \n", kubeconfig)
return clientcmd.BuildConfigFromFlags("", kubeconfig)
}
return rest.InClusterConfig()
}
func main() {
versionFlag := flag.Bool("version", false, "Version")
kubeconf := flag.String("kubeconf", "", "Path to a kube config. Only required if out-of-cluster.")
flag.Parse()
if *versionFlag {
fmt.Println("Git Commit:", GitCommit)
fmt.Println("Version:", Version)
if VersionPrerelease != "" {
fmt.Println("Version PreRelease:", VersionPrerelease)
}
return
}
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, scheme, err := crd.NewClient(config)
if err != nil {
panic(err)
}
// Create a CRD client interface
crdclient := client.CrdClient(crdcs, scheme, "default")
// Create a new Example object and write to k8s
environment := &crd.Environment{
ObjectMeta: meta_v1.ObjectMeta{
Name: "myenv",
Labels: map[string]string{"mylabel": "test"},
},
Spec: crd.EnvironmentSpec{
GUID: "<guid>",
SubAccountId: "<subaccountid>",
},
Status: crd.EnvironmentStatus{
State: "Created",
Message: "Created, not processed yet",
},
}
result, err := crdclient.Create(environment)
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 Environment objects
items, err := crdclient.List(meta_v1.ListOptions{})
if err != nil {
panic(err)
}
fmt.Printf("List:\n%s\n", items)
// Environment Controller
// Watch for changes in Environment objects and fire Add, Delete, Update callbacks
_, controller := cache.NewInformer(
crdclient.NewListWatch(),
&crd.Environment{},
time.Minute*10,
cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
fmt.Printf("Add: %s \n", obj)
//generate guid
//create namespace
},
DeleteFunc: func(obj interface{}) {
//remove namespace
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 {}
}