Skip to content
This repository was archived by the owner on Jul 28, 2026. It is now read-only.
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@
- [BUGFIX] Updated envsubst to v2.0.0-20210730161058-179042472c46. This version has a fix needed for escaping values
outside of variable substitutions. (@rlankfo)

- [BUGFIX] Grafana Agent Operator should no longer delete resources matching
the names of the resources it manages. (@rfratto)

- [BUGFIX] Grafana Agent Operator will now appropriately assign an
`app.kubernetes.io/managed-by=grafana-agent-operator` to all created
resources.

- [CHANGE] Configuration API now returns 404 instead of 400 when attempting to get or delete a config
which does not exist. (@kgeckhart)

Expand Down
3 changes: 3 additions & 0 deletions pkg/operator/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ func (r *reconciler) createSecrets(
Name: d.Agent.Name,
UID: d.Agent.UID,
}},
Labels: map[string]string{
managedByOperatorLabel: managedByOperatorLabelValue,
},
},
Data: data,
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/operator/reconciler_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (r *reconciler) createLogsDaemonSet(

var ds apps_v1.DaemonSet
err := r.Client.Get(ctx, key, &ds)
if k8s_errors.IsNotFound(err) {
if k8s_errors.IsNotFound(err) || !isManagedResource(&ds) {
return nil
} else if err != nil {
return fmt.Errorf("failed to find stale DaemonSet %s: %w", key, err)
Expand Down
7 changes: 4 additions & 3 deletions pkg/operator/reconciler_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (r *reconciler) createTelemetryConfigurationSecret(
if !shouldCreate {
var secret core_v1.Secret
err := r.Client.Get(ctx, key, &secret)
if k8s_errors.IsNotFound(err) {
if k8s_errors.IsNotFound(err) || !isManagedResource(&secret) {
return nil
} else if err != nil {
return fmt.Errorf("failed to find stale secret %s: %w", key, err)
Expand Down Expand Up @@ -125,7 +125,7 @@ func (r *reconciler) createMetricsGoverningService(

var service core_v1.Service
err := r.Client.Get(ctx, key, &service)
if k8s_errors.IsNotFound(err) {
if k8s_errors.IsNotFound(err) || !isManagedResource(&service) {
return nil
} else if err != nil {
return fmt.Errorf("failed to find stale Service %s: %w", key, err)
Expand Down Expand Up @@ -191,7 +191,8 @@ func (r *reconciler) createMetricsStatefulSets(
var statefulSets apps_v1.StatefulSetList
err := r.List(ctx, &statefulSets, &client.ListOptions{
LabelSelector: labels.SelectorFromSet(labels.Set{
agentNameLabelName: d.Agent.Name,
managedByOperatorLabel: managedByOperatorLabelValue,
agentNameLabelName: d.Agent.Name,
}),
})
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/operator/resources_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func generateLogsDaemonSet(
}
labels[agentNameLabelName] = d.Agent.Name
labels[agentTypeLabel] = "logs"
labels[managedByOperatorLabel] = managedByOperatorLabelValue

boolTrue := true
ds := &apps_v1.DaemonSet{
Expand Down
28 changes: 19 additions & 9 deletions pkg/operator/resources_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
v1 "k8s.io/api/core/v1"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"sigs.k8s.io/controller-runtime/pkg/client"
)

const (
Expand All @@ -33,6 +34,13 @@ var (
probeTimeoutSeconds int32 = 3
)

// isManagedResource returns true if the given object has a managed-by
// grafana-agent-operator label.
func isManagedResource(obj client.Object) bool {

@rlankfo rlankfo Oct 25, 2021

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.

nit: This implementation doesn't need to loop through the map labels vs more efficiently just looking up by key. Perhaps something like this could work, where callers are responsible to call GetLabels()

func isManagedResource(labels map[string]string) bool {
	if labels == nil {
		return false
	}
	value, ok := labels[managedByOperatorLabel]
	return ok && value == managedByOperatorLabelValue
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

d'oh :) I forgot it was a map. Dropping the for loop makes sense here, but I still think it should accept a client.Object as an argument just to make it a little easier to manage at the call site.

labelValue := obj.GetLabels()[managedByOperatorLabel]
return labelValue == managedByOperatorLabelValue
}

func generateMetricsStatefulSetService(cfg *Config, d config.Deployment) *v1.Service {
d = *d.DeepCopy()

Expand All @@ -55,7 +63,8 @@ func generateMetricsStatefulSetService(cfg *Config, d config.Deployment) *v1.Ser
UID: d.Agent.UID,
}},
Labels: cfg.Labels.Merge(map[string]string{
"operated-agent": "true",
managedByOperatorLabel: managedByOperatorLabelValue,
"operated-agent": "true",
}),
},
Spec: v1.ServiceSpec{
Expand Down Expand Up @@ -120,6 +129,7 @@ func generateMetricsStatefulSet(
}
labels[agentNameLabelName] = d.Agent.Name
labels[agentTypeLabel] = "metrics"
labels[managedByOperatorLabel] = managedByOperatorLabelValue

boolTrue := true

Expand Down Expand Up @@ -314,14 +324,14 @@ func generateMetricsStatefulSetSpec(
podAnnotations := map[string]string{}
podLabels := map[string]string{}
podSelectorLabels := map[string]string{
"app.kubernetes.io/name": "grafana-agent",
"app.kubernetes.io/version": build.Version,
"app.kubernetes.io/managed-by": "grafana-agent-operator",
"app.kubernetes.io/instance": d.Agent.Name,
"grafana-agent": d.Agent.Name,
shardLabelName: fmt.Sprintf("%d", shard),
agentNameLabelName: d.Agent.Name,
agentTypeLabel: "metrics",
"app.kubernetes.io/name": "grafana-agent",
"app.kubernetes.io/version": build.Version,
"app.kubernetes.io/instance": d.Agent.Name,
"grafana-agent": d.Agent.Name,
managedByOperatorLabel: managedByOperatorLabelValue,
shardLabelName: fmt.Sprintf("%d", shard),
agentNameLabelName: d.Agent.Name,
agentTypeLabel: "metrics",
}
if d.Agent.Spec.PodMetadata != nil {
for k, v := range d.Agent.Spec.PodMetadata.Labels {
Expand Down