forked from k3s-io/helm-controller
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd.go
More file actions
98 lines (86 loc) · 2.67 KB
/
Copy pathcmd.go
File metadata and controls
98 lines (86 loc) · 2.67 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
package cmd
import (
"context"
"fmt"
"log"
"net/http"
"time"
"github.com/go-logr/logr"
"github.com/k3s-io/helm-controller/pkg/config"
"github.com/k3s-io/helm-controller/pkg/controllers"
"github.com/k3s-io/helm-controller/pkg/controllers/common"
"github.com/k3s-io/helm-controller/pkg/crds"
"github.com/rancher/wrangler/v3/pkg/crd"
"github.com/rancher/wrangler/v3/pkg/kubeconfig"
"github.com/sirupsen/logrus"
"k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
"k8s.io/klog/v2"
)
const (
// readyDuration time to wait for CRDs to be ready.
readyDuration = time.Minute * 1
)
func SetupLogging(debug bool, level int) (logr.Logger, error) {
klog.EnableContextualLogging(true)
if debug {
logrus.SetLevel(logrus.DebugLevel)
}
if level >= 2 {
logrus.SetLevel(logrus.TraceLevel)
}
logrus.SetFormatter(&logrus.TextFormatter{DisableQuote: true})
logger := common.NewLogrusSink(nil).AsLogr()
klog.SetLoggerWithOptions(logger, klog.ContextualLogger(true))
return logger, nil
}
func Run(ctx context.Context, hc config.CLI) error {
if hc.Debug && hc.PprofPort > 0 {
go func() {
// Serves HTTP server runtime profiling data in the format expected by the
// pprof visualization tool at the provided endpoint on the local network
// See https://pkg.go.dev/net/http/pprof?utm_source=gopls for more information
log.Println(http.ListenAndServe(fmt.Sprintf("localhost:%d", hc.PprofPort), nil))
}()
}
logger, err := SetupLogging(hc.Debug, hc.DebugLevel)
if err != nil {
return err
}
ctx = klog.NewContext(ctx, logger)
cfg := getNonInteractiveClientConfig(hc)
rest, err := cfg.ClientConfig()
if err != nil {
return err
}
client, err := clientset.NewForConfig(rest)
if err != nil {
return err
}
crds, err := crds.List()
if err != nil {
return err
}
opts, err := hc.GetControllerConfig()
if err != nil {
return err
}
if err := crd.BatchCreateCRDs(ctx, client.ApiextensionsV1().CustomResourceDefinitions(), nil, readyDuration, crds); err != nil {
return err
}
if err := controllers.Register(ctx, hc.Namespace, hc.ControllerName, cfg, opts); err != nil {
return err
}
<-ctx.Done()
return ctx.Err()
}
func getNonInteractiveClientConfig(hc config.CLI) clientcmd.ClientConfig {
// Modified https://github.com/rancher/wrangler/blob/3ecd23dfea3bb4c76cbe8e06fb158eed6ae3dd31/pkg/kubeconfig/loader.go#L12-L32
return clientcmd.NewInteractiveDeferredLoadingClientConfig(
kubeconfig.GetLoadingRules(hc.Kubeconfig),
&clientcmd.ConfigOverrides{
ClusterDefaults: clientcmd.ClusterDefaults,
ClusterInfo: clientcmdapi.Cluster{Server: hc.MasterURL},
}, nil)
}