Skip to content

Commit

Permalink
Merge pull request #21 from codefresh-io/fix_docs
Browse files Browse the repository at this point in the history
doc fixes
  • Loading branch information
ATGardner authored Apr 29, 2021
2 parents c3f2ae5 + e0c6fa3 commit 05faeb3
Show file tree
Hide file tree
Showing 15 changed files with 161 additions and 65 deletions.
7 changes: 7 additions & 0 deletions .codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
coverage:
status:
patch: off
project:
default:
# allow test coverage to drop by 2%
threshold: 2
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ argocd-autopilot version
```

## Getting Started
```
```bash
# Most of the commands need your git token, you can provide with --token to each command
# or export it beforehand:

Expand Down
50 changes: 43 additions & 7 deletions cmd/commands/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/argoproj/argocd-autopilot/pkg/store"
"github.com/argoproj/argocd-autopilot/pkg/util"

argocdv1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
"github.com/ghodss/yaml"
memfs "github.com/go-git/go-billy/v5/memfs"
billyUtils "github.com/go-git/go-billy/v5/util"
Expand Down Expand Up @@ -130,16 +131,24 @@ func RunAppCreate(ctx context.Context, opts *AppCreateOptions) error {
if err != nil {
return err
}

log.G().Infof("using revision: \"%s\", installation path: \"%s\"", opts.CloneOptions.Revision, opts.FS.Root())

if !opts.FS.ExistsOrDie(store.Default.BootsrtrapDir) {
return fmt.Errorf(util.Doc("Bootstrap folder not found, please execute `<BIN> repo bootstrap --installation-path %s` command"), opts.FS.Root())
}

projectExists := opts.FS.ExistsOrDie(opts.FS.Join(store.Default.ProjectsDir, opts.ProjectName+".yaml"))
if !projectExists {
return fmt.Errorf(util.Doc("project '%[1]s' not found, please execute `<BIN> project create %[1]s`"), opts.ProjectName)
if opts.AppOpts.DestServer == store.Default.DestServer {
opts.AppOpts.DestServer, err = getProjectDestServer(opts.FS, opts.ProjectName)
if err != nil {
return err
}
}

if opts.AppOpts.DestNamespace == "" {
opts.AppOpts.DestNamespace = "default"
}

log.G().Debug("repository is ok")

app, err := opts.AppOpts.Parse()
Expand All @@ -159,6 +168,7 @@ func RunAppCreate(ctx context.Context, opts *AppCreateOptions) error {
if err = r.Persist(ctx, &git.PushOptions{CommitMsg: getCommitMsg(opts)}); err != nil {
return fmt.Errorf("failed to push to repo: %w", err)
}

log.G().Infof("installed application: %s", opts.AppOpts.AppName)

return nil
Expand Down Expand Up @@ -193,6 +203,7 @@ func createApplicationFiles(repoFS fs.FS, app application.Application, projectNa
if err != nil {
return fmt.Errorf("failed to marshal app overlay kustomization: %w", err)
}

if exists, err := writeApplicationFile(repoFS, overlayKustomizationPath, "overlay", overlayKustomizationYAML); err != nil {
return err
} else if exists {
Expand Down Expand Up @@ -225,6 +236,7 @@ func createApplicationFiles(repoFS fs.FS, app application.Application, projectNa
if err != nil {
return fmt.Errorf("failed to marshal app config.json: %w", err)
}

if _, err = writeApplicationFile(repoFS, configPath, "config", config); err != nil {
return err
}
Expand Down Expand Up @@ -260,6 +272,7 @@ func writeApplicationFile(repoFS fs.FS, path, name string, data []byte) (bool, e
log.G().Infof("'%s' file exists in '%s'", name, absPath)
return true, nil
}

log.G().Infof("created '%s' file at '%s'", name, absPath)
return false, nil
}
Expand All @@ -269,8 +282,29 @@ func getCommitMsg(opts *AppCreateOptions) string {
if opts.CloneOptions.RepoRoot != "" {
commitMsg += fmt.Sprintf(" installation-path: '%s'", opts.CloneOptions.RepoRoot)
}

return commitMsg
}

var getProjectDestServer = func(repofs fs.FS, projectName string) (string, error) {
f, err := repofs.Open(repofs.Join(store.Default.ProjectsDir, projectName+".yaml"))
if err != nil {
return "", err
}

d, err := ioutil.ReadAll(f)
if err != nil {
return "", fmt.Errorf("failed to read namespace file: %w", err)
}

p := &argocdv1alpha1.AppProject{}
if err = yaml.Unmarshal(d, p); err != nil {
return "", fmt.Errorf("failed to unmarshal project: %w", err)
}

return p.Annotations[store.Default.DestServerAnnotation], nil
}

func NewAppListCommand() *cobra.Command {
var (
projectName string
Expand Down Expand Up @@ -316,7 +350,6 @@ func NewAppListCommand() *cobra.Command {
}

func RunAppList(ctx context.Context, opts *AppListOptions) error {

var (
err error
)
Expand Down Expand Up @@ -351,6 +384,7 @@ func RunAppList(ctx context.Context, opts *AppListOptions) error {
if err != nil {
log.G().Fatalf("failed to run glob on %s", opts.ProjectName)
}

w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
_, _ = fmt.Fprintf(w, "PROJECT\tNAME\tDEST_NAMESPACE\tDEST_SERVER\t\n")

Expand All @@ -363,26 +397,28 @@ func RunAppList(ctx context.Context, opts *AppListOptions) error {

fmt.Fprintf(w, "%s\t%s\t%s\t%s\t\n", opts.ProjectName, conf.UserGivenName, conf.DestNamespace, conf.DestServer)
}

_ = w.Flush()
return nil

}
func getConfigFileFromPath(fs fs.FS, appName string) (*application.Config, error) {

func getConfigFileFromPath(fs fs.FS, appName string) (*application.Config, error) {
confFileName := fmt.Sprintf("%s/config.json", appName)
file, err := fs.Open(confFileName)
if err != nil {
return nil, fmt.Errorf("%s not found", confFileName)
}

b, err := ioutil.ReadAll(file)
if err != nil {
return nil, fmt.Errorf("failed to read file %s", confFileName)
}

conf := application.Config{}
err = json.Unmarshal(b, &conf)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal file %s", confFileName)
}
return &conf, nil

return &conf, nil
}
16 changes: 16 additions & 0 deletions cmd/commands/assets/kustomization_readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Kustomizations
This directory contains all of the applications you installed by using:
```bash
argocd-autopilot app create <APP_NAME> --app <APP_SPECIFIER> -p <PROJECT_NAME>
```

Every application you install has <u>exactly one</u>: `kustomize/<APP_NAME>/base/kustomization.yaml` and one or more `kustomize/<APP_NAME>/overlays/<PROJECT_NAME>/kustomization.yaml` files.

The `kustomize/<APP_NAME>/base/kustomization.yaml` file is created the first time you create the application. The `kustomize/<APP_NAME>/overlays/<PROJECT_NAME>/kustomization.yaml` is created for each project you install this application on. So all overlays of the same application are using the same base `kustomization.yaml`.

## Example:
Try running the following command:
```bash
argocd-autopilot app create hello-world --app github.com/argoproj-labs/argocd-autopilot/examples/demo-app/ -p <PROJECT_NAME>
```
###### * If you did not create a project yet take a look at: [creating a project](https://argocd-autopilot.readthedocs.io/en/stable/Getting-Started/#add-a-project-and-an-application).
21 changes: 21 additions & 0 deletions cmd/commands/assets/projects_readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Projects
This directory contains all of your `argocd-autopilot` projects. Projects provide a way to logically group applications and easily contol things such as defaults and restrictions.

### Creating a new project
To create a new project run:
```bash
export GIT_TOKEN=<YOUR_TOKEN>
export GIT_REPO=<REPO_URL>

argocd-autopilot project create <PROJECT_NAME>
```

### Creating a new project on different cluster
You can create a project that deploys applications to a different cluster, instead of the cluster where Argo-CD is installed. To do that run:
```bash
export GIT_TOKEN=<YOUR_TOKEN>
export GIT_REPO=<REPO_URL>

argocd-autopilot project create <PROJECT_NAME> --dest-kube-context <CONTEXT_NAME>
```
Now all applications in this project that do not explicitly specify a different `--dest-server` will be created on the project's destination server.
17 changes: 5 additions & 12 deletions cmd/commands/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ func NewProjectCreateCommand() *cobra.Command {
# Create a new project
<BIN> project create <new_project_name>
<BIN> project create <PROJECT_NAME>
# Create a new project in a specific path inside the GitOps repo
<BIN> project create <new_project_name> --installation-path path/to/bootstrap/root
<BIN> project create <PROJECT_NAME> --installation-path path/to/bootstrap/root
`),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
Expand Down Expand Up @@ -220,7 +220,8 @@ var generateProject = func(o *GenerateProjectOptions) (*argocdv1alpha1.AppProjec
Name: o.Name,
Namespace: o.Namespace,
Annotations: map[string]string{
"argocd.argoproj.io/sync-options": "PruneLast=true",
"argocd.argoproj.io/sync-options": "PruneLast=true",
store.Default.DestServerAnnotation: o.DefaultDestServer,
},
},
Spec: argocdv1alpha1.AppProjectSpec{
Expand Down Expand Up @@ -267,22 +268,14 @@ var generateProject = func(o *GenerateProjectOptions) (*argocdv1alpha1.AppProjec
Path: filepath.Join(o.InstallationPath, "kustomize", "**", "overlays", o.Name, "config.json"),
},
},
Template: appset.ApplicationSetTemplate{
Spec: appsetv1alpha1.ApplicationSpec{
Destination: appsetv1alpha1.ApplicationDestination{
Server: o.DefaultDestServer,
Namespace: "default",
},
},
},
RequeueAfterSeconds: &DefaultApplicationSetGeneratorInterval,
},
},
},
Template: appset.ApplicationSetTemplate{
ApplicationSetTemplateMeta: appset.ApplicationSetTemplateMeta{
Namespace: o.Namespace,
Name: "{{userGivenName}}",
Name: fmt.Sprintf("%s-{{userGivenName}}", o.Name),
Labels: map[string]string{
"app.kubernetes.io/managed-by": store.Default.ManagedBy,
"app.kubernetes.io/name": "{{appName}}",
Expand Down
3 changes: 2 additions & 1 deletion cmd/commands/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
fsmocks "github.com/argoproj/argocd-autopilot/pkg/fs/mocks"
"github.com/argoproj/argocd-autopilot/pkg/git"
gitmocks "github.com/argoproj/argocd-autopilot/pkg/git/mocks"
"github.com/argoproj/argocd-autopilot/pkg/store"
"github.com/argoproj/argocd-autopilot/pkg/util"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -192,12 +193,12 @@ func Test_generateProject(t *testing.T) {
assert.Equal(tt.wantName, gotProject.Name, "Project Name")
assert.Equal(tt.wantNamespace, gotProject.Namespace, "Project Namespace")
assert.Equal(tt.wantProjectDescription, gotProject.Spec.Description, "Project Description")
assert.Equal(tt.o.DefaultDestServer, gotProject.Annotations[store.Default.DestServerAnnotation], "Application Set Default Destination Server")

assert.Equal(tt.wantName, gotAppSet.Name, "Application Set Name")
assert.Equal(tt.wantNamespace, gotAppSet.Namespace, "Application Set Namespace")
assert.Equal(tt.wantRepoURL, gotAppSet.Spec.Generators[0].Git.RepoURL, "Application Set Repo URL")
assert.Equal(tt.wantRevision, gotAppSet.Spec.Generators[0].Git.Revision, "Application Set Revision")
assert.Equal(tt.o.DefaultDestServer, gotAppSet.Spec.Generators[0].Git.Template.Spec.Destination.Server, "Application Set Default Destination Server")

assert.Equal(tt.wantNamespace, gotAppSet.Spec.Template.Namespace, "Application Set Template Repo URL")
assert.Equal(tt.wantName, gotAppSet.Spec.Template.Spec.Project, "Application Set Template Project")
Expand Down
18 changes: 15 additions & 3 deletions cmd/commands/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package commands

import (
"context"
_ "embed"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -32,6 +33,12 @@ const (
installationModeNormal = "normal"
)

//go:embed assets/projects_readme.md
var projectReadme []byte

//go:embed assets/kustomization_readme.md
var kustomizationReadme []byte

var supportedProviders = []string{"github"}

type (
Expand Down Expand Up @@ -583,7 +590,7 @@ func writeManifestsToRepo(repoFS fs.FS, manifests *bootstrapManifests, installat
}
}

// write envs root app
// write projects root app
if _, err = repoFS.WriteFile(repoFS.Join(store.Default.BootsrtrapDir, store.Default.RootAppName+".yaml"), manifests.rootApp); err != nil {
return err
}
Expand All @@ -593,8 +600,13 @@ func writeManifestsToRepo(repoFS fs.FS, manifests *bootstrapManifests, installat
return err
}

// write ./envs/Dummy
if _, err = repoFS.WriteFile(repoFS.Join(store.Default.ProjectsDir, store.Default.DummyName), []byte{}); err != nil {
// write ./projects/README.md
if _, err = repoFS.WriteFile(repoFS.Join(store.Default.ProjectsDir, "README.md"), projectReadme); err != nil {
return err
}

// write ./kustomize/README.md
if _, err = repoFS.WriteFile(repoFS.Join(store.Default.KustomizeDir, "README.md"), kustomizationReadme); err != nil {
return err
}

Expand Down
8 changes: 8 additions & 0 deletions docs/Development.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ This guide is meant for developers who want to contribute or debug `argocd-autop
6. Push the changes to the remote branch and create a new PR: `git push --set-upstream upstream <remote-branch-name>`.
7. If you need to get changes from the upstream repo, run: `git pull upstream main`.

### Adding documentation:
1. Fork the repository.
2. Clone it and add the upstream remote with: `git remote add upstream https://github.com/argoproj-labs/argocd-autopilot.git`.
3. Run `make serve-docs` to run a docker container that will server the docs on http://localhost:8000.
4. Edit existing docs in `/docs` directory or add new `X.md` files and add them to the `mkdocs.yml`.
5. When you're ready, push your changes to your fork and submit a PR.

### Releasing a new version:
1. Checkout to a release branch: `v0.X.X`.
2. Change the `VERSION` in the Makefile to match the new version.
Expand All @@ -20,6 +27,7 @@ This guide is meant for developers who want to contribute or debug `argocd-autop

### Using pre-commit:
With pre-commit installed and properly set-up, both the pre-commit and pre-push hooks will run automatically.

1. Install [pre-commit](https://pre-commit.com/#install) on your machine
2. Install the hooks in the repo folder: `pre-commit install -t pre-commit -t pre-push`
3. Enjoy
Expand Down
4 changes: 2 additions & 2 deletions docs/Getting-Started.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ This guide assumes you are familiar with Argo CD and its basic concepts. See the
* Have a [kubeconfig](https://kubernetes.io/docs/tasks/access-application-cluster/configure-access-multiple-clusters/) file (default location is `~/.kube/config`)

### Git authentication
Make sure to have a valid token (https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token)
Make sure to have a [valid token](https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token)
![Github token](assets/github_token.png)
```
export GIT_TOKEN=ghp_PcZ...IP0
Expand Down Expand Up @@ -62,7 +62,7 @@ Running Applications:
Execute the following commands to create a `testing` Project, and add a example Application to it:
```
argocd-autopilot project create testing
argocd-autopilot app create hello-world github.com/argoproj-labs/argocd-autopilot/examples/demo-app/ --p testing
argocd-autopilot app create hello-world --app github.com/argoproj-labs/argocd-autopilot/examples/demo-app/ -p testing
```
<sub>* notice the trailing slash in the URL</sub>

Expand Down
4 changes: 2 additions & 2 deletions docs/commands/argocd-autopilot_project_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ argocd-autopilot project create [PROJECT] [flags]
# Create a new project
argocd-autopilot project create <new_project_name>
argocd-autopilot project create <PROJECT_NAME>
# Create a new project in a specific path inside the GitOps repo
argocd-autopilot project create <new_project_name> --installation-path path/to/bootstrap/root
argocd-autopilot project create <PROJECT_NAME> --installation-path path/to/bootstrap/root
```

Expand Down
Loading

0 comments on commit 05faeb3

Please sign in to comment.