forked from project-codeflare/codeflare-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
232 lines (197 loc) · 7.84 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
Copyright 2023.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"context"
"flag"
"fmt"
"os"
"strings"
"time"
instascale "github.com/project-codeflare/instascale/controllers"
instascaleconfig "github.com/project-codeflare/instascale/pkg/config"
mcadv1beta1 "github.com/project-codeflare/multi-cluster-app-dispatcher/pkg/apis/controller/v1beta1"
quotasubtreev1alpha1 "github.com/project-codeflare/multi-cluster-app-dispatcher/pkg/apis/quotaplugins/quotasubtree/v1alpha1"
mcadconfig "github.com/project-codeflare/multi-cluster-app-dispatcher/pkg/config"
mcad "github.com/project-codeflare/multi-cluster-app-dispatcher/pkg/controller/queuejob"
"go.uber.org/zap/zapcore"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/kubernetes"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/client-go/rest"
configv1alpha1 "k8s.io/component-base/config/v1alpha1"
"k8s.io/utils/pointer"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/yaml"
configv1 "github.com/openshift/api/config/v1"
machinev1beta1 "github.com/openshift/api/machine/v1beta1"
"github.com/project-codeflare/codeflare-operator/pkg/config"
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
// to ensure that exec-entrypoint and run can make use of them.
// +kubebuilder:scaffold:imports
)
var (
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
)
func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
// MCAD
utilruntime.Must(mcadv1beta1.AddToScheme(scheme))
utilruntime.Must(quotasubtreev1alpha1.AddToScheme(scheme))
// InstaScale
utilruntime.Must(configv1.Install(scheme))
utilruntime.Must(machinev1beta1.Install(scheme))
}
func main() {
var configMapName string
flag.StringVar(&configMapName, "config", "codeflare-operator-config",
"The name of the ConfigMap to load the operator configuration from. "+
"If it does not exist, the operator will create and initialise it.")
zapOptions := zap.Options{
Development: true,
TimeEncoder: zapcore.TimeEncoderOfLayout(time.RFC3339),
}
zapOptions.BindFlags(flag.CommandLine)
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&zapOptions)))
ctx := ctrl.SetupSignalHandler()
cfg := &config.CodeFlareOperatorConfiguration{
ClientConnection: &config.ClientConnection{
QPS: pointer.Float32(50),
Burst: pointer.Int32(100),
},
ControllerManager: config.ControllerManager{
Metrics: config.MetricsConfiguration{
BindAddress: ":8080",
},
Health: config.HealthConfiguration{
BindAddress: ":8081",
ReadinessEndpointName: "readyz",
LivenessEndpointName: "healthz",
},
LeaderElection: &configv1alpha1.LeaderElectionConfiguration{},
},
MCAD: &mcadconfig.MCADConfiguration{},
InstaScale: &config.InstaScaleConfiguration{
Enabled: pointer.Bool(false),
InstaScaleConfiguration: instascaleconfig.InstaScaleConfiguration{
MaxScaleoutAllowed: 5,
},
},
}
kubeConfig, err := ctrl.GetConfig()
exitOnError(err, "unable to get client config")
if kubeConfig.UserAgent == "" {
kubeConfig.UserAgent = "codeflare-operator"
}
kubeClient, err := kubernetes.NewForConfig(kubeConfig)
exitOnError(err, "unable to create Kubernetes client")
exitOnError(loadIntoOrCreate(ctx, kubeClient, namespaceOrDie(), configMapName, cfg), "unable to initialise configuration")
kubeConfig.Burst = int(pointer.Int32Deref(cfg.ClientConnection.Burst, int32(rest.DefaultBurst)))
kubeConfig.QPS = pointer.Float32Deref(cfg.ClientConnection.QPS, rest.DefaultQPS)
setupLog.V(2).Info("REST client", "qps", kubeConfig.QPS, "burst", kubeConfig.Burst)
mgr, err := ctrl.NewManager(kubeConfig, ctrl.Options{
Scheme: scheme,
MetricsBindAddress: cfg.Metrics.BindAddress,
HealthProbeBindAddress: cfg.Health.BindAddress,
LeaderElection: pointer.BoolDeref(cfg.LeaderElection.LeaderElect, false),
LeaderElectionID: cfg.LeaderElection.ResourceName,
LeaderElectionNamespace: cfg.LeaderElection.ResourceNamespace,
LeaderElectionResourceLock: cfg.LeaderElection.ResourceLock,
LeaseDuration: &cfg.LeaderElection.LeaseDuration.Duration,
RetryPeriod: &cfg.LeaderElection.RetryPeriod.Duration,
RenewDeadline: &cfg.LeaderElection.RenewDeadline.Duration,
})
exitOnError(err, "unable to start manager")
mcadQueueController := mcad.NewJobController(mgr.GetConfig(), cfg.MCAD, &mcadconfig.MCADConfigurationExtended{})
if mcadQueueController == nil {
// FIXME: update NewJobController so it follows Go idiomatic error handling and return an error instead of a nil object
os.Exit(1)
}
mcadQueueController.Run(ctx.Done())
if pointer.BoolDeref(cfg.InstaScale.Enabled, false) {
instaScaleController := &instascale.AppWrapperReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Config: cfg.InstaScale.InstaScaleConfiguration,
}
exitOnError(instaScaleController.SetupWithManager(mgr), "Error setting up InstaScale controller")
}
exitOnError(mgr.AddHealthzCheck(cfg.Health.LivenessEndpointName, healthz.Ping), "unable to set up health check")
exitOnError(mgr.AddReadyzCheck(cfg.Health.ReadinessEndpointName, healthz.Ping), "unable to set up ready check")
setupLog.Info("starting manager")
exitOnError(mgr.Start(ctx), "error running manager")
}
func loadIntoOrCreate(ctx context.Context, client kubernetes.Interface, ns, name string, cfg *config.CodeFlareOperatorConfiguration) error {
configMap, err := client.CoreV1().ConfigMaps(ns).Get(ctx, name, metav1.GetOptions{})
if apierrors.IsNotFound(err) {
return createConfigMap(ctx, client, ns, name, cfg)
} else if err != nil {
return err
}
if len(configMap.Data) != 1 {
return fmt.Errorf("cannot resolve config from ConfigMap %s/%s", configMap.Namespace, configMap.Name)
}
for _, data := range configMap.Data {
return yaml.Unmarshal([]byte(data), cfg)
}
return nil
}
func createConfigMap(ctx context.Context, client kubernetes.Interface, ns, name string, cfg *config.CodeFlareOperatorConfiguration) error {
content, err := yaml.Marshal(cfg)
if err != nil {
return err
}
configMap := &corev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
Kind: "ConfigMap",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: ns,
},
Data: map[string]string{
"config.yaml": string(content),
},
}
_, err = client.CoreV1().ConfigMaps(ns).Create(ctx, configMap, metav1.CreateOptions{})
return err
}
func namespaceOrDie() string {
// This way assumes you've set the NAMESPACE environment variable either manually, when running
// the operator standalone, or using the downward API, when running the operator in-cluster.
if ns := os.Getenv("NAMESPACE"); ns != "" {
return ns
}
// Fall back to the namespace associated with the service account token, if available
if data, err := os.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace"); err == nil {
if ns := strings.TrimSpace(string(data)); len(ns) > 0 {
return ns
}
}
panic("unable to determine current namespace")
}
func exitOnError(err error, msg string) {
if err != nil {
setupLog.Error(err, msg)
os.Exit(1)
}
}