forked from kyma-project/kyma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
174 lines (148 loc) · 5.04 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package main
import (
"context"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"os/signal"
"syscall"
"time"
scCs "github.com/kubernetes-incubator/service-catalog/pkg/client/clientset_generated/clientset"
"github.com/sirupsen/logrus"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"github.com/kyma-project/kyma/components/helm-broker/internal/bind"
"github.com/kyma-project/kyma/components/helm-broker/internal/broker"
"github.com/kyma-project/kyma/components/helm-broker/internal/bundle"
"github.com/kyma-project/kyma/components/helm-broker/internal/config"
"github.com/kyma-project/kyma/components/helm-broker/internal/helm"
"github.com/kyma-project/kyma/components/helm-broker/internal/storage"
"github.com/kyma-project/kyma/components/helm-broker/platform/logger"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "k8s.io/client-go/informers/core/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
)
const (
mapLabelKey = "helm-broker-repo"
mapLabelValue = "true"
)
func main() {
verbose := flag.Bool("verbose", false, "specify if log verbosely loading configuration")
flag.Parse()
cfg, err := config.Load(*verbose)
fatalOnError(err)
// creates the in-cluster k8sConfig
k8sConfig, err := newRestClientConfig(cfg.KubeconfigPath)
fatalOnError(err)
// creates the clientset
clientset, err := kubernetes.NewForConfig(k8sConfig)
fatalOnError(err)
log := logger.New(&cfg.Logger)
storageConfig := storage.ConfigList(cfg.Storage)
sFact, err := storage.NewFactory(&storageConfig)
fatalOnError(err)
// ServiceCatalog
scClientSet, err := scCs.NewForConfig(k8sConfig)
fatalOnError(err)
csbInterface := scClientSet.ServicecatalogV1beta1().ClusterServiceBrokers()
// broker sync
stopCh := make(chan struct{})
bLoader := bundle.NewLoader(cfg.TmpDir, log)
bundleSyncer := bundle.NewSyncer(sFact.Bundle(), sFact.Chart(), log)
brokerSyncer := broker.NewClusterServiceBrokerSyncer(csbInterface, log)
cfgMapInformer := v1.NewFilteredConfigMapInformer(clientset, cfg.Namespace, 10*time.Minute, cache.Indexers{}, func(options *metav1.ListOptions) {
options.LabelSelector = fmt.Sprintf("%s=%s", mapLabelKey, mapLabelValue)
})
repositoryWatcher := bundle.NewRepositoryController(bundleSyncer, bLoader, brokerSyncer, cfg.ClusterServiceBrokerName, cfgMapInformer, log)
go repositoryWatcher.Run(stopCh)
go cfgMapInformer.Run(stopCh)
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
cancelOnChanInterrupt(ctx, stopCh, cancelFunc)
helmClient := helm.NewClient(cfg.Helm, log)
srv := broker.New(sFact.Bundle(), sFact.Chart(), sFact.InstanceOperation(), sFact.Instance(), sFact.InstanceBindData(),
bind.NewRenderer(), bind.NewResolver(clientset.CoreV1()), helmClient, bundleSyncer, log)
cancelOnInterrupt(ctx, cancelFunc)
startedCh := make(chan struct{})
go func() {
// wait for server HTTP to be ready
<-startedCh
log.Infof("Waiting for service %s to be ready", cfg.HelmBrokerURL)
// Running Helm Broker does not mean it is visible to the service catalog
// This is the reason of the check cfg.HelmBrokerURL
waitForHelmBrokerIsReady(cfg.HelmBrokerURL, 15*time.Second, log)
log.Infof("%s service ready", cfg.HelmBrokerURL)
err := brokerSyncer.Sync(cfg.ClusterServiceBrokerName, 5)
if err != nil {
log.Errorf("Could not synchronize Service Catalog with the broker: %s", err)
}
}()
err = srv.Run(ctx, fmt.Sprintf(":%d", cfg.Port), startedCh)
fatalOnError(err)
}
func waitForHelmBrokerIsReady(url string, timeout time.Duration, log logrus.FieldLogger) {
timeoutCh := time.After(timeout)
for {
r, err := http.Get(fmt.Sprintf("%s/statusz", url))
if err == nil {
// no need to read the response
ioutil.ReadAll(r.Body)
r.Body.Close()
}
if err == nil && r.StatusCode == http.StatusOK {
break
}
select {
case <-timeoutCh:
log.Errorf("Waiting for service %s to be ready timeout %s exceeded.", url, timeout.String())
if err != nil {
log.Errorf("Last call error: %s", err.Error())
} else {
log.Errorf("Last call response status: %s", r.StatusCode)
}
return
default:
time.Sleep(time.Second)
}
}
}
func fatalOnError(err error) {
if err != nil {
logrus.Fatal(err.Error())
}
}
// cancelOnInterrupt calls cancel func when os.Interrupt or SIGTERM is received
func cancelOnInterrupt(ctx context.Context, cancel context.CancelFunc) {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
select {
case <-ctx.Done():
case <-c:
cancel()
}
}()
}
// cancelOnInterrupt closes given channel and also calls cancel func when os.Interrupt or SIGTERM is received
func cancelOnChanInterrupt(ctx context.Context, ch chan<- struct{}, cancel context.CancelFunc) {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
select {
case <-ctx.Done():
close(ch)
case <-c:
close(ch)
cancel()
}
}()
}
func newRestClientConfig(kubeConfigPath string) (*rest.Config, error) {
if kubeConfigPath != "" {
return clientcmd.BuildConfigFromFlags("", kubeConfigPath)
}
return rest.InClusterConfig()
}