Skip to content

Commit

Permalink
Adds support for ARGO_CD_[TARGET_REVISION|REVISION] and pass to Custo…
Browse files Browse the repository at this point in the history
…m Tool/Helm/Jsonnet (argoproj#2415)
  • Loading branch information
alexec authored Oct 21, 2019
1 parent bbfb96c commit 5706a17
Show file tree
Hide file tree
Showing 30 changed files with 462 additions and 151 deletions.
17 changes: 17 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,20 @@ release-precheck: manifests

.PHONY: release
release: pre-commit release-precheck image release-cli

.PHONY: build-docs
build-docs:
mkdocs build

.PHONY: serve-docs
serve-docs:
mkdocs serve

.PHONY: lint-docs
lint-docs:
# https://github.com/dkhamsing/awesome_bot
find docs -name '*.md' -exec grep -l http {} + | xargs docker run --rm -v $(PWD):/mnt:ro dkhamsing/awesome_bot -t 3 --allow-dupe --allow-redirect --white-list `cat white-list | grep -v "#" | tr "\n" ','` --skip-save-results --

.PHONY: publish-docs
publish-docs: lint-docs
mkdocs gh-deploy
21 changes: 19 additions & 2 deletions cmd/argocd/commands/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,10 @@ func setAppSpecOptions(flags *pflag.FlagSet, spec *argoappv1.ApplicationSpec, ap
setJsonnetOpt(&spec.Source, appOpts.jsonnetTlaStr, false)
case "jsonnet-tla-code":
setJsonnetOpt(&spec.Source, appOpts.jsonnetTlaCode, true)
case "jsonnet-ext-var-str":
setJsonnetOptExtVar(&spec.Source, appOpts.jsonnetExtVarStr, false)
case "jsonnet-ext-var-code":
setJsonnetOptExtVar(&spec.Source, appOpts.jsonnetExtVarCode, true)
case "sync-policy":
switch appOpts.syncPolicy {
case "automated":
Expand Down Expand Up @@ -645,7 +649,15 @@ func setJsonnetOpt(src *argoappv1.ApplicationSource, tlaParameters []string, cod
if src.Directory.IsZero() {
src.Directory = nil
}
}

func setJsonnetOptExtVar(src *argoappv1.ApplicationSource, jsonnetExtVar []string, code bool) {
if src.Directory == nil {
src.Directory = &argoappv1.ApplicationSourceDirectory{}
}
for _, j := range jsonnetExtVar {
src.Directory.Jsonnet.ExtVars = append(src.Directory.Jsonnet.ExtVars, argoappv1.NewJsonnetVar(j, code))
}
}

type appOptions struct {
Expand All @@ -671,6 +683,8 @@ type appOptions struct {
configManagementPlugin string
jsonnetTlaStr []string
jsonnetTlaCode []string
jsonnetExtVarStr []string
jsonnetExtVarCode []string
kustomizeImages []string
}

Expand All @@ -697,6 +711,8 @@ func addAppFlags(command *cobra.Command, opts *appOptions) {
command.Flags().StringVar(&opts.configManagementPlugin, "config-management-plugin", "", "Config management plugin name")
command.Flags().StringArrayVar(&opts.jsonnetTlaStr, "jsonnet-tla-str", []string{}, "Jsonnet top level string arguments")
command.Flags().StringArrayVar(&opts.jsonnetTlaCode, "jsonnet-tla-code", []string{}, "Jsonnet top level code arguments")
command.Flags().StringArrayVar(&opts.jsonnetExtVarStr, "jsonnet-ext-var-str", []string{}, "Jsonnet string ext var")
command.Flags().StringArrayVar(&opts.jsonnetExtVarCode, "jsonnet-ext-var-code", []string{}, "Jsonnet ext var")
command.Flags().StringArrayVar(&opts.kustomizeImages, "kustomize-image", []string{}, "Kustomize images (e.g. --kustomize-image node:8.15.0 --kustomize-image mysql=mariadb,alpine@sha256:24a0c4b4a4c0eb97a1aabb8e29f18e917d05abfe1b7a7c07857230879ce7d3d)")
}

Expand Down Expand Up @@ -815,11 +831,12 @@ func getLocalObjects(app *argoappv1.Application, local, appLabelKey, kubeVersion
}

func getLocalObjectsString(app *argoappv1.Application, local, appLabelKey, kubeVersion string, kustomizeOptions *argoappv1.KustomizeOptions) []string {
res, err := repository.GenerateManifests(local, &repoapiclient.ManifestRequest{
ApplicationSource: &app.Spec.Source,
res, err := repository.GenerateManifests(local, app.Spec.Source.TargetRevision, &repoapiclient.ManifestRequest{
Repo: &argoappv1.Repository{Repo: app.Spec.Source.RepoURL},
AppLabelKey: appLabelKey,
AppLabelValue: app.Name,
Namespace: app.Spec.Destination.Namespace,
ApplicationSource: &app.Spec.Source,
KustomizeOptions: kustomizeOptions,
KubeVersion: kubeVersion,
})
Expand Down
5 changes: 2 additions & 3 deletions docs/developer-guide/api-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@ $ curl $ARGOCD_SERVER/api/v1/applications --cookie "argocd.token=$ARGOCD_TOKEN"
{"metadata":{"selfLink":"/apis/argoproj.io/v1alpha1/namespaces/argocd/applications","resourceVersion":"37755"},"items":...}
```

> >v1.3
> v1.3
Then pass using the HTTP `Authorization` header, prefixing with `Bearer `:

```bash
$ curl $ARGOCD_SERVER/api/v1/applications -H "Authorization: Bearer $ARGOCD_TOKEN"
{"metadata":{"selfLink":"/apis/argoproj.io/v1alpha1/namespaces/argocd/applications","resourceVersion":"37755"},"items":...}
```

You sh

6 changes: 3 additions & 3 deletions docs/developer-guide/site.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ The web site is build using `mkdocs` and `mkdocs-material`.
To test:

```bash
mkdocs serve
make serve-docs
```

Check for broken external links:

```bash
find docs -name '*.md' -exec grep -l http {} + | xargs awesome_bot -t 3 --allow-dupe --allow-redirect -w argocd.example.com:443,argocd.example.com,kubernetes.default.svc:443,kubernetes.default.svc,mycluster.com,https://github.com/argoproj/my-private-repository,192.168.0.20,storage.googleapis.com,localhost:8080,localhost:6443,your-kubernetes-cluster-addr,10.97.164.88 --skip-save-results --
make lint-docs
```

## Deploying

```bash
mkdocs gh-deploy
make publish-docs
```

## Analytics
Expand Down
2 changes: 1 addition & 1 deletion docs/user-guide/application_sources.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Argo CD supports several different ways in which Kubernetes manifests can be def
* [Kustomize](kustomize.md) applications
* [Helm](helm.md) charts
* [Ksonnet](ksonnet.md) applications
* A directory of YAML/JSON/Jsonnet manifests
* A directory of YAML/JSON/Jsonnet manifests, including [Jsonnet](jsonnet.md).
* Any [custom config management tool](config-management-plugins.md) configured as a config management plugin

## Development
Expand Down
10 changes: 10 additions & 0 deletions docs/user-guide/build-environment.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Build Environment

[Custom tools](config-management-plugins.md), [Helm](helm.md), and [Jsonnet](jsonnet.md) support the following build env vars:

* `ARGOCD_APP_NAME` - name of application
* `ARGOCD_APP_NAMESPACE` - destination application namespace.
* `ARGOCD_APP_REVISION` - the resolved revision, e.g. `f913b6cbf58aa5ae5ca1f8a2b149477aebcbd9d8`
* `ARGOCD_APP_SOURCE_PATH` - the path of the app within the repo
* `ARGOCD_APP_SOURCE_REPO_URL` the repo's URL
* `ARGOCD_APP_SOURCE_TARGET_REVISION` - the target revision from the spec, e.g. `master`.
10 changes: 3 additions & 7 deletions docs/user-guide/config-management-plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,9 @@ More config management plugin examples are available in [argocd-example-apps](ht

Commands have access to

(1) The system environment variables
(2) Argo CD environment variables:

* `ARGOCD_APP_NAME` - name of application
* `ARGOCD_APP_NAMESPACE` - destination application namespace.

(3) Variables in the application spec:
1. The system environment variables
2. [Standard build environment](build-environment.md)
3. Variables in the application spec:

> v1.2

Expand Down
22 changes: 22 additions & 0 deletions docs/user-guide/helm.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,25 @@ value, in the values.yaml such that the value is stable between each comparison.
```bash
argocd app set redis -p password=abc123
```

## Build Environment

Helm apps have access to the [standard build environment](build-environment.md) via substitution as parameters.

E.g. via the CLI:

```bash
argocd app create APPNAME \
--helm-set-string 'app=${ARGOCD_APP_NAME}'
```

Or via declarative syntax:

```yaml
spec:
source:
helm:
parameters:
- name: app
value: $ARGOCD_APP_NAME
```
28 changes: 28 additions & 0 deletions docs/user-guide/jsonnet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Jsonnet

Any file matching `*.jsonnet` in a directory app is treated as a Jsonnet file.

## Build Environment

Jsonnet apps have access to the [standard build environment](build-environment.md) via substitution into *TLAs* and *external variables*.

E.g. via the CLI:

```bash
argocd app create APPNAME \
--jsonnet-ext-str 'app=${ARGOCD_APP_NAME}' \
--jsonnet-tla-str 'ns=${ARGOCD_APP_NAMESPACE}'
```

Or by declarative syntax:

```yaml
directory:
jsonnet:
extVars:
- name: app
value: $ARGOCD_APP_NAME
tlas:
- name: ns
value: $ARGOCD_APP_NAMESPACE
```
4 changes: 4 additions & 0 deletions docs/user-guide/ksonnet.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Ksonnet

!!! tip Warning "Ksonnet is defunct and no longer supported."

## Environments
Ksonnet has a first class concept of an "environment." To create an application from a ksonnet
app directory, an environment must be specified. For example, the following command creates the
Expand Down Expand Up @@ -32,4 +34,6 @@ When overriding ksonnet parameters in Argo CD, the component name should also be
argocd app set guestbook-default -p guestbook-ui=image=gcr.io/heptio-images/ks-guestbook-demo:0.1
```

## Build Environment

We do not support the [standard build environment](build-environment.md) for Ksonnet.
4 changes: 4 additions & 0 deletions docs/user-guide/kustomize.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@ metadata:
data:
kustomize.buildOptions: --load_restrictor none
```
## Build Environment
Kustomize does not support parameters and therefore cannot support the standard [build environment](build-environment.md).
2 changes: 2 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ nav:
- user-guide/kustomize.md
- user-guide/helm.md
- user-guide/ksonnet.md
- user-guide/jsonnet.md
- user-guide/config-management-plugins.md
- user-guide/tool_detection.md
- user-guide/projects.md
Expand All @@ -57,6 +58,7 @@ nav:
- user-guide/compare-options.md
- user-guide/sync-options.md
- user-guide/parameters.md
- user-guide/build-environment.md
- user-guide/tracking_strategies.md
- user-guide/resource_hooks.md
- user-guide/selective_sync.md
Expand Down
20 changes: 20 additions & 0 deletions pkg/apis/application/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,17 @@ func (e Env) Environ() []string {
return environ
}

// does an operation similar to `envstubst` tool,
// but unlike envsubst it does not change missing names into empty string
// see https://linux.die.net/man/1/envsubst
func (e Env) Envsubst(s string) string {
for _, v := range e {
s = strings.ReplaceAll(s, fmt.Sprintf("$%s", v.Name), v.Value)
s = strings.ReplaceAll(s, fmt.Sprintf("${%s}", v.Name), v.Value)
}
return s
}

// ApplicationSource contains information about github repository, path within repository and target application environment.
type ApplicationSource struct {
// RepoURL is the repository URL of the application manifests
Expand Down Expand Up @@ -277,6 +288,15 @@ type JsonnetVar struct {
Code bool `json:"code,omitempty" protobuf:"bytes,3,opt,name=code"`
}

func NewJsonnetVar(s string, code bool) JsonnetVar {
parts := strings.SplitN(s, "=", 2)
if len(parts) == 2 {
return JsonnetVar{Name: parts[0], Value: parts[1], Code: code}
} else {
return JsonnetVar{Name: s, Code: code}
}
}

// ApplicationSourceJsonnet holds jsonnet specific options
type ApplicationSourceJsonnet struct {
// ExtVars is a list of Jsonnet External Variables
Expand Down
14 changes: 14 additions & 0 deletions pkg/apis/application/v1alpha1/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,13 @@ func TestEnv_IsZero(t *testing.T) {
}
}

func TestEnv_Envsubst(t *testing.T) {
env := Env{&EnvEntry{"FOO", "bar"}}
assert.Equal(t, "", env.Envsubst(""))
assert.Equal(t, "bar", env.Envsubst("$FOO"))
assert.Equal(t, "bar", env.Envsubst("${FOO}"))
}

func TestEnv_Environ(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -1442,6 +1449,13 @@ func newTestApp() *Application {
return a
}

func TestNewJsonnetVar(t *testing.T) {
assert.Equal(t, JsonnetVar{}, NewJsonnetVar("", false))
assert.Equal(t, JsonnetVar{Name: "a"}, NewJsonnetVar("a=", false))
assert.Equal(t, JsonnetVar{Name: "a", Code: true}, NewJsonnetVar("a=", true))
assert.Equal(t, JsonnetVar{Name: "a", Value: "b", Code: true}, NewJsonnetVar("a=b", true))
}

func testCond(t ApplicationConditionType, msg string, lastTransitionTime *metav1.Time) ApplicationCondition {
return ApplicationCondition{
Type: t,
Expand Down
Loading

0 comments on commit 5706a17

Please sign in to comment.