Skip to content

Commit fbe24cc

Browse files
committed
feat: logs
1 parent 71b4c10 commit fbe24cc

File tree

3 files changed

+46
-18
lines changed

3 files changed

+46
-18
lines changed

cmd/deploy_flux.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ var deployFluxCmd = &cobra.Command{
3232
},
3333
RunE: func(cmd *cobra.Command, args []string) error {
3434
log := logging.GetLogger()
35-
log.Debugf("Executing deploy-flux with component-location: %s, deployment-templates: %s, "+
35+
log.Infof("Starting flux deployment with component-location: %s, deployment-templates: %s, "+
3636
"deployment-repository: %s, deployment-repository-branch: %s, deployment-repository-path: %s",
3737
args[0], args[1], args[2], args[3], args[4])
3838

@@ -51,7 +51,14 @@ var deployFluxCmd = &cobra.Command{
5151
cmd.Flag(flagFluxCDNamespace).Value.String(),
5252
platformKubeconfig,
5353
platformCluster, log)
54-
return d.Deploy(cmd.Context())
54+
err := d.Deploy(cmd.Context())
55+
if err != nil {
56+
log.Errorf("Flux deployment failed: %v", err)
57+
return err
58+
}
59+
60+
log.Info("Flux deployment completed")
61+
return nil
5562
},
5663
}
5764

internal/flux_deployer/deployer.go

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,15 @@ func NewFluxDeployer(componentLocation, deploymentTemplates, deploymentRepositor
5858

5959
func (d *FluxDeployer) Deploy(ctx context.Context) error {
6060

61-
// Build helper object to get root component and templates component.
61+
// Get root component and gitops-templates component.
62+
d.log.Info("Loading root component and gitops-templates component")
6263
componentGetter := ocmcli.NewComponentGetter(d.componentLocation, d.deploymentTemplates, d.ocmConfig)
6364
if err := componentGetter.InitializeComponents(ctx); err != nil {
6465
return err
6566
}
6667

6768
// Create a temporary directory to store the downloaded resource
69+
d.log.Info("Creating download directory for gitops-templates")
6870
downloadDir, err := os.MkdirTemp("", "flux-resource-")
6971
if err != nil {
7072
return fmt.Errorf("error creating temporary download directory for flux resource: %w", err)
@@ -74,9 +76,10 @@ func (d *FluxDeployer) Deploy(ctx context.Context) error {
7476
fmt.Printf("error removing temporary download directory for flux resource: %v\n", err)
7577
}
7678
}()
77-
fmt.Println("created temporary download directory:", downloadDir)
79+
d.log.Debugf("Download directory: %s", downloadDir)
7880

79-
// Download resource from templates component into the download directory
81+
// Download resource from gitops-templates component into the download directory
82+
d.log.Info("Downloading gitops-templates")
8083
if err := componentGetter.DownloadTemplatesResource(ctx, downloadDir); err != nil {
8184
return fmt.Errorf("error downloading templates: %w", err)
8285
}
@@ -86,13 +89,14 @@ func (d *FluxDeployer) Deploy(ctx context.Context) error {
8689
}
8790

8891
if err := d.establishFluxSync(ctx, downloadDir); err != nil {
89-
return fmt.Errorf("error establishing flux sync: %w", err)
92+
return fmt.Errorf("error establishing flux synchronization: %w", err)
9093
}
9194

9295
return nil
9396
}
9497

9598
func (d *FluxDeployer) DeployFluxControllers(ctx context.Context, rootComponentVersion *ocmcli.ComponentVersion, downloadDir string) error {
99+
d.log.Info("Deploying flux")
96100

97101
images, err := GetFluxCDImages(rootComponentVersion)
98102
if err != nil {
@@ -101,9 +105,10 @@ func (d *FluxDeployer) DeployFluxControllers(ctx context.Context, rootComponentV
101105

102106
// Read manifest file
103107
filepath := path.Join(downloadDir, "resources", "gotk-components.yaml")
108+
d.log.Debugf("Reading flux deployment objects from file %s", filepath)
104109
manifestTpl, err := d.readFileContent(filepath)
105110
if err != nil {
106-
return fmt.Errorf("error reading manifests for flux controllers: %w", err)
111+
return fmt.Errorf("error reading flux deployment objects from file %s: %w", filepath, err)
107112
}
108113

109114
// Template
@@ -113,12 +118,14 @@ func (d *FluxDeployer) DeployFluxControllers(ctx context.Context, rootComponentV
113118
"images": images,
114119
},
115120
}
116-
manifest, err := template.NewTemplateExecution().Execute("syncManifest", string(manifestTpl), values)
121+
d.log.Debug("Templating flux deployment objects")
122+
manifest, err := template.NewTemplateExecution().Execute("flux-deployment", string(manifestTpl), values)
117123
if err != nil {
118-
return fmt.Errorf("error templating flux sync manifest: %w", err)
124+
return fmt.Errorf("error templating flux deployment objects: %w", err)
119125
}
120126

121127
// Apply
128+
d.log.Debug("Applying flux deployment objects")
122129
if err := d.applyManifests(ctx, manifest); err != nil {
123130
return err
124131
}
@@ -127,20 +134,24 @@ func (d *FluxDeployer) DeployFluxControllers(ctx context.Context, rootComponentV
127134
}
128135

129136
func (d *FluxDeployer) establishFluxSync(ctx context.Context, downloadDir string) error {
137+
d.log.Info("Establishing flux synchronization with deployment repository")
138+
130139
const secretName = "git"
131140

132-
if err := CreateGitCredentialsSecret(ctx, d.gitCredentials, secretName, d.fluxcdNamespace, d.platformCluster.Client()); err != nil {
141+
if err := CreateGitCredentialsSecret(ctx, d.log, d.gitCredentials, secretName, d.fluxcdNamespace, d.platformCluster.Client()); err != nil {
133142
return err
134143
}
135144

136145
// Read manifest file
137146
filepath := path.Join(downloadDir, "resources", "gotk-sync.yaml")
147+
d.log.Debugf("Reading flux synchronization objects from file %s", filepath)
138148
manifestTpl, err := d.readFileContent(filepath)
139149
if err != nil {
140150
return fmt.Errorf("error reading manifests for flux sync: %w", err)
141151
}
142152

143153
// Template
154+
d.log.Debug("Templating flux synchronization objects")
144155
values := map[string]any{
145156
"Values": map[string]any{
146157
"namespace": d.fluxcdNamespace,
@@ -154,10 +165,11 @@ func (d *FluxDeployer) establishFluxSync(ctx context.Context, downloadDir string
154165
}
155166
manifest, err := template.NewTemplateExecution().Execute("flux-sync", string(manifestTpl), values)
156167
if err != nil {
157-
return fmt.Errorf("error templating manifests for flux sync: %w", err)
168+
return fmt.Errorf("error templating flux synchronization objects: %w", err)
158169
}
159170

160171
// Apply
172+
d.log.Debug("Applying flux synchronization objects")
161173
if err := d.applyManifests(ctx, manifest); err != nil {
162174
return err
163175
}
@@ -166,6 +178,8 @@ func (d *FluxDeployer) establishFluxSync(ctx context.Context, downloadDir string
166178
}
167179

168180
func (d *FluxDeployer) readFileContent(filepath string) ([]byte, error) {
181+
d.log.Debugf("Reading file: %s", filepath)
182+
169183
if _, err := os.Stat(filepath); os.IsNotExist(err) {
170184
return nil, fmt.Errorf("file does not exist at path: %s", filepath)
171185
}

internal/flux_deployer/git_credentials.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ package flux_deployer
22

33
import (
44
"context"
5-
"encoding/base64"
65
"fmt"
76
"os"
87

98
"github.com/openmcp-project/controller-utils/pkg/resources"
9+
"github.com/sirupsen/logrus"
1010
corev1 "k8s.io/api/core/v1"
1111
"sigs.k8s.io/controller-runtime/pkg/client"
1212

@@ -25,31 +25,37 @@ const (
2525
// The secret contains git credentials for flux sync, read from the file d.gitCredentials.
2626
// The file should contain a YAML of a map[string]string, whose keys are described
2727
// in https://fluxcd.io/flux/components/source/gitrepositories/#secret-reference, e.g. username and password.
28-
func CreateGitCredentialsSecret(ctx context.Context, gitCredentials string, secretName, secretNamespace string, platformClient client.Client) error {
28+
func CreateGitCredentialsSecret(ctx context.Context, log *logrus.Logger, gitCredentialsPath string, secretName, secretNamespace string, platformClient client.Client) error {
29+
log.Debug("Creating or updating git credentials secret")
30+
2931
gitCredentialsData := map[string][]byte{}
3032

31-
if gitCredentials != "" {
32-
filepath := gitCredentials
33-
config, err := gitconfig.ParseConfig(filepath)
33+
if gitCredentialsPath != "" {
34+
log.Debugf("Reading and parsing git credentials from path: %s", gitCredentialsPath)
35+
config, err := gitconfig.ParseConfig(gitCredentialsPath)
3436
if err != nil {
3537
return fmt.Errorf("error reading and parsing git credentials for flux sync: %w", err)
3638
}
3739

40+
log.Debugf("Validating git credentials configuration")
3841
if err = config.Validate(); err != nil {
3942
return fmt.Errorf("error validating git credentials for flux sync: %w", err)
4043
}
4144

4245
if config.Authentication.BasicAuth != nil {
46+
log.Debug("Using basic auth credentials for git operations")
4347
gitCredentialsData[username] = []byte(config.Authentication.BasicAuth.Username)
4448
gitCredentialsData[password] = []byte(config.Authentication.BasicAuth.Password)
4549
}
4650
if config.Authentication.BearerToken != nil {
51+
log.Debug("Using bearer token for git operations")
4752
gitCredentialsData[token] = []byte(config.Authentication.BearerToken.Token)
4853
}
4954
if config.Authentication.SSHPrivateKey != nil {
50-
privateKey, err := base64.StdEncoding.DecodeString(config.Authentication.SSHPrivateKey.PrivateKey)
55+
log.Debug("Using ssh private key for git operations")
56+
privateKey, err := config.Authentication.SSHPrivateKey.DecodePrivateKey()
5157
if err != nil {
52-
return fmt.Errorf("error base64 decoding SSH private key: %w", err)
58+
return err
5359
}
5460

5561
gitCredentialsData[identity] = privateKey
@@ -69,6 +75,7 @@ func CreateGitCredentialsSecret(ctx context.Context, gitCredentials string, secr
6975
}
7076

7177
secretMutator := resources.NewSecretMutator(secretName, secretNamespace, gitCredentialsData, corev1.SecretTypeOpaque)
78+
log.Debugf("Storing git credentials in secret %s", secretMutator.String())
7279
if err := resources.CreateOrUpdateResource(ctx, platformClient, secretMutator); err != nil {
7380
return fmt.Errorf("error creating or updating git credentials secret: %w", err)
7481
}

0 commit comments

Comments
 (0)