From 5e209a448397930044163487602b948e4c919843 Mon Sep 17 00:00:00 2001 From: Blaize M Kaye Date: Tue, 18 Apr 2023 09:26:05 +1200 Subject: [PATCH 01/10] Adds insights remote lib --- cmd/gatherInCluster.go | 113 ++++++++++++++++++++++++++++ gatherers/FilledFileGatherer.go | 33 ++++++++ gatherers/graphql.go | 3 +- gatherers/insights-remote-writer.go | 38 ++++++++++ go.work | 6 ++ insightRemoteLib/go.mod | 3 + insightRemoteLib/remote.go | 22 ++++++ 7 files changed, 216 insertions(+), 2 deletions(-) create mode 100644 cmd/gatherInCluster.go create mode 100644 gatherers/FilledFileGatherer.go create mode 100644 gatherers/insights-remote-writer.go create mode 100644 go.work create mode 100644 insightRemoteLib/go.mod create mode 100644 insightRemoteLib/remote.go diff --git a/cmd/gatherInCluster.go b/cmd/gatherInCluster.go new file mode 100644 index 0000000..5d4cf7c --- /dev/null +++ b/cmd/gatherInCluster.go @@ -0,0 +1,113 @@ +package cmd + +import ( + "encoding/json" + "fmt" + "github.com/spf13/cobra" + "github.com/spf13/viper" + "github.com/uselagoon/lagoon-facts-app/gatherers" + "log" + "os" +) + +var tokenValue string +var tokenFile string +var insightsRemoteEndpoint string + +// gatherCmd represents the gather command +var gatherInClusterCmd = &cobra.Command{ + Use: "gather-in-cluster", + Short: "Running this command will invoke the registered gatherers in cluster", + Long: `Running all the registered gatherers will inspect the system and write FACT data back to the Lagoon insights system via insights-remote`, + PersistentPreRun: func(cmd *cobra.Command, args []string) { + //get the basic env vars + if argStatic && argDynamic { + log.Fatalf("Cannot use both 'static' and 'dynamic' only gatherers - exiting") + os.Exit(1) + } + }, + Run: func(cmd *cobra.Command, args []string) { + + if tokenValue != "" && tokenFile != "" { + log.Fatal("Either a token or a token file needs to be passed as an argument, not both") + } + + if tokenValue == "" { + //tokenValue = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJFbnZpcm9ubWVudElkIjoiMiIsIkVudmlyb25tZW50TmFtZSI6Im1haW4iLCJOYW1lc3BhY2UiOiJkZWxldGVtZSIsIlByb2plY3ROYW1lIjoiZGVsZXRlbWUifQ.r5hoa7-459Kl87fOinCIJPTkjbBw9rxOl2R8KKKQahQ" + + if tokenFile == "" { + log.Fatal("Either a token or a token file needs to be passed as an argument") + } + + _, err := os.Stat(tokenFile) + if err != nil { + log.Fatal(err) + } + + ba, err := os.ReadFile(tokenFile) + if err != nil { + log.Fatal(err) + } + tokenValue = string(ba) + + } + + //set gatherer type to be static by default + gathererTypeArg := gatherers.GATHERER_TYPE_STATIC + if argDynamic { + gathererTypeArg = gatherers.GATHERER_TYPE_DYNAMIC + } + + //run the gatherers... + gathererSlice := gatherers.GetGatherers() + + var facts []gatherers.GatheredFact + + for _, e := range gathererSlice { + if e.GetGathererCmdType() == gathererTypeArg { + if e.AppliesToEnvironment() { + gatheredFacts, err := e.GatherFacts() + if err != nil { + log.Println(err.Error()) + continue + } + for _, f := range gatheredFacts { + if verbose := viper.Get("verbose"); verbose == true { + log.Printf("Registering %s", f.Name) + } + } + facts = append(facts, gatheredFacts...) + } + } + } + + if !dryRun { + fmt.Print(facts) + //err := gatherers.Writefacts(projectName, environmentName, facts) + err := gatherers.WriteFactsToInsightsRemote(tokenValue, facts) + if err != nil { + log.Println(err.Error()) + } + } + + if dryRun { + if facts != nil { + log.Println("---- Dry run ----") + log.Printf("Would post the follow facts to '%s:%s'", projectName, environmentName) + s, _ := json.MarshalIndent(facts, "", "\t") + log.Println(string(s)) + } + } + }, +} + +//var GatherCommand = gatherCmd + +func init() { + gatherInClusterCmd.PersistentFlags().StringVarP(&tokenValue, "token", "t", "", "The Lagoon insights remote token") + gatherInClusterCmd.PersistentFlags().StringVarP(&tokenFile, "token-file", "", "", "Read the Lagoon insights remote token from a file") + gatherInClusterCmd.PersistentFlags().BoolVarP(&dryRun, "dry-run", "d", false, "run gathers and print to screen without running write methods") + gatherInClusterCmd.PersistentFlags().StringVar(&insightsRemoteEndpoint, "insights-remote-endpoint", "http://localhost:10999/facts", "The Lagoon insights remote endpoint") + viper.BindPFlag("insights-remote-endpoint", gatherInClusterCmd.PersistentFlags().Lookup("insights-remote-endpoint")) + rootCmd.AddCommand(gatherInClusterCmd) +} diff --git a/gatherers/FilledFileGatherer.go b/gatherers/FilledFileGatherer.go new file mode 100644 index 0000000..70303a4 --- /dev/null +++ b/gatherers/FilledFileGatherer.go @@ -0,0 +1,33 @@ +package gatherers + +import ( + "path/filepath" +) + +// This Gatherer will search a particular directory for .json files that contain arrays of facts + +type filledFileGatherer struct { + GatheredFacts []GatheredFact +} + +func (p *filledFileGatherer) GetGathererCmdType() string { + return GATHERER_TYPE_STATIC +} + +func (p *filledFileGatherer) AppliesToEnvironment() bool { + + applies := false + + // Look into /tmp/facts for any .json file + filepath.Glob("/tmp/facts/*.json") + + return applies +} + +func (p *filledFileGatherer) GatherFacts() ([]GatheredFact, error) { + return p.GatheredFacts, nil +} + +func init() { + RegisterGatherer("Application gatherer", &filledFileGatherer{}) +} diff --git a/gatherers/graphql.go b/gatherers/graphql.go index 63f48f1..040f910 100644 --- a/gatherers/graphql.go +++ b/gatherers/graphql.go @@ -3,11 +3,10 @@ package gatherers import ( "context" "fmt" - "log" - "github.com/machinebox/graphql" "github.com/uselagoon/lagoon-facts-app/utils" "golang.org/x/oauth2" + "log" ) const lagoonAPIEndpoint = "https://api.lagoon.amazeeio.cloud/graphql" diff --git a/gatherers/insights-remote-writer.go b/gatherers/insights-remote-writer.go new file mode 100644 index 0000000..07b4e29 --- /dev/null +++ b/gatherers/insights-remote-writer.go @@ -0,0 +1,38 @@ +package gatherers + +import ( + "bytes" + "encoding/json" + "github.com/spf13/viper" + "github.com/uselagoon/insights-remote-lib" + "net/http" +) + +func WriteFactsToInsightsRemote(token string, facts []GatheredFact) error { + + insightsRemoteFacts := insightRemoteLib.Facts{ + Facts: []insightRemoteLib.Fact{}, + } + + for _, fact := range facts { + f := insightRemoteLib.Fact{ + Name: fact.Name, + Value: fact.Value, + Source: fact.Source, + Description: fact.Description, + Type: "type", + Category: string(fact.Category), + //Service: fact., + } + + insightsRemoteFacts.Facts = append(insightsRemoteFacts.Facts, f) + } + + bodyString, _ := json.Marshal(insightsRemoteFacts) + req, _ := http.NewRequest(http.MethodPost, viper.GetString("insights-remote-endpoint"), bytes.NewBuffer(bodyString)) + req.Header.Set("Authorization", token) + req.Header.Set("Content-Type", "application/json") + client := &http.Client{} + _, err := client.Do(req) + return err +} diff --git a/go.work b/go.work new file mode 100644 index 0000000..08851dc --- /dev/null +++ b/go.work @@ -0,0 +1,6 @@ +go 1.18 + +use ( + . + insightRemoteLib +) \ No newline at end of file diff --git a/insightRemoteLib/go.mod b/insightRemoteLib/go.mod new file mode 100644 index 0000000..ff637c3 --- /dev/null +++ b/insightRemoteLib/go.mod @@ -0,0 +1,3 @@ +module github.com/uselagoon/insights-remote-lib + +go 1.18 diff --git a/insightRemoteLib/remote.go b/insightRemoteLib/remote.go new file mode 100644 index 0000000..d9bd94e --- /dev/null +++ b/insightRemoteLib/remote.go @@ -0,0 +1,22 @@ +package insightRemoteLib + +type Fact struct { + EnvironmentId string `json:"environment"` + ProjectName string `json:"projectName"` + EnvironmentName string `json:"environmentName"` + Name string `json:"name"` + Value string `json:"value"` + Source string `json:"source"` + Description string `json:"description"` + Type string `json:"type"` + Category string `json:"category"` + Service string `json:"service"` +} + +type Facts struct { + EnvironmentId string `json:"environment"` + ProjectName string `json:"projectName"` + EnvironmentName string `json:"environmentName"` + Facts []Fact `json:"facts"` + InsightsType string `json:"insightsType"` +} From e2f1a7eeafbdf309eb4875bf94cea4280cb6dc41 Mon Sep 17 00:00:00 2001 From: Blaize M Kaye Date: Tue, 23 May 2023 13:49:09 +1200 Subject: [PATCH 02/10] Updates arguments --- .github/workflows/go.yml | 2 +- cmd/gatherInCluster.go | 9 ++++----- go.mod | 2 +- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 5d4b8fb..e22a58b 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -16,7 +16,7 @@ jobs: - name: Set up Go 1.x uses: actions/setup-go@v2 with: - go-version: ^1.13 + go-version: ^1.18 - name: Check out code into the Go module directory uses: actions/checkout@v2 diff --git a/cmd/gatherInCluster.go b/cmd/gatherInCluster.go index 5d4cf7c..e62c42b 100644 --- a/cmd/gatherInCluster.go +++ b/cmd/gatherInCluster.go @@ -28,9 +28,9 @@ var gatherInClusterCmd = &cobra.Command{ }, Run: func(cmd *cobra.Command, args []string) { - if tokenValue != "" && tokenFile != "" { - log.Fatal("Either a token or a token file needs to be passed as an argument, not both") - } + //if tokenValue != "" && tokenFile != "" { + // log.Fatal("Either a token or a token file needs to be passed as an argument, not both") + //} if tokenValue == "" { //tokenValue = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJFbnZpcm9ubWVudElkIjoiMiIsIkVudmlyb25tZW50TmFtZSI6Im1haW4iLCJOYW1lc3BhY2UiOiJkZWxldGVtZSIsIlByb2plY3ROYW1lIjoiZGVsZXRlbWUifQ.r5hoa7-459Kl87fOinCIJPTkjbBw9rxOl2R8KKKQahQ" @@ -49,7 +49,6 @@ var gatherInClusterCmd = &cobra.Command{ log.Fatal(err) } tokenValue = string(ba) - } //set gatherer type to be static by default @@ -105,7 +104,7 @@ var gatherInClusterCmd = &cobra.Command{ func init() { gatherInClusterCmd.PersistentFlags().StringVarP(&tokenValue, "token", "t", "", "The Lagoon insights remote token") - gatherInClusterCmd.PersistentFlags().StringVarP(&tokenFile, "token-file", "", "", "Read the Lagoon insights remote token from a file") + gatherInClusterCmd.PersistentFlags().StringVarP(&tokenFile, "token-file", "", "/var/run/secret/lagoon/dynamic/insights-token", "Read the Lagoon insights remote token from a file") gatherInClusterCmd.PersistentFlags().BoolVarP(&dryRun, "dry-run", "d", false, "run gathers and print to screen without running write methods") gatherInClusterCmd.PersistentFlags().StringVar(&insightsRemoteEndpoint, "insights-remote-endpoint", "http://localhost:10999/facts", "The Lagoon insights remote endpoint") viper.BindPFlag("insights-remote-endpoint", gatherInClusterCmd.PersistentFlags().Lookup("insights-remote-endpoint")) diff --git a/go.mod b/go.mod index 19c21cc..d19c4a9 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/uselagoon/lagoon-facts-app -go 1.13 +go 1.18 require ( github.com/machinebox/graphql v0.2.2 From b19fd4449cc032041d9c18c24cff39b8607ff7fb Mon Sep 17 00:00:00 2001 From: Blaize M Kaye Date: Tue, 23 May 2023 13:50:53 +1200 Subject: [PATCH 03/10] Removes local token --- cmd/gatherInCluster.go | 1 - 1 file changed, 1 deletion(-) diff --git a/cmd/gatherInCluster.go b/cmd/gatherInCluster.go index e62c42b..98f6126 100644 --- a/cmd/gatherInCluster.go +++ b/cmd/gatherInCluster.go @@ -33,7 +33,6 @@ var gatherInClusterCmd = &cobra.Command{ //} if tokenValue == "" { - //tokenValue = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJFbnZpcm9ubWVudElkIjoiMiIsIkVudmlyb25tZW50TmFtZSI6Im1haW4iLCJOYW1lc3BhY2UiOiJkZWxldGVtZSIsIlByb2plY3ROYW1lIjoiZGVsZXRlbWUifQ.r5hoa7-459Kl87fOinCIJPTkjbBw9rxOl2R8KKKQahQ" if tokenFile == "" { log.Fatal("Either a token or a token file needs to be passed as an argument") From 4b9f123a72a9f101d03e7908dc517d874454161a Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 24 May 2023 08:51:14 +1000 Subject: [PATCH 04/10] Small updates to Facts --- cmd/gatherInCluster.go | 5 +++-- gatherers/insights-remote-writer.go | 15 ++++++++++----- gatherers/main.go | 2 +- insightRemoteLib/remote.go | 4 ++-- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/cmd/gatherInCluster.go b/cmd/gatherInCluster.go index 98f6126..ad9a8ab 100644 --- a/cmd/gatherInCluster.go +++ b/cmd/gatherInCluster.go @@ -3,11 +3,12 @@ package cmd import ( "encoding/json" "fmt" + "log" + "os" + "github.com/spf13/cobra" "github.com/spf13/viper" "github.com/uselagoon/lagoon-facts-app/gatherers" - "log" - "os" ) var tokenValue string diff --git a/gatherers/insights-remote-writer.go b/gatherers/insights-remote-writer.go index 07b4e29..a602717 100644 --- a/gatherers/insights-remote-writer.go +++ b/gatherers/insights-remote-writer.go @@ -3,9 +3,12 @@ package gatherers import ( "bytes" "encoding/json" - "github.com/spf13/viper" - "github.com/uselagoon/insights-remote-lib" + "fmt" "net/http" + "os" + + "github.com/spf13/viper" + insightRemoteLib "github.com/uselagoon/insights-remote-lib" ) func WriteFactsToInsightsRemote(token string, facts []GatheredFact) error { @@ -20,9 +23,7 @@ func WriteFactsToInsightsRemote(token string, facts []GatheredFact) error { Value: fact.Value, Source: fact.Source, Description: fact.Description, - Type: "type", Category: string(fact.Category), - //Service: fact., } insightsRemoteFacts.Facts = append(insightsRemoteFacts.Facts, f) @@ -33,6 +34,10 @@ func WriteFactsToInsightsRemote(token string, facts []GatheredFact) error { req.Header.Set("Authorization", token) req.Header.Set("Content-Type", "application/json") client := &http.Client{} - _, err := client.Do(req) + response, err := client.Do(req) + if response.StatusCode != 200 { + fmt.Println(response.Body) + os.Exit(1) + } return err } diff --git a/gatherers/main.go b/gatherers/main.go index 3fc23fd..b191d1a 100644 --- a/gatherers/main.go +++ b/gatherers/main.go @@ -11,4 +11,4 @@ func LoadYamlConfig(yamlFilePath string) ([]byte, error) { return []byte{}, err } return data, nil -} \ No newline at end of file +} diff --git a/insightRemoteLib/remote.go b/insightRemoteLib/remote.go index d9bd94e..0468f49 100644 --- a/insightRemoteLib/remote.go +++ b/insightRemoteLib/remote.go @@ -14,9 +14,9 @@ type Fact struct { } type Facts struct { - EnvironmentId string `json:"environment"` + EnvironmentId int `json:"environment"` ProjectName string `json:"projectName"` EnvironmentName string `json:"environmentName"` Facts []Fact `json:"facts"` - InsightsType string `json:"insightsType"` + // InsightsType string `json:"insightsType"` } From 63b5b49f224ff3906f0ea0cc6ca0f70c6d51a324 Mon Sep 17 00:00:00 2001 From: Blaize M Kaye Date: Thu, 20 Jul 2023 11:32:35 +1200 Subject: [PATCH 05/10] Updates defaults to bring in line with insights-remote --- cmd/gatherInCluster.go | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/cmd/gatherInCluster.go b/cmd/gatherInCluster.go index ad9a8ab..b133982 100644 --- a/cmd/gatherInCluster.go +++ b/cmd/gatherInCluster.go @@ -29,10 +29,6 @@ var gatherInClusterCmd = &cobra.Command{ }, Run: func(cmd *cobra.Command, args []string) { - //if tokenValue != "" && tokenFile != "" { - // log.Fatal("Either a token or a token file needs to be passed as an argument, not both") - //} - if tokenValue == "" { if tokenFile == "" { @@ -82,7 +78,6 @@ var gatherInClusterCmd = &cobra.Command{ if !dryRun { fmt.Print(facts) - //err := gatherers.Writefacts(projectName, environmentName, facts) err := gatherers.WriteFactsToInsightsRemote(tokenValue, facts) if err != nil { log.Println(err.Error()) @@ -104,9 +99,9 @@ var gatherInClusterCmd = &cobra.Command{ func init() { gatherInClusterCmd.PersistentFlags().StringVarP(&tokenValue, "token", "t", "", "The Lagoon insights remote token") - gatherInClusterCmd.PersistentFlags().StringVarP(&tokenFile, "token-file", "", "/var/run/secret/lagoon/dynamic/insights-token", "Read the Lagoon insights remote token from a file") + gatherInClusterCmd.PersistentFlags().StringVarP(&tokenFile, "token-file", "", "/var/run/secrets/lagoon/dynamic/insights-token/INSIGHTS_TOKEN", "Read the Lagoon insights remote token from a file") gatherInClusterCmd.PersistentFlags().BoolVarP(&dryRun, "dry-run", "d", false, "run gathers and print to screen without running write methods") - gatherInClusterCmd.PersistentFlags().StringVar(&insightsRemoteEndpoint, "insights-remote-endpoint", "http://localhost:10999/facts", "The Lagoon insights remote endpoint") + gatherInClusterCmd.PersistentFlags().StringVar(&insightsRemoteEndpoint, "insights-remote-endpoint", "http://lagoon-remote-insights-remote.lagoon.svc/facts", "The Lagoon insights remote endpoint") viper.BindPFlag("insights-remote-endpoint", gatherInClusterCmd.PersistentFlags().Lookup("insights-remote-endpoint")) rootCmd.AddCommand(gatherInClusterCmd) } From 0a4197b56a5493db16e197956e844d86322cb56b Mon Sep 17 00:00:00 2001 From: Blaize M Kaye Date: Thu, 20 Jul 2023 14:05:19 +1200 Subject: [PATCH 06/10] Completing work on in cluster gathering --- cmd/gatherInCluster.go | 2 -- gatherers/insights-remote-writer.go | 8 ++++++-- insightRemoteLib/remote.go | 1 - 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/cmd/gatherInCluster.go b/cmd/gatherInCluster.go index b133982..b1c55aa 100644 --- a/cmd/gatherInCluster.go +++ b/cmd/gatherInCluster.go @@ -2,7 +2,6 @@ package cmd import ( "encoding/json" - "fmt" "log" "os" @@ -77,7 +76,6 @@ var gatherInClusterCmd = &cobra.Command{ } if !dryRun { - fmt.Print(facts) err := gatherers.WriteFactsToInsightsRemote(tokenValue, facts) if err != nil { log.Println(err.Error()) diff --git a/gatherers/insights-remote-writer.go b/gatherers/insights-remote-writer.go index a602717..47030e9 100644 --- a/gatherers/insights-remote-writer.go +++ b/gatherers/insights-remote-writer.go @@ -30,13 +30,17 @@ func WriteFactsToInsightsRemote(token string, facts []GatheredFact) error { } bodyString, _ := json.Marshal(insightsRemoteFacts) - req, _ := http.NewRequest(http.MethodPost, viper.GetString("insights-remote-endpoint"), bytes.NewBuffer(bodyString)) + + fmt.Printf("Sending %v fact(s) to insights core\n", len(facts)) + + serviceEndpoint := viper.GetString("insights-remote-endpoint") + req, _ := http.NewRequest(http.MethodPost, serviceEndpoint, bytes.NewBuffer(bodyString)) req.Header.Set("Authorization", token) req.Header.Set("Content-Type", "application/json") client := &http.Client{} response, err := client.Do(req) if response.StatusCode != 200 { - fmt.Println(response.Body) + fmt.Printf("There was an error sending the facts to '%s' : %s\n", serviceEndpoint, response.Body) os.Exit(1) } return err diff --git a/insightRemoteLib/remote.go b/insightRemoteLib/remote.go index 0468f49..dbe992f 100644 --- a/insightRemoteLib/remote.go +++ b/insightRemoteLib/remote.go @@ -18,5 +18,4 @@ type Facts struct { ProjectName string `json:"projectName"` EnvironmentName string `json:"environmentName"` Facts []Fact `json:"facts"` - // InsightsType string `json:"insightsType"` } From 0fe3aea805c9d5c2a81c3441b877d32ae83e8d41 Mon Sep 17 00:00:00 2001 From: Blaize M Kaye Date: Tue, 7 Nov 2023 03:50:04 +1300 Subject: [PATCH 07/10] Removes extraneous exit --- cmd/gatherInCluster.go | 1 - 1 file changed, 1 deletion(-) diff --git a/cmd/gatherInCluster.go b/cmd/gatherInCluster.go index b1c55aa..0784a92 100644 --- a/cmd/gatherInCluster.go +++ b/cmd/gatherInCluster.go @@ -23,7 +23,6 @@ var gatherInClusterCmd = &cobra.Command{ //get the basic env vars if argStatic && argDynamic { log.Fatalf("Cannot use both 'static' and 'dynamic' only gatherers - exiting") - os.Exit(1) } }, Run: func(cmd *cobra.Command, args []string) { From ab6f301b9c14f69d4c5695b866376902046669b8 Mon Sep 17 00:00:00 2001 From: Blaize M Kaye Date: Tue, 7 Nov 2023 03:58:58 +1300 Subject: [PATCH 08/10] Fixes error handling in remote writer --- gatherers/insights-remote-writer.go | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/gatherers/insights-remote-writer.go b/gatherers/insights-remote-writer.go index 47030e9..5e4406e 100644 --- a/gatherers/insights-remote-writer.go +++ b/gatherers/insights-remote-writer.go @@ -4,11 +4,10 @@ import ( "bytes" "encoding/json" "fmt" - "net/http" - "os" - "github.com/spf13/viper" insightRemoteLib "github.com/uselagoon/insights-remote-lib" + "log" + "net/http" ) func WriteFactsToInsightsRemote(token string, facts []GatheredFact) error { @@ -29,7 +28,10 @@ func WriteFactsToInsightsRemote(token string, facts []GatheredFact) error { insightsRemoteFacts.Facts = append(insightsRemoteFacts.Facts, f) } - bodyString, _ := json.Marshal(insightsRemoteFacts) + bodyString, err := json.Marshal(insightsRemoteFacts) + if err != nil { + log.Fatal(err.Error()) + } fmt.Printf("Sending %v fact(s) to insights core\n", len(facts)) @@ -39,9 +41,16 @@ func WriteFactsToInsightsRemote(token string, facts []GatheredFact) error { req.Header.Set("Content-Type", "application/json") client := &http.Client{} response, err := client.Do(req) + + if err != nil { + return err + } + if response.StatusCode != 200 { - fmt.Printf("There was an error sending the facts to '%s' : %s\n", serviceEndpoint, response.Body) - os.Exit(1) + log.Fatalf("There was an error sending the facts to '%s' : %s\n", serviceEndpoint, response.Body) } + + defer response.Body.Close() + return err } From f36fb51a7675cc2173621809b48200e5f9e089b4 Mon Sep 17 00:00:00 2001 From: Blaize M Kaye Date: Tue, 7 Nov 2023 05:54:13 +1300 Subject: [PATCH 09/10] Refactors token gathering --- cmd/gatherInCluster.go | 26 ++++++++++++++++---------- gatherers/insights-remote-writer.go | 8 +++++++- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/cmd/gatherInCluster.go b/cmd/gatherInCluster.go index 0784a92..14bc47a 100644 --- a/cmd/gatherInCluster.go +++ b/cmd/gatherInCluster.go @@ -28,21 +28,14 @@ var gatherInClusterCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { if tokenValue == "" { - if tokenFile == "" { log.Fatal("Either a token or a token file needs to be passed as an argument") } - - _, err := os.Stat(tokenFile) + var err error + tokenValue, err = getTokenFromFile(tokenFile) if err != nil { - log.Fatal(err) + log.Fatalf("Unable to load token: %v - %v", tokenFile, err.Error()) } - - ba, err := os.ReadFile(tokenFile) - if err != nil { - log.Fatal(err) - } - tokenValue = string(ba) } //set gatherer type to be static by default @@ -92,6 +85,19 @@ var gatherInClusterCmd = &cobra.Command{ }, } +func getTokenFromFile(tokenFile string) (string, error) { + _, err := os.Stat(tokenFile) + if err != nil { + return "", err + } + + ba, err := os.ReadFile(tokenFile) + if err != nil { + return "", err + } + return string(ba), nil +} + //var GatherCommand = gatherCmd func init() { diff --git a/gatherers/insights-remote-writer.go b/gatherers/insights-remote-writer.go index 5e4406e..d95775a 100644 --- a/gatherers/insights-remote-writer.go +++ b/gatherers/insights-remote-writer.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/spf13/viper" insightRemoteLib "github.com/uselagoon/insights-remote-lib" + "io/ioutil" "log" "net/http" ) @@ -47,7 +48,12 @@ func WriteFactsToInsightsRemote(token string, facts []GatheredFact) error { } if response.StatusCode != 200 { - log.Fatalf("There was an error sending the facts to '%s' : %s\n", serviceEndpoint, response.Body) + bodyData, err := ioutil.ReadAll(response.Body) + if err != nil { + log.Fatal(err.Error()) + } + + log.Fatalf("There was an error sending the facts to '%s': %v- %v \n", serviceEndpoint, response.StatusCode, string(bodyData)) } defer response.Body.Close() From ea8a1c7111593788b838d931fba92bb07ed33cfc Mon Sep 17 00:00:00 2001 From: Blaize M Kaye Date: Tue, 7 Nov 2023 06:07:56 +1300 Subject: [PATCH 10/10] Removes test gatherer --- gatherers/FilledFileGatherer.go | 33 --------------------------------- 1 file changed, 33 deletions(-) delete mode 100644 gatherers/FilledFileGatherer.go diff --git a/gatherers/FilledFileGatherer.go b/gatherers/FilledFileGatherer.go deleted file mode 100644 index 70303a4..0000000 --- a/gatherers/FilledFileGatherer.go +++ /dev/null @@ -1,33 +0,0 @@ -package gatherers - -import ( - "path/filepath" -) - -// This Gatherer will search a particular directory for .json files that contain arrays of facts - -type filledFileGatherer struct { - GatheredFacts []GatheredFact -} - -func (p *filledFileGatherer) GetGathererCmdType() string { - return GATHERER_TYPE_STATIC -} - -func (p *filledFileGatherer) AppliesToEnvironment() bool { - - applies := false - - // Look into /tmp/facts for any .json file - filepath.Glob("/tmp/facts/*.json") - - return applies -} - -func (p *filledFileGatherer) GatherFacts() ([]GatheredFact, error) { - return p.GatheredFacts, nil -} - -func init() { - RegisterGatherer("Application gatherer", &filledFileGatherer{}) -}