-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.go
205 lines (177 loc) · 6.99 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
package main
import (
"context"
"flag"
"os"
"time"
"github.com/Azure/Orkestra/pkg/utils"
"github.com/Azure/Orkestra/pkg/workflow"
"github.com/Azure/Orkestra/pkg/registry"
"k8s.io/apimachinery/pkg/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
orkestrav1alpha1 "github.com/Azure/Orkestra/api/v1alpha1"
"github.com/Azure/Orkestra/controllers"
v1alpha13 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
fluxhelmv2beta1 "github.com/fluxcd/helm-controller/api/v2beta1"
// +kubebuilder:scaffold:imports
)
const (
stagingRepoURLEnv = "STAGING_REPO_URL"
)
var (
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
)
func init() {
_ = clientgoscheme.AddToScheme(scheme)
_ = orkestrav1alpha1.AddToScheme(scheme)
// +kubebuilder:scaffold:scheme
// Add Argo Workflow scheme to operator
_ = v1alpha13.AddToScheme(scheme)
// Add HelmRelease scheme to operator
_ = fluxhelmv2beta1.AddToScheme(scheme)
}
func main() {
var (
metricsAddr string
enableLeaderElection bool
configPath string
stagingRepoURL string
tempChartStoreTargetDir string
disableRemediation bool
cleanupDownloadedCharts bool
debug bool
workflowParallelism int64
logLevel int
enableZapLogDevMode bool
)
flag.StringVar(&metricsAddr, "metrics-addr", ":8081", "The address the metric endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "enable-leader-election", false,
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
flag.StringVar(&configPath, "config", "", "The path to the controller config file")
flag.StringVar(&stagingRepoURL, "staging-repo-url", "", "The URL for the helm registry used for staging artifacts (ENV - STAGING_REPO_URL). NOTE: Flag overrides env value")
flag.StringVar(&tempChartStoreTargetDir, "chart-store-path", "", "The temporary storage path for the downloaded and staged chart artifacts")
flag.BoolVar(&disableRemediation, "disable-remediation", false, "Disable the remediation (delete/rollback) of the workflow on failure (useful if you wish to debug failures in the workflow/executor container")
flag.BoolVar(&cleanupDownloadedCharts, "cleanup-downloaded-charts", false, "Enable/disable the cleanup of the charts downloaded to the chart-store-path")
flag.BoolVar(&debug, "debug", false, "Enable debug run of the appgroup controller")
flag.Int64Var(&workflowParallelism, "workflow-parallelism", 10, "Specifies the max number of workflow pods that can be executed in parallel")
flag.IntVar(&logLevel, "log-level", 0, "Log Level")
flag.Parse()
if logLevel < 0 {
enableZapLogDevMode = true
}
ctrl.SetLogger(zap.New(zap.UseDevMode(enableZapLogDevMode)))
// Start the probe at the very beginning
probe, err := utils.ProbeHandler(stagingRepoURL, "health")
if err != nil {
setupLog.Error(err, "unable to start readiness/liveness probes", "controller", "ApplicationGroup")
os.Exit(1)
}
probe.Start("8086")
ctrl.Log.V(logLevel)
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
Port: 9443,
LeaderElection: enableLeaderElection,
LeaderElectionID: "fdcf4a0d.azure.microsoft.com",
})
if err != nil {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
}
// Grabbing the values based on the passed helm flags, these values change if we run in debug mode
stagingHelmURL, workflowHelmURL, tempChartStoreTargetDir := getValues(stagingRepoURL, tempChartStoreTargetDir, debug)
if stagingHelmURL == "" {
s := os.Getenv(stagingRepoURLEnv)
if s == "" {
setupLog.Error(err, "staging repo URL must be set")
os.Exit(1)
}
stagingHelmURL = s
}
rc, err := registry.NewClient(
ctrl.Log,
registry.TargetDir(tempChartStoreTargetDir),
)
if err != nil {
setupLog.Error(err, "unable to create new registry client", "controller", "registry-client")
os.Exit(1)
}
// Register the staging helm repository/registry
// We perform retry on this so that we don't go into a crash loop backoff
retryChan := make(chan bool)
retryCtx, cancel := context.WithTimeout(context.Background(), time.Minute*5)
go func() {
for {
err = rc.AddRepo(®istry.Config{
Name: "staging",
URL: stagingHelmURL,
})
if err != nil {
setupLog.Error(err, "failed to add staging helm repo, retrying...")
time.Sleep(time.Second * 5)
} else {
retryChan <- true
break
}
}
}()
select {
case <-retryChan:
cancel()
close(retryChan)
setupLog.Info("successfully set-up the local chartmuseum helm repository")
case <-retryCtx.Done():
cancel()
close(retryChan)
setupLog.Error(err, "pod timed out while trying to setup the helm chart museum...")
os.Exit(1)
}
baseLogger := ctrl.Log.WithName("controllers").WithName("ApplicationGroup")
if err = (&controllers.ApplicationGroupReconciler{
Client: mgr.GetClient(),
Log: baseLogger,
Scheme: mgr.GetScheme(),
RegistryClient: rc,
StagingRepoName: "staging",
WorkflowClientBuilder: workflow.NewBuilder(mgr.GetClient(), baseLogger).WithStagingRepo(workflowHelmURL).WithParallelism(workflowParallelism).InNamespace(workflow.GetNamespace()),
TargetDir: tempChartStoreTargetDir,
Recorder: mgr.GetEventRecorderFor("appgroup-controller"),
DisableRemediation: disableRemediation,
CleanupDownloadedCharts: cleanupDownloadedCharts,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "ApplicationGroup")
os.Exit(1)
}
if err = (&controllers.WorkflowStatusReconciler{
Client: mgr.GetClient(),
Log: baseLogger,
Scheme: mgr.GetScheme(),
WorkflowClientBuilder: workflow.NewBuilder(mgr.GetClient(), baseLogger).WithStagingRepo(workflowHelmURL).WithParallelism(workflowParallelism).InNamespace(workflow.GetNamespace()),
Recorder: mgr.GetEventRecorderFor("appgroup-controller"),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "WorkflowStatus")
os.Exit(1)
}
// +kubebuilder:scaffold:builder
setupLog.Info("starting manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
setupLog.Error(err, "problem running manager")
os.Exit(1)
}
}
// getValues returns the stagingRepoUrl unless the appGroup controller
// is run in a debug mode, then it returns the port forwarded url
func getValues(stagingHelmURL, tempChartStoreTargetDir string, debug bool) (string, string, string) {
if debug {
return "http://127.0.0.1:8080", "http://orkestra-chartmuseum.orkestra:8080", os.TempDir()
}
return stagingHelmURL, stagingHelmURL, tempChartStoreTargetDir
}