|
| 1 | +/* |
| 2 | +Copyright 2024 IBM Corporation. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package main |
| 18 | + |
| 19 | +import ( |
| 20 | + "crypto/tls" |
| 21 | + "flag" |
| 22 | + "os" |
| 23 | + |
| 24 | + // Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.) |
| 25 | + // to ensure that exec-entrypoint and run can make use of them. |
| 26 | + _ "k8s.io/client-go/plugin/pkg/client/auth" |
| 27 | + |
| 28 | + "k8s.io/apimachinery/pkg/runtime" |
| 29 | + utilruntime "k8s.io/apimachinery/pkg/util/runtime" |
| 30 | + clientgoscheme "k8s.io/client-go/kubernetes/scheme" |
| 31 | + ctrl "sigs.k8s.io/controller-runtime" |
| 32 | + "sigs.k8s.io/controller-runtime/pkg/healthz" |
| 33 | + "sigs.k8s.io/controller-runtime/pkg/log/zap" |
| 34 | + metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server" |
| 35 | + "sigs.k8s.io/controller-runtime/pkg/webhook" |
| 36 | + |
| 37 | + workloadv1beta2 "github.com/project-codeflare/appwrapper/api/v1beta2" |
| 38 | + "github.com/project-codeflare/appwrapper/internal/config" |
| 39 | + "github.com/project-codeflare/appwrapper/internal/controller" |
| 40 | + //+kubebuilder:scaffold:imports |
| 41 | +) |
| 42 | + |
| 43 | +var ( |
| 44 | + scheme = runtime.NewScheme() |
| 45 | + setupLog = ctrl.Log.WithName("setup") |
| 46 | + BuildVersion = "UNKNOWN" |
| 47 | + BuildDate = "UNKNOWN" |
| 48 | +) |
| 49 | + |
| 50 | +func init() { |
| 51 | + utilruntime.Must(clientgoscheme.AddToScheme(scheme)) |
| 52 | + utilruntime.Must(workloadv1beta2.AddToScheme(scheme)) |
| 53 | + //+kubebuilder:scaffold:scheme |
| 54 | +} |
| 55 | + |
| 56 | +func main() { |
| 57 | + var metricsAddr string |
| 58 | + var enableLeaderElection bool |
| 59 | + var probeAddr string |
| 60 | + var secureMetrics bool |
| 61 | + var enableHTTP2 bool |
| 62 | + |
| 63 | + awConfig := config.AppWrapperConfig{StandaloneMode: true, ManageJobsWithoutQueueName: false} |
| 64 | + |
| 65 | + flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.") |
| 66 | + flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.") |
| 67 | + flag.BoolVar(&enableLeaderElection, "leader-elect", false, |
| 68 | + "Enable leader election for controller manager. "+ |
| 69 | + "Enabling this will ensure there is only one active controller manager.") |
| 70 | + flag.BoolVar(&secureMetrics, "metrics-secure", false, |
| 71 | + "If set the metrics endpoint is served securely") |
| 72 | + flag.BoolVar(&enableHTTP2, "enable-http2", false, |
| 73 | + "If set, HTTP/2 will be enabled for the metrics and webhook servers") |
| 74 | + opts := zap.Options{ |
| 75 | + Development: true, |
| 76 | + } |
| 77 | + opts.BindFlags(flag.CommandLine) |
| 78 | + flag.Parse() |
| 79 | + |
| 80 | + ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts))) |
| 81 | + setupLog.Info("Build info", "version", BuildVersion, "date", BuildDate) |
| 82 | + setupLog.Info("Configuration", "config", awConfig) |
| 83 | + |
| 84 | + // if the enable-http2 flag is false (the default), http/2 should be disabled |
| 85 | + // due to its vulnerabilities. More specifically, disabling http/2 will |
| 86 | + // prevent from being vulnerable to the HTTP/2 Stream Cancelation and |
| 87 | + // Rapid Reset CVEs. For more information see: |
| 88 | + // - https://github.com/advisories/GHSA-qppj-fm5r-hxr3 |
| 89 | + // - https://github.com/advisories/GHSA-4374-p667-p6c8 |
| 90 | + disableHTTP2 := func(c *tls.Config) { |
| 91 | + setupLog.Info("disabling http/2") |
| 92 | + c.NextProtos = []string{"http/1.1"} |
| 93 | + } |
| 94 | + |
| 95 | + tlsOpts := []func(*tls.Config){} |
| 96 | + if !enableHTTP2 { |
| 97 | + tlsOpts = append(tlsOpts, disableHTTP2) |
| 98 | + } |
| 99 | + |
| 100 | + webhookServer := webhook.NewServer(webhook.Options{ |
| 101 | + TLSOpts: tlsOpts, |
| 102 | + }) |
| 103 | + |
| 104 | + mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{ |
| 105 | + Scheme: scheme, |
| 106 | + Metrics: metricsserver.Options{ |
| 107 | + BindAddress: metricsAddr, |
| 108 | + SecureServing: secureMetrics, |
| 109 | + TLSOpts: tlsOpts, |
| 110 | + }, |
| 111 | + WebhookServer: webhookServer, |
| 112 | + HealthProbeBindAddress: probeAddr, |
| 113 | + LeaderElection: enableLeaderElection, |
| 114 | + LeaderElectionID: "f134c674.codeflare.dev", |
| 115 | + // LeaderElectionReleaseOnCancel defines if the leader should step down voluntarily |
| 116 | + // when the Manager ends. This requires the binary to immediately end when the |
| 117 | + // Manager is stopped, otherwise, this setting is unsafe. Setting this significantly |
| 118 | + // speeds up voluntary leader transitions as the new leader don't have to wait |
| 119 | + // LeaseDuration time first. |
| 120 | + // |
| 121 | + // In the default scaffold provided, the program ends immediately after |
| 122 | + // the manager stops, so would be fine to enable this option. However, |
| 123 | + // if you are doing or is intended to do any operation such as perform cleanups |
| 124 | + // after the manager stops then its usage might be unsafe. |
| 125 | + // LeaderElectionReleaseOnCancel: true, |
| 126 | + }) |
| 127 | + if err != nil { |
| 128 | + setupLog.Error(err, "unable to start manager") |
| 129 | + os.Exit(1) |
| 130 | + } |
| 131 | + |
| 132 | + ctx := ctrl.SetupSignalHandler() |
| 133 | + err = controller.SetupWithManager(ctx, mgr, &awConfig) |
| 134 | + if err != nil { |
| 135 | + setupLog.Error(err, "unable to start appwrapper controllers") |
| 136 | + os.Exit(1) |
| 137 | + } |
| 138 | + |
| 139 | + //+kubebuilder:scaffold:builder |
| 140 | + if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil { |
| 141 | + setupLog.Error(err, "unable to set up health check") |
| 142 | + os.Exit(1) |
| 143 | + } |
| 144 | + if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil { |
| 145 | + setupLog.Error(err, "unable to set up ready check") |
| 146 | + os.Exit(1) |
| 147 | + } |
| 148 | + |
| 149 | + setupLog.Info("starting manager") |
| 150 | + if err := mgr.Start(ctx); err != nil { |
| 151 | + setupLog.Error(err, "problem running manager") |
| 152 | + os.Exit(1) |
| 153 | + } |
| 154 | +} |
0 commit comments