Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cluster overrides functionality in gen2 clusters #364

Merged

Conversation

jayanth-tjvrr
Copy link
Member

@jayanth-tjvrr jayanth-tjvrr commented Dec 12, 2022

Fixes #111
This PR adds cluster overrides functionality to gen2 clusters. A user can now generate a cluster with changes from a single base manifest without the need to change the original manifest. The design doc of the overrides: overrides-doc
The cluster which are deployed using patches( with --override flag) are turning out to be healthy as well.
Screenshot 2022-12-12 at 8 58 31 PM

@jayanth-tjvrr jayanth-tjvrr temporarily deployed to arlon December 12, 2022 19:30 — with GitHub Actions Inactive
@jayanth-tjvrr jayanth-tjvrr temporarily deployed to arlon December 13, 2022 09:17 — with GitHub Actions Inactive
@jayanth-tjvrr jayanth-tjvrr force-pushed the private/main/jayanth-reddy/clusteroverride-gen2clusters branch from da1f438 to ffdc923 Compare December 13, 2022 09:21
@jayanth-tjvrr jayanth-tjvrr temporarily deployed to arlon December 13, 2022 09:21 — with GitHub Actions Inactive
@jayanth-tjvrr jayanth-tjvrr marked this pull request as ready for review December 13, 2022 09:22
@jayanth-tjvrr jayanth-tjvrr temporarily deployed to arlon December 13, 2022 12:37 — with GitHub Actions Inactive
@@ -17,7 +10,7 @@ func NewCommand() *cobra.Command {
Short: "Manage clusters",
Long: "Manage clusters",
DisableAutoGenTag: true,
PersistentPreRun: checkForArgocd,
//PersistentPreRun: checkForArgocd,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete

os.Exit(1)
}
}
// func checkForArgocd(c *cobra.Command, args []string) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete if unused

@@ -55,6 +63,19 @@ func createClusterCommand() *cobra.Command {
if err != nil {
return fmt.Errorf("failed to get repository credentials: %s", err)
}
overRiden := false
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spelling is incorrect. Rename to overridden

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I corrected the spelling everywhere in the code now.

var clusterRepoPath string
var clusterName string
var overRides string
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming is weird. Rename to overrides.

@@ -55,6 +63,19 @@ func createClusterCommand() *cobra.Command {
if err != nil {
return fmt.Errorf("failed to get repository credentials: %s", err)
}
overRiden := false
if overRides != " " {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks wrong. There's a space between the quotes.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed the space between quotes

@@ -58,3 +59,29 @@ func CommitChanges(tmpDir string, wt *gogit.Worktree, commitMsg string) (changed
}
return
}

func CommitDeletechanges(tmpDir string, wt *gogit.Worktree, commitMsg string) (changed bool, err error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Rename: CommitDeleteChanges
  • How is this different from CommitChanges()? If the differences are none or minimal, consider consolidating into one function.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CommitChanges() function doesn't find the files which have been deleted and hence, it shows that there is nothing to commit even if few files have been deleted. Whereas the CommitDeleteChanges() finds the files which have been deleted and add and commit them.

pkg/gitutils/manifests.go Outdated Show resolved Hide resolved
pkg/gitutils/manifests.go Show resolved Hide resolved
return fmt.Errorf("failed to open embedded file %s: %s", filePath, err)
}

kustomfile, err := ioutil.ReadFile(newFilePath)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested naming: kustomFile

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I renamed all the other variables as well

var targetcomp []string
var kind string
var information info
for kustomkey, kustomval := range parsedData {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested naming: kustomKey, kustomVal. Same thing for others like targetcomp and strkustomval.

@jayanth-tjvrr jayanth-tjvrr temporarily deployed to arlon December 14, 2022 19:16 — with GitHub Actions Inactive
@jayanth-tjvrr jayanth-tjvrr requested a review from bcle December 14, 2022 19:32
@jayanth-tjvrr jayanth-tjvrr temporarily deployed to arlon December 14, 2022 19:47 — with GitHub Actions Inactive
cmd/cluster/create.go Outdated Show resolved Hide resolved
) (*argoappv1.Application, error) {
app := constructClusterApp(argocdNs, clusterName, baseClusterName,
repoUrl, repoRevision, repoPath)
repoUrl, repoRevision, repoPath, overRiden)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename to overridden

) *argoappv1.Application {
clusterOverridden := "false"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is cleaner: clusterOverridden := fmt.Sprintf("%v", overridden)

pkg/cluster/delete.go Outdated Show resolved Hide resolved
pkg/cluster/delete.go Outdated Show resolved Hide resolved
pkg/cluster/delete.go Outdated Show resolved Hide resolved
pkg/cluster/git.go Show resolved Hide resolved
@jayanth-tjvrr jayanth-tjvrr temporarily deployed to arlon December 16, 2022 01:28 — with GitHub Actions Inactive
@jayanth-tjvrr jayanth-tjvrr requested a review from bcle December 16, 2022 01:30
Copy link
Collaborator

@bcle bcle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The functionality that copies / transforms the patch files from the patch directory to the destination git directory is a good candidate for a unit test. See if you can add a unit test for it.

@@ -126,3 +90,57 @@ func Delete(
}
return nil
}

func DeleteOverrideDir(appIf argoapp.ApplicationServiceClient, kubeClient *kubernetes.Clientset, argocdNs string, name string) error {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few nits:

  • Rename the function to DeleteOverridesDir()
  • Rename name to clusterName since it's not clear what thing we're talking about.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed both the fields

if err != nil {
return fmt.Errorf("failed to push to remote repository: %s", err)
return fmt.Errorf("failed to delete the override directory: %s", err)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"overrides directory"

clusterQuery := "arlon-cluster=" + name
apps, err := appIf.List(context.Background(),
&argoapp.ApplicationQuery{Selector: &clusterQuery})
if err != nil {
return fmt.Errorf("failed to list apps related to cluster: %s", err)
}

app, err := appIf.Get(context.Background(),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's unnecessary to get the "cluster-app" app here, because line 53 already gets all apps related to this cluster, and lines 77-92 iterate over those apps. So you should insert code in that loop to catch the one of type cluster-app, and then handle it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed the unnecessary calling of appIf.List again and used the loop in lines 77-92 to do the same


func DeleteOverrideDir(appIf argoapp.ApplicationServiceClient, kubeClient *kubernetes.Clientset, argocdNs string, name string) error {
log := logpkg.GetLogger()
app, err := appIf.Get(context.Background(),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function should not need to get the app again. The caller already has a copy of the app. Pass it by pointer.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passed the app pointer instead of appIf

return fmt.Errorf("failed to get argocd application: %s", err)
}
typ := app.Labels["arlon-type"]
if typ == "cluster-app" {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no need to check the type if we assume the caller has already done so. You can:

  • Not check
  • Still check, and return an error if it's not of the expected type.

pkg/cluster/delete.go Show resolved Hide resolved
@@ -15,6 +15,7 @@ type BaseClusterInfo struct {
RepoUrl string
RepoRevision string
RepoPath string
OverRidden string
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename to overridden

src, err := os.OpenFile(newFilePath, os.O_RDONLY, os.ModePerm)
if err != nil {
_ = src.Close()
return fmt.Errorf("failed to open embedded file %s: %s", filePath, err)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this say embedded? Should it say patch file instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

renamed it to patch file

kustomFile, err := ioutil.ReadFile(newFilePath)
if err != nil {
_ = src.Close()
return fmt.Errorf("Failed to read the embedded file %s", err)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment about "embedded" terminology.

pkg/gitutils/manifests.go Show resolved Hide resolved
@jayanth-tjvrr jayanth-tjvrr temporarily deployed to arlon December 16, 2022 02:43 — with GitHub Actions Inactive
@jayanth-tjvrr jayanth-tjvrr requested a review from bcle December 16, 2022 02:43
@jayanth-tjvrr jayanth-tjvrr temporarily deployed to arlon December 16, 2022 02:54 — with GitHub Actions Inactive
Copy link
Collaborator

@bcle bcle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some missing error checks.

pkg/cluster/delete.go Show resolved Hide resolved
pkg/cluster/delete.go Show resolved Hide resolved
pkg/cluster/delete.go Show resolved Hide resolved
@jayanth-tjvrr jayanth-tjvrr temporarily deployed to arlon December 16, 2022 03:20 — with GitHub Actions Inactive
@jayanth-tjvrr jayanth-tjvrr requested a review from bcle December 16, 2022 03:21
Copy link
Collaborator

@bcle bcle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more minor code change.

}
wt, err := repo.Worktree()
fileInfo, err := wt.Filesystem.Lstat(repoPath)
if err == nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's more elegant to check for err != nil and return early (see line 112). This way, lines 104-110 can be re-indented and aligned with the rest of the function body.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I'm checking for err!=nil case first now

@jayanth-tjvrr jayanth-tjvrr temporarily deployed to arlon December 16, 2022 07:07 — with GitHub Actions Inactive
@jayanth-tjvrr jayanth-tjvrr requested a review from bcle December 16, 2022 07:09
@jayanth-tjvrr jayanth-tjvrr merged commit 0fc1274 into main Dec 16, 2022
cruizen pushed a commit that referenced this pull request Dec 16, 2022
* Added overrides support for gen2 clusters

* refactored the code to make the override feature work on existing bc design

* Added a check if the cluster already exists before creating patch file

* Added documentation for clusteroverides design proposal

* Added override in user facing docs(gen2_Tutorial.md)

* Addressed all the reviews(Cleared confusion in patch revision and patch repo path)

* Edited the desc for patchreporevision

* Addressed the reviews(Code refactoring)

* Removed unnecessary code

* renamed overriden

* Handled the errors in delete function

* Refactored the code

Co-authored-by: Jayanth Reddy <[email protected]>
@Rohitrajak1807 Rohitrajak1807 deleted the private/main/jayanth-reddy/clusteroverride-gen2clusters branch December 22, 2022 17:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

gen2 clusters don't support overrides (basecluster is cloned verbatim)
2 participants