Skip to content

Commit 2395b25

Browse files
committed
controller
1 parent 874a214 commit 2395b25

File tree

7 files changed

+227
-23
lines changed

7 files changed

+227
-23
lines changed

api/install/install.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package install
2+
3+
import (
4+
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
5+
"k8s.io/apimachinery/pkg/runtime"
6+
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
7+
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
8+
)
9+
10+
// InstallCRDAPIs installs the CRD APIs in the scheme.
11+
// This is used for the init subcommand.
12+
func InstallCRDAPIs(scheme *runtime.Scheme) *runtime.Scheme {
13+
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
14+
utilruntime.Must(apiextv1.AddToScheme(scheme))
15+
return scheme
16+
}

cmd/openmcp-operator/app/app.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package app
2+
3+
import (
4+
"context"
5+
6+
"github.com/spf13/cobra"
7+
)
8+
9+
func NewOpenMCPOperatorCommand(ctx context.Context) *cobra.Command {
10+
cmd := &cobra.Command{
11+
Use: "openmcp-operator",
12+
Short: "Commands for interacting with the openmcp-operator",
13+
}
14+
15+
cmd.AddCommand(NewRunCommand(ctx))
16+
17+
return cmd
18+
}

cmd/openmcp-operator/app/run.go

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package app
2+
3+
import (
4+
"context"
5+
goflag "flag"
6+
"fmt"
7+
"os"
8+
9+
"github.com/openmcp-project/controller-utils/pkg/clusters"
10+
"github.com/openmcp-project/controller-utils/pkg/logging"
11+
"github.com/spf13/cobra"
12+
flag "github.com/spf13/pflag"
13+
"k8s.io/apimachinery/pkg/runtime"
14+
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
15+
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
16+
"k8s.io/client-go/tools/clientcmd/api"
17+
ctrl "sigs.k8s.io/controller-runtime"
18+
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
19+
20+
"github.com/openmcp-project/openmcp-operator/api/install"
21+
"github.com/openmcp-project/openmcp-operator/internal/controller"
22+
)
23+
24+
func NewRunCommand(_ context.Context) *cobra.Command {
25+
options := &runOptions{
26+
PlatformCluster: clusters.New("platform"),
27+
}
28+
29+
cmd := &cobra.Command{
30+
Use: "run",
31+
Short: "Start openmcp-operator",
32+
Run: func(cmd *cobra.Command, args []string) {
33+
if err := options.complete(); err != nil {
34+
fmt.Print(err)
35+
os.Exit(1)
36+
}
37+
if err := options.run(); err != nil {
38+
options.Log.Error(err, "unable to run the openmcp-operator")
39+
os.Exit(1)
40+
}
41+
},
42+
}
43+
44+
options.addFlags(cmd.Flags())
45+
46+
return cmd
47+
}
48+
49+
type runOptions struct {
50+
PlatformCluster *clusters.Cluster
51+
52+
Log logging.Logger
53+
}
54+
55+
func (o *runOptions) addFlags(fs *flag.FlagSet) {
56+
// register flag '--platform-cluster' for the path to the kubeconfig of the onboarding cluster
57+
o.PlatformCluster.RegisterConfigPathFlag(fs)
58+
59+
logging.InitFlags(fs)
60+
flag.CommandLine.AddGoFlagSet(goflag.CommandLine)
61+
}
62+
63+
func (o *runOptions) complete() (err error) {
64+
if err = o.setupLogger(); err != nil {
65+
return err
66+
}
67+
if err = o.setupPlatformClusterClient(); err != nil {
68+
return err
69+
}
70+
71+
return nil
72+
}
73+
74+
func (o *runOptions) setupLogger() error {
75+
log, err := logging.GetLogger()
76+
if err != nil {
77+
return err
78+
}
79+
o.Log = log
80+
ctrl.SetLogger(log.Logr())
81+
return nil
82+
}
83+
84+
func (o *runOptions) setupPlatformClusterClient() error {
85+
if err := o.PlatformCluster.InitializeRESTConfig(); err != nil {
86+
return fmt.Errorf("unable to initialize onboarding cluster rest config: %w", err)
87+
}
88+
if err := o.PlatformCluster.InitializeClient(install.InstallCRDAPIs(runtime.NewScheme())); err != nil {
89+
return fmt.Errorf("unable to initialize onboarding cluster client: %w", err)
90+
}
91+
return nil
92+
}
93+
94+
func (o *runOptions) run() error {
95+
o.Log.Info("starting openmcp-operator", "platform-cluster", o.PlatformCluster.ConfigPath())
96+
97+
mgrOptions := ctrl.Options{
98+
Metrics: metricsserver.Options{BindAddress: "0"},
99+
LeaderElection: false,
100+
}
101+
102+
mgr, err := ctrl.NewManager(o.PlatformCluster.RESTConfig(), mgrOptions)
103+
if err != nil {
104+
return fmt.Errorf("unable to setup manager: %w", err)
105+
}
106+
107+
utilruntime.Must(clientgoscheme.AddToScheme(mgr.GetScheme()))
108+
utilruntime.Must(api.AddToScheme(mgr.GetScheme()))
109+
110+
if err = (&controller.DeployableReconciler{
111+
PlatformClient: mgr.GetClient(),
112+
Scheme: mgr.GetScheme(),
113+
}).SetupWithManager(mgr); err != nil {
114+
return fmt.Errorf("unable to create controller: %w", err)
115+
}
116+
117+
o.Log.Info("starting manager")
118+
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
119+
o.Log.Error(err, "error while running manager")
120+
os.Exit(1)
121+
}
122+
123+
return nil
124+
}

cmd/openmcp-operator/main.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
8+
"github.com/openmcp-project/openmcp-operator/cmd/openmcp-operator/app"
9+
)
10+
11+
func main() {
12+
ctx := context.Background()
13+
defer ctx.Done()
14+
cmd := app.NewOpenMCPOperatorCommand(ctx)
15+
16+
if err := cmd.Execute(); err != nil {
17+
fmt.Print(err)
18+
os.Exit(1)
19+
}
20+
}

go.mod

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ go 1.24.2
77
require (
88
github.com/onsi/ginkgo/v2 v2.23.4
99
github.com/onsi/gomega v1.37.0
10+
github.com/openmcp-project/controller-utils v0.6.0
11+
github.com/spf13/cobra v1.9.1
12+
github.com/spf13/pflag v1.0.6
1013
k8s.io/apimachinery v0.33.0
1114
k8s.io/client-go v0.33.0
1215
sigs.k8s.io/controller-runtime v0.20.4
@@ -21,16 +24,16 @@ require (
2124
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
2225
github.com/cespare/xxhash/v2 v2.3.0 // indirect
2326
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
24-
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
27+
github.com/emicklei/go-restful/v3 v3.12.1 // indirect
2528
github.com/evanphx/json-patch/v5 v5.9.11 // indirect
2629
github.com/felixge/httpsnoop v1.0.4 // indirect
27-
github.com/fsnotify/fsnotify v1.7.0 // indirect
30+
github.com/fsnotify/fsnotify v1.8.0 // indirect
2831
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
2932
github.com/go-logr/logr v1.4.2 // indirect
3033
github.com/go-logr/stdr v1.2.2 // indirect
3134
github.com/go-logr/zapr v1.3.0 // indirect
3235
github.com/go-openapi/jsonpointer v0.21.0 // indirect
33-
github.com/go-openapi/jsonreference v0.20.2 // indirect
36+
github.com/go-openapi/jsonreference v0.21.0 // indirect
3437
github.com/go-openapi/swag v0.23.0 // indirect
3538
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
3639
github.com/gogo/protobuf v1.3.2 // indirect
@@ -44,17 +47,15 @@ require (
4447
github.com/inconshreveable/mousetrap v1.1.0 // indirect
4548
github.com/josharian/intern v1.0.0 // indirect
4649
github.com/json-iterator/go v1.1.12 // indirect
47-
github.com/mailru/easyjson v0.7.7 // indirect
50+
github.com/mailru/easyjson v0.9.0 // indirect
4851
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
4952
github.com/modern-go/reflect2 v1.0.2 // indirect
5053
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
5154
github.com/pkg/errors v0.9.1 // indirect
52-
github.com/prometheus/client_golang v1.19.1 // indirect
55+
github.com/prometheus/client_golang v1.20.5 // indirect
5356
github.com/prometheus/client_model v0.6.1 // indirect
54-
github.com/prometheus/common v0.55.0 // indirect
57+
github.com/prometheus/common v0.62.0 // indirect
5558
github.com/prometheus/procfs v0.15.1 // indirect
56-
github.com/spf13/cobra v1.8.1 // indirect
57-
github.com/spf13/pflag v1.0.5 // indirect
5859
github.com/stoewer/go-strcase v1.3.0 // indirect
5960
github.com/x448/float16 v0.8.4 // indirect
6061
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.53.0 // indirect
@@ -68,15 +69,15 @@ require (
6869
go.uber.org/automaxprocs v1.6.0 // indirect
6970
go.uber.org/multierr v1.11.0 // indirect
7071
go.uber.org/zap v1.27.0 // indirect
71-
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 // indirect
72-
golang.org/x/net v0.38.0 // indirect
72+
golang.org/x/exp v0.0.0-20250408133849-7e4ce0ab07d0 // indirect
73+
golang.org/x/net v0.39.0 // indirect
7374
golang.org/x/oauth2 v0.27.0 // indirect
74-
golang.org/x/sync v0.12.0 // indirect
75+
golang.org/x/sync v0.13.0 // indirect
7576
golang.org/x/sys v0.32.0 // indirect
76-
golang.org/x/term v0.30.0 // indirect
77-
golang.org/x/text v0.23.0 // indirect
78-
golang.org/x/time v0.9.0 // indirect
79-
golang.org/x/tools v0.31.0 // indirect
77+
golang.org/x/term v0.31.0 // indirect
78+
golang.org/x/text v0.24.0 // indirect
79+
golang.org/x/time v0.10.0 // indirect
80+
golang.org/x/tools v0.32.0 // indirect
8081
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
8182
google.golang.org/genproto/googleapis/api v0.0.0-20240826202546-f6391c0de4c7 // indirect
8283
google.golang.org/genproto/googleapis/rpc v0.0.0-20240826202546-f6391c0de4c7 // indirect
@@ -86,14 +87,14 @@ require (
8687
gopkg.in/inf.v0 v0.9.1 // indirect
8788
gopkg.in/yaml.v3 v3.0.1 // indirect
8889
k8s.io/api v0.33.0 // indirect
89-
k8s.io/apiextensions-apiserver v0.32.1 // indirect
90-
k8s.io/apiserver v0.32.1 // indirect
91-
k8s.io/component-base v0.32.1 // indirect
90+
k8s.io/apiextensions-apiserver v0.32.3 // indirect
91+
k8s.io/apiserver v0.32.3 // indirect
92+
k8s.io/component-base v0.32.3 // indirect
9293
k8s.io/klog/v2 v2.130.1 // indirect
9394
k8s.io/kube-openapi v0.0.0-20250318190949-c8a335a9a2ff // indirect
94-
k8s.io/utils v0.0.0-20241104100929-3ea5e8cea738 // indirect
95+
k8s.io/utils v0.0.0-20250321185631-1f6e0b77f77e // indirect
9596
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.31.0 // indirect
96-
sigs.k8s.io/json v0.0.0-20241010143419-9aa6b5e7a4b3 // indirect
97+
sigs.k8s.io/json v0.0.0-20241014173422-cfa47c3a1cc8 // indirect
9798
sigs.k8s.io/randfill v1.0.0 // indirect
9899
sigs.k8s.io/structured-merge-diff/v4 v4.6.0 // indirect
99100
sigs.k8s.io/yaml v1.4.0 // indirect

0 commit comments

Comments
 (0)