-
Notifications
You must be signed in to change notification settings - Fork 128
/
management_controller.go
120 lines (101 loc) · 3.04 KB
/
management_controller.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
package main
import (
"context"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/metrics/server"
kargoapi "github.com/akuity/kargo/api/v1alpha1"
"github.com/akuity/kargo/internal/api/kubernetes"
"github.com/akuity/kargo/internal/controller/management/namespaces"
"github.com/akuity/kargo/internal/controller/management/projects"
"github.com/akuity/kargo/internal/os"
versionpkg "github.com/akuity/kargo/internal/version"
)
type managementControllerOptions struct {
KubeConfig string
Logger *log.Logger
}
func newManagementControllerCommand() *cobra.Command {
cmdOpts := &managementControllerOptions{
Logger: log.StandardLogger(),
}
cmd := &cobra.Command{
Use: "management-controller",
DisableAutoGenTag: true,
SilenceErrors: true,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, _ []string) error {
cmdOpts.complete()
return cmdOpts.run(cmd.Context())
},
}
return cmd
}
func (o *managementControllerOptions) complete() {
o.KubeConfig = os.GetEnv("KUBECONFIG", "")
}
func (o *managementControllerOptions) run(ctx context.Context) error {
version := versionpkg.GetVersion()
o.Logger.WithFields(log.Fields{
"version": version.Version,
"commit": version.GitCommit,
}).Info("Starting Kargo Management Controller")
kargoMgr, err := o.setupManager(ctx)
if err != nil {
return fmt.Errorf("error initializing Kargo controller manager: %w", err)
}
if err := namespaces.SetupReconcilerWithManager(kargoMgr); err != nil {
return fmt.Errorf("error setting up Namespaces reconciler: %w", err)
}
if err := projects.SetupReconcilerWithManager(
kargoMgr,
projects.ReconcilerConfigFromEnv(),
); err != nil {
return fmt.Errorf("error setting up Projects reconciler: %w", err)
}
if err := kargoMgr.Start(ctx); err != nil {
return fmt.Errorf("error starting kargo manager: %w", err)
}
return nil
}
func (o *managementControllerOptions) setupManager(ctx context.Context) (manager.Manager, error) {
restCfg, err := kubernetes.GetRestConfig(ctx, o.KubeConfig)
if err != nil {
return nil, fmt.Errorf("error loading REST config for Kargo controller manager: %w", err)
}
restCfg.ContentType = runtime.ContentTypeJSON
scheme := runtime.NewScheme()
if err = corev1.AddToScheme(scheme); err != nil {
return nil, fmt.Errorf(
"error adding Kubernetes core API to Kargo controller manager scheme: %w",
err,
)
}
if err = rbacv1.AddToScheme(scheme); err != nil {
return nil, fmt.Errorf(
"error adding Kubernetes RBAC API to Kargo controller manager scheme: %w",
err,
)
}
if err = kargoapi.AddToScheme(scheme); err != nil {
return nil, fmt.Errorf(
"error adding Kargo API to Kargo controller manager scheme: %w",
err,
)
}
return ctrl.NewManager(
restCfg,
ctrl.Options{
Scheme: scheme,
Metrics: server.Options{
BindAddress: "0",
},
},
)
}