Skip to content
This repository was archived by the owner on Jul 28, 2026. It is now read-only.

[dev.operator-integrations] Reconcile operator integrations - #1556

Merged
rfratto merged 11 commits into
grafana-cold-storage:dev.operator-integrationsfrom
rfratto:operator-integrations-reconcile
Apr 12, 2022
Merged

rfratto merged 11 commits into
grafana-cold-storage:dev.operator-integrationsfrom
rfratto:operator-integrations-reconcile

Conversation

@rfratto

@rfratto rfratto commented Apr 4, 2022 •

Copy link
Copy Markdown
Contributor

Related to #1414

Implements the reconcile loop and produces a working end-to-end example.

This is a massive PR; a lot of work has been done around refactoring and sharing as much code as possible to reduce bloating up the operator codebase even further.

I have done a somewhat-decent job and breaking up the changes across commits to make them more easily reviewable; you may find reviewing the PR commit-by-commit easier than reviewing the final diffs.

Example deployment of CRDs
apiVersion: monitoring.grafana.com/v1alpha1
kind: GrafanaAgent
metadata:
  name: grafana-agent-integrations-example
  namespace: default
  labels:
    app: grafana-agent-integrations-example
spec:
  image: grafana/agent:main
  logLevel: info
  serviceAccountName: grafana-agent
  storage:
    volumeClaimTemplate:
      spec:
        resources:
          requests:
            storage: 1Gi
  logs:
    instanceSelector:
      matchLabels:
        agent: grafana-agent-integrations-example
  metrics:
    instanceSelector:
      matchLabels:
        agent: grafana-agent-integrations-example
  integrations:
    selector:
      matchLabels:
        agent: grafana-agent-integrations-example

---

apiVersion: monitoring.grafana.com/v1alpha1
kind: MetricsInstance
metadata:
  name: primary
  namespace: default
  labels:
    agent: grafana-agent-integrations-example
spec:
  remoteWrite:
  - url: http://prometheus:9090/api/v1/write
  podMonitorNamespaceSelector: {}
  podMonitorSelector:
    matchLabels:
      instance: primary

---

apiVersion: monitoring.grafana.com/v1alpha1
kind: LogsInstance
metadata:
  name: primary
  namespace: default
  labels:
    agent: grafana-agent-integrations-example
spec:
  clients:
  - url: http://loki:8080/loki/api/v1/push

  # Supply an empty namespace selector to look in all namespaces.
  podLogsNamespaceSelector: {}
  podLogsSelector:
    matchLabels:
      instance: primary

---

# Have the Agent monitor itself.
apiVersion: monitoring.coreos.com/v1
kind: PodMonitor
metadata:
  name: grafana-agents
  namespace: default
  labels:
    instance: primary
spec:
  selector:
    matchLabels:
      app.kubernetes.io/name: grafana-agent
  podMetricsEndpoints:
    - port: http-metrics

---

# Have the Agent get logs from itself.
apiVersion: monitoring.grafana.com/v1alpha1
kind: PodLogs
metadata:
  name: grafana-agents
  namespace: default
  labels:
    instance: primary
spec:
  selector:
    matchLabels:
      app.kubernetes.io/name: grafana-agent
  pipelineStages:
    - cri: {}

---

# Collect node_exporter metrics.
apiVersion: monitoring.grafana.com/v1alpha1
kind: Integration
metadata:
  name: node-exporter
  namespace: default
  labels:
    agent: grafana-agent-integrations-example
spec:
  name: node_exporter
  type:
    allNodes: true
    unique: true
  config:
    autoscrape:
      enable: true
      metrics_instance: default/primary
    rootfs_path: /default/node_exporter/rootfs
    sysfs_path: /default/node_exporter/sys
    procfs_path: /default/node_exporter/proc
  volumeMounts:
    - mountPath: /default/node_exporter/proc
      name: proc
    - mountPath: /default/node_exporter/sys
      name: sys
    - mountPath: /default/node_exporter/rootfs
      name: root
  volumes:
    - name: proc
      hostPath:
        path: /proc
    - name: sys
      hostPath:
        path: /sys
    - name: root
      hostPath:
        path: /root
  # These aren't really used here, but just being mounted for local testing.
  secrets:
  - name: fake-secret
    key: key
  configMaps:
  - name: fake-configmap
    key: foo

---

# Collect kubernetes API events.
apiVersion: monitoring.grafana.com/v1alpha1
kind: Integration
metadata:
  name: eventhandler
  namespace: default
  labels:
    agent: grafana-agent-integrations-example
spec:
  name: eventhandler
  type:
    unique: true
  config:
    logs_instance: default/primary
    cache_path: "/var/lib/grafana-agent/data/eventhandler.cache"

---

#
# Pretend Secrets/ConfigMaps
#

---
apiVersion: v1
kind: Secret
metadata:
  name: fake-secret
  namespace: default
stringData:
  key: "value"

---
apiVersion: v1
kind: ConfigMap
metadata:
  name: fake-configmap
  namespace: default
data:
  foo: "bar"

#
# Extra resources
#

---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: grafana-agent
  namespace: default
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: grafana-agent
rules:
- apiGroups:
  - ""
  resources:
  - nodes
  - nodes/proxy
  - nodes/metrics
  - services
  - endpoints
  - pods
  - events # needed for eventhandler integration
  verbs:
  - get
  - list
  - watch
- nonResourceURLs:
  - /metrics
  - /metrics/cadvisor
  verbs:
  - get
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: grafana-agent
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: grafana-agent
subjects:
- kind: ServiceAccount
  name: grafana-agent
  namespace: default

rfratto added 5 commits March 29, 2022 13:57
Extract the generation of pod templates out into its own function: the
implementation of generating the DaemonSetSpec (for logs) and
StatefulSetSpec (for metrics) was incredibly similar, and this will be
doubly true for integrations. A shared function for generating the
template will help remove duplication and avoid bugs where template
generation becomes out of sync for the various generated resources.
Comment thread pkg/operator/build_hierarchy.go
Comment thread pkg/operator/operator.go
Comment thread pkg/operator/operator.go
Comment thread pkg/operator/resources_integrations.go
@rfratto
rfratto marked this pull request as ready for review April 4, 2022 18:30
@rfratto
rfratto requested review from captncraig and rlankfo April 4, 2022 18:32
Comment thread pkg/operator/apis/monitoring/v1alpha1/types_integrations.go Outdated
Comment thread pkg/operator/reconciler_integrations.go

@rlankfo rlankfo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@rfratto
rfratto merged commit a9caccd into grafana-cold-storage:dev.operator-integrations Apr 12, 2022
@rfratto
rfratto deleted the operator-integrations-reconcile branch April 12, 2022 18:23
rfratto added a commit to rfratto/agent that referenced this pull request Apr 12, 2022
…cold-storage#1556)

* pkg/operator: refactor pod template generation into its own function

Extract the generation of pod templates out into its own function: the
implementation of generating the DaemonSetSpec (for logs) and
StatefulSetSpec (for metrics) was incredibly similar, and this will be
doubly true for integrations. A shared function for generating the
template will help remove duplication and avoid bugs where template
generation becomes out of sync for the various generated resources.

* pkg/operator: move more common logic to generatePodTemplate

* pkg/operator: make generating pod templates its own function

* pkg/operator: share generated ObjectMeta between pod controllers

* pkg/operator: reconcile integrations

* pkg/operator: resolve lint errors

* pkg/operator: e2e test deployment of integrations

* docs: update docs for RBAC additions

* pkg/operator: fix lint errors

* pkg/operator/apis: fix "order resources" typo (other resources)

* pkg/operator: elaborate why deleteManagedResource gets called
rfratto added a commit that referenced this pull request Apr 12, 2022
* [dev.operator-integrations] Add Integration into operator resource hierarchy (#1417)

* add CRDs for integrations

* move pkg/operator/config.Deployment to pkg/operator/apis/monitoring/v1alpha1

This allows us to easily generate the DeepCopy implementation for the
struct instead of manually creating it.

This commit also makes gragent the consistent name to refer to
pkg/operator/apis/monitoring/v1alpha1, which is short and
understandable.

* Mark CRDs as generated to hide their diffs from GitHub PR reviews

* test Integrations in resource hierarchy

* go mod tidy

* fix doc error for ConfigMaps

* [dev.operator-integrations] pkg/operator/config: Generate config for integrations (#1503)

* pkg/operator/config: Support integration-specific configs

* document why we want to skip adding a __replica__ label for integration metrics

* [dev.operator-integrations] Reconcile operator integrations (#1556)

* pkg/operator: refactor pod template generation into its own function

Extract the generation of pod templates out into its own function: the
implementation of generating the DaemonSetSpec (for logs) and
StatefulSetSpec (for metrics) was incredibly similar, and this will be
doubly true for integrations. A shared function for generating the
template will help remove duplication and avoid bugs where template
generation becomes out of sync for the various generated resources.

* pkg/operator: move more common logic to generatePodTemplate

* pkg/operator: make generating pod templates its own function

* pkg/operator: share generated ObjectMeta between pod controllers

* pkg/operator: reconcile integrations

* pkg/operator: resolve lint errors

* pkg/operator: e2e test deployment of integrations

* docs: update docs for RBAC additions

* pkg/operator: fix lint errors

* pkg/operator/apis: fix "order resources" typo (other resources)

* pkg/operator: elaborate why deleteManagedResource gets called

* update changelog
@github-actions github-actions Bot added the frozen-due-to-age Locked due to a period of inactivity. Please open new issues or PRs if more discussion is needed. label Mar 29, 2024
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Mar 29, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

frozen-due-to-age Locked due to a period of inactivity. Please open new issues or PRs if more discussion is needed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants