diff --git a/cmd/reporter/ecsEnv.go b/cmd/reporter/ecsEnv.go index 9863ebe19..524e4e80e 100644 --- a/cmd/reporter/ecsEnv.go +++ b/cmd/reporter/ecsEnv.go @@ -23,6 +23,7 @@ merkely report env ecs prod --api-token 1234 --owner exampleOrg type ecsEnvOptions struct { cluster string serviceName string + id string } func newEcsEnvCmd(out io.Writer) *cobra.Command { @@ -44,6 +45,15 @@ func newEcsEnvCmd(out io.Writer) *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { envName := args[0] + if o.id == "" { + if o.serviceName != "" { + o.id = o.serviceName + } else if o.cluster != "" { + o.id = o.cluster + } else { + o.id = envName + } + } url := fmt.Sprintf("%s/api/v1/environments/%s/%s/data", global.host, global.owner, envName) client, err := aws.NewAWSClient() if err != nil { @@ -56,6 +66,8 @@ func newEcsEnvCmd(out io.Writer) *cobra.Command { requestBody := &requests.EcsEnvRequest{ Data: tasksData, + Type: "ECS", + Id: o.id, } js, _ := json.MarshalIndent(requestBody, "", " ") @@ -66,6 +78,8 @@ func newEcsEnvCmd(out io.Writer) *cobra.Command { cmd.Flags().StringVarP(&o.cluster, "cluster", "C", "", "name of the ECS cluster") cmd.Flags().StringVarP(&o.serviceName, "service-name", "s", "", "name of the ECS service") + cmd.Flags().StringVarP(&o.id, "id", "i", "", "the unique identifier of the source infrastructure of the report (e.g. the ECS cluster/service name)."+ + "If not set, it is defaulted based on the following order: --service-name, --cluster, environment name.") return cmd } diff --git a/cmd/reporter/k8sEnv.go b/cmd/reporter/k8sEnv.go index 26227233c..bfccd0b97 100644 --- a/cmd/reporter/k8sEnv.go +++ b/cmd/reporter/k8sEnv.go @@ -20,7 +20,7 @@ and report them to Merkely. const k8sEnvExample = ` * report what's running in an entire cluster using kubeconfig at $HOME/.kube/config: -merkely report env k8s prod --api-token 1234 --owner exampleOrg +merkely report env k8s prod --api-token 1234 --owner exampleOrg --id prod-cluster * report what's running in an entire cluster using kubeconfig at $HOME/.kube/config (with global flags defined in environment or in a config file): @@ -40,6 +40,7 @@ type k8sEnvOptions struct { kubeconfig string namespaces []string excludeNamespaces []string + id string } func newK8sEnvCmd(out io.Writer) *cobra.Command { @@ -56,8 +57,8 @@ func newK8sEnvCmd(out io.Writer) *cobra.Command { o := new(k8sEnvOptions) cmd := &cobra.Command{ - Use: "k8s [-n namespace | -x namespace]... [-k /path/to/kube/config] env-name", - Short: "Report images data from specific namespace or entire cluster to Merkely.", + Use: "k8s [-n namespace | -x namespace]... [-k /path/to/kube/config] [-i infrastructure-identifier] env-name", + Short: "Report images data from specific namespace(s) or entire cluster to Merkely.", Long: k8sEnvDesc, Aliases: []string{"kubernetes"}, Example: k8sEnvExample, @@ -75,6 +76,9 @@ func newK8sEnvCmd(out io.Writer) *cobra.Command { return fmt.Errorf("--namespace and --exclude-namespace can't be used together. This can also happen if you set one of the two options in a config file or env var and the other on the command line") } envName := args[0] + if o.id == "" { + o.id = envName + } url := fmt.Sprintf("%s/api/v1/environments/%s/%s/data", global.host, global.owner, envName) clientset, err := kube.NewK8sClientSet(o.kubeconfig) if err != nil { @@ -87,6 +91,8 @@ func newK8sEnvCmd(out io.Writer) *cobra.Command { requestBody := &requests.K8sEnvRequest{ Data: podsData, + Type: "K8S", + Id: o.id, } js, _ := json.MarshalIndent(requestBody, "", " ") @@ -96,8 +102,9 @@ func newK8sEnvCmd(out io.Writer) *cobra.Command { } cmd.Flags().StringVarP(&o.kubeconfig, "kubeconfig", "k", defaultKubeConfigPath, "kubeconfig path for the target cluster") - cmd.Flags().StringSliceVarP(&o.namespaces, "namespace", "n", []string{}, "the comma separated list of namespaces (or namespaces regex patterns) to harvest artifacts info from. Can't be used together with --exclude-namespace.") - cmd.Flags().StringSliceVarP(&o.excludeNamespaces, "exclude-namespace", "x", []string{}, "the comma separated list of namespaces (or namespaces regex patterns) NOT to harvest artifacts info from. Can't be used together with --namespace.") + cmd.Flags().StringSliceVarP(&o.namespaces, "namespace", "n", []string{}, "the comma separated list of namespaces regex patterns to report artifacts info from. Can't be used together with --exclude-namespace.") + cmd.Flags().StringSliceVarP(&o.excludeNamespaces, "exclude-namespace", "x", []string{}, "the comma separated list of namespaces regex patterns NOT to report artifacts info from. Can't be used together with --namespace.") + cmd.Flags().StringVarP(&o.id, "id", "i", "", "the unique identifier of the source infrastructure of the report (e.g. the K8S cluster/namespace name). If not set, it is defaulted to environment name.") return cmd } diff --git a/internal/requests/requests.go b/internal/requests/requests.go index cdc428367..1d8c45a9a 100644 --- a/internal/requests/requests.go +++ b/internal/requests/requests.go @@ -20,11 +20,15 @@ type HTTPResponse struct { // K8sEnvRequest represents the PUT request body to be sent to merkely from k8s type K8sEnvRequest struct { Data []*kube.PodData `json:"data"` + Type string `json:"type"` + Id string `json:"id"` } // EcsEnvRequest represents the PUT request body to be sent to merkely from ECS type EcsEnvRequest struct { Data []*aws.EcsTaskData `json:"data"` + Type string `json:"type"` + Id string `json:"id"` } func getRetryableHttpClient(maxAPIRetries int) *http.Client {