Skip to content

Commit 40a3a11

Browse files
feat: add new feature of reloading configmap dynamically
Signed-off-by: gyu-young-park <[email protected]>
1 parent e2b9294 commit 40a3a11

File tree

9 files changed

+200
-70
lines changed

9 files changed

+200
-70
lines changed

commitserver/apiclient/commit.pb.go

Lines changed: 88 additions & 35 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

commitserver/commit/commit.go

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -47,29 +47,6 @@ type hydratorMetadataFile struct {
4747
References []v1alpha1.RevisionReference `json:"references,omitempty"`
4848
}
4949

50-
// TODO: make this configurable via ConfigMap.
51-
var manifestHydrationReadmeTemplate = `# Manifest Hydration
52-
53-
To hydrate the manifests in this repository, run the following commands:
54-
55-
` + "```shell" + `
56-
git clone {{ .RepoURL }}
57-
# cd into the cloned directory
58-
git checkout {{ .DrySHA }}
59-
{{ range $command := .Commands -}}
60-
{{ $command }}
61-
{{ end -}}` + "```" + `
62-
{{ if .References -}}
63-
64-
## References
65-
66-
{{ range $ref := .References -}}
67-
{{ if $ref.Commit -}}
68-
* [{{ $ref.Commit.SHA | mustRegexFind "[0-9a-f]+" | trunc 7 }}]({{ $ref.Commit.RepoURL }}): {{ $ref.Commit.Subject }} ({{ $ref.Commit.Author }})
69-
{{ end -}}
70-
{{ end -}}
71-
{{ end -}}`
72-
7350
// CommitHydratedManifests handles a commit request. It clones the repository, checks out the sync branch, checks out
7451
// the target branch, clears the repository contents, writes the manifests to the repository, commits the changes, and
7552
// pushes the changes. It returns the hydrated revision SHA and an error if one occurred.
@@ -120,6 +97,7 @@ func (s *Service) handleCommitRequest(logCtx *log.Entry, r *apiclient.CommitHydr
12097
if r.Repo == nil {
12198
return "", "", errors.New("repo is required")
12299
}
100+
123101
if r.Repo.Repo == "" {
124102
return "", "", errors.New("repo URL is required")
125103
}
@@ -179,7 +157,7 @@ func (s *Service) handleCommitRequest(logCtx *log.Entry, r *apiclient.CommitHydr
179157
}
180158

181159
logCtx.Debug("Writing manifests")
182-
err = WriteForPaths(root, r.Repo.Repo, r.DrySha, r.DryCommitMetadata, r.Paths)
160+
err = WriteForPaths(root, r.Repo.Repo, r.DrySha, r.DryCommitMetadata, r.Paths, r.ReadmeMessage)
183161
if err != nil {
184162
return "", "", fmt.Errorf("failed to write manifests: %w", err)
185163
}

commitserver/commit/commit.proto

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ message CommitHydratedManifestsRequest {
2020
repeated PathDetails paths = 6;
2121
// DryCommitMetadata contains metadata about the DRY commit, such as the author and committer.
2222
github.com.argoproj.argo_cd.v3.pkg.apis.application.v1alpha1.RevisionMetadata dryCommitMetadata = 7;
23+
// ReadmeMessage is the message content for README template updates.
24+
string readmeMessage = 8;
2325
}
2426

2527
// PathDetails holds information about hydrated manifests to be written to a particular path in the hydrated manifests

commitserver/commit/hydratorhelper.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func init() {
3333

3434
// WriteForPaths writes the manifests, hydrator.metadata, and README.md files for each path in the provided paths. It
3535
// also writes a root-level hydrator.metadata file containing the repo URL and dry SHA.
36-
func WriteForPaths(root *os.Root, repoUrl, drySha string, dryCommitMetadata *appv1.RevisionMetadata, paths []*apiclient.PathDetails) error { //nolint:revive //FIXME(var-naming)
36+
func WriteForPaths(root *os.Root, repoUrl, drySha string, dryCommitMetadata *appv1.RevisionMetadata, paths []*apiclient.PathDetails, rawReadmeTemplate string) error { //nolint:revive //FIXME(var-naming)
3737
hydratorMetadata, err := hydrator.GetCommitMetadata(repoUrl, drySha, dryCommitMetadata)
3838
if err != nil {
3939
return fmt.Errorf("failed to retrieve hydrator metadata: %w", err)
@@ -83,7 +83,7 @@ func WriteForPaths(root *os.Root, repoUrl, drySha string, dryCommitMetadata *app
8383
}
8484

8585
// Write README
86-
err = writeReadme(root, hydratePath, hydratorMetadata)
86+
err = writeReadme(root, hydratePath, hydratorMetadata, rawReadmeTemplate)
8787
if err != nil {
8888
return fmt.Errorf("failed to write readme: %w", err)
8989
}
@@ -111,8 +111,8 @@ func writeMetadata(root *os.Root, dirPath string, metadata hydrator.HydratorComm
111111
}
112112

113113
// writeReadme writes the readme to the README.md file.
114-
func writeReadme(root *os.Root, dirPath string, metadata hydrator.HydratorCommitMetadata) error {
115-
readmeTemplate, err := template.New("readme").Funcs(sprigFuncMap).Parse(manifestHydrationReadmeTemplate)
114+
func writeReadme(root *os.Root, dirPath string, metadata hydrator.HydratorCommitMetadata, rawReadmeTemplate string) error {
115+
readmeTemplate, err := template.New("readme").Funcs(sprigFuncMap).Parse(rawReadmeTemplate)
116116
if err != nil {
117117
return fmt.Errorf("failed to parse readme template: %w", err)
118118
}

commitserver/commit/hydratorhelper_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/argoproj/argo-cd/v3/commitserver/apiclient"
2020
appsv1 "github.com/argoproj/argo-cd/v3/pkg/apis/application/v1alpha1"
2121
"github.com/argoproj/argo-cd/v3/util/hydrator"
22+
"github.com/argoproj/argo-cd/v3/util/settings"
2223
)
2324

2425
// tempRoot creates a temporary directory and returns an os.Root object for it.
@@ -93,7 +94,7 @@ Argocd-reference-commit-sha: abc123
9394
},
9495
}
9596

96-
err := WriteForPaths(root, repoURL, drySha, metadata, paths)
97+
err := WriteForPaths(root, repoURL, drySha, metadata, paths, settings.ManifestHydrationReadmeTemplate)
9798
require.NoError(t, err)
9899

99100
// Check if the top-level hydrator.metadata exists and contains the repo URL and dry SHA
@@ -188,7 +189,7 @@ func TestWriteReadme(t *testing.T) {
188189
},
189190
}
190191

191-
err = writeReadme(root, "", metadata)
192+
err = writeReadme(root, "", metadata, settings.ManifestHydrationReadmeTemplate)
192193
require.NoError(t, err)
193194

194195
readmePath := filepath.Join(root.Name(), "README.md")

controller/hydrator/hydrator.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ type Dependencies interface {
6767

6868
// GetHydratorCommitMessageTemplate gets the configured template for rendering commit messages.
6969
GetHydratorCommitMessageTemplate() (string, error)
70+
71+
// GetHydratorReadmeMessageTemplate gets the configured template for rendering README messages.
72+
GetHydratorReadmeMessageTemplate() (string, error)
7073
}
7174

7275
// Hydrator is the main struct that implements the hydration logic. It uses the Dependencies interface to access the
@@ -374,6 +377,12 @@ func (h *Hydrator) hydrate(logCtx *log.Entry, apps []*appv1.Application, project
374377
return "", "", fmt.Errorf("failed to get hydrator commit templated message: %w", errMsg)
375378
}
376379

380+
// get the readme message template
381+
readmeTemplate, err := h.dependencies.GetHydratorReadmeMessageTemplate()
382+
if err != nil {
383+
return "", "", fmt.Errorf("failed to get hydrated readme message template: %w", err)
384+
}
385+
377386
manifestsRequest := commitclient.CommitHydratedManifestsRequest{
378387
Repo: repo,
379388
SyncBranch: syncBranch,
@@ -382,6 +391,7 @@ func (h *Hydrator) hydrate(logCtx *log.Entry, apps []*appv1.Application, project
382391
CommitMessage: commitMessage,
383392
Paths: paths,
384393
DryCommitMetadata: revisionMetadata,
394+
ReadmeMessage: readmeTemplate,
385395
}
386396

387397
closer, commitService, err := h.commitClientset.NewCommitServerClient()

controller/hydrator/mocks/Dependencies.go

Lines changed: 53 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

controller/hydrator_dependencies.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,12 @@ func (ctrl *ApplicationController) GetHydratorCommitMessageTemplate() (string, e
106106

107107
return sourceHydratorCommitMessageKey, nil
108108
}
109+
110+
func (ctrl *ApplicationController) GetHydratorReadmeMessageTemplate() (string, error) {
111+
sourceHydratorReadmeMessageKey, err := ctrl.settingsMgr.GetHydratorReadmeTemplate()
112+
if err != nil {
113+
return "", fmt.Errorf("failed to get sourceHydrator README message template key: %w", err)
114+
}
115+
116+
return sourceHydratorReadmeMessageKey, nil
117+
}

0 commit comments

Comments
 (0)