Like Prometheus, but for logs.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
loki/controllers/lokistack_controller.go

102 lines
3.6 KiB

4 years ago
/*
Copyright 2021.
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"
"time"
4 years ago
4 years ago
"github.com/ViaQ/logerr/log"
"github.com/ViaQ/loki-operator/internal/manifests"
4 years ago
"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
4 years ago
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/predicate"
4 years ago
lokiv1beta1 "github.com/ViaQ/loki-operator/api/v1beta1"
4 years ago
)
// LokiStackReconciler reconciles a LokiStack object
type LokiStackReconciler struct {
client.Client
Log logr.Logger
Scheme *runtime.Scheme
}
// +kubebuilder:rbac:groups=loki.openshift.io,resources=lokistacks,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=loki.openshift.io,resources=lokistacks/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=loki.openshift.io,resources=lokistacks/finalizers,verbs=update
// +kubebuilder:rbac:groups="",resources=pods;nodes;services;endpoints;configmaps,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=apps,resources=deployments,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=rbac.authorization.k8s.io,resources=clusterrolebindings;clusterroles,verbs=get;list;watch;create;update;patch;delete
4 years ago
// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
// TODO(user): Modify the Reconcile function to compare the state specified by
// the LokiStack object against the actual cluster state, and then
// perform operations to make the cluster state reflect the state specified by
// the user.
//
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.7.0/pkg/reconcile
func (r *LokiStackReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
4 years ago
ll := log.WithValues("lokistack", req.NamespacedName)
4 years ago
ll.Info("begin building manifests")
objects, err := manifests.BuildAll(req.Name, req.Namespace)
4 years ago
if err != nil {
ll.Error(err, "failed to build manifests")
return ctrl.Result{
Requeue: true,
RequeueAfter: time.Second,
}, err
4 years ago
}
ll.Info("manifests built", "count", len(objects))
for _, obj := range objects {
l := ll.WithValues("object_name", obj.GetName(),
"object_kind", obj.GetObjectKind(),
"object", obj)
obj.SetNamespace(req.Namespace)
if err := r.Create(ctx, obj); err != nil {
l.Error(err, "failed to create object")
continue
}
l.Info("Resource created", "resource", obj.GetName())
4 years ago
}
4 years ago
return ctrl.Result{}, nil
}
// SetupWithManager sets up the controller with the Manager.
func (r *LokiStackReconciler) SetupWithManager(mgr ctrl.Manager) error {
4 years ago
createPredicate := predicate.Funcs{
4 years ago
UpdateFunc: func(e event.UpdateEvent) bool { return false },
4 years ago
CreateFunc: func(e event.CreateEvent) bool { return true },
DeleteFunc: func(e event.DeleteEvent) bool { return false },
GenericFunc: func(e event.GenericEvent) bool { return false },
}
4 years ago
return ctrl.NewControllerManagedBy(mgr).
For(&lokiv1beta1.LokiStack{}).
4 years ago
WithEventFilter(createPredicate).
4 years ago
Complete(r)
}