diff --git a/api/operator/v1beta1/vmagent_types.go b/api/operator/v1beta1/vmagent_types.go index 72e6b6c2..12804806 100644 --- a/api/operator/v1beta1/vmagent_types.go +++ b/api/operator/v1beta1/vmagent_types.go @@ -166,7 +166,7 @@ type VMAgentSpec struct { // If both nil - behaviour controlled by selectAllByDefault // +optional NodeScrapeNamespaceSelector *metav1.LabelSelector `json:"nodeScrapeNamespaceSelector,omitempty"` - // StaticScrapeSelector defines PodScrapes to be selected for target discovery. + // StaticScrapeSelector defines VMStaticScrape to be selected for target discovery. // Works in combination with NamespaceSelector. // If both nil - match everything. // NamespaceSelector nil - only objects at VMAgent namespace. @@ -636,7 +636,7 @@ func (cr VMAgent) ProbeNeedLiveness() bool { return true } -// IsUnmanaged checks if object should managed any config objects +// IsUnmanaged checks if object should managed any config objects func (cr *VMAgent) IsUnmanaged() bool { // fast path if cr.Spec.IngestOnlyMode { @@ -647,9 +647,70 @@ func (cr *VMAgent) IsUnmanaged() bool { cr.Spec.ServiceScrapeSelector == nil && cr.Spec.ServiceScrapeNamespaceSelector == nil && cr.Spec.PodScrapeSelector == nil && cr.Spec.PodScrapeNamespaceSelector == nil && cr.Spec.ProbeSelector == nil && cr.Spec.ProbeNamespaceSelector == nil && + cr.Spec.StaticScrapeSelector == nil && cr.Spec.StaticScrapeNamespaceSelector == nil && + cr.Spec.ScrapeConfigSelector == nil && cr.Spec.ScrapeConfigNamespaceSelector == nil +} + +// IsNodeScrapeUnmanaged checks if vmagent should managed any VMNodeScrape objects +func (cr *VMAgent) IsNodeScrapeUnmanaged() bool { + // fast path + if cr.Spec.IngestOnlyMode { + return true + } + return !cr.Spec.SelectAllByDefault && + cr.Spec.NodeScrapeSelector == nil && cr.Spec.NodeScrapeNamespaceSelector == nil +} + +// IsServiceScrapeUnmanaged checks if vmagent should managed any VMServiceScrape objects +func (cr *VMAgent) IsServiceScrapeUnmanaged() bool { + // fast path + if cr.Spec.IngestOnlyMode { + return true + } + return !cr.Spec.SelectAllByDefault && + cr.Spec.ServiceScrapeSelector == nil && cr.Spec.ServiceScrapeNamespaceSelector == nil +} + +// IsUnmanaged checks if vmagent should managed any VMPodScrape objects +func (cr *VMAgent) IsPodScrapeUnmanaged() bool { + // fast path + if cr.Spec.IngestOnlyMode { + return true + } + return !cr.Spec.SelectAllByDefault && + cr.Spec.PodScrapeSelector == nil && cr.Spec.PodScrapeNamespaceSelector == nil +} + +// IsProbeUnmanaged checks if vmagent should managed any VMProbe objects +func (cr *VMAgent) IsProbeUnmanaged() bool { + // fast path + if cr.Spec.IngestOnlyMode { + return true + } + return !cr.Spec.SelectAllByDefault && + cr.Spec.ProbeSelector == nil && cr.Spec.ProbeNamespaceSelector == nil +} + +// IsStaticScrapeUnmanaged checks if vmagent should managed any VMStaticScrape objects +func (cr *VMAgent) IsStaticScrapeUnmanaged() bool { + // fast path + if cr.Spec.IngestOnlyMode { + return true + } + return !cr.Spec.SelectAllByDefault && cr.Spec.StaticScrapeSelector == nil && cr.Spec.StaticScrapeNamespaceSelector == nil } +// IsScrapeConfigUnmanaged checks if vmagent should managed any VMScrapeConfig objects +func (cr *VMAgent) IsScrapeConfigUnmanaged() bool { + // fast path + if cr.Spec.IngestOnlyMode { + return true + } + return !cr.Spec.SelectAllByDefault && + cr.Spec.ScrapeConfigSelector == nil && cr.Spec.ScrapeConfigNamespaceSelector == nil +} + // LastAppliedSpecAsPatch return last applied cluster spec as patch annotation func (cr *VMAgent) LastAppliedSpecAsPatch() (client.Patch, error) { data, err := json.Marshal(cr.Spec) @@ -708,7 +769,6 @@ func (cr *VMAgent) HasAnyStreamAggrRule() bool { // SetStatusTo changes update status with optional reason of fail func (cr *VMAgent) SetUpdateStatusTo(ctx context.Context, r client.Client, status UpdateStatus, maybeErr error) error { - return updateObjectStatus(ctx, r, &patchStatusOpts[*VMAgent, *VMAgentStatus]{ actualStatus: status, cr: cr, diff --git a/config/crd/overlay/crd.yaml b/config/crd/overlay/crd.yaml index f88c39bb..2324b2df 100644 --- a/config/crd/overlay/crd.yaml +++ b/config/crd/overlay/crd.yaml @@ -4929,7 +4929,7 @@ spec: type: array staticScrapeSelector: description: |- - StaticScrapeSelector defines PodScrapes to be selected for target discovery. + StaticScrapeSelector defines VMStaticScrape to be selected for target discovery. Works in combination with NamespaceSelector. If both nil - match everything. NamespaceSelector nil - only objects at VMAgent namespace. diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index f97c4e26..4bb4deb3 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -21,6 +21,8 @@ aliases: - [vmoperator](https://docs.victoriametrics.com/operator/): bump default version of VictoriaMetrics components to [1.106.1](https://github.com/VictoriaMetrics/VictoriaMetrics/releases/tag/v1.106.1). - [vmoperator](https://docs.victoriametrics.com/operator/): add new variable `VM_VMSERVICESCRAPEDEFAULT_ENFORCEENDPOINTSLICES` to use `endpointslices` instead of `endpoints` as discovery role for VMServiceScrape when generate scrape config for VMAgent. - [vmoperator](https://docs.victoriametrics.com/operator/): adds new flag `loggerJSONFields` to the operator logger configuration. It allows to change json encoder fields. See [this issue](https://github.com/VictoriaMetrics/operator/issues/1157) for details. +- [vmoperator](https://docs.victoriametrics.com/operator/): fix the behaviors of `vmagentSpec.ScrapeConfigSelector` and `vmagentSpec.scrapeConfigNamespaceSelector` when `vmagentSpec.selectAllByDefault=false`. Previously, the VMScrapeConfig could be ignored. +- [vmoperator](https://docs.victoriametrics.com/operator/): fix the behaviors of `xxxNamespaceSelector` when `vmagentSpec.selectAllByDefault=true`. See [this doc](https://docs.victoriametrics.com/operator/resources/vmagent/#scraping) for detailed rules. - [api](https://docs.victoriametrics.com/operator/api): adds new status field `observedGeneration`. See [this issue](https://github.com/VictoriaMetrics/operator/issues/1155) for details. - [api](https://docs.victoriametrics.com/operator/api): unify `updateStatus` field for CRD objects. It replaces `status`, `clusterStatus` and `singleStatus` for `VLogs`, `VMCluster` and `VMSingle` with generic `updateStatus`. - [alerts]: added cluster label for multicluster alerts. diff --git a/docs/api.md b/docs/api.md index f4dd7bc7..38e0aa2c 100644 --- a/docs/api.md +++ b/docs/api.md @@ -2696,7 +2696,7 @@ _Appears in:_ | `statefulStorage` | StatefulStorage configures storage for StatefulSet | _[StorageSpec](#storagespec)_ | false | | `staticScrapeNamespaceSelector` | StaticScrapeNamespaceSelector defines Namespaces to be selected for VMStaticScrape discovery.
Works in combination with NamespaceSelector.
NamespaceSelector nil - only objects at VMAgent namespace.
Selector nil - only objects at NamespaceSelector namespaces.
If both nil - behaviour controlled by selectAllByDefault | _[LabelSelector](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.30/#labelselector-v1-meta)_ | false | | `staticScrapeRelabelTemplate` | StaticScrapeRelabelTemplate defines relabel config, that will be added to each VMStaticScrape.
it's useful for adding specific labels to all targets | _[RelabelConfig](#relabelconfig) array_ | false | -| `staticScrapeSelector` | StaticScrapeSelector defines PodScrapes to be selected for target discovery.
Works in combination with NamespaceSelector.
If both nil - match everything.
NamespaceSelector nil - only objects at VMAgent namespace.
Selector nil - only objects at NamespaceSelector namespaces. | _[LabelSelector](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.30/#labelselector-v1-meta)_ | false | +| `staticScrapeSelector` | StaticScrapeSelector defines VMStaticScrape to be selected for target discovery.
Works in combination with NamespaceSelector.
If both nil - match everything.
NamespaceSelector nil - only objects at VMAgent namespace.
Selector nil - only objects at NamespaceSelector namespaces. | _[LabelSelector](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.30/#labelselector-v1-meta)_ | false | | `streamAggrConfig` | StreamAggrConfig defines global stream aggregation configuration for VMAgent | _[StreamAggrConfig](#streamaggrconfig)_ | false | | `terminationGracePeriodSeconds` | TerminationGracePeriodSeconds period for container graceful termination | _integer_ | false | | `tolerations` | Tolerations If specified, the pod's tolerations. | _[Toleration](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.30/#toleration-v1-core) array_ | false | diff --git a/internal/controller/operator/controllers.go b/internal/controller/operator/controllers.go index ee35d74f..fc07f5b9 100644 --- a/internal/controller/operator/controllers.go +++ b/internal/controller/operator/controllers.go @@ -199,8 +199,14 @@ func isNamespaceSelectorMatches(ctx context.Context, rclient client.Client, sour return false, nil } -func isSelectorsMatchesTargetCRD(ctx context.Context, rclient client.Client, sourceCRD, targetCRD client.Object, selector, namespaceSelector *metav1.LabelSelector) (bool, error) { - // check namespace selector +// isSelectorsMatchesTargetCRD checks if targetCRD matches sourceCRD by selector, namespaceSelector and selectAll. +// see https://docs.victoriametrics.com/operator/resources/vmagent/#scraping for details +func isSelectorsMatchesTargetCRD(ctx context.Context, rclient client.Client, sourceCRD, targetCRD client.Object, selector, namespaceSelector *metav1.LabelSelector, selectAll bool) (bool, error) { + // selectAll only works when NamespaceSelector and Selector both undefined + if selector == nil && namespaceSelector == nil { + return selectAll, nil + } + // check namespace selector, only return when NS not match if isNsMatch, err := isNamespaceSelectorMatches(ctx, rclient, sourceCRD, targetCRD, namespaceSelector); !isNsMatch || err != nil { return isNsMatch, err } diff --git a/internal/controller/operator/controllers_test.go b/internal/controller/operator/controllers_test.go index 4a147d93..cee7eb06 100644 --- a/internal/controller/operator/controllers_test.go +++ b/internal/controller/operator/controllers_test.go @@ -16,6 +16,7 @@ import ( func TestIsSelectorsMatchesTargetCRD(t *testing.T) { tests := []struct { name string + selectAll bool sourceCRD client.Object targetCRD client.Object selector *metav1.LabelSelector @@ -24,11 +25,12 @@ func TestIsSelectorsMatchesTargetCRD(t *testing.T) { isMatch bool }{ { - name: "matches cause under same namespace", + name: "match: selectors are nil, selectAll=true", + selectAll: true, sourceCRD: &vmv1beta1.VMRule{ ObjectMeta: metav1.ObjectMeta{ Name: "rule", - Namespace: "default", + Namespace: "n1", Labels: map[string]string{ "app": "target-app", }, @@ -37,7 +39,7 @@ func TestIsSelectorsMatchesTargetCRD(t *testing.T) { targetCRD: &vmv1beta1.VMAlert{ ObjectMeta: metav1.ObjectMeta{ Name: "test-vmalert", - Namespace: "default", + Namespace: "n2", }, Spec: vmv1beta1.VMAlertSpec{ RuleSelector: &metav1.LabelSelector{}, @@ -49,7 +51,8 @@ func TestIsSelectorsMatchesTargetCRD(t *testing.T) { isMatch: true, }, { - name: "not match", + name: "not match: selectors are nil, selectAll=false", + selectAll: false, sourceCRD: &vmv1beta1.VMRule{ ObjectMeta: metav1.ObjectMeta{ Name: "rule", @@ -62,7 +65,7 @@ func TestIsSelectorsMatchesTargetCRD(t *testing.T) { targetCRD: &vmv1beta1.VMAlert{ ObjectMeta: metav1.ObjectMeta{ Name: "test-vmalert", - Namespace: "vm-stack", + Namespace: "default", }, Spec: vmv1beta1.VMAlertSpec{ RuleSelector: &metav1.LabelSelector{}, @@ -74,7 +77,8 @@ func TestIsSelectorsMatchesTargetCRD(t *testing.T) { isMatch: false, }, { - name: "selector matches", + name: "match: selector matches, selectAll=any", + selectAll: false, sourceCRD: &vmv1beta1.VMRule{ ObjectMeta: metav1.ObjectMeta{ Name: "rule", @@ -112,7 +116,8 @@ func TestIsSelectorsMatchesTargetCRD(t *testing.T) { isMatch: true, }, { - name: "selector not match", + name: "not match: selector not match, selectAll=any", + selectAll: true, sourceCRD: &vmv1beta1.VMRule{ ObjectMeta: metav1.ObjectMeta{ Name: "rule", @@ -163,7 +168,8 @@ func TestIsSelectorsMatchesTargetCRD(t *testing.T) { isMatch: false, }, { - name: "namespaceselector matches", + name: "match: namespaceselector matches, selectAll=any", + selectAll: false, sourceCRD: &vmv1beta1.VMRule{ ObjectMeta: metav1.ObjectMeta{ Name: "rule", @@ -204,7 +210,8 @@ func TestIsSelectorsMatchesTargetCRD(t *testing.T) { isMatch: true, }, { - name: "namespaceselector not matches", + name: "not match: namespaceselector not matches, selectAll=any", + selectAll: true, sourceCRD: &vmv1beta1.VMRule{ ObjectMeta: metav1.ObjectMeta{ Name: "rule", @@ -245,7 +252,8 @@ func TestIsSelectorsMatchesTargetCRD(t *testing.T) { isMatch: false, }, { - name: "selector+namespaceSelector match", + name: "match: selector+namespaceSelector match, selectAll=any", + selectAll: false, sourceCRD: &vmv1beta1.VMRule{ ObjectMeta: metav1.ObjectMeta{ Name: "rule", @@ -305,7 +313,7 @@ func TestIsSelectorsMatchesTargetCRD(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { fclient := k8stools.GetTestClientWithObjects(tt.predefinedObjects) - matches, err := isSelectorsMatchesTargetCRD(context.Background(), fclient, tt.sourceCRD, tt.targetCRD, tt.selector, tt.namespaceSelector) + matches, err := isSelectorsMatchesTargetCRD(context.Background(), fclient, tt.sourceCRD, tt.targetCRD, tt.selector, tt.namespaceSelector, tt.selectAll) if err != nil { t.Fatal(err) } diff --git a/internal/controller/operator/factory/alertmanager/config_test.go b/internal/controller/operator/factory/alertmanager/config_test.go index 57ad182f..c28a0969 100644 --- a/internal/controller/operator/factory/alertmanager/config_test.go +++ b/internal/controller/operator/factory/alertmanager/config_test.go @@ -1459,11 +1459,9 @@ func Test_UpdateDefaultAMConfig(t *testing.T) { Namespace: "default", }, Spec: vmv1beta1.VMAlertmanagerSpec{ - ConfigSecret: "vmalertmanager-test-am-config", - ConfigRawYaml: "global: {}", - ConfigSelector: &metav1.LabelSelector{}, - ConfigNamespaceSelector: &metav1.LabelSelector{}, - SelectAllByDefault: true, + ConfigSecret: "vmalertmanager-test-am-config", + ConfigRawYaml: "global: {}", + SelectAllByDefault: true, }, }, }, diff --git a/internal/controller/operator/factory/k8stools/selectors.go b/internal/controller/operator/factory/k8stools/selectors.go index 95a5f26d..da034d9c 100644 --- a/internal/controller/operator/factory/k8stools/selectors.go +++ b/internal/controller/operator/factory/k8stools/selectors.go @@ -38,24 +38,23 @@ func VisitObjectsForSelectorsAtNs[T any, PT interface { case objectSelector != nil && nsSelector == nil: // in single namespace mode, return object ns namespaces = append(namespaces, objNamespace) - default: + case nsSelector != nil: // perform a cluster wide request for namespaces with given filters nsSelector, err := metav1.LabelSelectorAsSelector(nsSelector) if err != nil { - return fmt.Errorf("cannot convert selector: %w", err) + return fmt.Errorf("cannot convert selector: %w", err) } namespaces, err = SelectNamespaces(ctx, rclient, nsSelector) if err != nil { return fmt.Errorf("cannot select namespaces for match: %w", err) } - } - // fast path nothing selected - if namespaces == nil && !selectAllByDefault { - return nil + // if nsSelector is specified and no match, return directly + if len(namespaces) == 0 { + return nil + } } - // if namespaces isn't nil, then nameSpaceSelector is defined - // but userSelector maybe be nil and we must set it to catch all values + // if userSelector is nil, we must set it to catch all values if objectSelector == nil { objectSelector = &metav1.LabelSelector{} } @@ -63,6 +62,7 @@ func VisitObjectsForSelectorsAtNs[T any, PT interface { if err != nil { return fmt.Errorf("cannot convert to Selector: %w", err) } + // namespaces could still be empty if nsSelector&objectSelector are nil and selectAllByDefault=true, and it's ok return ListObjectsByNamespace(ctx, rclient, namespaces, cb, &client.ListOptions{LabelSelector: objLabelSelector}) } diff --git a/internal/controller/operator/factory/vmagent/collect_scrapes.go b/internal/controller/operator/factory/vmagent/collect_scrapes.go index 33547126..702529d8 100644 --- a/internal/controller/operator/factory/vmagent/collect_scrapes.go +++ b/internal/controller/operator/factory/vmagent/collect_scrapes.go @@ -17,7 +17,7 @@ func selectScrapeConfig(ctx context.Context, cr *vmv1beta1.VMAgent, rclient clie var scrapeConfigsCombined []*vmv1beta1.VMScrapeConfig var namespacedNames []string - if err := k8stools.VisitObjectsForSelectorsAtNs(ctx, rclient, cr.Spec.NodeScrapeNamespaceSelector, cr.Spec.ScrapeConfigSelector, cr.Namespace, cr.Spec.SelectAllByDefault, + if err := k8stools.VisitObjectsForSelectorsAtNs(ctx, rclient, cr.Spec.ScrapeConfigNamespaceSelector, cr.Spec.ScrapeConfigSelector, cr.Namespace, cr.Spec.SelectAllByDefault, func(list *vmv1beta1.VMScrapeConfigList) { for _, item := range list.Items { if !item.DeletionTimestamp.IsZero() { diff --git a/internal/controller/operator/vmalertmanagerconfig_controller.go b/internal/controller/operator/vmalertmanagerconfig_controller.go index 1750bfea..4dbef325 100644 --- a/internal/controller/operator/vmalertmanagerconfig_controller.go +++ b/internal/controller/operator/vmalertmanagerconfig_controller.go @@ -92,9 +92,10 @@ func (r *VMAlertmanagerConfigReconciler) Reconcile(ctx context.Context, req ctrl l := l.WithValues("parent_alertmanager", am.Name, "parent_namespace", am.Namespace) ctx := logger.AddToContext(ctx, l) - // only check selector when deleting, since labels can be changed when updating and we can't tell if it was selected before. - if instance.DeletionTimestamp.IsZero() && !am.Spec.SelectAllByDefault { - match, err := isSelectorsMatchesTargetCRD(ctx, r.Client, &instance, am, am.Spec.ConfigSelector, am.Spec.ConfigNamespaceSelector) + // only check selector when deleting object, + // since labels can be changed when updating and we can't tell if it was selected before, and we can't tell if it's creating or updating. + if !instance.DeletionTimestamp.IsZero() { + match, err := isSelectorsMatchesTargetCRD(ctx, r.Client, &instance, am, am.Spec.ConfigSelector, am.Spec.ConfigNamespaceSelector, am.Spec.SelectAllByDefault) if err != nil { l.Error(err, "cannot match alertmanager against selector, probably bug") continue diff --git a/internal/controller/operator/vmnodescrape_controller.go b/internal/controller/operator/vmnodescrape_controller.go index 3de8360c..7cdfd9d4 100644 --- a/internal/controller/operator/vmnodescrape_controller.go +++ b/internal/controller/operator/vmnodescrape_controller.go @@ -85,16 +85,17 @@ func (r *VMNodeScrapeReconciler) Reconcile(ctx context.Context, req ctrl.Request } for _, vmagentItem := range objects.Items { - if !vmagentItem.DeletionTimestamp.IsZero() || vmagentItem.Spec.ParsingError != "" || vmagentItem.IsUnmanaged() { + if !vmagentItem.DeletionTimestamp.IsZero() || vmagentItem.Spec.ParsingError != "" || vmagentItem.IsNodeScrapeUnmanaged() { continue } currentVMagent := &vmagentItem reqLogger := reqLogger.WithValues("parent_vmagent", currentVMagent.Name, "parent_namespace", currentVMagent.Namespace) ctx := logger.AddToContext(ctx, reqLogger) - // only check selector when deleting, since labels can be changed when updating and we can't tell if it was selected before. - if instance.DeletionTimestamp.IsZero() && !currentVMagent.Spec.SelectAllByDefault { - match, err := isSelectorsMatchesTargetCRD(ctx, r.Client, instance, currentVMagent, currentVMagent.Spec.NodeScrapeSelector, currentVMagent.Spec.NodeScrapeNamespaceSelector) + // only check selector when deleting object, + // since labels can be changed when updating and we can't tell if it was selected before, and we can't tell if it's creating or updating. + if !instance.DeletionTimestamp.IsZero() { + match, err := isSelectorsMatchesTargetCRD(ctx, r.Client, instance, currentVMagent, currentVMagent.Spec.NodeScrapeSelector, currentVMagent.Spec.NodeScrapeNamespaceSelector, currentVMagent.Spec.SelectAllByDefault) if err != nil { reqLogger.Error(err, "cannot match vmagent and vmNodeScrape") continue diff --git a/internal/controller/operator/vmpodscrape_controller.go b/internal/controller/operator/vmpodscrape_controller.go index 98424885..171130eb 100644 --- a/internal/controller/operator/vmpodscrape_controller.go +++ b/internal/controller/operator/vmpodscrape_controller.go @@ -83,15 +83,17 @@ func (r *VMPodScrapeReconciler) Reconcile(ctx context.Context, req ctrl.Request) } for _, vmagentItem := range objects.Items { - if !vmagentItem.DeletionTimestamp.IsZero() || vmagentItem.Spec.ParsingError != "" || vmagentItem.IsUnmanaged() { + if !vmagentItem.DeletionTimestamp.IsZero() || vmagentItem.Spec.ParsingError != "" || vmagentItem.IsPodScrapeUnmanaged() { continue } currentVMagent := &vmagentItem reqLogger := reqLogger.WithValues("parent_vmagent", currentVMagent.Name, "parent_namespace", currentVMagent.Namespace) ctx := logger.AddToContext(ctx, reqLogger) - // only check selector when deleting, since labels can be changed when updating and we can't tell if it was selected before. - if instance.DeletionTimestamp.IsZero() && !currentVMagent.Spec.SelectAllByDefault { - match, err := isSelectorsMatchesTargetCRD(ctx, r.Client, instance, currentVMagent, currentVMagent.Spec.PodScrapeSelector, currentVMagent.Spec.PodScrapeNamespaceSelector) + + // only check selector when deleting object, + // since labels can be changed when updating and we can't tell if it was selected before, and we can't tell if it's creating or updating. + if !instance.DeletionTimestamp.IsZero() { + match, err := isSelectorsMatchesTargetCRD(ctx, r.Client, instance, currentVMagent, currentVMagent.Spec.PodScrapeSelector, currentVMagent.Spec.PodScrapeNamespaceSelector, currentVMagent.Spec.SelectAllByDefault) if err != nil { reqLogger.Error(err, "cannot match vmagent and vmPodScrape") continue diff --git a/internal/controller/operator/vmprobe_controller.go b/internal/controller/operator/vmprobe_controller.go index 7700912d..3f75d6c1 100644 --- a/internal/controller/operator/vmprobe_controller.go +++ b/internal/controller/operator/vmprobe_controller.go @@ -82,16 +82,17 @@ func (r *VMProbeReconciler) Reconcile(ctx context.Context, req ctrl.Request) (re } for _, vmagentItem := range objects.Items { - if !vmagentItem.DeletionTimestamp.IsZero() || vmagentItem.Spec.ParsingError != "" || vmagentItem.IsUnmanaged() { + if !vmagentItem.DeletionTimestamp.IsZero() || vmagentItem.Spec.ParsingError != "" || vmagentItem.IsProbeUnmanaged() { continue } currentVMagent := &vmagentItem reqLogger := reqLogger.WithValues("parent_vmagent", currentVMagent.Name, "parent_namespace", currentVMagent.Namespace) ctx := logger.AddToContext(ctx, reqLogger) - // only check selector when deleting, since labels can be changed when updating and we can't tell if it was selected before. - if instance.DeletionTimestamp.IsZero() && !currentVMagent.Spec.SelectAllByDefault { - match, err := isSelectorsMatchesTargetCRD(ctx, r.Client, instance, currentVMagent, currentVMagent.Spec.ProbeSelector, currentVMagent.Spec.ProbeNamespaceSelector) + // only check selector when deleting object, + // since labels can be changed when updating and we can't tell if it was selected before, and we can't tell if it's creating or updating. + if !instance.DeletionTimestamp.IsZero() { + match, err := isSelectorsMatchesTargetCRD(ctx, r.Client, instance, currentVMagent, currentVMagent.Spec.ProbeSelector, currentVMagent.Spec.ProbeNamespaceSelector, currentVMagent.Spec.SelectAllByDefault) if err != nil { reqLogger.Error(err, "cannot match vmagent and vmProbe") continue diff --git a/internal/controller/operator/vmrule_controller.go b/internal/controller/operator/vmrule_controller.go index 6e72ae24..bf1e9faa 100644 --- a/internal/controller/operator/vmrule_controller.go +++ b/internal/controller/operator/vmrule_controller.go @@ -89,9 +89,10 @@ func (r *VMRuleReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res reqLogger := reqLogger.WithValues("parent_vmalert", currVMAlert.Name, "parent_namespace", currVMAlert.Namespace) ctx := logger.AddToContext(ctx, reqLogger) - // only check selector when deleting, since labels can be changed when updating and we can't tell if it was selected before. - if instance.DeletionTimestamp.IsZero() && !currVMAlert.Spec.SelectAllByDefault { - match, err := isSelectorsMatchesTargetCRD(ctx, r.Client, instance, currVMAlert, currVMAlert.Spec.RuleSelector, currVMAlert.Spec.RuleNamespaceSelector) + // only check selector when deleting object, + // since labels can be changed when updating and we can't tell if it was selected before, and we can't tell if it's creating or updating. + if !instance.DeletionTimestamp.IsZero() { + match, err := isSelectorsMatchesTargetCRD(ctx, r.Client, instance, currVMAlert, currVMAlert.Spec.RuleSelector, currVMAlert.Spec.RuleNamespaceSelector, currVMAlert.Spec.SelectAllByDefault) if err != nil { reqLogger.Error(err, "cannot match vmalert and vmRule") continue diff --git a/internal/controller/operator/vmscrapeconfig_controller.go b/internal/controller/operator/vmscrapeconfig_controller.go index 4521fcdb..fea96429 100644 --- a/internal/controller/operator/vmscrapeconfig_controller.go +++ b/internal/controller/operator/vmscrapeconfig_controller.go @@ -77,20 +77,21 @@ func (r *VMScrapeConfigReconciler) Reconcile(ctx context.Context, req ctrl.Reque if err := k8stools.ListObjectsByNamespace(ctx, r.Client, config.MustGetWatchNamespaces(), func(dst *vmv1beta1.VMAgentList) { objects.Items = append(objects.Items, dst.Items...) }); err != nil { - return result, fmt.Errorf("cannot list vmauths for vmuser: %w", err) + return result, fmt.Errorf("cannot list vmagent for vmscrapeconfig: %w", err) } for _, vmagentItem := range objects.Items { - if !vmagentItem.DeletionTimestamp.IsZero() || vmagentItem.Spec.ParsingError != "" || vmagentItem.IsUnmanaged() { + if !vmagentItem.DeletionTimestamp.IsZero() || vmagentItem.Spec.ParsingError != "" || vmagentItem.IsScrapeConfigUnmanaged() { continue } currentVMagent := &vmagentItem reqLogger := reqLogger.WithValues("parent_vmagent", currentVMagent.Name, "parent_namespace", currentVMagent.Namespace) ctx := logger.AddToContext(ctx, reqLogger) - // only check selector when deleting, since labels can be changed when updating and we can't tell if it was selected before. - if instance.DeletionTimestamp.IsZero() && !currentVMagent.Spec.SelectAllByDefault { - match, err := isSelectorsMatchesTargetCRD(ctx, r.Client, instance, currentVMagent, currentVMagent.Spec.ScrapeConfigSelector, currentVMagent.Spec.ScrapeConfigNamespaceSelector) + // only check selector when deleting object, + // since labels can be changed when updating and we can't tell if it was selected before, and we can't tell if it's creating or updating. + if !instance.DeletionTimestamp.IsZero() { + match, err := isSelectorsMatchesTargetCRD(ctx, r.Client, instance, currentVMagent, currentVMagent.Spec.ScrapeConfigSelector, currentVMagent.Spec.ScrapeConfigNamespaceSelector, currentVMagent.Spec.SelectAllByDefault) if err != nil { reqLogger.Error(err, "cannot match vmagent and vmScrapeConfig") continue diff --git a/internal/controller/operator/vmservicescrape_controller.go b/internal/controller/operator/vmservicescrape_controller.go index 4201c05b..de77a93e 100644 --- a/internal/controller/operator/vmservicescrape_controller.go +++ b/internal/controller/operator/vmservicescrape_controller.go @@ -82,15 +82,16 @@ func (r *VMServiceScrapeReconciler) Reconcile(ctx context.Context, req ctrl.Requ } for _, vmagentItem := range objects.Items { - if !vmagentItem.DeletionTimestamp.IsZero() || vmagentItem.Spec.ParsingError != "" || vmagentItem.IsUnmanaged() { + if !vmagentItem.DeletionTimestamp.IsZero() || vmagentItem.Spec.ParsingError != "" || vmagentItem.IsServiceScrapeUnmanaged() { continue } currentVMagent := &vmagentItem reqLogger := reqLogger.WithValues("parent_vmagent", currentVMagent.Name, "parent_namespace", currentVMagent.Namespace) ctx := logger.AddToContext(ctx, reqLogger) - // only check selector when deleting, since labels can be changed when updating and we can't tell if it was selected before. - if instance.DeletionTimestamp.IsZero() && !currentVMagent.Spec.SelectAllByDefault { - match, err := isSelectorsMatchesTargetCRD(ctx, r.Client, instance, currentVMagent, currentVMagent.Spec.ServiceScrapeSelector, currentVMagent.Spec.ServiceScrapeNamespaceSelector) + // only check selector when deleting object, + // since labels can be changed when updating and we can't tell if it was selected before, and we can't tell if it's creating or updating. + if !instance.DeletionTimestamp.IsZero() { + match, err := isSelectorsMatchesTargetCRD(ctx, r.Client, instance, currentVMagent, currentVMagent.Spec.ServiceScrapeSelector, currentVMagent.Spec.ServiceScrapeNamespaceSelector, currentVMagent.Spec.SelectAllByDefault) if err != nil { reqLogger.Error(err, "cannot match vmagent and vmServiceScrape") continue diff --git a/internal/controller/operator/vmstaticscrape_controller.go b/internal/controller/operator/vmstaticscrape_controller.go index cc9533a5..4e8e1d61 100644 --- a/internal/controller/operator/vmstaticscrape_controller.go +++ b/internal/controller/operator/vmstaticscrape_controller.go @@ -62,16 +62,17 @@ func (r *VMStaticScrapeReconciler) Reconcile(ctx context.Context, req ctrl.Reque } for _, vmagentItem := range objects.Items { - if !vmagentItem.DeletionTimestamp.IsZero() || vmagentItem.Spec.ParsingError != "" || vmagentItem.IsUnmanaged() { + if !vmagentItem.DeletionTimestamp.IsZero() || vmagentItem.Spec.ParsingError != "" || vmagentItem.IsStaticScrapeUnmanaged() { continue } currentVMagent := &vmagentItem reqLogger := reqLogger.WithValues("parent_vmagent", currentVMagent.Name, "parent_namespace", currentVMagent.Namespace) ctx := logger.AddToContext(ctx, reqLogger) - // only check selector when deleting, since labels can be changed when updating and we can't tell if it was selected before. - if instance.DeletionTimestamp.IsZero() && !currentVMagent.Spec.SelectAllByDefault { - match, err := isSelectorsMatchesTargetCRD(ctx, r.Client, instance, currentVMagent, currentVMagent.Spec.StaticScrapeSelector, currentVMagent.Spec.StaticScrapeNamespaceSelector) + // only check selector when deleting object, + // since labels can be changed when updating and we can't tell if it was selected before, and we can't tell if it's creating or updating. + if !instance.DeletionTimestamp.IsZero() { + match, err := isSelectorsMatchesTargetCRD(ctx, r.Client, instance, currentVMagent, currentVMagent.Spec.StaticScrapeSelector, currentVMagent.Spec.StaticScrapeNamespaceSelector, currentVMagent.Spec.SelectAllByDefault) if err != nil { reqLogger.Error(err, "cannot match vmagent and vmStaticScrape") continue diff --git a/internal/controller/operator/vmuser_controller.go b/internal/controller/operator/vmuser_controller.go index 2ab23788..64650b82 100644 --- a/internal/controller/operator/vmuser_controller.go +++ b/internal/controller/operator/vmuser_controller.go @@ -102,9 +102,10 @@ func (r *VMUserReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res l = l.WithValues("parent_vmauth", currentVMAuth.Name, "parent_namespace", currentVMAuth.Namespace) ctx := logger.AddToContext(ctx, l) - // only check selector when deleting, since labels can be changed when updating and we can't tell if it was selected before. - if instance.DeletionTimestamp.IsZero() && !currentVMAuth.Spec.SelectAllByDefault { - match, err := isSelectorsMatchesTargetCRD(ctx, r.Client, &instance, currentVMAuth, currentVMAuth.Spec.UserSelector, currentVMAuth.Spec.UserNamespaceSelector) + // only check selector when deleting object, + // since labels can be changed when updating and we can't tell if it was selected before, and we can't tell if it's creating or updating. + if !instance.DeletionTimestamp.IsZero() { + match, err := isSelectorsMatchesTargetCRD(ctx, r.Client, &instance, currentVMAuth, currentVMAuth.Spec.UserSelector, currentVMAuth.Spec.UserNamespaceSelector, currentVMAuth.Spec.SelectAllByDefault) if err != nil { l.Error(err, "cannot match vmauth and VMUser") continue