forked from openshift/kubernetes-nmstate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodenetworkconfigurationpolicy_controller.go
More file actions
97 lines (76 loc) · 2.78 KB
/
nodenetworkconfigurationpolicy_controller.go
File metadata and controls
97 lines (76 loc) · 2.78 KB
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
/*
Copyright The Kubernetes NMState Authors.
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 metrics
import (
"context"
"fmt"
"github.com/go-logr/logr"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/nmstate/kubernetes-nmstate/api/shared"
nmstatev1 "github.com/nmstate/kubernetes-nmstate/api/v1"
"github.com/nmstate/kubernetes-nmstate/pkg/monitoring"
)
// NodeNetworkConfigurationPolicyReconciler reconciles NNCP objects for status metrics
type NodeNetworkConfigurationPolicyReconciler struct {
client.Client
Log logr.Logger
Scheme *runtime.Scheme
}
func (r *NodeNetworkConfigurationPolicyReconciler) Reconcile(ctx context.Context, request ctrl.Request) (ctrl.Result, error) {
log := r.Log.WithValues("metrics.nodenetworkconfigurationpolicy", request.NamespacedName)
log.Info("Reconcile")
if err := r.reportStatistics(ctx); err != nil {
return ctrl.Result{}, fmt.Errorf("failed reporting NNCP status statistics: %w", err)
}
return ctrl.Result{}, nil
}
func (r *NodeNetworkConfigurationPolicyReconciler) SetupWithManager(mgr ctrl.Manager) error {
onConditionChange := conditionChangePredicate(func(obj client.Object) (shared.ConditionList, bool) {
nncp, ok := obj.(*nmstatev1.NodeNetworkConfigurationPolicy)
if !ok {
return nil, false
}
return nncp.Status.Conditions, true
})
err := ctrl.NewControllerManagedBy(mgr).
For(&nmstatev1.NodeNetworkConfigurationPolicy{}).
WithEventFilter(onConditionChange).
Complete(r)
if err != nil {
return errors.Wrap(err, "failed to add controller to NNCP status metrics Reconciler")
}
return nil
}
func (r *NodeNetworkConfigurationPolicyReconciler) reportStatistics(ctx context.Context) error {
nncpList := nmstatev1.NodeNetworkConfigurationPolicyList{}
if err := r.List(ctx, &nncpList); err != nil {
return err
}
counts := make(map[shared.ConditionType]float64)
for _, condType := range shared.NodeNetworkConfigurationPolicyConditionTypes {
counts[condType] = 0
}
for i := range nncpList.Items {
status := activeConditionType(nncpList.Items[i].Status.Conditions)
if status != "" {
counts[status]++
}
}
for condType, count := range counts {
monitoring.PolicyStatus.WithLabelValues(string(condType)).Set(count)
}
return nil
}