From fadb7c5d59f3d9e2ea3cdf9eb1ba1e397679b645 Mon Sep 17 00:00:00 2001 From: Srevin Saju Date: Thu, 30 Mar 2023 23:34:19 +0530 Subject: [PATCH 1/2] feat: add label feature --- cmd/go-cat/infra.go | 13 +++++++++ cmd/go-cat/main.go | 2 ++ config/base.go | 4 +++ infrastructure/clouds/meta.go | 3 +- infrastructure/clouds/togomak.go | 48 ++++++++++++++++++++++++++++++++ infrastructure/types.go | 4 +++ ops/push.go | 2 +- parser/markdown.go | 18 ++++++++++-- 8 files changed, 89 insertions(+), 5 deletions(-) create mode 100644 infrastructure/clouds/togomak.go diff --git a/cmd/go-cat/infra.go b/cmd/go-cat/infra.go index cc40fd3..96ee967 100644 --- a/cmd/go-cat/infra.go +++ b/cmd/go-cat/infra.go @@ -24,7 +24,19 @@ func parseCliParameters(p string) map[string]interface{} { m[strings.TrimSpace(keyValue[0])] = strings.TrimSpace(keyValue[1]) } return m +} +func parseCliParametersKV(p string) map[string]string { + k := strings.Split(p, ",") + m := map[string]string{} + for i := range k { + if k[i] == "" { + continue + } + keyValue := strings.SplitN(k[i], "=", 2) + m[strings.TrimSpace(keyValue[0])] = strings.TrimSpace(keyValue[1]) + } + return m } func parseDeploymentLinks(p string) []string { @@ -47,6 +59,7 @@ func newInfrastructureFromCliContext(context *cli.Context) *infrastructure.Metad MonitoringLinks: parseDeploymentLinks(context.String("monitoring-links")), LoggingLinks: parseDeploymentLinks(context.String("logging-links")), Parameters: parseCliParameters(context.String("parameters")), + Labels: parseCliParametersKV(context.String("labels")), } return infra diff --git a/cmd/go-cat/main.go b/cmd/go-cat/main.go index b189c0f..32f39f5 100644 --- a/cmd/go-cat/main.go +++ b/cmd/go-cat/main.go @@ -22,6 +22,8 @@ func main() { &cli.StringFlag{Name: "git.url", Usage: "URL to the git repository", EnvVars: []string{meta.GitUrlEnvVar}}, &cli.StringFlag{Name: "git.username", Usage: "Username, if the Git repository requires HTTP Auth", EnvVars: []string{meta.GitUsernameEnvVar}}, &cli.StringFlag{Name: "git.password", Usage: "Password, if the Git repository requires HTTP Auth", EnvVars: []string{meta.GitPasswordEnvVar}}, + + &cli.StringFlag{Name: "title", Required: false, Usage: "Title of the catalog page"}, } infraFlags := []cli.Flag{ &cli.StringFlag{Name: "name", Usage: "Name of the service or endpoint"}, diff --git a/config/base.go b/config/base.go index aea9ceb..5e39407 100644 --- a/config/base.go +++ b/config/base.go @@ -15,6 +15,8 @@ type GlobalConfig struct { // GitUsername and GitPassword is optional, if the repository is private, it will be required GitUsername string GitPassword string + + Title string } func NewGlobalConfigFromCliContext(context *cli.Context) GlobalConfig { @@ -22,5 +24,7 @@ func NewGlobalConfigFromCliContext(context *cli.Context) GlobalConfig { GitRepository: context.String("git.url"), GitUsername: context.String("git.username"), GitPassword: context.String("git.password"), + + Title: context.String("title"), } } diff --git a/infrastructure/clouds/meta.go b/infrastructure/clouds/meta.go index 72fcb23..e65d8c4 100644 --- a/infrastructure/clouds/meta.go +++ b/infrastructure/clouds/meta.go @@ -11,7 +11,8 @@ import ( ) var Meta = map[string]Metadata{ - "GCP": googleInfrastructureMetadata, + "GCP": googleInfrastructureMetadata, + "togomak": togomakInfrastructureMetadata, } func getInfraMetaFromCloud(infra infrastructure.Metadata) *TypeMetadata { diff --git a/infrastructure/clouds/togomak.go b/infrastructure/clouds/togomak.go new file mode 100644 index 0000000..3a3d846 --- /dev/null +++ b/infrastructure/clouds/togomak.go @@ -0,0 +1,48 @@ +// Copyright 2021 Sorcero, Inc. +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +package clouds + +import ( + "fmt" + "gitlab.com/sorcero/community/go-cat/infrastructure" + "time" +) + +var togomakInfrastructureMetadata = Metadata{ + Id: "togomak", + Name: "Togomak", + Types: []*TypeMetadata{ + { + Id: "togomak.srev.in/release", + Name: "Release", + GetLoggingLink: func(m infrastructure.Metadata) string { + stage := m.Name + overrideStage, ok := m.Parameters["togomak.srev.in/v1/stage.id"] + if ok && overrideStage.(string) != "" { + stage = overrideStage.(string) + } + instanceID := "" + overrideInstanceId, ok := m.Parameters["togomak.srev.in/v1/instance.id"] + if ok && overrideInstanceId.(string) != "" { + instanceID = overrideInstanceId.(string) + } + gcl, ok := m.Parameters["togomak.srev.in/v1/logging"] + if !ok || (ok && gcl.(string) != "google-cloud") { + return "" + } + + query := "jsonPayload.labels.stage = \"%s\"\nlabels.instanceId = \"%s\";timeRange=%s/%s--PT24H;" + query = fmt.Sprintf(query, stage, instanceID, m.DeployedOn.Format(time.RFC3339Nano), m.DeployedOn.Format(time.RFC3339Nano)) + + return fmt.Sprintf("https://console.cloud.google.com/logs/query;query=%s?project=%s", query, m.CloudProjectId) + }, + GetMonitoringLink: func(m infrastructure.Metadata) string { + return "" + }, + }, + }, +} diff --git a/infrastructure/types.go b/infrastructure/types.go index f0a9047..de4d4fa 100644 --- a/infrastructure/types.go +++ b/infrastructure/types.go @@ -68,6 +68,9 @@ type Metadata struct { // DeploymentLinks specifies the link to deployment, if it is HTTP API endpoint. Optional. DeploymentLinks []string `json:"deployment_links,omitempty"` + + // Labels are key value pairs to store additional information + Labels map[string]string `json:"labels,omitempty"` } // GetId returns a unique identification id of the infrastructure @@ -82,6 +85,7 @@ func (i *Metadata) GetId() string { // time the entire file was updated, etc. type MetadataGroup struct { Version string `json:"version"` + Title string `json:"title"` UpdatedAt time.Time `json:"updated_at"` Infra []*Metadata `json:"infra"` } diff --git a/ops/push.go b/ops/push.go index 7f06eda..b0db2c3 100644 --- a/ops/push.go +++ b/ops/push.go @@ -46,7 +46,7 @@ func PushWithDbQueue(cfg config.GlobalConfig, queueDB string) error { if err != nil { panic(err) } - return nil + return nil } diff --git a/parser/markdown.go b/parser/markdown.go index a287dd5..dfc484e 100644 --- a/parser/markdown.go +++ b/parser/markdown.go @@ -30,7 +30,11 @@ func InfrastructureMetaToString(infraMeta *infrastructure.MetadataGroup) (string } book := doc.NewMarkDown() - book.WriteTitle("Infrastructure", 1).WriteLines(2) + if infraMeta.Title != "" { + book.WriteTitle(infraMeta.Title, 1).WriteLines(2) + } else { + book.WriteTitle("Infrastructure", 1).WriteLines(2) + } book.Write(fmt.Sprintf("Last updated on %s", infraMeta.UpdatedAt)).WriteLines(2) legend := doc.NewTable(3, 3) @@ -80,7 +84,7 @@ func InfrastructureMetaToString(infraMeta *infrastructure.MetadataGroup) (string t := doc.NewTable(len(components), 6) t.SetTitle(0, "Component") t.SetTitle(1, "Subsystem") - t.SetTitle(2, "SHA") + t.SetTitle(2, "Labels") t.SetTitle(3, "Deployed On") t.SetTitle(4, "Type") t.SetTitle(5, "API Endpoints") @@ -89,7 +93,15 @@ func InfrastructureMetaToString(infraMeta *infrastructure.MetadataGroup) (string infra := components[i] t.SetContent(i, 0, fmt.Sprintf("**%s**", infra.Name)) t.SetContent(i, 1, infra.Subsystem) - t.SetContent(i, 2, fmt.Sprintf("`%s`", infra.CommitSha)) + + labels := "" + if infra.Labels != nil { + labels += ",
" + for k := range infra.Labels { + labels += fmt.Sprintf("`%s:%s`,
", k, infra.Labels[k]) + } + } + t.SetContent(i, 2, fmt.Sprintf("`sha:%s`%s", infra.CommitSha, something)) t.SetContent(i, 3, infra.DeployedOn.Format("2006-01-02 15:04:05 -0700 MST")) // get logging and monitoring links, and only show them if we support monitoring -- GitLab From 2a949942619333d02ba43fdf94ac9843149e5876 Mon Sep 17 00:00:00 2001 From: Srevin Saju Date: Thu, 30 Mar 2023 23:59:11 +0530 Subject: [PATCH 2/2] feat: add label display on release UI --- cmd/go-cat/main.go | 1 + parser/markdown.go | 2 +- ui/go-cat-ui/src/App.svelte | 19 +++++++++++++++---- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/cmd/go-cat/main.go b/cmd/go-cat/main.go index 32f39f5..43a7741 100644 --- a/cmd/go-cat/main.go +++ b/cmd/go-cat/main.go @@ -28,6 +28,7 @@ func main() { infraFlags := []cli.Flag{ &cli.StringFlag{Name: "name", Usage: "Name of the service or endpoint"}, &cli.StringFlag{Name: "type", Usage: "Type of infrastructure on which the service is deployed to"}, + &cli.StringFlag{Name: "labels", Usage: "Additional key:value pairs, separated by comma"}, &cli.StringFlag{Name: "commit-sha", Usage: "Deployed Commit SHA"}, &cli.StringFlag{Name: "cloud", Usage: "Name of the cloud, example: gcp, aws"}, &cli.StringFlag{Name: "cloud-project-id", Usage: "A unique identifier of the project / environment in which the service is deployed"}, diff --git a/parser/markdown.go b/parser/markdown.go index dfc484e..ff25252 100644 --- a/parser/markdown.go +++ b/parser/markdown.go @@ -101,7 +101,7 @@ func InfrastructureMetaToString(infraMeta *infrastructure.MetadataGroup) (string labels += fmt.Sprintf("`%s:%s`,
", k, infra.Labels[k]) } } - t.SetContent(i, 2, fmt.Sprintf("`sha:%s`%s", infra.CommitSha, something)) + t.SetContent(i, 2, fmt.Sprintf("`sha:%s`%s", infra.CommitSha, labels)) t.SetContent(i, 3, infra.DeployedOn.Format("2006-01-02 15:04:05 -0700 MST")) // get logging and monitoring links, and only show them if we support monitoring diff --git a/ui/go-cat-ui/src/App.svelte b/ui/go-cat-ui/src/App.svelte index 473ab94..1152860 100644 --- a/ui/go-cat-ui/src/App.svelte +++ b/ui/go-cat-ui/src/App.svelte @@ -69,9 +69,6 @@ import InfraType from './InfraType.svelte'; }, 700) } - - - @@ -102,7 +99,7 @@ import InfraType from './InfraType.svelte'; Name - Deployment Links + Links Tags @@ -195,6 +192,7 @@ import InfraType from './InfraType.svelte'; + {#if d.infra_type } @@ -204,6 +202,19 @@ import InfraType from './InfraType.svelte'; {#if d.monitoring_links} {/if} + + + {#each Object.entries(d.labels) as [k, v]} + +
+
+ {k} + {v} +
+
+ {/each} + + -- GitLab