-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
157 lines (128 loc) · 4.43 KB
/
main.go
File metadata and controls
157 lines (128 loc) · 4.43 KB
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
/*
Copyright 2017 Kamesh Sampath<kamesh.sampath@hotmail.com>
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 (
"flag"
"os"
"path/filepath"
"strings"
"k8s.io/client-go/rest"
"k8s.io/client-go/pkg/api/v1"
"k8s.io/client-go/util/workqueue"
"github.com/kameshsampath/checontroller/che"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
log "github.com/sirupsen/logrus"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func init() {
log.SetFormatter(&log.JSONFormatter{})
log.SetOutput(os.Stdout)
log.SetLevel(log.InfoLevel)
}
func main() {
var kubeconfig, podNamespace, cheEndpointURI, newStackURL *string
var incluster *bool
var clientset *kubernetes.Clientset
home := homedir.HomeDir()
//fmt.Printf("Home Dir :%s\n", home)
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
//TODO - get the selectors from user
incluster = flag.Bool("incluster", false, "Whether the application is deployed inside Kubernetes cluster or outside")
podNamespace = flag.String("namespace", "", "The Kubernetes Namespace to use")
cheEndpointURI = flag.String("cheEndpointURI", "", "The Che EndpointURI")
newStackURL = flag.String("newStackURL", "https://raw.githubusercontent.com/redhat-developer/rh-che/master/assembly/fabric8-stacks/src/main/resources/stacks.json", "The New Stacks URL")
flag.Parse()
if *incluster {
log.Infoln("Accessing from inside cluster")
config, err := rest.InClusterConfig()
if err != nil {
log.Fatalf("Unable to build kubeconfig %s", err)
}
clientset, err = kubernetes.NewForConfig(config)
if err != nil {
log.Fatalf("Unable to build client %s", err)
}
*podNamespace = os.Getenv("KUBERNETES_NAMESPACE")
} else {
log.Infoln("Accessing from outside cluster")
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
log.Fatalf("Unable to build kubeconfig %s", err)
}
if *podNamespace == "" {
*podNamespace = defaultNamespaceFromConfig(kubeconfig)
}
//creates clientset
clientset, err = kubernetes.NewForConfig(config)
if err != nil {
log.Fatalf("Unable to build client %s", err)
}
}
log.Infof("Using Namespace: %s", *podNamespace)
podListWatcher := cache.NewListWatchFromClient(clientset.Core().RESTClient(), "pods",
*podNamespace, fields.Everything())
queue := workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter())
indexer, informer := cache.NewIndexerInformer(podListWatcher, &v1.Pod{}, 0, cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
key, err := cache.MetaNamespaceKeyFunc(obj)
if err == nil {
if che.IsChePod(obj) {
log.Infof("Adding Pod %s to queue", key)
queue.Add(key)
}
}
},
UpdateFunc: func(obj, newObj interface{}) {
key, err := cache.MetaNamespaceKeyFunc(newObj)
if err == nil {
if che.IsChePod(newObj) {
log.Infof("Updating Pod %s to queue", key)
queue.Add(key)
}
}
},
DeleteFunc: func(obj interface{}) {
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(obj)
if err == nil {
log.Infof("Deleteing Pod %s from queue", key)
queue.Add(key)
}
},
}, cache.Indexers{})
//create controller
controller := che.NewCheController(indexer, informer, queue, *cheEndpointURI, *newStackURL, *incluster)
indexer.Add(&v1.Pod{
ObjectMeta: meta_v1.ObjectMeta{
Namespace: *podNamespace,
Labels: map[string]string{
"deploymentconfig": "che",
},
},
})
stopCh := make(chan struct{})
defer close(stopCh)
go controller.Run(1, stopCh)
select {}
}
//defaultNamespaceFromConfig detect the namespace from currentContext
func defaultNamespaceFromConfig(kubeconfig *string) string {
config, err := clientcmd.LoadFromFile(*kubeconfig)
if err != nil {
log.Errorf("Unable to get NS from context of config %s \n", err)
}
return strings.Split(config.CurrentContext, "/")[0]
}