From 2a02be4bba378178e48965df230a9b81640f67c2 Mon Sep 17 00:00:00 2001 From: Joni Collinge Date: Mon, 4 Jul 2022 10:19:41 +0100 Subject: [PATCH] require -k flag to target kubernetes for annotate command Signed-off-by: Joni Collinge --- README.md | 12 +++++++----- cmd/annotate.go | 16 +++++++++++----- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index bf6afde31..6d2d77325 100644 --- a/README.md +++ b/README.md @@ -630,9 +630,9 @@ 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 @@ -640,6 +640,8 @@ 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 `--` 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. @@ -647,13 +649,13 @@ All dapr annotations are available to set if a value is provided for the appropr 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. @@ -661,7 +663,7 @@ If the input contains multiple manifests then the command will search for the fi 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 diff --git a/cmd/annotate.go b/cmd/annotate.go index 31aa2990f..a6db78478 100644 --- a/cmd/annotate.go +++ b/cmd/annotate.go @@ -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 '--'. -------------------------------------------------------------------------------- `, 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) } @@ -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")