From bca22839d6d08d55b7cb7e297a795d40302b575a Mon Sep 17 00:00:00 2001 From: Blaize Kaye Date: Thu, 10 Mar 2022 14:06:10 +1300 Subject: [PATCH 1/2] Adds Insight Label Processing --- default_filter_transformers.json | 4 ++ internal/handler/imageInspectParserFilter.go | 51 ++++++++++++++++++-- internal/handler/main.go | 5 ++ 3 files changed, 57 insertions(+), 3 deletions(-) diff --git a/default_filter_transformers.json b/default_filter_transformers.json index 9c875cd..6e87fe9 100644 --- a/default_filter_transformers.json +++ b/default_filter_transformers.json @@ -39,6 +39,10 @@ } ], "keyfact": true + }, + { + "type": "handler.InsightsInspectLabel", + "keyfact": true } ] } \ No newline at end of file diff --git a/internal/handler/imageInspectParserFilter.go b/internal/handler/imageInspectParserFilter.go index 475ea06..ce82394 100644 --- a/internal/handler/imageInspectParserFilter.go +++ b/internal/handler/imageInspectParserFilter.go @@ -44,12 +44,16 @@ func processImageInspectInsightsData(h *Messaging, insights InsightsData, v stri return nil, "", err } - facts, err := processFactsFromImageInspect(imageInspect, environment.Id, source) + facts, err := processEnvironmentVariableFactsFromImageInspect(imageInspect, environment.Id, source) if err != nil { return nil, "", err } + labelFacts, err := processLabelFactsFromImageInspect(imageInspect, environment.Id, source) + if err != nil { + return nil, "", err + } + facts = append(facts, labelFacts...) log.Printf("Successfully decoded image-inspect") - facts, err = KeyFactsFilter(facts) if err != nil { return nil, "", err @@ -60,7 +64,48 @@ func processImageInspectInsightsData(h *Messaging, insights InsightsData, v stri return []LagoonFact{}, "", nil } -func processFactsFromImageInspect(imageInspectData ImageData, id int, source string) ([]LagoonFact, error) { +func processLabelFactsFromImageInspect(imageInspectData ImageData, id int, source string) ([]LagoonFact, error) { + var factsInput []LagoonFact + + var filteredFacts []InsightsInspectLabel + keyFactsExistMap := make(map[string]bool) + + // Check if image inspect contains useful environment variables + if imageInspectData.Labels != nil { + for k, v := range imageInspectData.Labels { + + env := InsightsInspectLabel{ + Key: k, + Value: v, + } + + // Remove duplicate key facts + if _, ok := keyFactsExistMap[env.Key]; !ok { + keyFactsExistMap[env.Key] = true + filteredFacts = append(filteredFacts, env) + } + } + } + + for _, f := range filteredFacts { + + fact := LagoonFact{ + Environment: id, + Name: f.Key, + Value: f.Value, + Source: source, + Description: "Insights Inspect Label", + KeyFact: false, + Type: FactTypeText, + } + fmt.Println("Processing fact name " + f.Key) + fact, _ = ProcessLagoonFactAgainstRegisteredFilters(fact, f) + factsInput = append(factsInput, fact) + } + return factsInput, nil +} + +func processEnvironmentVariableFactsFromImageInspect(imageInspectData ImageData, id int, source string) ([]LagoonFact, error) { var factsInput []LagoonFact var filteredFacts []EnvironmentVariable diff --git a/internal/handler/main.go b/internal/handler/main.go index d0bf064..1a50c8c 100644 --- a/internal/handler/main.go +++ b/internal/handler/main.go @@ -159,6 +159,11 @@ type EnvironmentVariable struct { Value string } +type InsightsInspectLabel struct { + Key string + Value string +} + type ResourceDestination struct { Project string Environment string From 4bb28089eed721568f44d52b00899b0ed9e90829 Mon Sep 17 00:00:00 2001 From: Blaize Kaye Date: Thu, 10 Mar 2022 14:12:14 +1300 Subject: [PATCH 2/2] Renames file to bring it in line with the standard --- internal/handler/{fact_filters.go => factFilters.go} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename internal/handler/{fact_filters.go => factFilters.go} (100%) diff --git a/internal/handler/fact_filters.go b/internal/handler/factFilters.go similarity index 100% rename from internal/handler/fact_filters.go rename to internal/handler/factFilters.go