@@ -3,70 +3,79 @@ package cmd
33import (
44 "fmt"
55
6- "github.com/openmcp-project/controller-utils/pkg/clusters"
76 "github.com/spf13/cobra"
7+ v1 "k8s.io/api/core/v1"
8+ "k8s.io/apimachinery/pkg/runtime"
89
10+ cfg "github.com/openmcp-project/bootstrapper/internal/config"
911 "github.com/openmcp-project/bootstrapper/internal/flux_deployer"
1012 logging "github.com/openmcp-project/bootstrapper/internal/log"
11- )
12-
13- const (
14- flagOCMConfig = "ocm-config"
15- flagGitCredentials = "git-credentials"
16- flagKubeconfig = "kubeconfig"
17- flagFluxCDNamespace = "fluxcd-namespace"
13+ "github.com/openmcp-project/bootstrapper/internal/util"
1814)
1915
2016// deployFluxCmd represents the "deploy flux" command
2117var deployFluxCmd = & cobra.Command {
22- Use : "deploy-flux source target " ,
23- Short : "Transfer an OCM component from a source to a target location " ,
24- Long : `Transfers the specified OCM component version from the source location to the target location .` ,
25- Args : cobra .ExactArgs (5 ),
18+ Use : "deploy-flux" ,
19+ Short : "Deploys Flux controllers on the platform cluster, and establishes synchronization with a Git repository " ,
20+ Long : `Deploys Flux controllers on the platform cluster, and establishes synchronization with a Git repository .` ,
21+ Args : cobra .ExactArgs (1 ),
2622 ArgAliases : []string {
27- "component-location" ,
28- "deployment-templates" ,
29- "deployment-repository" ,
30- "deployment-repository-branch" ,
31- "deployment-repository-path" ,
23+ "configFile" ,
3224 },
3325 RunE : func (cmd * cobra.Command , args []string ) error {
26+ configFilePath := args [0 ]
27+
3428 log := logging .GetLogger ()
35- log .Infof ("Starting flux deployment with component-location: %s, deployment-templates: %s, " +
36- "deployment-repository: %s, deployment-repository-branch: %s, deployment-repository-path: %s" ,
37- args [0 ], args [1 ], args [2 ], args [3 ], args [4 ])
29+ log .Infof ("Starting deployment of Flux controllers with config file: %s." , configFilePath )
3830
39- platformKubeconfig := cmd .Flag (flagKubeconfig ).Value .String ()
40- platformCluster := clusters .New ("platform" ).WithConfigPath (platformKubeconfig )
31+ // Configuration
32+ config := & cfg.BootstrapperConfig {}
33+ err := config .ReadFromFile (configFilePath )
34+ if err != nil {
35+ return fmt .Errorf ("failed to read config file: %w" , err )
36+ }
37+ config .SetDefaults ()
38+ err = config .Validate ()
39+ if err != nil {
40+ return fmt .Errorf ("invalid config file: %w" , err )
41+ }
42+
43+ // Platform cluster
44+ scheme := runtime .NewScheme ()
45+ if err := v1 .AddToScheme (scheme ); err != nil {
46+ return fmt .Errorf ("error adding corev1 to scheme: %w" , err )
47+ }
48+
49+ platformCluster , err := util .GetCluster (cmd .Flag (FlagKubeConfig ).Value .String (), "platform" , scheme )
50+ if err != nil {
51+ return fmt .Errorf ("failed to get platform cluster: %w" , err )
52+ }
4153 if err := platformCluster .InitializeRESTConfig (); err != nil {
4254 return fmt .Errorf ("error initializing REST config for platform cluster: %w" , err )
4355 }
4456 if err := platformCluster .InitializeClient (nil ); err != nil {
4557 return fmt .Errorf ("error initializing client for platform cluster: %w" , err )
4658 }
4759
48- d := flux_deployer .NewFluxDeployer (args [0 ], args [1 ], args [2 ], args [3 ], args [4 ],
49- cmd .Flag (flagOCMConfig ).Value .String (),
50- cmd .Flag (flagGitCredentials ).Value .String (),
51- cmd .Flag (flagFluxCDNamespace ).Value .String (),
52- platformKubeconfig ,
53- platformCluster , log )
54- err := d .Deploy (cmd .Context ())
55- if err != nil {
56- log .Errorf ("Flux deployment failed: %v" , err )
60+ d := flux_deployer .NewFluxDeployer (config , cmd .Flag (FlagGitConfig ).Value .String (), cmd .Flag (FlagOcmConfig ).Value .String (), platformCluster , log )
61+ if err = d .Deploy (cmd .Context ()); err != nil {
62+ log .Errorf ("Deployment of flux controllers failed: %v" , err )
5763 return err
5864 }
5965
60- log .Info ("Flux deployment completed" )
66+ log .Info ("Deployment of flux controllers completed" )
6167 return nil
6268 },
6369}
6470
6571func init () {
6672 RootCmd .AddCommand (deployFluxCmd )
73+ deployFluxCmd .Flags ().SortFlags = false
74+ deployFluxCmd .Flags ().String (FlagOcmConfig , "" , "OCM configuration file" )
75+ deployFluxCmd .Flags ().String (FlagGitConfig , "" , "Git credentials configuration file that configures basic auth or ssh private key. This will be used in the fluxcd GitSource for spec.secretRef to authenticate against the deploymentRepository. If not set, no authentication will be configured." )
76+ deployFluxCmd .Flags ().String (FlagKubeConfig , "" , "Kubernetes configuration file" )
6777
68- deployFluxCmd .Flags ().StringP (flagOCMConfig , "c" , "" , "ocm configuration file" )
69- deployFluxCmd .Flags ().StringP (flagGitCredentials , "g" , "" , "git credentials configuration file that configures basic auth, personal access token, ssh private key. This will be used in the fluxcd GitSource for spec.secretRef to authenticate against the deploymentRepository. If not set, no authentication will be configured." )
70- deployFluxCmd .Flags ().StringP (flagKubeconfig , "k" , "" , "kubeconfig of the Kubernetes cluster on which the flux deployment will be created/updated. If not set, the current context will be used." )
71- deployFluxCmd .Flags ().StringP (flagFluxCDNamespace , "n" , "" , "namespace on the Kubernetes cluster in which the namespaced fluxcd resources will be deployed. Default 'flux-system'." )
78+ if err := deployFluxCmd .MarkFlagRequired (FlagGitConfig ); err != nil {
79+ panic (err )
80+ }
7281}
0 commit comments