From 05a2b24fb48719dfffeb49eb9c9469f22dbd6848 Mon Sep 17 00:00:00 2001 From: Frank Jogeleit Date: Fri, 20 Sep 2024 14:23:40 +0200 Subject: [PATCH] improve target lifecycle hanlding Signed-off-by: Frank Jogeleit --- pkg/kubernetes/secrets/__debug_bin266582482 | 0 pkg/listener/send_result_test.go | 4 ++++ pkg/listener/sync_results.go | 3 +++ pkg/target/client.go | 10 ++++++++++ pkg/target/collection.go | 12 ++++++++++++ pkg/target/elasticsearch/elasticsearch.go | 5 ----- pkg/target/factory/factory.go | 2 -- pkg/target/gcs/gcs.go | 5 ----- pkg/target/googlechat/googlechat.go | 5 ----- pkg/target/kinesis/kinesis.go | 5 ----- pkg/target/loki/loki.go | 3 --- pkg/target/s3/s3.go | 5 ----- pkg/target/securityhub/securityhub.go | 5 +++-- pkg/target/slack/slack.go | 3 --- pkg/target/telegram/telegram.go | 4 ++++ pkg/target/webhook/webhook.go | 6 ------ 16 files changed, 36 insertions(+), 41 deletions(-) delete mode 100644 pkg/kubernetes/secrets/__debug_bin266582482 diff --git a/pkg/kubernetes/secrets/__debug_bin266582482 b/pkg/kubernetes/secrets/__debug_bin266582482 deleted file mode 100644 index e69de29b..00000000 diff --git a/pkg/listener/send_result_test.go b/pkg/listener/send_result_test.go index 66c2c6b4..565e1b2b 100644 --- a/pkg/listener/send_result_test.go +++ b/pkg/listener/send_result_test.go @@ -44,6 +44,10 @@ func (c client) Validate(rep v1alpha2.ReportInterface, result v1alpha2.PolicyRep return c.validated } +func (c *client) Reset(_ context.Context) error { + return nil +} + func (c *client) CleanUp(_ context.Context, _ v1alpha2.ReportInterface) { c.cleanupCalled = true } diff --git a/pkg/listener/sync_results.go b/pkg/listener/sync_results.go index 052efa65..9ce9a1d2 100644 --- a/pkg/listener/sync_results.go +++ b/pkg/listener/sync_results.go @@ -1,6 +1,7 @@ package listener import ( + "context" "sync" "github.com/kyverno/policy-reporter/pkg/crd/api/policyreport/v1alpha2" @@ -12,6 +13,8 @@ import ( const SendSyncResults = "send_sync_results_listener" func NewSendSyncResultsListener(targets *target.Collection) report.SyncResultsListener { + targets.Reset(context.Background()) + return func(rep v1alpha2.ReportInterface) { clients := targets.SyncClients() if len(clients) == 0 { diff --git a/pkg/target/client.go b/pkg/target/client.go index 3c26a0b2..25d849ce 100644 --- a/pkg/target/client.go +++ b/pkg/target/client.go @@ -41,6 +41,8 @@ type Client interface { Type() ClientType // CleanUp old results if supported by the target CleanUp(context.Context, v1alpha2.ReportInterface) + // Reset the current state in the related target + Reset(context.Context) error } type ResultFilterFactory struct { @@ -236,6 +238,14 @@ func (c *BaseClient) SkipExistingOnStartup() bool { return c.skipExistingOnStartup } +func (c *BaseClient) Reset(_ context.Context) error { + return nil +} + +func (c *BaseClient) CleanUp(_ context.Context, _ v1alpha2.ReportInterface) {} + +func (c *BaseClient) BatchSend(_ v1alpha2.ReportInterface, _ []v1alpha2.PolicyReportResult) {} + func NewBaseClient(options ClientOptions) BaseClient { return BaseClient{options.Name, options.SkipExistingOnStartup, options.ResultFilter, options.ReportFilter} } diff --git a/pkg/target/collection.go b/pkg/target/collection.go index cbf8657b..f1ad7a54 100644 --- a/pkg/target/collection.go +++ b/pkg/target/collection.go @@ -1,9 +1,11 @@ package target import ( + "context" "sync" "github.com/kyverno/policy-reporter/pkg/helper" + "go.uber.org/zap" ) type TargetType = string @@ -56,6 +58,16 @@ func (c *Collection) Update(t *Target) { c.mx.Unlock() } +func (c *Collection) Reset(ctx context.Context) { + clients := c.SyncClients() + + for _, c := range clients { + if err := c.Reset(ctx); err != nil { + zap.L().Error("failed to reset target", zap.String("type", c.Type()), zap.String("name", c.Name())) + } + } +} + func (c *Collection) Targets() []*Target { return helper.ToList(c.targets) } diff --git a/pkg/target/elasticsearch/elasticsearch.go b/pkg/target/elasticsearch/elasticsearch.go index d723f740..211f6626 100644 --- a/pkg/target/elasticsearch/elasticsearch.go +++ b/pkg/target/elasticsearch/elasticsearch.go @@ -1,7 +1,6 @@ package elasticsearch import ( - "context" "time" "github.com/kyverno/policy-reporter/pkg/crd/api/policyreport/v1alpha2" @@ -98,10 +97,6 @@ func (e *client) Send(result v1alpha2.PolicyReportResult) { http.ProcessHTTPResponse(e.Name(), resp, err) } -func (e *client) CleanUp(_ context.Context, _ v1alpha2.ReportInterface) {} - -func (e *client) BatchSend(_ v1alpha2.ReportInterface, _ []v1alpha2.PolicyReportResult) {} - func (e *client) Type() target.ClientType { return target.SingleSend } diff --git a/pkg/target/factory/factory.go b/pkg/target/factory/factory.go index d3d1a6dd..bb27bc4e 100644 --- a/pkg/target/factory/factory.go +++ b/pkg/target/factory/factory.go @@ -672,8 +672,6 @@ func (f *TargetFactory) CreateSecurityHubTarget(config, parent *target.Config[ta Synchronize: config.Config.Synchronize, }) - hub.ResetFindings(context.Background()) - return &target.Target{ ID: uuid.NewString(), Type: target.SecurityHub, diff --git a/pkg/target/gcs/gcs.go b/pkg/target/gcs/gcs.go index 657eacce..c6638245 100644 --- a/pkg/target/gcs/gcs.go +++ b/pkg/target/gcs/gcs.go @@ -2,7 +2,6 @@ package gcs import ( "bytes" - "context" "encoding/json" "fmt" "time" @@ -63,10 +62,6 @@ func (c *client) Send(result v1alpha2.PolicyReportResult) { zap.L().Info(c.Name() + ": PUSH OK") } -func (c *client) CleanUp(_ context.Context, _ v1alpha2.ReportInterface) {} - -func (c *client) BatchSend(_ v1alpha2.ReportInterface, _ []v1alpha2.PolicyReportResult) {} - func (c *client) Type() target.ClientType { return target.SingleSend } diff --git a/pkg/target/googlechat/googlechat.go b/pkg/target/googlechat/googlechat.go index c9760eb5..46a67930 100644 --- a/pkg/target/googlechat/googlechat.go +++ b/pkg/target/googlechat/googlechat.go @@ -2,7 +2,6 @@ package googlechat import ( "bytes" - "context" "text/template" "time" @@ -220,10 +219,6 @@ func (e *client) Send(result v1alpha2.PolicyReportResult) { http.ProcessHTTPResponse(e.Name(), resp, err) } -func (e *client) CleanUp(_ context.Context, _ v1alpha2.ReportInterface) {} - -func (e *client) BatchSend(_ v1alpha2.ReportInterface, _ []v1alpha2.PolicyReportResult) {} - func (e *client) Type() target.ClientType { return target.SingleSend } diff --git a/pkg/target/kinesis/kinesis.go b/pkg/target/kinesis/kinesis.go index b375d5e8..04699a26 100644 --- a/pkg/target/kinesis/kinesis.go +++ b/pkg/target/kinesis/kinesis.go @@ -2,7 +2,6 @@ package kinesis import ( "bytes" - "context" "encoding/json" "fmt" "time" @@ -60,10 +59,6 @@ func (c *client) Send(result v1alpha2.PolicyReportResult) { zap.L().Info("PUSH OK", zap.String("name", c.Name())) } -func (c *client) CleanUp(_ context.Context, _ v1alpha2.ReportInterface) {} - -func (c *client) BatchSend(_ v1alpha2.ReportInterface, _ []v1alpha2.PolicyReportResult) {} - func (c *client) Type() target.ClientType { return target.SingleSend } diff --git a/pkg/target/loki/loki.go b/pkg/target/loki/loki.go index 8272e03b..32eea1e0 100644 --- a/pkg/target/loki/loki.go +++ b/pkg/target/loki/loki.go @@ -1,7 +1,6 @@ package loki import ( - "context" "fmt" "strings" "time" @@ -137,8 +136,6 @@ func (l *client) Type() target.ClientType { return target.BatchSend } -func (l *client) CleanUp(_ context.Context, _ v1alpha2.ReportInterface) {} - // NewClient creates a new loki.client to send Results to Loki func NewClient(options Options) target.Client { return &client{ diff --git a/pkg/target/s3/s3.go b/pkg/target/s3/s3.go index ecbe29d7..2fafa38c 100644 --- a/pkg/target/s3/s3.go +++ b/pkg/target/s3/s3.go @@ -2,7 +2,6 @@ package s3 import ( "bytes" - "context" "encoding/json" "fmt" "time" @@ -62,10 +61,6 @@ func (c *client) Send(result v1alpha2.PolicyReportResult) { zap.L().Info(c.Name() + ": PUSH OK") } -func (c *client) CleanUp(_ context.Context, _ v1alpha2.ReportInterface) {} - -func (c *client) BatchSend(_ v1alpha2.ReportInterface, _ []v1alpha2.PolicyReportResult) {} - func (c *client) Type() target.ClientType { return target.SingleSend } diff --git a/pkg/target/securityhub/securityhub.go b/pkg/target/securityhub/securityhub.go index ef674bfd..4de9f57e 100644 --- a/pkg/target/securityhub/securityhub.go +++ b/pkg/target/securityhub/securityhub.go @@ -184,11 +184,12 @@ func (c *client) BatchSend(polr v1alpha2.ReportInterface, results []v1alpha2.Pol zap.L().Info(c.Name()+": PUSH OK", zap.Int32("imported", *res.SuccessCount), zap.Int32("failed", *res.FailedCount), zap.String("report", polr.GetKey())) } -func (c *client) ResetFindings(ctx context.Context) error { +func (c *client) Reset(ctx context.Context) error { if !c.synchronize { return nil } - defer zap.L().Info(c.Name() + ": START SYNC") + + zap.L().Info(c.Name() + ": START SYNC") list, err := c.getFindings(ctx) if err != nil { diff --git a/pkg/target/slack/slack.go b/pkg/target/slack/slack.go index 7996eb21..35b33449 100644 --- a/pkg/target/slack/slack.go +++ b/pkg/target/slack/slack.go @@ -1,7 +1,6 @@ package slack import ( - "context" "fmt" "github.com/slack-go/slack" @@ -313,8 +312,6 @@ func (s *client) Type() target.ClientType { return target.BatchSend } -func (s *client) CleanUp(_ context.Context, _ v1alpha2.ReportInterface) {} - // NewClient creates a new slack.client to send Results to Slack func NewClient(options Options) target.Client { return &client{ diff --git a/pkg/target/telegram/telegram.go b/pkg/target/telegram/telegram.go index acb5e67f..dbbf075b 100644 --- a/pkg/target/telegram/telegram.go +++ b/pkg/target/telegram/telegram.go @@ -147,6 +147,10 @@ func (e *client) Send(result v1alpha2.PolicyReportResult) { func (e *client) CleanUp(_ context.Context, _ v1alpha2.ReportInterface) {} +func (e *client) Reset(_ context.Context) error { + return nil +} + func (e *client) BatchSend(_ v1alpha2.ReportInterface, _ []v1alpha2.PolicyReportResult) {} func (e *client) Type() target.ClientType { diff --git a/pkg/target/webhook/webhook.go b/pkg/target/webhook/webhook.go index 7ad7b618..032b7378 100644 --- a/pkg/target/webhook/webhook.go +++ b/pkg/target/webhook/webhook.go @@ -1,8 +1,6 @@ package webhook import ( - "context" - "github.com/kyverno/policy-reporter/pkg/crd/api/policyreport/v1alpha2" "github.com/kyverno/policy-reporter/pkg/target" "github.com/kyverno/policy-reporter/pkg/target/http" @@ -53,10 +51,6 @@ func (e *client) Send(result v1alpha2.PolicyReportResult) { http.ProcessHTTPResponse(e.Name(), resp, err) } -func (e *client) CleanUp(_ context.Context, _ v1alpha2.ReportInterface) {} - -func (e *client) BatchSend(_ v1alpha2.ReportInterface, _ []v1alpha2.PolicyReportResult) {} - func (e *client) Type() target.ClientType { return target.SingleSend }