Skip to content

feat: mvp controller #2036

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions axosyslog.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: logging.banzaicloud.io/v1beta1
kind: AxoSyslog
metadata:
name: axosyslog
namespace: axosyslog
spec:
logPaths:
- filterx: "1==1;"
destination: "axosyslog-test"
destinations:
- name: "axosyslog-test"
config: |
file ("/tmp/log/axosyslog-test.log");
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ spec:
type: object
spec:
properties:
configReloadImage:
properties:
repository:
type: string
tag:
type: string
type: object
destinations:
items:
properties:
Expand All @@ -45,6 +52,13 @@ spec:
type: string
type: object
type: array
image:
properties:
repository:
type: string
tag:
type: string
type: object
logPaths:
items:
properties:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ spec:
type: object
spec:
properties:
configReloadImage:
properties:
repository:
type: string
tag:
type: string
type: object
destinations:
items:
properties:
Expand All @@ -42,6 +49,13 @@ spec:
type: string
type: object
type: array
image:
properties:
repository:
type: string
tag:
type: string
type: object
logPaths:
items:
properties:
Expand Down
14 changes: 14 additions & 0 deletions config/crd/bases/logging.banzaicloud.io_axosyslogs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ spec:
type: object
spec:
properties:
configReloadImage:
properties:
repository:
type: string
tag:
type: string
type: object
destinations:
items:
properties:
Expand All @@ -42,6 +49,13 @@ spec:
type: string
type: object
type: array
image:
properties:
repository:
type: string
tag:
type: string
type: object
logPaths:
items:
properties:
Expand Down
100 changes: 85 additions & 15 deletions controllers/logging/axosyslog_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,55 +16,125 @@ package controllers

import (
"context"
"fmt"
"reflect"
"runtime"

"emperror.dev/errors"
"github.com/cisco-open/operator-tools/pkg/reconciler"
"github.com/go-logr/logr"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/kube-logging/logging-operator/pkg/sdk/logging/api/v1beta1"
"github.com/kube-logging/logging-operator/pkg/resources"
axosyslogresources "github.com/kube-logging/logging-operator/pkg/resources/axosyslog"
v1beta1 "github.com/kube-logging/logging-operator/pkg/sdk/logging/api/v1beta1"
)

// NewAxoSyslogReconciler creates a new AxoSyslogReconciler instance
func NewAxoSyslogReconciler(client client.Client, log logr.Logger) *AxoSyslogReconciler {
func NewAxoSyslogReconciler(client client.Client, log logr.Logger, opts reconciler.ReconcilerOpts) *AxoSyslogReconciler {
return &AxoSyslogReconciler{
Client: client,
Log: log,
Client: client,
GenericResourceReconciler: reconciler.NewGenericReconciler(client, log, opts),
Log: log,
}
}

type AxoSyslogReconciler struct {
client.Client
*reconciler.GenericResourceReconciler
Log logr.Logger
}

// +kubebuilder:rbac:groups=logging.banzaicloud.io,resources=axosyslogs,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=logging.banzaicloud.io,resources=axosyslogs/status,verbs=get;update;patch
// +kubebuilder:rbac:groups="",resources=configmaps;secrets,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=apps,resources=statefulsets,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups="",resources=services;persistentvolumeclaims;serviceaccounts;pods,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups="rbac.authorization.k8s.io",resources=clusterroles;clusterrolebindings;roles;rolebindings,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups="rbac.authorization.k8s.io",resources=roles;rolebindings,verbs=get;list;watch;create;update;patch;delete

// Reconciles axosyslog resource
// Reconcile implements the reconciliation logic for AxoSyslog resources
func (r *AxoSyslogReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
log := r.Log.WithValues("axosyslog", req.NamespacedName)
r.Log.V(1).Info("Reconciling AxoSyslog")

log.V(1).Info("Reconciling AxoSyslog")

var axosyslog v1beta1.AxoSyslog
if err := r.Get(ctx, req.NamespacedName, &axosyslog); err != nil {
var axoSyslog v1beta1.AxoSyslog
if err := r.Get(ctx, req.NamespacedName, &axoSyslog); err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
}

if err := axosyslog.SetDefaults(); err != nil {
log := r.Log.WithValues("axosyslog", fmt.Sprintf("%s/%s", axoSyslog.Namespace, axoSyslog.Name))

if err := axoSyslog.SetDefaults(); err != nil {
return ctrl.Result{}, errors.WrapIf(err, "failed to set defaults")
}

// TODO: config-check ?

if result, err := r.reconcileWorkloadResources(log, &axoSyslog); err != nil {
return ctrl.Result{}, err
} else if result != nil {
return *result, nil
}

return ctrl.Result{}, nil
}

// reconcileWorkloadResources handles resources related to AxoSyslog and requires it's spec
func (r *AxoSyslogReconciler) reconcileWorkloadResources(log logr.Logger, axoSyslog *v1beta1.AxoSyslog) (*ctrl.Result, error) {
resourceBuilders := []resources.ResourceWithSpec{
axosyslogresources.CreateAxoSyslogConfig,
axosyslogresources.StatefulSet,
axosyslogresources.Service,
axosyslogresources.HeadlessService,
// TODO: service-metrics & buffer-metrics ?
// axosyslogresources.ServiceMetrics,
// axosyslogresources.ServiceBufferMetrics,
}

for _, buildObject := range resourceBuilders {
builderName := getFunctionName(buildObject)
log.V(2).Info("Processing resource", "builder", builderName)

o, state, err := buildObject(axoSyslog)
if err != nil {
return nil, errors.WrapIff(err, "failed to build object with %s", builderName)
}
if o == nil {
return nil, errors.Errorf("reconcile error: %s returned nil object", builderName)
}

metaObj, ok := o.(metav1.Object)
if !ok {
return nil, errors.Errorf("reconcile error: %s returned non-metav1.Object", builderName)
}

if metaObj.GetNamespace() == "" {
return nil, errors.Errorf("reconcile error: %s returned resource without namespace set", builderName)
}

if err := ctrl.SetControllerReference(axoSyslog, metaObj, r.Scheme()); err != nil {
return nil, errors.WrapIff(err, "failed to set controller reference for %s", metaObj.GetName())
}

result, err := r.ReconcileResource(o, state)
if err != nil {
return nil, errors.WrapIff(err, "failed to reconcile resource %s/%s", metaObj.GetNamespace(), metaObj.GetName())
}
if result != nil {
return result, nil
}
}

return nil, nil
}

func SetupAxoSyslogWithManager(mgr ctrl.Manager, logger logr.Logger) error {
return ctrl.NewControllerManagedBy(mgr).
For(&v1beta1.AxoSyslog{}).
Named("AxoSyslogController").
Complete(NewAxoSyslogReconciler(mgr.GetClient(), logger))
Named("axosyslog").
Complete(NewAxoSyslogReconciler(mgr.GetClient(), logger, reconciler.ReconcilerOpts{}))
}

func getFunctionName(i any) string {
return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
}
10 changes: 10 additions & 0 deletions docs/configuration/crds/v1beta1/axosyslog_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,21 @@ AxoSyslog is the Schema for the AxoSyslogs API

AxoSyslogSpec defines the desired state of AxoSyslog

### configReloadImage (*BasicImageSpec, optional) {#axosyslogspec-configreloadimage}

ConfigReloadImage is the image specification for the config reload


### destinations ([]Destination, optional) {#axosyslogspec-destinations}

Destinations is a list of destinations to be rendered in the AxoSyslog configuration


### image (*BasicImageSpec, optional) {#axosyslogspec-image}

Image is the image specification for AxoSyslog


### logPaths ([]LogPath, optional) {#axosyslogspec-logpaths}

LogPaths is a list of log paths to be rendered in the AxoSyslog configuration
Expand Down
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.24.1
require (
emperror.dev/errors v0.8.1
github.com/MakeNowJust/heredoc v1.0.0
github.com/Masterminds/sprig/v3 v3.3.0
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883
github.com/cisco-open/operator-tools v0.37.0
github.com/go-logr/logr v1.4.2
Expand All @@ -31,6 +32,7 @@ require (
require (
cloud.google.com/go/compute/metadata v0.6.0 // indirect
dario.cat/mergo v1.0.1 // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.3.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/briandowns/spinner v1.23.2 // indirect
Expand Down Expand Up @@ -58,13 +60,16 @@ require (
github.com/google/uuid v1.6.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.3 // indirect
github.com/hashicorp/go-version v1.7.0 // indirect
github.com/huandu/xstrings v1.5.0 // indirect
github.com/iancoleman/orderedmap v0.3.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.9.0 // indirect
github.com/mattn/go-colorable v0.1.14 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
Expand All @@ -77,6 +82,7 @@ require (
github.com/prometheus/procfs v0.16.1 // indirect
github.com/prometheus/prometheus v1.8.2-0.20210621150501-ff58416a0b02 // indirect
github.com/sergi/go-diff v1.3.1 // indirect
github.com/shopspring/decimal v1.4.0 // indirect
github.com/spf13/pflag v1.0.6 // indirect
github.com/wayneashleyberry/terminal-dimensions v1.1.0 // indirect
github.com/x448/float16 v0.8.4 // indirect
Expand Down Expand Up @@ -106,6 +112,7 @@ require (
go.opentelemetry.io/proto/otlp v1.5.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/crypto v0.37.0 // indirect
golang.org/x/net v0.39.0 // indirect
golang.org/x/oauth2 v0.29.0 // indirect
golang.org/x/sync v0.13.0 // indirect
Expand Down
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,14 @@ github.com/HdrHistogram/hdrhistogram-go v1.0.1/go.mod h1:BWJ+nMSHY3L41Zj7CA3uXnl
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
github.com/MakeNowJust/heredoc v1.0.0 h1:cXCdzVdstXyiTqTvfqk9SDHpKNjxuom+DOlyEeQ4pzQ=
github.com/MakeNowJust/heredoc v1.0.0/go.mod h1:mG5amYoWBHf8vpLOuehzbGGw0EHxpZZ6lCpQ4fNJ8LE=
github.com/Masterminds/goutils v1.1.1 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJLSYI=
github.com/Masterminds/goutils v1.1.1/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
github.com/Masterminds/semver v1.4.2/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
github.com/Masterminds/semver/v3 v3.3.1 h1:QtNSWtVZ3nBfk8mAOu/B6v7FMJ+NHTIgUPi7rj+4nv4=
github.com/Masterminds/semver/v3 v3.3.1/go.mod h1:4V+yj/TJE1HU9XfppCwVMZq3I84lprf4nC11bSS5beM=
github.com/Masterminds/sprig v2.16.0+incompatible/go.mod h1:y6hNFY5UBTIWBxnzTeuNhlNS5hqE0NB0E6fgfo2Br3o=
github.com/Masterminds/sprig/v3 v3.3.0 h1:mQh0Yrg1XPo6vjYXgtf5OtijNAKJRNcTdOOGZe3tPhs=
github.com/Masterminds/sprig/v3 v3.3.0/go.mod h1:Zy1iXRYNqNLUolqCpL4uhk6SHUMAOSCzdgBfDb35Lz0=
github.com/Microsoft/go-winio v0.4.16/go.mod h1:XB6nPKklQyQ7GC9LdcBEcBl8PF76WugXOPRXwdLnMv0=
github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow=
github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM=
Expand Down Expand Up @@ -667,6 +671,8 @@ github.com/hetznercloud/hcloud-go v1.26.2 h1:fI8BXAGJI4EFeCDd2a/I4EhqyK32cDdxGeW
github.com/hetznercloud/hcloud-go v1.26.2/go.mod h1:2C5uMtBiMoFr3m7lBFPf7wXTdh33CevmZpQIIDPGYJI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/huandu/xstrings v1.0.0/go.mod h1:4qWG/gcEcfX4z/mBDHJ++3ReCw9ibxbsNJbcucJdbSo=
github.com/huandu/xstrings v1.5.0 h1:2ag3IFq9ZDANvthTwTiqSSZLjDc+BedvHPAp5tJy2TI=
github.com/huandu/xstrings v1.5.0/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
github.com/hudl/fargo v1.3.0/go.mod h1:y3CKSmjA+wD2gak7sUSXTAoopbhU08POFhmITJgmKTg=
github.com/iancoleman/orderedmap v0.3.0 h1:5cbR2grmZR/DiVt+VJopEhtVs9YGInGIxAoMJn+Ichc=
github.com/iancoleman/orderedmap v0.3.0/go.mod h1:XuLcCUkdL5owUCQeF2Ue9uuw1EptkJDkXXS7VoV7XGE=
Expand Down Expand Up @@ -800,6 +806,8 @@ github.com/miekg/dns v1.1.62/go.mod h1:mvDlcItzm+br7MToIKqkglaGhlFMHJ9DTNNWONWXb
github.com/mileusna/useragent v0.0.0-20190129205925-3e331f0949a5/go.mod h1:JWhYAp2EXqUtsxTKdeGlY8Wp44M7VxThC9FEoNGi2IE=
github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc=
github.com/mitchellh/cli v1.1.0/go.mod h1:xcISNoH86gajksDmfB23e/pu+B+GeFRMYmoHXxx3xhI=
github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw=
github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HKCj9FbZEVFJRxO9s=
github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
Expand All @@ -816,6 +824,8 @@ github.com/mitchellh/mapstructure v1.4.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RR
github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ=
github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/moby/docker-image-spec v1.3.1 h1:jMKff3w6PgbfSa69GfNg+zN/XLhfXJGnEx3Nl2EsFP0=
github.com/moby/docker-image-spec v1.3.1/go.mod h1:eKmb5VW8vQEh/BAr2yvVNvuiJuY6UIocYsFu/DxxRpo=
github.com/moby/spdystream v0.2.0/go.mod h1:f7i0iNDQJ059oMTcWxx8MA/zKFIuD/lY+0GqbN2Wy8c=
Expand Down Expand Up @@ -999,6 +1009,8 @@ github.com/segmentio/kafka-go v0.2.0/go.mod h1:X6itGqS9L4jDletMsxZ7Dz+JFWxM6JHfP
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8=
github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NFbPK1I=
github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k=
github.com/shopspring/decimal v1.4.0/go.mod h1:gawqmDU56v4yIKSwfBSFip1HdCCXN8/+DMd9qYNcwME=
github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749/go.mod h1:ZY1cvUeJuFPAdZ/B6v7RHavJWZn2YPVFQ1OSXhCGOkg=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/shurcooL/vfsgen v0.0.0-20181202132449-6a9ea43bcacd/go.mod h1:TrYk7fJVaAttu97ZZKrO9UbRa8izdowaMIZcxYMbVaw=
Expand Down
Loading