-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
174 lines (147 loc) · 5.97 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"
"fmt"
applicationv1alpha1 "github.com/giantswarm/apiextensions-application/api/v1alpha1"
"github.com/giantswarm/k8sclient/v7/pkg/k8sclient"
"github.com/giantswarm/k8sclient/v7/pkg/k8srestconfig"
"github.com/giantswarm/microerror"
"github.com/giantswarm/microkit/command"
microserver "github.com/giantswarm/microkit/server"
"github.com/giantswarm/micrologger"
prometheusMonitoringV1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
"github.com/spf13/viper"
"k8s.io/client-go/rest"
"github.com/giantswarm/app-operator/v7/flag"
"github.com/giantswarm/app-operator/v7/pkg/project"
"github.com/giantswarm/app-operator/v7/server"
"github.com/giantswarm/app-operator/v7/service"
)
var (
f = flag.New()
)
func main() {
err := mainWithError()
if err != nil {
panic(fmt.Sprintf("%#v\n", err))
}
}
func mainWithError() (err error) {
ctx := context.Background()
// Create a new logger that is used by all packages.
var newLogger micrologger.Logger
{
c := micrologger.Config{}
newLogger, err = micrologger.New(c)
if err != nil {
return microerror.Mask(err)
}
}
// Define server factory to create the custom server once all command line
// flags are parsed and all microservice configuration is processed.
newServerFactory := func(v *viper.Viper) microserver.Server {
var restConfig *rest.Config
{
c := k8srestconfig.Config{
Logger: newLogger,
Address: v.GetString(f.Service.Kubernetes.Address),
InCluster: v.GetBool(f.Service.Kubernetes.InCluster),
KubeConfig: v.GetString(f.Service.Kubernetes.KubeConfig),
TLS: k8srestconfig.ConfigTLS{
CAFile: v.GetString(f.Service.Kubernetes.TLS.CAFile),
CrtFile: v.GetString(f.Service.Kubernetes.TLS.CrtFile),
KeyFile: v.GetString(f.Service.Kubernetes.TLS.KeyFile),
},
}
restConfig, err = k8srestconfig.New(c)
if err != nil {
panic(err)
}
}
var k8sClient k8sclient.Interface
{
c := k8sclient.ClientsConfig{
Logger: newLogger,
SchemeBuilder: k8sclient.SchemeBuilder{
prometheusMonitoringV1.AddToScheme,
applicationv1alpha1.AddToScheme,
},
RestConfig: restConfig,
}
k8sClient, err = k8sclient.NewClients(c)
if err != nil {
panic(err)
}
}
// New custom service implements the business logic.
var newService *service.Service
{
c := service.Config{
K8sClient: k8sClient,
Logger: newLogger,
Flag: f,
Viper: v,
}
newService, err = service.New(c)
if err != nil {
panic(fmt.Sprintf("%#v\n", microerror.Mask(err)))
}
go newService.Boot(ctx)
}
// New custom server that bundles microkit endpoints.
var newServer microserver.Server
{
c := server.Config{
Logger: newLogger,
Service: newService,
Viper: v,
}
newServer, err = server.New(c)
if err != nil {
panic(fmt.Sprintf("%#v\n", microerror.Mask(err)))
}
}
return newServer
}
// Create a new microkit command that manages operator daemon.
var newCommand command.Command
{
c := command.Config{
Logger: newLogger,
ServerFactory: newServerFactory,
Description: project.Description(),
GitCommit: project.GitSHA(),
Name: project.Name(),
Source: project.Source(),
Version: project.Version(),
}
newCommand, err = command.New(c)
if err != nil {
return microerror.Mask(err)
}
}
daemonCommand := newCommand.DaemonCommand().CobraCommand()
daemonCommand.PersistentFlags().Bool(f.Service.App.Unique, false, "Whether the operator is deployed as a unique app.")
daemonCommand.PersistentFlags().String(f.Service.App.WatchNamespace, "", "Namespace to watch for app CRs.")
daemonCommand.PersistentFlags().String(f.Service.App.WorkloadClusterID, "", "Workload cluster ID for app CR label selector.")
daemonCommand.PersistentFlags().Int(f.Service.App.DependencyWaitTimeoutMinutes, 30, "Timeout in seconds after which to ignore dependencies and make app installation to move on.")
daemonCommand.PersistentFlags().Int(f.Service.AppCatalog.MaxEntriesPerApp, 5, "The maximum number of appCatalogEntries per app.")
daemonCommand.PersistentFlags().String(f.Service.Chart.Namespace, "giantswarm", "The namespace where chart CRs are located.")
daemonCommand.PersistentFlags().String(f.Service.Helm.HTTP.ClientTimeout, "5s", "HTTP timeout for pulling chart tarballs.")
daemonCommand.PersistentFlags().String(f.Service.Image.Registry, "quay.io", "The container registry for pulling Tiller images.")
daemonCommand.PersistentFlags().String(f.Service.Kubernetes.Address, "", "Address used to connect to Kubernetes. When empty in-cluster config is created.")
daemonCommand.PersistentFlags().Bool(f.Service.Kubernetes.DisableClientCache, false, "Disable Kubernetes client cache.")
daemonCommand.PersistentFlags().Bool(f.Service.Kubernetes.InCluster, true, "Whether to use the in-cluster config to authenticate with Kubernetes.")
daemonCommand.PersistentFlags().String(f.Service.Kubernetes.KubeConfig, "", "KubeConfig used to connect to Kubernetes. When empty other settings are used.")
daemonCommand.PersistentFlags().String(f.Service.Kubernetes.TLS.CAFile, "", "Certificate authority file path to use to authenticate with Kubernetes.")
daemonCommand.PersistentFlags().String(f.Service.Kubernetes.TLS.CrtFile, "", "Certificate file path to use to authenticate with Kubernetes.")
daemonCommand.PersistentFlags().String(f.Service.Kubernetes.TLS.KeyFile, "", "Key file path to use to authenticate with Kubernetes.")
daemonCommand.PersistentFlags().String(f.Service.Kubernetes.Watch.Namespace, "default", "The namespace where appcatalog and app CRs are located.")
daemonCommand.PersistentFlags().String(f.Service.Operatorkit.ResyncPeriod, "5m", "Resync period after which a complete resync of all runtime objects is performed.")
daemonCommand.PersistentFlags().String(f.Service.Provider.Kind, "", "Provider of the management cluster. One of aws, azure, kvm.")
err = newCommand.CobraCommand().Execute()
if err != nil {
return microerror.Mask(err)
}
return nil
}