Skip to content

Commit

Permalink
Use the app repo url to generate the app hash instead of the config a…
Browse files Browse the repository at this point in the history
…pp repo url (#471)

* Use the app repo url to generate the app hash

* Fixed unit tests for generateAppYaml

* Fixed unit tests for generateAppYaml

* Single place to generate and validate app hash

* Fixed unit tests

* Removed F from FDescribe

* Clean up function parameters

* Passing in appHash instead
  • Loading branch information
josecordaz authored Jul 16, 2021
1 parent c827d7a commit 7cf9515
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 59 deletions.
79 changes: 25 additions & 54 deletions pkg/services/app/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,25 @@ func (a *App) Add(params AddParams) error {
return errors.Wrap(err, "could not generate deploy key")
}

appHash, err := utils.GetAppHash(params.Url, params.Path, params.Branch)
if err != nil {
return err
}
// if appHash exists as a label in the cluster we fail to create a PR
if err = a.kube.LabelExistsInCluster(ctx, appHash); err != nil {
return err
}

switch strings.ToUpper(params.AppConfigUrl) {
case string(ConfigTypeNone):
return a.addAppWithNoConfigRepo(params, clusterName, secretRef)
return a.addAppWithNoConfigRepo(params, secretRef, appHash)
case string(ConfigTypeUserRepo):
return a.addAppWithConfigInAppRepo(params, clusterName, secretRef)
return a.addAppWithConfigInAppRepo(params, clusterName, secretRef, appHash)
default:
return a.addAppWithConfigInExternalRepo(params, clusterName, secretRef)
return a.addAppWithConfigInExternalRepo(params, clusterName, secretRef, appHash)
}
}

func (a *App) printAddSummary(params AddParams) {
a.logger.Println("Adding application:\n")
a.logger.Println("Name: %s", params.Name)
Expand Down Expand Up @@ -190,19 +200,9 @@ func (a *App) getGitRemoteUrl(params AddParams) (string, error) {
return sanitizeRepoUrl(urls[0]), nil
}

func (a *App) addAppWithNoConfigRepo(params AddParams, clusterName string, secretRef string) error {
ctx := context.Background()
appHash, err := utils.GetAppHash(params.Url, params.Path, params.Branch)
if err != nil {
return err
}
// if appHash exists as a label in the cluster we fail to create a PR
if err := a.kube.LabelExistsInCluster(ctx, appHash); err != nil {
return err
}

func (a *App) addAppWithNoConfigRepo(params AddParams, secretRef string, appHash string) error {
// Returns the source, app spec and kustomization
source, appGoat, appSpec, err := a.generateAppManifests(params, params.Url, secretRef, clusterName)
source, appGoat, appSpec, err := a.generateAppManifests(params, secretRef, appHash)
if err != nil {
return errors.Wrap(err, "could not generate application GitOps Automation manifests")
}
Expand All @@ -211,19 +211,10 @@ func (a *App) addAppWithNoConfigRepo(params AddParams, clusterName string, secre
return a.applyToCluster(params, source, appGoat, appSpec)
}

func (a *App) addAppWithConfigInAppRepo(params AddParams, clusterName string, secretRef string) error {
ctx := context.Background()
appHash, err := utils.GetAppHash(params.Url, params.Path, params.Branch)
if err != nil {
return err
}
// if appHash exists as a label in the cluster we fail to create a PR
if err := a.kube.LabelExistsInCluster(ctx, appHash); err != nil {
return err
}
func (a *App) addAppWithConfigInAppRepo(params AddParams, clusterName string, secretRef string, appHash string) error {

// Returns the source, app spec and kustomization
source, appGoat, appSpec, err := a.generateAppManifests(params, params.Url, secretRef, clusterName)
source, appGoat, appSpec, err := a.generateAppManifests(params, secretRef, appHash)
if err != nil {
return errors.Wrap(err, "could not generate application GitOps Automation manifests")
}
Expand All @@ -244,7 +235,7 @@ func (a *App) addAppWithConfigInAppRepo(params AddParams, clusterName string, se

if !params.DryRun {
if !params.AutoMerge {
if err := a.createPullRequestToRepo(params, ".wego", params.Url, clusterName, appSpec, appGoat); err != nil {
if err := a.createPullRequestToRepo(params, ".wego", params.Url, clusterName, appHash, appSpec, appGoat); err != nil {
return err
}
} else {
Expand All @@ -270,27 +261,17 @@ func (a *App) addAppWithConfigInAppRepo(params AddParams, clusterName string, se
})
}

func (a *App) addAppWithConfigInExternalRepo(params AddParams, clusterName string, appSecretRef string) error {
ctx := context.Background()
func (a *App) addAppWithConfigInExternalRepo(params AddParams, clusterName string, appSecretRef string, appHash string) error {
// making sure the url is in good format
params.AppConfigUrl = sanitizeRepoUrl(params.AppConfigUrl)

appHash, err := utils.GetAppHash(params.AppConfigUrl, params.Path, params.Branch)
if err != nil {
return err
}
// if appHash exists as a label in the cluster we fail to create a PR
if err := a.kube.LabelExistsInCluster(ctx, appHash); err != nil {
return err
}

appConfigSecretName, err := a.createAndUploadDeployKey(params.AppConfigUrl, SourceTypeGit, clusterName, params.Namespace, params.DryRun)
if err != nil {
return errors.Wrap(err, "could not generate deploy key")
}

// Returns the source, app spec and kustomization
appSource, appGoat, appSpec, err := a.generateAppManifests(params, params.AppConfigUrl, appSecretRef, clusterName)
appSource, appGoat, appSpec, err := a.generateAppManifests(params, appSecretRef, appHash)
if err != nil {
return errors.Wrap(err, "could not generate application GitOps Automation manifests")
}
Expand All @@ -306,7 +287,7 @@ func (a *App) addAppWithConfigInExternalRepo(params AddParams, clusterName strin

if !params.DryRun {
if !params.AutoMerge {
if err := a.createPullRequestToRepo(params, ".", params.AppConfigUrl, clusterName, appSpec, appGoat, appSource); err != nil {
if err := a.createPullRequestToRepo(params, ".", params.AppConfigUrl, clusterName, appHash, appSpec, appGoat, appSource); err != nil {
return err
}
} else {
Expand All @@ -330,7 +311,7 @@ func (a *App) addAppWithConfigInExternalRepo(params AddParams, clusterName strin
return a.commitAndPush(params)
}

func (a *App) generateAppManifests(params AddParams, repo string, secretRef string, clusterName string) ([]byte, []byte, []byte, error) {
func (a *App) generateAppManifests(params AddParams, secretRef string, appHash string) ([]byte, []byte, []byte, error) {
var sourceManifest, appManifest, appGoatManifest []byte
var err error
a.logger.Generatef("Generating Source manifest")
Expand All @@ -346,7 +327,7 @@ func (a *App) generateAppManifests(params AddParams, repo string, secretRef stri
}

a.logger.Generatef("Generating Application spec manifest")
appManifest, err = generateAppYaml(params, repo)
appManifest, err = generateAppYaml(params, appHash)
if err != nil {
return nil, nil, nil, errors.Wrap(err, fmt.Sprintf("could not create app.yaml for '%s'", params.Name))
}
Expand Down Expand Up @@ -575,14 +556,9 @@ func makeWegoApplication(params AddParams) wego.Application {
return app
}

func generateAppYaml(params AddParams, repo string) ([]byte, error) {
func generateAppYaml(params AddParams, appHash string) ([]byte, error) {
app := makeWegoApplication(params)

appHash, err := utils.GetAppHash(repo, params.Path, params.Branch)
if err != nil {
return nil, fmt.Errorf("could not generate app hash: %w", err)
}

app.ObjectMeta.Labels = map[string]string{
WeGOAppIdentifierLabelKey: appHash,
}
Expand Down Expand Up @@ -635,7 +611,7 @@ func sanitizeRepoUrl(url string) string {
return url
}

func (a *App) createPullRequestToRepo(params AddParams, basePath string, repo string, clusterName string, appYaml []byte, goatManifests ...[]byte) error {
func (a *App) createPullRequestToRepo(params AddParams, basePath string, repo string, clusterName string, appHash string, appYaml []byte, goatManifests ...[]byte) error {
repoName := generateResourceName(repo)

appPath := filepath.Join(basePath, "apps", params.Name, "app.yaml")
Expand Down Expand Up @@ -672,11 +648,6 @@ func (a *App) createPullRequestToRepo(params AddParams, basePath string, repo st
return nil
}

appHash, err := utils.GetAppHash(repo, params.Path, params.Branch)
if err != nil {
return err
}

if accountType == gitproviders.AccountTypeOrg {
org, err := fluxops.GetOwnerFromEnv()
if err != nil {
Expand Down
9 changes: 5 additions & 4 deletions pkg/services/app/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,22 +494,23 @@ var _ = Describe("Add", func() {

Describe("generateAppYaml", func() {
It("generates the app yaml", func() {
repoURL := "ssh://[email protected]/example/my-source"
params := AddParams{
Name: "my-app",
Namespace: "wego-system",
Url: "ssh://[email protected]/example/my-source",
Url: repoURL,
Path: "manifests",
Branch: "main",
}
repo := "some-repo"

desired2 := makeWegoApplication(params)
hash, err := utils.GetAppHash(repo, params.Path, params.Branch)
hash, err := utils.GetAppHash(repoURL, params.Path, params.Branch)
Expect(err).To(BeNil())

desired2.ObjectMeta.Labels = map[string]string{WeGOAppIdentifierLabelKey: hash}

out, err := generateAppYaml(params, repo)
Expect(err).NotTo(HaveOccurred())
out, err := generateAppYaml(params, hash)
Expect(err).To(BeNil())

result := wego.Application{}
Expand Down
2 changes: 1 addition & 1 deletion pkg/utils/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func GetAppHash(url, path, branch string) (string, error) {
h := md5.New()
_, err := h.Write([]byte(url + path + branch))
if err != nil {
return "", err
return "", fmt.Errorf("error generating app hash %s", err)
}
return "wego-" + hex.EncodeToString(h.Sum(nil)), nil
}
Expand Down

0 comments on commit 7cf9515

Please sign in to comment.