-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add operator controller to add ServiceMonitor
Signed-off-by: Jan Fajerski <[email protected]>
- Loading branch information
Showing
7 changed files
with
251 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package operator_controller | ||
|
||
import ( | ||
"fmt" | ||
|
||
monv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1" | ||
"github.com/rhobs/observability-operator/pkg/reconciler" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/utils/ptr" | ||
) | ||
|
||
const ( | ||
name = "observability-operato" | ||
) | ||
|
||
func operatorComponentReconcilers(owner metav1.Object, namespace string) []reconciler.Reconciler { | ||
return []reconciler.Reconciler{ | ||
reconciler.NewUpdater(newServiceMonitor(namespace), owner), | ||
} | ||
} | ||
|
||
func newServiceMonitor(namespace string) *monv1.ServiceMonitor { | ||
return &monv1.ServiceMonitor{ | ||
TypeMeta: metav1.TypeMeta{ | ||
APIVersion: monv1.SchemeGroupVersion.String(), | ||
Kind: "ServiceMonitor", | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: name, | ||
Namespace: namespace, | ||
Labels: map[string]string{ | ||
"app.kubernetes.io/component": "operator", | ||
"app.kubernetes.io/name": name, | ||
"app.kubernetes.io/part-of": name, | ||
"openshift.io/user-monitoring": "true", | ||
}, | ||
}, | ||
|
||
Spec: monv1.ServiceMonitorSpec{ | ||
Endpoints: []monv1.Endpoint{ | ||
{ | ||
Port: "metrics", | ||
Scheme: "https", | ||
TLSConfig: &monv1.TLSConfig{ | ||
CAFile: "/etc/prometheus/configmaps/serving-certs-ca-bundle/service-ca.crt", | ||
CertFile: "/etc/prometheus/secrets/metrics-client-certs/tls.crt", | ||
KeyFile: "/etc/prometheus/secrets/metrics-client-certs/tls.key", | ||
SafeTLSConfig: monv1.SafeTLSConfig{ | ||
ServerName: ptr.To(fmt.Sprintf("%s.%s.svc", name, namespace)), | ||
InsecureSkipVerify: ptr.To(false), | ||
}, | ||
}, | ||
}, | ||
}, | ||
Selector: metav1.LabelSelector{ | ||
MatchLabels: map[string]string{ | ||
"app.kubernetes.io/component": "operator", | ||
"app.kubernetes.io/name": name, | ||
}, | ||
}, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/* | ||
Copyright 2024. | ||
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 operator_controller | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/go-logr/logr" | ||
monv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1" | ||
appsv1 "k8s.io/api/apps/v1" | ||
"k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/types" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/builder" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/controller" | ||
"sigs.k8s.io/controller-runtime/pkg/handler" | ||
"sigs.k8s.io/controller-runtime/pkg/predicate" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
) | ||
|
||
type resourceManager struct { | ||
k8sClient client.Client | ||
scheme *runtime.Scheme | ||
logger logr.Logger | ||
controller controller.Controller | ||
namespace string | ||
} | ||
|
||
// RBAC for managing Prometheus Operator CRs | ||
//+kubebuilder:rbac:groups=monitoring.coreos.com,resources=servicemonitors,verbs=list;watch;create;update;delete;patch | ||
|
||
// RegisterWithManager registers the controller with Manager | ||
func RegisterWithManager(mgr ctrl.Manager, namespace string) error { | ||
|
||
rm := &resourceManager{ | ||
k8sClient: mgr.GetClient(), | ||
scheme: mgr.GetScheme(), | ||
logger: ctrl.Log.WithName("observability-operator"), | ||
namespace: namespace, | ||
} | ||
// We only want to trigger a reconciliation when the generation | ||
// of a child changes. Until we need to update our the status for our own objects, | ||
// we can save CPU cycles by avoiding reconciliations triggered by | ||
// child status changes. | ||
generationChanged := builder.WithPredicates(predicate.GenerationChangedPredicate{}) | ||
|
||
ctrl, err := ctrl.NewControllerManagedBy(mgr). | ||
Owns(&monv1.ServiceMonitor{}, generationChanged). | ||
Watches( | ||
&appsv1.Deployment{}, | ||
handler.EnqueueRequestsFromMapFunc(rm.operatorDeployment), | ||
builder.WithPredicates(predicate.ResourceVersionChangedPredicate{}), | ||
). | ||
Build(rm) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
rm.controller = ctrl | ||
return nil | ||
} | ||
|
||
func (rm resourceManager) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { | ||
logger := rm.logger.WithValues("operator", req.NamespacedName) | ||
logger.Info("Reconciling operator resources") | ||
|
||
op := &appsv1.Deployment{} | ||
err := rm.k8sClient.Get(ctx, req.NamespacedName, op) | ||
if errors.IsNotFound(err) { | ||
return ctrl.Result{}, nil | ||
} | ||
if err != nil { | ||
return ctrl.Result{}, err | ||
} | ||
|
||
reconcilers := operatorComponentReconcilers(op, rm.namespace) | ||
for _, reconciler := range reconcilers { | ||
err := reconciler.Reconcile(ctx, rm.k8sClient, rm.scheme) | ||
// handle create / update errors that can happen due to a stale cache by | ||
// retrying after some time. | ||
if errors.IsAlreadyExists(err) || errors.IsConflict(err) { | ||
logger.V(3).Info("skipping reconcile error", "err", err) | ||
return ctrl.Result{RequeueAfter: 2 * time.Second}, nil | ||
} | ||
if err != nil { | ||
return ctrl.Result{}, err | ||
} | ||
} | ||
|
||
return ctrl.Result{}, nil | ||
} | ||
func (rm resourceManager) operatorDeployment(ctx context.Context, ms client.Object) []reconcile.Request { | ||
var requests []reconcile.Request | ||
op := &appsv1.Deployment{} | ||
err := rm.k8sClient.Get(ctx, types.NamespacedName{Name: "observability-operator", Namespace: rm.namespace}, op) | ||
if errors.IsNotFound(err) { | ||
return requests | ||
} | ||
if err != nil { | ||
return requests | ||
} | ||
requests = append(requests, reconcile.Request{ | ||
NamespacedName: types.NamespacedName{ | ||
Name: op.GetName(), | ||
Namespace: op.GetNamespace(), | ||
}, | ||
}) | ||
return requests | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package utils | ||
|
||
import ( | ||
"slices" | ||
|
||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
// Add finalizer if not present | ||
func AddFinalizer(obj client.Object, finalizerName string) client.Object { | ||
finalizers := obj.GetFinalizers() | ||
if !slices.Contains(finalizers, finalizerName) { | ||
finalizers = append(finalizers, finalizerName) | ||
obj.SetFinalizers(finalizers) | ||
} | ||
return obj | ||
} | ||
|
||
func RemoveFinalizer(obj client.Object, finalizerName string) client.Object { | ||
finalizers := obj.GetFinalizers() | ||
if slices.Contains(finalizers, finalizerName) { | ||
finalizers = slices.DeleteFunc(finalizers, func(currentFinalizerName string) bool { | ||
return currentFinalizerName == finalizerName | ||
}) | ||
obj.SetFinalizers(finalizers) | ||
} | ||
return obj | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters