Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
Signed-off-by: Bence Csati <[email protected]>
  • Loading branch information
csatib02 committed Dec 4, 2024
1 parent b38428a commit 479c4e4
Show file tree
Hide file tree
Showing 16 changed files with 619 additions and 75 deletions.
6 changes: 6 additions & 0 deletions charts/logging-operator/Chart.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
dependencies:
- name: logging-operator-crds
repository: ""
version: 0.0.0
digest: sha256:cdfb5cdd4a7897d730175d7bfeb884fbaec8efcfdef1130680792abd9bff5a72
generated: "2024-11-28T16:10:58.540563+01:00"
Original file line number Diff line number Diff line change
Expand Up @@ -12593,6 +12593,39 @@ spec:
type: string
type: object
type: array
routeConfig:
properties:
disableLoggingRoute:
type: boolean
enableTelemetryControllerRoute:
type: boolean
tenantLabels:
properties:
matchExpressions:
items:
properties:
key:
type: string
operator:
type: string
values:
items:
type: string
type: array
x-kubernetes-list-type: atomic
required:
- key
- operator
type: object
type: array
x-kubernetes-list-type: atomic
matchLabels:
additionalProperties:
type: string
type: object
type: object
x-kubernetes-map-type: atomic
type: object
skipInvalidResources:
type: boolean
syslogNG:
Expand Down
33 changes: 33 additions & 0 deletions charts/logging-operator/crds/logging.banzaicloud.io_loggings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12590,6 +12590,39 @@ spec:
type: string
type: object
type: array
routeConfig:
properties:
disableLoggingRoute:
type: boolean
enableTelemetryControllerRoute:
type: boolean
tenantLabels:
properties:
matchExpressions:
items:
properties:
key:
type: string
operator:
type: string
values:
items:
type: string
type: array
x-kubernetes-list-type: atomic
required:
- key
- operator
type: object
type: array
x-kubernetes-list-type: atomic
matchLabels:
additionalProperties:
type: string
type: object
type: object
x-kubernetes-map-type: atomic
type: object
skipInvalidResources:
type: boolean
syslogNG:
Expand Down
16 changes: 16 additions & 0 deletions charts/logging-operator/templates/clusterrole.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,20 @@ rules:
- patch
- update
- watch
- apiGroups:
- telemetry.kube-logging.dev
resources:
- bridges
- collectors
- outputs
- subscriptions
- tenants
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
{{- end }}
33 changes: 33 additions & 0 deletions config/crd/bases/logging.banzaicloud.io_loggings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12590,6 +12590,39 @@ spec:
type: string
type: object
type: array
routeConfig:
properties:
disableLoggingRoute:
type: boolean
enableTelemetryControllerRoute:
type: boolean
tenantLabels:
properties:
matchExpressions:
items:
properties:
key:
type: string
operator:
type: string
values:
items:
type: string
type: array
x-kubernetes-list-type: atomic
required:
- key
- operator
type: object
type: array
x-kubernetes-list-type: atomic
matchLabels:
additionalProperties:
type: string
type: object
type: object
x-kubernetes-map-type: atomic
type: object
skipInvalidResources:
type: boolean
syslogNG:
Expand Down
16 changes: 16 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,19 @@ rules:
- patch
- update
- watch
- apiGroups:
- telemetry.kube-logging.dev
resources:
- bridges
- collectors
- outputs
- subscriptions
- tenants
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
173 changes: 173 additions & 0 deletions controllers/logging/telemetry_controller_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
// Copyright © 2024 Kube logging 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 controllers

import (
"context"
"fmt"

"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

telemetry_controller "github.com/kube-logging/logging-operator/pkg/resources/telemetry-controller"
"github.com/kube-logging/logging-operator/pkg/sdk/logging/api/v1beta1"
loggingv1beta1 "github.com/kube-logging/logging-operator/pkg/sdk/logging/api/v1beta1"
)

const (
TelemetryControllerFinalizer = "telemetrycontroller.logging.banzaicloud.io/finalizer"
)

// +kubebuilder:rbac:groups=telemetry.kube-logging.dev,resources=collectors;tenants;subscriptions;outputs;bridges;,verbs=get;list;watch;create;update;patch;delete

func NewTelemetryControllerReconciler(client client.Client, log logr.Logger) *TelemetryControllerReconciler {
return &TelemetryControllerReconciler{
Client: client,
Log: log,
}
}

// TelemetryControllerReconciler reconciles Logging resources for the Telemetry controller
type TelemetryControllerReconciler struct {
client.Client
Log logr.Logger
}

func (r *TelemetryControllerReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
log := r.Log.WithValues("telemetry-controller", req.Name)

var logging loggingv1beta1.Logging
if err := r.Client.Get(ctx, req.NamespacedName, &logging); err != nil {
return reconcile.Result{}, client.IgnoreNotFound(err)
}

if logging.Spec.RouteConfig.EnableTelemetryControllerRoute {
log.Info("Reconciling Logging resource for Telemetry controller", "name", logging.Name)

var objectsToCreate []client.Object
objectsToCreate = append(objectsToCreate, telemetry_controller.CreateCollector(&logging))
objectsToCreate = append(objectsToCreate, telemetry_controller.CreateOutput(&logging))
objectsToCreate = append(objectsToCreate, telemetry_controller.CreateSubscription(&logging))
objectsToCreate = append(objectsToCreate, telemetry_controller.CreateTenant(&logging))

if logging.ObjectMeta.DeletionTimestamp.IsZero() {
if !controllerutil.ContainsFinalizer(&logging, TelemetryControllerFinalizer) {
r.Log.Info("adding telemetrycontroller finalizer")
controllerutil.AddFinalizer(&logging, TelemetryControllerFinalizer)
if err := r.Client.Update(ctx, &logging); err != nil {
return ctrl.Result{}, err
}
}
} else {
// The object is being deleted
if controllerutil.ContainsFinalizer(&logging, TelemetryControllerFinalizer) {
if err := r.deleteTelemetryControllerResources(ctx, log, &objectsToCreate); err != nil {
return ctrl.Result{}, err
}

r.Log.Info("removing telemetrycontroller finalizer")
controllerutil.RemoveFinalizer(&logging, TelemetryControllerFinalizer)
if err := r.Client.Update(ctx, &logging); err != nil {
return ctrl.Result{}, err
}
}

return ctrl.Result{}, nil
}
if err := r.isAggregatorReady(ctx, log, logging); err != nil {
return ctrl.Result{RequeueAfter: 5}, err
}

if err := r.deployTelemetryControllerResources(ctx, log, &objectsToCreate); err != nil {
return ctrl.Result{}, err
}
}

return ctrl.Result{}, nil
}

func SetupTelemetryControllerWithManager(mgr ctrl.Manager, logger logr.Logger) error {
return ctrl.NewControllerManagedBy(mgr).
For(&loggingv1beta1.Logging{}).
Named("telemetrycontroller").
Complete(NewTelemetryControllerReconciler(mgr.GetClient(), logger))
}

func (r *TelemetryControllerReconciler) deployTelemetryControllerResources(ctx context.Context, logger logr.Logger, objectsToCreate *[]client.Object) error {
logger.Info("Deploying Telemetry controller resources")

for _, obj := range *objectsToCreate {
key := client.ObjectKeyFromObject(obj)
if err := r.Client.Get(ctx, key, obj); err != nil {
if !apierrors.IsNotFound(err) {
logger.Error(err, "Failed to get object", "object", obj.GetName())
return err
}
if err := r.Client.Create(ctx, obj); err != nil {
logger.Error(err, "Failed to create object", "object", obj.GetName())
return err
}
logger.Info("Created object", "object", obj.GetName())
} else {
logger.Info("Object already exists", "object", obj.GetName())
}
}

return nil
}

func (r *TelemetryControllerReconciler) deleteTelemetryControllerResources(ctx context.Context, logger logr.Logger, objectsToCreate *[]client.Object) error {
logger.Info("Logging resource is being deleted, deleting Telemetry controller resources")

for _, obj := range *objectsToCreate {
if err := r.Client.Delete(ctx, obj); err != nil {
logger.Error(err, "Failed to delete object", "object", obj.GetName())
return err
}
logger.Info("Deleted object", "object", obj.GetName())
}

return nil
}

func (r *TelemetryControllerReconciler) isAggregatorReady(ctx context.Context, logger logr.Logger, logging v1beta1.Logging) error {
logger.Info("Waiting for aggregator pod to be ready")

podName := fmt.Sprintf("%s-fluentd-0", logging.Name)
pod := &corev1.Pod{}
err := r.Client.Get(ctx, client.ObjectKey{Name: podName, Namespace: logging.Spec.ControlNamespace}, pod)
if err != nil {
if apierrors.IsNotFound(err) {
return fmt.Errorf("aggregator pod: %s not found", podName)
}

r.Log.Error(err, "failed to get aggregator pod", "pod", podName)
return err
}

for _, condition := range pod.Status.Conditions {
if condition.Type == corev1.PodReady && condition.Status == corev1.ConditionTrue {
r.Log.Info("Aggregator pod is ready", "pod", pod.Name)
return nil
}
}

return fmt.Errorf("aggregator pod: %s is not ready", podName)
}
23 changes: 23 additions & 0 deletions docs/configuration/crds/v1beta1/logging_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ Reference to the logging system. Each of the `loggingRef`s can manage a fluentbi
InlineNodeAgent Configuration Deprecated, will be removed with next major version


### routeConfig (*RouteConfig, optional) {#loggingspec-routeconfig}

RouteConfig determines whether to use loggingRoutes or to create resources based on the logging resource that can be managed by the Telmetry Controller.


### skipInvalidResources (bool, optional) {#loggingspec-skipinvalidresources}

Whether to skip invalid Flow and ClusterFlow resources
Expand Down Expand Up @@ -123,6 +128,24 @@ Configure timeout in seconds if strategy is StartWithTimeout



## RouteConfig

### disableLoggingRoute (bool, optional) {#routeconfig-disableloggingroute}

If DisableLoggingRoute is set to true, the logging route controller should remove the given tenant from the status of the logging resource.


### enableTelemetryControllerRoute (bool, optional) {#routeconfig-enabletelemetrycontrollerroute}

If EnableTelemtryControllerRoute set to true, the opoerator will create the corresponding Tenant, Subscription, Output based on the logging resource.


### tenantLabels (*metav1.LabelSelector, optional) {#routeconfig-tenantlabels}

TenantLabels is a map of labels that will be added to the tenant object so it can be matched with TelemetryController's TenantSelector ref: https://github.com/kube-logging/telemetry-controller/blob/main/api/telemetry/v1alpha1/collector_types.go



## LoggingStatus

LoggingStatus defines the observed state of Logging
Expand Down
Loading

0 comments on commit 479c4e4

Please sign in to comment.