@@ -2,18 +2,12 @@ package app
22
33import (
44 "context"
5- "fmt"
65 "os"
76
8- ctrl "sigs.k8s.io/controller-runtime"
9- "sigs.k8s.io/yaml"
10-
117 "github.com/spf13/cobra"
128
13- "github.com/openmcp-project/controller-utils/pkg/clusters"
14- "github.com/openmcp-project/controller-utils/pkg/logging"
15-
16- "github.com/openmcp-project/openmcp-operator/internal/config"
9+ "github.com/openmcp-project/openmcp-operator/cmd/openmcp-operator/app/mcp"
10+ "github.com/openmcp-project/openmcp-operator/cmd/openmcp-operator/app/options"
1711)
1812
1913func NewOpenMCPOperatorCommand (ctx context.Context ) * cobra.Command {
@@ -24,110 +18,11 @@ func NewOpenMCPOperatorCommand(ctx context.Context) *cobra.Command {
2418 cmd .SetOut (os .Stdout )
2519 cmd .SetErr (os .Stderr )
2620
27- so := & SharedOptions {
28- RawSharedOptions : & RawSharedOptions {},
29- PlatformCluster : clusters .New ("platform" ),
30- }
31- so .AddPersistentFlags (cmd )
32- cmd .AddCommand (NewInitCommand (so ))
33- cmd .AddCommand (NewRunCommand (so ))
21+ po := options .NewPersistentOptions ()
22+ po .AddPersistentFlags (cmd )
23+ cmd .AddCommand (NewInitCommand (po ))
24+ cmd .AddCommand (NewRunCommand (po ))
25+ cmd .AddCommand (mcp .NewMCPControllerSubcommand (ctx , po ))
3426
3527 return cmd
3628}
37-
38- type RawSharedOptions struct {
39- Environment string `json:"environment"`
40- DryRun bool `json:"dry-run"`
41- ConfigPaths []string `json:"configPaths"`
42- PlatformClusterKubeconfigPath string `json:"kubeconfig"` // dummy for printing, actual path is in Clusters
43- }
44-
45- type SharedOptions struct {
46- * RawSharedOptions
47- PlatformCluster * clusters.Cluster
48-
49- // fields filled in Complete()
50- Log logging.Logger
51- Config * config.Config
52- }
53-
54- func (o * SharedOptions ) AddPersistentFlags (cmd * cobra.Command ) {
55- // logging
56- logging .InitFlags (cmd .PersistentFlags ())
57- // clusters
58- o .PlatformCluster .RegisterSingleConfigPathFlag (cmd .PersistentFlags ())
59- // environment
60- cmd .PersistentFlags ().StringVar (& o .Environment , "environment" , "" , "Environment name. Required. This is used to distinguish between different environments that are watching the same Onboarding cluster. Must be globally unique." )
61- // config
62- cmd .PersistentFlags ().StringSliceVar (& o .ConfigPaths , "config" , nil , "Paths to the config files (separate with comma or specify flag multiple times). Each path can be a file or directory. In the latter case, all files within with '.yaml', '.yml', and '.json' extensions are evaluated. The config is merged together from the different sources, with later configs overriding earlier ones." )
63- // misc
64- cmd .PersistentFlags ().BoolVar (& o .DryRun , "dry-run" , false , "If set, the command aborts after evaluation of the given flags." )
65- }
66-
67- func (o * SharedOptions ) Complete () error {
68- if o .Environment == "" {
69- return fmt .Errorf ("environment must not be empty" )
70- }
71- config .SetEnvironment (o .Environment )
72-
73- // build logger
74- log , err := logging .GetLogger ()
75- if err != nil {
76- return err
77- }
78- o .Log = log
79- ctrl .SetLogger (o .Log .Logr ())
80-
81- // construct cluster clients
82- if err := o .PlatformCluster .InitializeRESTConfig (); err != nil {
83- return err
84- }
85-
86- // load config
87- if len (o .ConfigPaths ) > 0 {
88- cfg , err := config .LoadFromFiles (o .ConfigPaths ... )
89- if err != nil {
90- return fmt .Errorf ("error loading config from files: %w" , err )
91- }
92- if err := cfg .Default (); err != nil {
93- _ = cfg .Dump (os .Stderr )
94- return fmt .Errorf ("error defaulting config: %w" , err )
95- }
96- if err := cfg .Validate (); err != nil {
97- _ = cfg .Dump (os .Stderr )
98- return fmt .Errorf ("config is invalid: %w" , err )
99- }
100- if err := cfg .Complete (); err != nil {
101- _ = cfg .Dump (os .Stderr )
102- return fmt .Errorf ("error completing config: %w" , err )
103- }
104- o .Config = cfg
105- }
106-
107- return nil
108- }
109-
110- func (o * SharedOptions ) PrintRaw (cmd * cobra.Command ) {
111- // fill dummy paths
112- o .PlatformClusterKubeconfigPath = o .PlatformCluster .ConfigPath ()
113-
114- data , err := yaml .Marshal (o .RawSharedOptions )
115- if err != nil {
116- cmd .Println (fmt .Errorf ("error marshalling raw shared options: %w" , err ).Error ())
117- return
118- }
119- cmd .Print (string (data ))
120- }
121-
122- func (o * SharedOptions ) PrintCompleted (cmd * cobra.Command ) {
123- raw := map [string ]any {
124- "platformCluster" : o .PlatformCluster .APIServerEndpoint (),
125- "config" : o .Config ,
126- }
127- data , err := yaml .Marshal (raw )
128- if err != nil {
129- cmd .Println (fmt .Errorf ("error marshalling completed shared options: %w" , err ).Error ())
130- return
131- }
132- cmd .Print (string (data ))
133- }
0 commit comments