datadog-operator icon indicating copy to clipboard operation
datadog-operator copied to clipboard

Support for CRD-specific watch namespaces configuration

Open levan-m opened this issue 1 year ago • 1 comments
trafficstars

What does this PR do?

CECO-1021

For Reviewer: first commit is actual change; second commit is mostly refactor.

controller-runtime 0.16 allows cache configuration on resource level. This change adds CRD-specific watch namespace env vars and sets them on corresponding CRDs or related resources.

Motivation

What inspired you to submit this pull request?

Additional Notes

Anything else we should know when reviewing?

Minimum Agent Versions

Are there minimum versions of the Datadog Agent and/or Cluster Agent required?

  • Agent: vX.Y.Z
  • Cluster Agent: vX.Y.Z

Describe your test plan

Test using DatadogMonitors CRD, use following manifests to create monitors in two different namespaces.

  • Namespace monitor1

    apiVersion: datadoghq.com/v1alpha1
    kind: DatadogMonitor
    metadata:
      name: datadog-monitor-test1
      namespace: monitor1
    spec:
      query: "avg(last_10m):avg:system.disk.in_use{*} by {host} > 0.5"
      type: "metric alert"
      name: "Test monitor made from DatadogMonitor monitor1"
      message: "We are running out of disk space!"
      tags:
        - "test:datadog"
    
  • Namespace monitor2

    apiVersion: datadoghq.com/v1alpha1
    kind: DatadogMonitor
    metadata:
      name: datadog-monitor-test2
      namespace: monitor2
    spec:
      query: "avg(last_10m):avg:system.disk.in_use{*} by {host} > 0.5"
      type: "metric alert"
      name: "Test monitor made from DatadogMonitor monitor2"
      message: "We are running out of disk space!"
      tags:
        - "test:datadog"
    

Operator doesn't watch namespace1 or namespace2.

  1. Run operator with datadogMonitorEnabled set to true (with API, APP key set), running in datadog namespace.
  2. Set default watch namespaces env variable to current namespace
        - name: WATCH_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace

Operator shouldn't reconcile the monitors from other namespaces (nothing logged or created).

Add monitor1 namespace

  1. In above setup add following env variable
      - name: MONITOR_WATCH_NAMESPACE
        value: "monitor1"

Observe monitor from monitor1 namespace getting reconciled - check logs and datadog app to confirm it's created.

  1. Add monitor2 to above env var

Observer monitor in monitor2 reconciled as well.

Write there any instructions and details you may have to test your PR.

Checklist

  • [ ] PR has at least one valid label: bug, enhancement, refactoring, documentation, tooling, and/or dependencies
  • [ ] PR has a milestone or the qa/skip-qa label

levan-m avatar Jun 14 '24 14:06 levan-m

Codecov Report

Attention: Patch coverage is 63.63636% with 40 lines in your changes missing coverage. Please review.

Project coverage is 55.03%. Comparing base (4bc766f) to head (10a4a65). Report is 354 commits behind head on main.

Files with missing lines Patch % Lines
pkg/config/config.go 67.96% 30 Missing and 3 partials :warning:
main.go 0.00% 7 Missing :warning:

:x: Your patch status has failed because the patch coverage (63.63%) is below the target coverage (80.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1235      +/-   ##
==========================================
+ Coverage   54.87%   55.03%   +0.16%     
==========================================
  Files         243      243              
  Lines       28120    28165      +45     
==========================================
+ Hits        15430    15502      +72     
+ Misses      11815    11785      -30     
- Partials      875      878       +3     
Flag Coverage Δ
unittests 55.03% <63.63%> (+0.16%) :arrow_up:

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
main.go 0.00% <0.00%> (ø)
pkg/config/config.go 67.92% <67.96%> (+67.92%) :arrow_up:

Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 4bc766f...10a4a65. Read the comment docs.

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

codecov-commenter avatar Jun 14 '24 14:06 codecov-commenter