Skip to content

Commit

Permalink
Add infrastructure id and type to the reported data
Browse files Browse the repository at this point in the history
  • Loading branch information
Sami Alajrami committed Sep 22, 2021
1 parent b20d9f5 commit 1570f88
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
14 changes: 14 additions & 0 deletions cmd/reporter/ecsEnv.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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, "", " ")

Expand All @@ -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
}
17 changes: 12 additions & 5 deletions cmd/reporter/k8sEnv.go
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -40,6 +40,7 @@ type k8sEnvOptions struct {
kubeconfig string
namespaces []string
excludeNamespaces []string
id string
}

func newK8sEnvCmd(out io.Writer) *cobra.Command {
Expand All @@ -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,
Expand All @@ -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 {
Expand All @@ -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, "", " ")

Expand All @@ -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
}
4 changes: 4 additions & 0 deletions internal/requests/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 1570f88

Please sign in to comment.