-
Notifications
You must be signed in to change notification settings - Fork 142
/
vmprobe_controller.go
110 lines (94 loc) · 3.79 KB
/
vmprobe_controller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package controllers
import (
"context"
"fmt"
victoriametricsv1beta1 "github.com/VictoriaMetrics/operator/api/v1beta1"
"github.com/VictoriaMetrics/operator/controllers/factory/k8stools"
"github.com/VictoriaMetrics/operator/controllers/factory/logger"
"github.com/VictoriaMetrics/operator/controllers/factory/vmagent"
"github.com/VictoriaMetrics/operator/internal/config"
"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// VMProbeReconciler reconciles a VMProbe object
type VMProbeReconciler struct {
client.Client
Log logr.Logger
OriginScheme *runtime.Scheme
BaseConf *config.BaseOperatorConf
}
// Scheme implements interface.
func (r *VMProbeReconciler) Scheme() *runtime.Scheme {
return r.OriginScheme
}
// Reconcile - syncs VMProbe
// +kubebuilder:rbac:groups=operator.victoriametrics.com,resources=vmprobes,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=operator.victoriametrics.com,resources=vmprobes/status,verbs=get;update;patch
func (r *VMProbeReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, err error) {
reqLogger := r.Log.WithValues("vmprobe", req.NamespacedName)
ctx = logger.AddToContext(ctx, reqLogger)
defer func() {
result, err = handleReconcileErr(ctx, r.Client, nil, result, err)
}()
// Fetch the VMPodScrape instance
instance := &victoriametricsv1beta1.VMProbe{}
if err := r.Get(ctx, req.NamespacedName, instance); err != nil {
return result, &getError{err, "vmprobescrape", req}
}
RegisterObjectStat(instance, "vmprobescrape")
if vmAgentReconcileLimit.MustThrottleReconcile() {
// fast path, rate limited
return
}
vmAgentSync.Lock()
defer vmAgentSync.Unlock()
var objects victoriametricsv1beta1.VMAgentList
if err := k8stools.ListObjectsByNamespace(ctx, r.Client, config.MustGetWatchNamespaces(), func(dst *victoriametricsv1beta1.VMAgentList) {
objects.Items = append(objects.Items, dst.Items...)
}); err != nil {
return result, fmt.Errorf("cannot list vmauths for vmuser: %w", err)
}
for _, vmagentItem := range objects.Items {
if !vmagentItem.DeletionTimestamp.IsZero() || vmagentItem.Spec.ParsingError != "" || vmagentItem.IsUnmanaged() {
continue
}
currentVMagent := &vmagentItem
// 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)
if err != nil {
reqLogger.Error(err, "cannot match vmagent and vmProbe")
continue
}
if !match {
continue
}
}
reqLogger := reqLogger.WithValues("vmagent", currentVMagent.Name)
ctx := logger.AddToContext(ctx, reqLogger)
if err := vmagent.CreateOrUpdateConfigurationSecret(ctx, currentVMagent, r, r.BaseConf); err != nil {
continue
}
}
return
}
// SetupWithManager - setups VMProbe manager
func (r *VMProbeReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&victoriametricsv1beta1.VMProbe{}).
WithOptions(getDefaultOptions()).
Complete(r)
}