Skip to content

Commit b1525eb

Browse files
authored
feat: deploy flux command (#44)
* feat: deploy flux command * feat: template input * feat: template input * feat: ensure flux system namespace exists
1 parent db315d2 commit b1525eb

File tree

10 files changed

+479
-137
lines changed

10 files changed

+479
-137
lines changed

cmd/deploy_flux.go

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,70 +3,79 @@ package cmd
33
import (
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
2117
var 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

6571
func 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
}

docs/technical/flux-deployer.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Flux Deployer
2+
3+
The flux deployer creates a temporary working directory with subdirectories `download`, `templates`, and `repo`.
4+
5+
The final directory structure looks like this:
6+
7+
```shell
8+
WORKDIR
9+
├── download
10+
│ ├── Chart.yaml
11+
│ ├── templates
12+
│ │ ├── overlays
13+
│ │ │ ├── flux-kustomization.yaml
14+
│ │ │ ├── gitrepo.yaml
15+
│ │ │ └── kustomization.yaml
16+
│ │ └── resources
17+
│ │ ├── components.yaml
18+
│ │ ├── flux-kustomization.yaml
19+
│ │ ├── gitrepo.yaml
20+
│ │ └── kustomization.yaml
21+
│ └── values.yaml
22+
23+
├── templates
24+
│ ├── envs
25+
│ │ └── dev
26+
│ │ └── fluxcd
27+
│ │ ├── flux-kustomization.yaml
28+
│ │ ├── gitrepo.yaml
29+
│ │ └── kustomization.yaml
30+
│ └── resources
31+
│ └── fluxcd
32+
│ ├── components.yaml
33+
│ ├── flux-kustomization.yaml
34+
│ ├── gitrepo.yaml
35+
│ └── kustomization.yaml
36+
37+
└── repo # same structure as in WORKDIR/templates
38+
├── envs
39+
│ └── dev
40+
│ └── fluxcd # entry point for the kustomization
41+
│ ├── flux-kustomization.yaml
42+
│ ├── gitrepo.yaml
43+
│ └── kustomization.yaml
44+
└── resources
45+
└── fluxcd
46+
├── components.yaml
47+
├── flux-kustomization.yaml
48+
├── gitrepo.yaml
49+
└── kustomization.yaml
50+
```
Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
11
package flux_deployer
22

33
const (
4-
// Names of ocm resources of the root component
4+
// FluxSystemNamespace is the namespace on the platform cluster in which the flux controllers are deployed.
5+
FluxSystemNamespace = "flux-system"
56

6-
FluxcdHelmController = "fluxcd-helm-controller"
7-
FluxcdKustomizeController = "fluxcd-kustomize-controller"
8-
FluxcdSourceController = "fluxcd-source-controller"
7+
// GitSecretName is the name of the secret in the flux system namespace that contains the git credentials for accessing the deployment repository.
8+
// The secret is references in the GitRepository resource which establishes the synchronization with the deployment git repository.
9+
GitSecretName = "git"
10+
11+
// Directory names
12+
EnvsDirectoryName = "envs"
13+
FluxCDDirectoryName = "fluxcd"
14+
OpenMCPDirectoryName = "openmcp"
15+
ResourcesDirectoryName = "resources"
16+
TemplatesDirectoryName = "templates"
17+
OverlaysDirectoryName = "overlays"
18+
19+
// Resource names
20+
FluxCDSourceControllerResourceName = "fluxcd-source-controller"
21+
FluxCDKustomizationControllerResourceName = "fluxcd-kustomize-controller"
22+
FluxCDHelmControllerResourceName = "fluxcd-helm-controller"
923
)

0 commit comments

Comments
 (0)