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/fact_filters.go b/internal/handler/factFilters.go similarity index 100% rename from internal/handler/fact_filters.go rename to internal/handler/factFilters.go 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