From 17e038e703faecdb73fee012f788acac9d1e1bea Mon Sep 17 00:00:00 2001 From: "M. Mert Yildiran" Date: Tue, 31 Jan 2023 22:31:44 +0300 Subject: [PATCH] :sparkles: Do `POST /license` after Hub is started --- cmd/tapRunner.go | 4 ++++ config/configStruct.go | 1 + internal/connect/hub.go | 30 ++++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+) diff --git a/cmd/tapRunner.go b/cmd/tapRunner.go index f8da6eba2..bb032b259 100644 --- a/cmd/tapRunner.go +++ b/cmd/tapRunner.go @@ -398,6 +398,10 @@ func postHubStarted(ctx context.Context, kubernetesProvider *kubernetes.Provider connector.PostRegexToHub(config.Config.Tap.PodRegexStr, state.targetNamespaces) + if config.Config.License != "" { + connector.PostLicense(config.Config.License) + } + url := kubernetes.GetLocalhostOnPort(config.Config.Tap.Proxy.Hub.SrcPort) log.Info().Str("url", url).Msg(fmt.Sprintf(utils.Green, "Hub is available at:")) } diff --git a/config/configStruct.go b/config/configStruct.go index 21a0be613..ad0face60 100644 --- a/config/configStruct.go +++ b/config/configStruct.go @@ -35,6 +35,7 @@ type ConfigStruct struct { DumpLogs bool `yaml:"dumplogs" default:"false"` ConfigFilePath string `yaml:"configpath,omitempty" readonly:""` HeadlessMode bool `yaml:"headless" default:"false"` + License string `yaml:"license" default:""` } func (config *ConfigStruct) SetDefaults() { diff --git a/internal/connect/hub.go b/internal/connect/hub.go index 7404c650f..da847de7e 100644 --- a/internal/connect/hub.go +++ b/internal/connect/hub.go @@ -145,3 +145,33 @@ func (connector *Connector) PostRegexToHub(regex string, namespaces []string) { } } } + +type postLicenseRequest struct { + License string `json:"license"` +} + +func (connector *Connector) PostLicense(license string) { + postLicenseUrl := fmt.Sprintf("%s/license", connector.url) + + payload := postLicenseRequest{ + License: license, + } + + if payloadMarshalled, err := json.Marshal(payload); err != nil { + log.Error().Err(err).Msg("Failed to marshal the payload:") + } else { + ok := false + for !ok { + if _, err = utils.Post(postLicenseUrl, "application/json", bytes.NewBuffer(payloadMarshalled), connector.client); err != nil { + if _, ok := err.(*url.Error); ok { + break + } + log.Debug().Err(err).Msg("Failed sending the license to Hub:") + } else { + ok = true + log.Debug().Str("license", license).Msg("Reported license to Hub:") + } + time.Sleep(time.Second) + } + } +}