Skip to content

Commit

Permalink
feat: adds periodic health reporting to the k8s inventory agent
Browse files Browse the repository at this point in the history
Addresses: Enterprise-2483

Signed-off-by: Bob Melander <[email protected]>
  • Loading branch information
bobmel committed Oct 14, 2024
1 parent 70a81f1 commit b458e9d
Show file tree
Hide file tree
Showing 19 changed files with 3,007 additions and 16 deletions.
14 changes: 14 additions & 0 deletions anchore-k8s-inventory.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ log:
# enable/disable checking for application updates on startup
check-for-app-update: true

anchore-registration:
# The id to register the agent as with Enterprise, so Enterprise can map the agent to its integration uuid.
# If left unspecified, the agent will attempt to set registration-id to the uid of the K8s Deployment for the agent.
# If that fails (e.g., if the agent is not deployed on K8s), the agent will generate a UUID to use as registration-id.
registration-id:
# The name that the agent should have. If left unspecified, the agent will attempt to set it to the name of the K8s
# Deployment for the agent. If that fails it will be empty.
integration-name:
# A short description for the agent
integration-description:

kubeconfig:
path:
cluster: docker-desktop
Expand Down Expand Up @@ -117,6 +128,9 @@ ignore-not-running: true
# Only respected if mode is periodic
polling-interval-seconds: 300

# Only respected if mode is periodic
health-report-interval-seconds: 60

# Batch Request configuration
inventory-report-limits:
namespaces: 0 # default of 0 means no limit per report
Expand Down
27 changes: 21 additions & 6 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package cmd
import (
"errors"
"fmt"
"os"
"runtime/pprof"

"github.com/anchore/k8s-inventory/pkg/healthreporter"
"github.com/anchore/k8s-inventory/pkg/integration"
"github.com/anchore/k8s-inventory/pkg/mode"
"github.com/anchore/k8s-inventory/pkg/reporter"
"os"
"runtime/pprof"

"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -47,7 +48,20 @@ var rootCmd = &cobra.Command{

switch appConfig.RunMode {
case mode.PeriodicPolling:
pkg.PeriodicallyGetInventoryReport(appConfig)
neverDone := make(chan bool, 1)

ch := integration.GetChannels()
gatedReportInfo := healthreporter.GetGatedReportInfo()

go healthreporter.PeriodicallySendHealthReport(appConfig, ch, gatedReportInfo)
go pkg.PeriodicallyGetInventoryReport(appConfig, ch, gatedReportInfo)

_, err := integration.PerformRegistration(appConfig, ch)
if err != nil {
os.Exit(1)
}

<-neverDone
default:
reports, err := pkg.GetInventoryReports(appConfig)
if appConfig.Dev.ProfileCPU {
Expand All @@ -58,18 +72,19 @@ var rootCmd = &cobra.Command{
os.Exit(1)
}
anErrorOccurred := false
reportInfo := healthreporter.InventoryReportInfo{}
for account, reportsForAccount := range reports {
for count, report := range reportsForAccount {
log.Infof("Sending Inventory Report to Anchore Account %s, %d of %d", account, count+1, len(reportsForAccount))
err = pkg.HandleReport(report, appConfig, account)
err = pkg.HandleReport(report, &reportInfo, appConfig, account)
if errors.Is(err, reporter.ErrAnchoreAccountDoesNotExist) {
// Retry with default account
retryAccount := appConfig.AnchoreDetails.Account
if appConfig.AccountRouteByNamespaceLabel.DefaultAccount != "" {
retryAccount = appConfig.AccountRouteByNamespaceLabel.DefaultAccount
}
log.Warnf("Error sending to Anchore Account %s, sending to default account", account)
err = pkg.HandleReport(report, appConfig, retryAccount)
err = pkg.HandleReport(report, &reportInfo, appConfig, retryAccount)
}
if err != nil {
log.Errorf("Failed to handle Image Results: %+v", err)
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ go 1.22.5
require (
github.com/adrg/xdg v0.5.0
github.com/anchore/go-testutils v0.0.0-20200925183923-d5f45b0d3c04
github.com/google/uuid v1.6.0
github.com/h2non/gock v1.2.0
github.com/hashicorp/go-version v1.7.0
github.com/mitchellh/go-homedir v1.1.0
github.com/nsf/jsondiff v0.0.0-20230430225905-43f6cf3098c1
github.com/sirupsen/logrus v1.9.3
Expand Down Expand Up @@ -33,7 +35,6 @@ require (
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/imdario/mergo v0.3.6 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ github.com/h2non/gock v1.2.0 h1:K6ol8rfrRkUOefooBC8elXoaNGYkpp7y2qcxGG6BzUE=
github.com/h2non/gock v1.2.0/go.mod h1:tNhoxHYW2W42cYkYb1WqzdbYIieALC99kpYr7rH/BQk=
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslCrtky5vbi9dd7HrQPQIx6wqiw=
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542/go.mod h1:Ow0tF8D4Kplbc8s8sSb3V2oUCygFHVp8gC3Dn6U4MNI=
github.com/hashicorp/go-version v1.7.0 h1:5tqGy27NaOTB8yJKUZELlFAS/LTKJkrmONwQKeRZfjY=
github.com/hashicorp/go-version v1.7.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
Expand Down
Loading

0 comments on commit b458e9d

Please sign in to comment.