-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
156 lines (138 loc) · 4.62 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
package main
import (
"context"
"flag"
"fmt"
"os"
"os/signal"
"syscall"
"time"
"github.com/krateoplatformops/composition-dynamic-controller/internal/composition"
"github.com/krateoplatformops/composition-dynamic-controller/internal/support"
"github.com/krateoplatformops/composition-dynamic-controller/internal/tools/helmchart/archive"
genctrl "github.com/krateoplatformops/unstructured-runtime"
"github.com/krateoplatformops/unstructured-runtime/pkg/controller"
"github.com/krateoplatformops/unstructured-runtime/pkg/eventrecorder"
"github.com/krateoplatformops/unstructured-runtime/pkg/logging"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/selection"
"k8s.io/client-go/discovery"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
)
const (
serviceName = "composition-dynamic-controller"
compositionVersionLabel = "krateo.io/composition-version"
)
var (
Build string
)
func main() {
// Flags
kubeconfig := flag.String("kubeconfig", support.EnvString("KUBECONFIG", ""),
"absolute path to the kubeconfig file")
debug := flag.Bool("debug",
support.EnvBool("COMPOSITION_CONTROLLER_DEBUG", false), "dump verbose output")
workers := flag.Int("workers", support.EnvInt("COMPOSITION_CONTROLLER_WORKERS", 1), "number of workers")
resyncInterval := flag.Duration("resync-interval",
support.EnvDuration("COMPOSITION_CONTROLLER_RESYNC_INTERVAL", time.Minute*3), "resync interval")
resourceGroup := flag.String("group",
support.EnvString("COMPOSITION_CONTROLLER_GROUP", ""), "resource api group")
resourceVersion := flag.String("version",
support.EnvString("COMPOSITION_CONTROLLER_VERSION", ""), "resource api version")
resourceName := flag.String("resource",
support.EnvString("COMPOSITION_CONTROLLER_RESOURCE", ""), "resource plural name")
namespace := flag.String("namespace",
support.EnvString("COMPOSITION_CONTROLLER_NAMESPACE", "default"), "namespace")
chart := flag.String("chart",
support.EnvString("COMPOSITION_CONTROLLER_CHART", ""), "chart")
flag.Usage = func() {
fmt.Fprintln(flag.CommandLine.Output(), "Flags:")
flag.PrintDefaults()
}
flag.Parse()
zl := zap.New(zap.UseDevMode(*debug))
log := logging.NewLogrLogger(zl.WithName(serviceName))
// Kubernetes configuration
var cfg *rest.Config
var err error
if len(*kubeconfig) > 0 {
cfg, err = clientcmd.BuildConfigFromFlags("", *kubeconfig)
} else {
cfg, err = rest.InClusterConfig()
}
if err != nil {
log.Debug("Building kubeconfig.", "error", err)
}
dyn, err := dynamic.NewForConfig(cfg)
if err != nil {
log.Debug("Creating dynamic client.", "error", err)
}
discovery, err := discovery.NewDiscoveryClientForConfig(cfg)
if err != nil {
log.Debug("Creating discovery client.", "error", err)
}
rec, err := eventrecorder.Create(cfg)
if err != nil {
log.Debug("Creating event recorder.", "error", err)
}
var pig archive.Getter
if len(*chart) > 0 {
pig = archive.Static(*chart)
} else {
pig, err = archive.Dynamic(cfg, *debug, log)
if err != nil {
log.Debug("Creating chart url info getter.", "error", err)
}
}
handler := composition.NewHandler(cfg, log, pig, rec)
log.WithValues("build", Build).
WithValues("debug", *debug).
WithValues("resyncInterval", *resyncInterval).
WithValues("group", *resourceGroup).
WithValues("version", *resourceVersion).
WithValues("resource", *resourceName).
Info("Starting composition dynamic controller.")
// Create a label requirement for the composition version
labelreq, err := labels.NewRequirement(compositionVersionLabel, selection.Equals, []string{*resourceVersion})
if err != nil {
log.Debug("Creating label requirement.", "error", err)
}
labelselector := labels.NewSelector().Add(*labelreq)
controller := genctrl.New(genctrl.Options{
Discovery: discovery,
Client: dyn,
ResyncInterval: *resyncInterval,
GVR: schema.GroupVersionResource{
Group: *resourceGroup,
Version: *resourceVersion,
Resource: *resourceName,
},
Namespace: *namespace,
Config: cfg,
Debug: *debug,
Logger: log,
ProviderName: serviceName,
ListWatcher: controller.ListWatcherConfiguration{
LabelSelector: ptr.To(labelselector.String()),
},
})
controller.SetExternalClient(handler)
ctx, cancel := signal.NotifyContext(context.Background(), []os.Signal{
os.Interrupt,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGKILL,
syscall.SIGHUP,
syscall.SIGQUIT,
}...)
defer cancel()
err = controller.Run(ctx, *workers)
if err != nil {
log.Debug("Running controller.", "error", err)
}
}