Skip to content

Commit b6b6d95

Browse files
committed
controller
1 parent 210d300 commit b6b6d95

File tree

14 files changed

+486
-92
lines changed

14 files changed

+486
-92
lines changed

api/crds/manifests/openmcp.cloud_deployableproviders.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,30 @@ spec:
4040
spec:
4141
description: DeployableProviderSpec defines the desired state of DeployableProvider.
4242
properties:
43+
deploymentSpec:
44+
description: DeployableSpec defines the desired state of DeployableProvider.
45+
properties:
46+
image:
47+
description: Image is the image of the provider to be deployed.
48+
type: string
49+
imagePullPolicy:
50+
type: string
51+
imagePullSecrets:
52+
items:
53+
type: string
54+
type: array
55+
type: object
4356
foo:
4457
description: Foo is an example field of DeployableProvider. Edit deployableprovider_types.go
4558
to remove/update
4659
type: string
4760
type: object
4861
status:
4962
description: DeployableProviderStatus defines the observed state of DeployableProvider.
63+
properties:
64+
deploymentStatus:
65+
description: DeployableStatus defines the observed state of DeployableProvider.
66+
type: object
5067
type: object
5168
type: object
5269
served: true

api/install/install.go

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

api/v1alpha1/deployable_types.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
Copyright 2025.
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 v1alpha1
18+
19+
// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
20+
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.
21+
22+
// DeployableSpec defines the desired state of DeployableProvider.
23+
type DeployableSpec struct {
24+
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
25+
// Important: Run "make" to regenerate code after modifying this file
26+
27+
// Image is the image of the provider to be deployed.
28+
Image string `json:"image,omitempty"`
29+
ImagePullSecrets []string `json:"imagePullSecrets,omitempty"`
30+
ImagePullPolicy string `json:"imagePullPolicy,omitempty"`
31+
}
32+
33+
// DeployableStatus defines the observed state of DeployableProvider.
34+
type DeployableStatus struct {
35+
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
36+
// Important: Run "make" to regenerate code after modifying this file
37+
}

api/v1alpha1/deployableprovider_types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ type DeployableProviderSpec struct {
2828
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
2929
// Important: Run "make" to regenerate code after modifying this file
3030

31+
DeploymentSpec DeployableSpec `json:"deploymentSpec,omitempty"`
32+
3133
// Foo is an example field of DeployableProvider. Edit deployableprovider_types.go to remove/update
3234
Foo string `json:"foo,omitempty"`
3335
}
@@ -36,6 +38,8 @@ type DeployableProviderSpec struct {
3638
type DeployableProviderStatus struct {
3739
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
3840
// Important: Run "make" to regenerate code after modifying this file
41+
42+
DeploymentStatus DeployableStatus `json:"deploymentStatus,omitempty"`
3943
}
4044

4145
// +kubebuilder:object:root=true

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 38 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/main.go renamed to cmd/main_backup.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,10 @@ func main() {
202202
os.Exit(1)
203203
}
204204

205-
if err = (&controller.DeployableProviderReconciler{
206-
Client: mgr.GetClient(),
207-
Scheme: mgr.GetScheme(),
208-
}).SetupWithManager(mgr); err != nil {
205+
if err = (&controller.DeployableReconciler{
206+
PlatformClient: mgr.GetClient(),
207+
Scheme: mgr.GetScheme(),
208+
}).SetupWithManager(mgr, nil); err != nil {
209209
setupLog.Error(err, "unable to create controller", "controller", "DeployableProvider")
210210
os.Exit(1)
211211
}

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

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+
}

0 commit comments

Comments
 (0)