Skip to content

Commit 282a269

Browse files
feat: add init + run command line argument (#40)
* chore: add run + init command * chore: use API types from openmcp-operator
1 parent 2426468 commit 282a269

File tree

9 files changed

+131
-289
lines changed

9 files changed

+131
-289
lines changed

api/v1alpha1/cluster_types.go

Lines changed: 0 additions & 113 deletions
This file was deleted.

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 0 additions & 111 deletions
This file was deleted.

cmd/cluster-provider-kind/main.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,24 @@ package main
1818

1919
import (
2020
"crypto/tls"
21+
"embed"
2122
"flag"
2223
"os"
2324
"path/filepath"
2425
"time"
2526

2627
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
2728
// to ensure that exec-entrypoint and run can make use of them.
29+
"github.com/openmcp-project/controller-utils/pkg/init/crds"
30+
"github.com/openmcp-project/controller-utils/pkg/init/webhooks"
2831
_ "k8s.io/client-go/plugin/pkg/client/auth"
2932

3033
"k8s.io/apimachinery/pkg/runtime"
3134
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
3235
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
3336
ctrl "sigs.k8s.io/controller-runtime"
3437
"sigs.k8s.io/controller-runtime/pkg/certwatcher"
38+
"sigs.k8s.io/controller-runtime/pkg/client"
3539
"sigs.k8s.io/controller-runtime/pkg/healthz"
3640
"sigs.k8s.io/controller-runtime/pkg/log/zap"
3741
"sigs.k8s.io/controller-runtime/pkg/metrics/filters"
@@ -50,6 +54,12 @@ import (
5054
var (
5155
scheme = runtime.NewScheme()
5256
setupLog = ctrl.Log.WithName("setup")
57+
58+
//go:embed embedded/crds
59+
_ embed.FS
60+
61+
_ = crds.BindFlags(flag.CommandLine)
62+
_ = webhooks.BindFlags(flag.CommandLine)
5363
)
5464

5565
func init() {
@@ -61,6 +71,40 @@ func init() {
6171
// +kubebuilder:scaffold:scheme
6272
}
6373

74+
func runInit(_ client.Client) {
75+
// _ = context.Background()
76+
77+
// if webhooksFlags.Install {
78+
// // Generate webhook certificate
79+
// if err := webhooks.GenerateCertificate(initContext, setupClient, webhooksFlags.CertOptions...); err != nil {
80+
// setupLog.Error(err, "unable to generate webhook certificates")
81+
// os.Exit(1)
82+
// }
83+
84+
// Install webhooks
85+
// err := webhooks.Install(
86+
// initContext,
87+
// setupClient,
88+
// scheme,
89+
// []client.Object{
90+
// &corev1beta1.ControlPlane{},
91+
// },
92+
// )
93+
// if err != nil {
94+
// setupLog.Error(err, "unable to configure webhooks")
95+
// os.Exit(1)
96+
// }
97+
//}
98+
99+
// if crdFlags.Install {
100+
// // Install CRDs
101+
// if err := crds.Install(initContext, setupClient, crdFiles); err != nil {
102+
// setupLog.Error(err, "unable to install Custom Resource Definitions")
103+
// os.Exit(1)
104+
// }
105+
// }
106+
}
107+
64108
// nolint:gocyclo
65109
func main() {
66110
var metricsAddr string
@@ -71,6 +115,7 @@ func main() {
71115
var secureMetrics bool
72116
var enableHTTP2 bool
73117
var tlsOpts []func(*tls.Config)
118+
var environment, verbosity string
74119
flag.StringVar(&metricsAddr, "metrics-bind-address", "0", "The address the metrics endpoint binds to. "+
75120
"Use :8443 for HTTPS or :8080 for HTTP, or leave as 0 to disable the metrics service.")
76121
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
@@ -88,12 +133,20 @@ func main() {
88133
flag.StringVar(&metricsCertKey, "metrics-cert-key", "tls.key", "The name of the metrics server key file.")
89134
flag.BoolVar(&enableHTTP2, "enable-http2", false,
90135
"If set, HTTP/2 will be enabled for the metrics and webhook servers")
136+
flag.StringVar(&environment, "environment", "", "The name of the environment to use for the provider.")
137+
flag.StringVar(&verbosity, "verbosity", "", "The verbosity level for the logger.")
91138
opts := zap.Options{
92139
Development: true,
93140
}
94141
opts.BindFlags(flag.CommandLine)
95142
flag.Parse()
96143

144+
// skip os.Args[1] which is the command (run or init)
145+
if err := flag.CommandLine.Parse(os.Args[2:]); err != nil {
146+
setupLog.Error(err, "failed to parse flags")
147+
os.Exit(1)
148+
}
149+
97150
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
98151

99152
// if the enable-http2 flag is false (the default), http/2 should be disabled
@@ -117,6 +170,17 @@ func main() {
117170
// Initial webhook TLS options
118171
webhookTLSOpts := tlsOpts
119172

173+
setupClient, err := client.New(ctrl.GetConfigOrDie(), client.Options{Scheme: scheme})
174+
if err != nil {
175+
setupLog.Error(err, "unable to create client")
176+
os.Exit(1)
177+
}
178+
179+
if os.Args[1] == "init" {
180+
runInit(setupClient)
181+
return
182+
}
183+
120184
if len(webhookCertPath) > 0 {
121185
setupLog.Info("Initializing webhook certificate watcher using provided certificates",
122186
"webhook-cert-path", webhookCertPath, "webhook-cert-name", webhookCertName, "webhook-cert-key", webhookCertKey)

0 commit comments

Comments
 (0)