Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check isLeader in compliance checks if function given #6311

Merged
merged 1 commit into from
Sep 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/cluster-agent/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ func start(cmd *cobra.Command, args []string) error {
go func() {
defer wg.Done()

if err := runCompliance(mainCtx); err != nil {
if err := runCompliance(mainCtx, apiCl, le.IsLeader); err != nil {
log.Errorf("Error while running compliance agent: %v", err)
}
}()
Expand Down
12 changes: 4 additions & 8 deletions cmd/cluster-agent/app/compliance.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,9 @@ import (
"github.com/DataDog/datadog-agent/pkg/util/log"
)

func runCompliance(ctx context.Context) error {
apiCl, err := apiserver.WaitForAPIClient(ctx)
if err != nil {
return err
}

func runCompliance(ctx context.Context, apiCl *apiserver.APIClient, isLeader func() bool) error {
stopper := restart.NewSerialStopper()
if err := startCompliance(stopper, apiCl); err != nil {
if err := startCompliance(stopper, apiCl, isLeader); err != nil {
return err
}

Expand All @@ -47,7 +42,7 @@ func runCompliance(ctx context.Context) error {
}

// TODO: Factorize code with pkg/compliance
func startCompliance(stopper restart.Stopper, apiCl *apiserver.APIClient) error {
func startCompliance(stopper restart.Stopper, apiCl *apiserver.APIClient, isLeader func() bool) error {
httpConnectivity := config.HTTPConnectivityFailure
if endpoints, err := config.BuildHTTPEndpoints(); err == nil {
httpConnectivity = http.CheckConnectivity(endpoints.Main)
Expand Down Expand Up @@ -105,6 +100,7 @@ func startCompliance(stopper restart.Stopper, apiCl *apiserver.APIClient) error
return rule.Scope.Includes(compliance.KubernetesClusterScope)
}),
checks.WithKubernetesClient(apiCl.DynamicCl),
checks.WithIsLeader(isLeader),
)
if err != nil {
return err
Expand Down
16 changes: 16 additions & 0 deletions pkg/compliance/checks/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,14 @@ func WithKubernetesClient(cli env.KubeClient) BuilderOption {
}
}

// WithIsLeader allows check runner to know if its a leader instance or not (DCA)
func WithIsLeader(isLeader func() bool) BuilderOption {
return func(b *builder) error {
b.isLeaderFunc = isLeader
return nil
}
}

// SuiteMatcher checks if a compliance suite is included
type SuiteMatcher func(*compliance.SuiteMeta) bool

Expand Down Expand Up @@ -220,6 +228,7 @@ type builder struct {
dockerClient env.DockerClient
auditClient env.AuditClient
kubeClient env.KubeClient
isLeaderFunc func() bool

status *status
}
Expand Down Expand Up @@ -506,6 +515,13 @@ func (b *builder) RelativeToHostRoot(path string) string {
return b.pathMapper.relativeToHostRoot(path)
}

func (b *builder) IsLeader() bool {
if b.isLeaderFunc != nil {
return b.isLeaderFunc()
}
return true
}

func (b *builder) EvaluateFromCache(ev eval.Evaluatable) (interface{}, error) {
instance := &eval.Instance{
Functions: eval.FunctionMap{
Expand Down
4 changes: 4 additions & 0 deletions pkg/compliance/checks/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ func (c *complianceCheck) IsTelemetryEnabled() bool {
}

func (c *complianceCheck) Run() error {
if !c.IsLeader() {
return nil
}

report, err := c.checkable.check(c)
if err != nil {
log.Warnf("%s: check run failed: %v", c.ruleID, err)
Expand Down
36 changes: 36 additions & 0 deletions pkg/compliance/checks/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ func TestCheckRun(t *testing.T) {
}

if test.configErr == nil {
env.On("IsLeader").Return(true)
env.On("Reporter").Return(reporter)
reporter.On("Report", test.expectEvent).Once()
checkable.On("check", check).Return(test.checkReport, test.checkErr)
Expand All @@ -115,3 +116,38 @@ func TestCheckRun(t *testing.T) {
})
}
}

func TestCheckRunNoLeader(t *testing.T) {
const (
ruleID = "rule-id"
resourceType = "resource-type"
resourceID = "resource-id"
)

assert := assert.New(t)

env := &mocks.Env{}
defer env.AssertExpectations(t)

reporter := &mocks.Reporter{}
defer reporter.AssertExpectations(t)

checkable := &mockCheckable{}
defer checkable.AssertExpectations(t)

check := &complianceCheck{
Env: env,

ruleID: ruleID,
resourceType: resourceType,
resourceID: resourceID,
checkable: checkable,
}

// Not leader
env.On("IsLeader").Return(false)
checkable.AssertNotCalled(t, "check")

err := check.Run()
assert.Nil(err)
}
1 change: 1 addition & 0 deletions pkg/compliance/checks/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ type Configuration interface {
NormalizeToHostRoot(path string) string
RelativeToHostRoot(path string) string
EvaluateFromCache(e eval.Evaluatable) (interface{}, error)
IsLeader() bool
}
2 changes: 1 addition & 1 deletion pkg/compliance/mocks/audit_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 17 additions & 1 deletion pkg/compliance/mocks/builder.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/compliance/mocks/clients.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion pkg/compliance/mocks/configuration.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/compliance/mocks/docker_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion pkg/compliance/mocks/env.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/compliance/mocks/evaluatable.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/compliance/mocks/iterator.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/compliance/mocks/reporter.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/compliance/mocks/scheduler.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.