Skip to content

Commit 76ed1ad

Browse files
committed
feat: template input
1 parent 9f428d3 commit 76ed1ad

File tree

7 files changed

+42
-49
lines changed

7 files changed

+42
-49
lines changed

internal/common/constants.go

Lines changed: 0 additions & 13 deletions
This file was deleted.

internal/flux_deployer/constants.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,16 @@ const (
88
// The secret is references in the GitRepository resource which establishes the synchronization with the deployment git repository.
99
GitSecretName = "git"
1010

11-
// Names of ocm resources of the root component
12-
FluxcdHelmController = "fluxcd-helm-controller"
13-
FluxcdKustomizeController = "fluxcd-kustomize-controller"
14-
FluxcdSourceController = "fluxcd-source-controller"
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"
1523
)

internal/flux_deployer/deployer.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"sigs.k8s.io/kustomize/api/krusty"
1313
"sigs.k8s.io/kustomize/kyaml/filesys"
1414

15-
"github.com/openmcp-project/bootstrapper/internal/common"
1615
cfg "github.com/openmcp-project/bootstrapper/internal/config"
1716
ocmcli "github.com/openmcp-project/bootstrapper/internal/ocm-cli"
1817
"github.com/openmcp-project/bootstrapper/internal/template"
@@ -104,7 +103,7 @@ func (d *FluxDeployer) Deploy(ctx context.Context) (err error) {
104103
return err
105104
}
106105

107-
d.fluxcdCV, err = componentGetter.GetComponentVersionForResourceRecursive(ctx, componentGetter.RootComponentVersion(), common.FluxCDSourceControllerResourceName)
106+
d.fluxcdCV, err = componentGetter.GetComponentVersionForResourceRecursive(ctx, componentGetter.RootComponentVersion(), FluxCDSourceControllerResourceName)
108107
if err != nil {
109108
return fmt.Errorf("failed to get fluxcd source controller component version: %w", err)
110109
}
@@ -126,7 +125,7 @@ func (d *FluxDeployer) Deploy(ctx context.Context) (err error) {
126125
}
127126

128127
// Kustomize <workdir>/repo/envs/<envName>/fluxcd
129-
fluxCDEnvDir := filepath.Join(d.repoDir, common.EnvsDirectoryName, d.Config.Environment, common.FluxCDDirectoryName)
128+
fluxCDEnvDir := filepath.Join(d.repoDir, EnvsDirectoryName, d.Config.Environment, FluxCDDirectoryName)
130129
manifests, err := d.Kustomize(fluxCDEnvDir)
131130
if err != nil {
132131
return fmt.Errorf("error kustomizing templated files: %w", err)
@@ -146,14 +145,14 @@ func (d *FluxDeployer) ArrangeTemplates() (err error) {
146145
d.log.Info("Arranging template files")
147146

148147
// Create directory <templatesDir>/envs/<envName>/fluxcd
149-
fluxCDEnvDir := filepath.Join(d.templatesDir, common.EnvsDirectoryName, d.Config.Environment, common.FluxCDDirectoryName)
148+
fluxCDEnvDir := filepath.Join(d.templatesDir, EnvsDirectoryName, d.Config.Environment, FluxCDDirectoryName)
150149
err = os.MkdirAll(fluxCDEnvDir, 0755)
151150
if err != nil {
152151
return fmt.Errorf("failed to create fluxcd environment directory: %w", err)
153152
}
154153

155154
// Create directory <templatesDir>/resources/fluxcd
156-
fluxCDResourcesDir := filepath.Join(d.templatesDir, common.ResourcesDirectoryName, common.FluxCDDirectoryName)
155+
fluxCDResourcesDir := filepath.Join(d.templatesDir, ResourcesDirectoryName, FluxCDDirectoryName)
157156
err = os.MkdirAll(fluxCDResourcesDir, 0755)
158157
if err != nil {
159158
return fmt.Errorf("failed to create fluxcd resources directory: %w", err)
@@ -162,13 +161,13 @@ func (d *FluxDeployer) ArrangeTemplates() (err error) {
162161
d.log.Debug("Copying template files to target directories")
163162

164163
// copy all files from <downloadDir>/templates/overlays to <templatesDir>/envs/<envName>/fluxcd
165-
err = util.CopyDir(filepath.Join(d.downloadDir, common.TemplatesDirectoryName, common.OverlaysDirectoryName), fluxCDEnvDir)
164+
err = util.CopyDir(filepath.Join(d.downloadDir, TemplatesDirectoryName, OverlaysDirectoryName), fluxCDEnvDir)
166165
if err != nil {
167166
return fmt.Errorf("failed to copy fluxcd overlays: %w", err)
168167
}
169168

170169
// copy all files from <downloadDir>/templates/resources to <templatesDir>/resources/fluxcd
171-
err = util.CopyDir(filepath.Join(d.downloadDir, common.TemplatesDirectoryName, common.ResourcesDirectoryName), fluxCDResourcesDir)
170+
err = util.CopyDir(filepath.Join(d.downloadDir, TemplatesDirectoryName, ResourcesDirectoryName), fluxCDResourcesDir)
172171
if err != nil {
173172
return fmt.Errorf("failed to copy fluxcd resources: %w", err)
174173
}
@@ -181,15 +180,15 @@ func (d *FluxDeployer) ArrangeTemplates() (err error) {
181180
// The resulting files are written to the repo directory.
182181
func (d *FluxDeployer) Template() (err error) {
183182
d.log.Infof("Applying templates from %s to deployment repository", d.Config.Component.FluxcdTemplateResourcePath)
184-
templateInput := common.NewTemplateInputFromConfig(d.Config)
183+
templateInput := NewTemplateInputFromConfig(d.Config)
185184

186-
if err = templateInput.AddImageResource(d.fluxcdCV, common.FluxCDSourceControllerResourceName, "sourceController"); err != nil {
185+
if err = templateInput.AddImageResource(d.fluxcdCV, FluxCDSourceControllerResourceName, "sourceController"); err != nil {
187186
return fmt.Errorf("failed to apply fluxcd source controller template input: %w", err)
188187
}
189-
if err = templateInput.AddImageResource(d.fluxcdCV, common.FluxCDKustomizationControllerResourceName, "kustomizeController"); err != nil {
188+
if err = templateInput.AddImageResource(d.fluxcdCV, FluxCDKustomizationControllerResourceName, "kustomizeController"); err != nil {
190189
return fmt.Errorf("failed to apply fluxcd kustomize controller template input: %w", err)
191190
}
192-
if err = templateInput.AddImageResource(d.fluxcdCV, common.FluxCDHelmControllerResourceName, "helmController"); err != nil {
191+
if err = templateInput.AddImageResource(d.fluxcdCV, FluxCDHelmControllerResourceName, "helmController"); err != nil {
193192
return fmt.Errorf("failed to apply fluxcd helm controller template input: %w", err)
194193
}
195194

internal/flux_deployer/images.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
// GetFluxCDImages retrieves the images of the FluxCD controllers from the root component version.
1010
func GetFluxCDImages(rootCV *ocmcli.ComponentVersion) (map[string]any, error) {
11-
return GetImages(rootCV, FluxcdHelmController, FluxcdKustomizeController, FluxcdSourceController)
11+
return GetImages(rootCV, FluxCDHelmControllerResourceName, FluxCDKustomizationControllerResourceName, FluxCDSourceControllerResourceName)
1212
}
1313

1414
// GetImages retrieves the images references for a list of resources of a component version.

internal/flux_deployer/images_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ func TestGetFluxCDImages(t *testing.T) {
1313

1414
imageMap, err := flux_deployer.GetFluxCDImages(cv)
1515
assert.NoError(t, err, "error getting images")
16-
img, found := imageMap[flux_deployer.FluxcdSourceController]
16+
img, found := imageMap[flux_deployer.FluxCDSourceControllerResourceName]
1717
assert.True(t, found, "fluxcd source controller image should be found")
1818
assert.Equal(t, "test-source-controller-image:v0.0.1", img, "fluxcd source controller image should match")
19-
img, found = imageMap[flux_deployer.FluxcdHelmController]
19+
img, found = imageMap[flux_deployer.FluxCDHelmControllerResourceName]
2020
assert.True(t, found, "fluxcd helm controller image should be found")
2121
assert.Equal(t, "test-helm-controller-image:v0.0.1", img, "fluxcd helm controller image should match")
22-
img, found = imageMap[flux_deployer.FluxcdKustomizeController]
22+
img, found = imageMap[flux_deployer.FluxCDKustomizationControllerResourceName]
2323
assert.True(t, found, "fluxcd kustomize controller image should be found")
2424
assert.Equal(t, "test-kustomize-controller-image:v0.0.1", img, "fluxcd kustomize controller image should match")
2525
}

internal/flux_deployer/templating.go renamed to internal/flux_deployer/template.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@ import (
77

88
"github.com/sirupsen/logrus"
99

10-
"github.com/openmcp-project/bootstrapper/internal/common"
1110
"github.com/openmcp-project/bootstrapper/internal/template"
1211
)
1312

1413
// TemplateDirectory processes the template files in the specified directory and writes
1514
// the rendered content to the corresponding files in the Git repository's worktree.
1615
// It uses the provided template directory and Git repository to perform the operations.
17-
func TemplateDirectory(templateDirectory string, templateInput common.TemplateInput, repo string, logger *logrus.Logger) error {
16+
func TemplateDirectory(templateDirectory string, templateInput TemplateInput, repo string, logger *logrus.Logger) error {
1817

1918
templateDir, err := os.Open(templateDirectory)
2019
if err != nil {

internal/common/template_input.go renamed to internal/flux_deployer/template_input.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package common
1+
package flux_deployer
22

33
import (
44
"fmt"
@@ -16,27 +16,17 @@ func NewTemplateInput() TemplateInput {
1616

1717
func NewTemplateInputFromConfig(c *config.BootstrapperConfig) TemplateInput {
1818
t := TemplateInput{
19-
"fluxCDEnvPath": "./" + EnvsDirectoryName + "/" + c.Environment + "/" + FluxCDDirectoryName,
20-
"fluxCDResourcesPath": "../../../" + ResourcesDirectoryName + "/" + FluxCDDirectoryName,
19+
"fluxCDEnvPath": "./" + EnvsDirectoryName + "/" + c.Environment + "/" + FluxCDDirectoryName,
20+
"fluxCDResourcesPath": "../../../" + ResourcesDirectoryName + "/" + FluxCDDirectoryName,
21+
"openMCPResourcesPath": "../../../" + ResourcesDirectoryName + "/" + OpenMCPDirectoryName,
2122

2223
"git": map[string]interface{}{
2324
"repoUrl": c.DeploymentRepository.RepoURL,
2425
"mainBranch": c.DeploymentRepository.RepoBranch,
2526
},
2627
"gitRepoEnvBranch": c.DeploymentRepository.RepoBranch,
2728

28-
"imagePullSecrets": func() []map[string]string {
29-
if len(c.ImagePullSecrets) == 0 {
30-
return nil
31-
}
32-
secrets := make([]map[string]string, 0, len(c.ImagePullSecrets))
33-
for _, secret := range c.ImagePullSecrets {
34-
secrets = append(secrets, map[string]string{
35-
"name": secret,
36-
})
37-
}
38-
return secrets
39-
}(),
29+
"imagePullSecrets": wrapImagePullSecrets(c.ImagePullSecrets),
4030
}
4131

4232
return t
@@ -69,3 +59,13 @@ func (t TemplateInput) ValuesWrapper() map[string]any {
6959
"Values": t,
7060
}
7161
}
62+
63+
func wrapImagePullSecrets(secrets []string) []map[string]string {
64+
wrappedSecrets := make([]map[string]string, len(secrets))
65+
for i, secret := range secrets {
66+
wrappedSecrets[i] = map[string]string{
67+
"name": secret,
68+
}
69+
}
70+
return wrappedSecrets
71+
}

0 commit comments

Comments
 (0)