Skip to content

Commit

Permalink
require -k flag to target kubernetes for annotate command (#1016)
Browse files Browse the repository at this point in the history
Signed-off-by: Joni Collinge <jonathancollinge@live.com>

Co-authored-by: Mukundan Sundararajan <65565396+mukundansundar@users.noreply.github.com>
  • Loading branch information
jjcollinge and mukundansundar authored Jul 5, 2022
1 parent f58c920 commit 992b493
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -630,38 +630,40 @@ The default is `false`.

For more details, please run the command and check the examples to apply to your shell.

### Annotate a Kubernetes manifest
### Apply Dapr annotations

To add or modify dapr annotations on an existing Kubernetes manifest, use the `dapr annotate` command:
To add or modify dapr annotations on an existing Kubernetes manifest, use the `dapr annotate` command:

```bash
dapr annotate [flags] mydeployment.yaml
```

This will add the `dapr.io/enabled` and the `dapr.io/app-id` annotations. The dapr app id will be genereated using the format `<namespace>-<kind>-<name>` where the values are taken from the existing Kubernetes object metadata.

> NOTE: The annotate command currently only supports annotating Kubernetes manifests. You must provide the `-k` flag to target Kubernetes.
To provide your own dapr app id, provide the flag `--app-id`.

All dapr annotations are available to set if a value is provided for the appropriate flag on the `dapr annotate` command.

You can also provide the Kubernetes manifest via stdin:

```bash
kubectl get deploy mydeploy -o yaml | dapr annotate - | kubectl apply -f -
kubectl get deploy mydeploy -o yaml | dapr annotate -k - | kubectl apply -f -
```

Or you can provide the Kubernetes manifest via a URL:

```bash
dapr annotate --log-level debug https://raw.githubusercontent.com/dapr/quickstarts/master/tutorials/hello-kubernetes/deploy/node.yaml | kubectl apply -f -
dapr annotate -k --log-level debug https://raw.githubusercontent.com/dapr/quickstarts/master/tutorials/hello-kubernetes/deploy/node.yaml | kubectl apply -f -
```

If the input contains multiple manifests then the command will search for the first appropriate one to apply the annotations. If you'd rather it applied to a specific manifest then you can provide the `--resource` flag with the value set to the name of the object you'd like to apply the annotations to. If you have a conflict between namespaces you can also provide the namespace via the `--namespace` flag to isolate the manifest you wish to target.

If you want to annotate multiple manifests, you can chain together the `dapr annotate` commands with each applying the annotation to a specific manifest.

```bash
kubectl get deploy -o yaml | dapr annotate -r nodeapp --log-level debug - | dapr annotate --log-level debug -r pythonapp - | kubectl apply -f -
kubectl get deploy -o yaml | dapr annotate -k -r nodeapp --log-level debug - | dapr annotate -k --log-level debug -r pythonapp - | kubectl apply -f -
```

## Reference for the Dapr CLI
Expand Down
16 changes: 11 additions & 5 deletions cmd/annotate.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,24 +71,29 @@ var AnnotateCmd = &cobra.Command{
Short: "Add dapr annotations to a Kubernetes configuration. Supported platforms: Kubernetes",
Example: `
# Annotate the first deployment found in the input
kubectl get deploy -l app=node -o yaml | dapr annotate - | kubectl apply -f -
kubectl get deploy -l app=node -o yaml | dapr annotate -k - | kubectl apply -f -
# Annotate multiple deployments by name in a chain
kubectl get deploy -o yaml | dapr annotate -r nodeapp - | dapr annotate -r pythonapp - | kubectl apply -f -
kubectl get deploy -o yaml | dapr annotate -k -r nodeapp - | dapr annotate -k -r pythonapp - | kubectl apply -f -
# Annotate deployment in a specific namespace from file or directory by name
dapr annotate -r nodeapp -n namespace mydeploy.yaml | kubectl apply -f -
dapr annotate -k -r nodeapp -n namespace mydeploy.yaml | kubectl apply -f -
# Annotate deployment from url by name
dapr annotate -r nodeapp --log-level debug https://raw.githubusercontent.com/dapr/quickstarts/master/tutorials/hello-kubernetes/deploy/node.yaml | kubectl apply -f -
dapr annotate -k -r nodeapp --log-level debug https://raw.githubusercontent.com/dapr/quickstarts/master/tutorials/hello-kubernetes/deploy/node.yaml | kubectl apply -f -
--------------------------------------------------------------------------------
WARNING: If an app id is not provided, we will generate one using the format '<namespace>-<kind>-<name>'.
--------------------------------------------------------------------------------
`,
Run: func(cmd *cobra.Command, args []string) {
if !kubernetesMode {
print.FailureStatusEvent(os.Stderr, "annotate command is only supported for Kubernetes, please provide the -k flag")
os.Exit(1)
}

if len(args) < 1 {
print.FailureStatusEvent(os.Stderr, "please specify a kubernetes resource file")
print.FailureStatusEvent(os.Stderr, "please specify a Kubernetes resource file")
os.Exit(1)
}

Expand Down Expand Up @@ -321,6 +326,7 @@ func getOptionsFromFlags() kubernetes.AnnotateOptions {
}

func init() {
AnnotateCmd.Flags().BoolVarP(&kubernetesMode, "kubernetes", "k", false, "Apply annotations to Kubernetes resources")
AnnotateCmd.Flags().StringVarP(&annotateTargetResource, "resource", "r", "", "The resource to target to annotate")
AnnotateCmd.Flags().StringVarP(&annotateTargetNamespace, "namespace", "n", "", "The namespace the resource target is in (can only be set if --resource is also set)")
AnnotateCmd.Flags().StringVarP(&annotateAppID, "app-id", "a", "", "The app id to annotate")
Expand Down

0 comments on commit 992b493

Please sign in to comment.