1
0
Fork 0
mirror of https://github.com/arangodb/kube-arangodb.git synced 2024-12-14 11:57:37 +00:00

[Feature] Add new logger services (#750)

This commit is contained in:
Adam Janikowski 2021-07-04 19:06:07 +02:00 committed by GitHub
parent b068b884c1
commit 88ab3c2d19
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 106 additions and 38 deletions

View file

@ -5,6 +5,7 @@
- Deprecate Alpine image usage
- Use persistent name and namespace in ArangoDeployment reconcilation loop
- Remove finalizers when Server container is already terminated and reduce initial reconciliation delay
- Add new logger services - reconciliation and event
## [1.1.9](https://github.com/arangodb/kube-arangodb/tree/1.1.9) (2021-05-28)
- Add IP, DNS, ShortDNS, HeadlessService (Default) communication methods

View file

@ -139,7 +139,7 @@ func init() {
f.StringVar(&serverOptions.tlsSecretName, "server.tls-secret-name", "", "Name of secret containing tls.crt & tls.key for HTTPS server (if empty, self-signed certificate is used)")
f.StringVar(&serverOptions.adminSecretName, "server.admin-secret-name", defaultAdminSecretName, "Name of secret containing username + password for login to the dashboard")
f.BoolVar(&serverOptions.allowAnonymous, "server.allow-anonymous-access", false, "Allow anonymous access to the dashboard")
f.StringArrayVar(&logLevels, "log.level", []string{defaultLogLevel}, "Set log levels in format <level> or <logger>=<level>")
f.StringArrayVar(&logLevels, "log.level", []string{defaultLogLevel}, fmt.Sprintf("Set log levels in format <level> or <logger>=<level>. Possible loggers: %s", strings.Join(logging.LoggerNames(), ", ")))
f.BoolVar(&operatorOptions.enableDeployment, "operator.deployment", false, "Enable to run the ArangoDeployment operator")
f.BoolVar(&operatorOptions.enableDeploymentReplication, "operator.deployment-replication", false, "Enable to run the ArangoDeploymentReplication operator")
f.BoolVar(&operatorOptions.enableStorage, "operator.storage", false, "Enable to run the ArangoLocalStorage operator")
@ -193,7 +193,7 @@ func cmdMainRun(cmd *cobra.Command, args []string) {
return log.With().Str("operator-id", operatorID).Logger()
})
klog.SetOutput(logService.MustGetLogger("klog"))
klog.SetOutput(logService.MustGetLogger(logging.LoggerNameKLog))
klog.Info("nice to meet you")
klog.Flush()
@ -253,7 +253,7 @@ func cmdMainRun(cmd *cobra.Command, args []string) {
AdminSecretName: serverOptions.adminSecretName,
AllowAnonymous: serverOptions.allowAnonymous,
}, server.Dependencies{
Log: logService.MustGetLogger("server"),
Log: logService.MustGetLogger(logging.LoggerNameServer),
LivenessProbe: &livenessProbe,
Deployment: server.OperatorDependency{
Enabled: cfg.EnableDeployment,

View file

@ -27,6 +27,8 @@ import (
"fmt"
"testing"
"github.com/rs/zerolog/log"
"github.com/arangodb/kube-arangodb/pkg/util/errors"
"github.com/arangodb/go-driver"
@ -59,7 +61,7 @@ func newFakeHandler() *handler {
kubeClient: k,
arangoClientTimeout: defaultArangoClientTimeout,
eventRecorder: newEventInstance(event.NewEventRecorder("mock", k)),
eventRecorder: newEventInstance(event.NewEventRecorder(log.Logger, "mock", k)),
}
}

View file

@ -27,6 +27,8 @@ import (
"fmt"
"testing"
"github.com/rs/zerolog/log"
"github.com/arangodb/kube-arangodb/pkg/apis/backup"
"github.com/arangodb/kube-arangodb/pkg/apis/deployment"
@ -50,7 +52,7 @@ func newFakeHandler() *handler {
h := &handler{
client: f,
kubeClient: k,
eventRecorder: newEventInstance(event.NewEventRecorder("mock", k)),
eventRecorder: newEventInstance(event.NewEventRecorder(log.Logger, "mock", k)),
}
return h

View file

@ -26,7 +26,8 @@ import (
"context"
"fmt"
"github.com/rs/zerolog/log"
"github.com/rs/zerolog"
core "k8s.io/api/core/v1"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/uuid"
@ -34,10 +35,11 @@ import (
)
// NewEventRecorder creates new event recorder
func NewEventRecorder(name string, kubeClientSet kubernetes.Interface) Recorder {
func NewEventRecorder(logger zerolog.Logger, name string, kubeClientSet kubernetes.Interface) Recorder {
return &eventRecorder{
kubeClientSet: kubeClientSet,
name: name,
logger: logger,
}
}
@ -51,6 +53,7 @@ type Recorder interface {
type eventRecorder struct {
name string
kubeClientSet kubernetes.Interface
logger zerolog.Logger
}
func (e *eventRecorder) newEvent(group, version, kind string, object meta.Object, eventType, reason, message string) *core.Event {
@ -90,7 +93,7 @@ func (e *eventRecorder) newObjectReference(group, version, kind string, object m
func (e *eventRecorder) event(group, version, kind string, object meta.Object, eventType, reason, message string) {
_, err := e.kubeClientSet.CoreV1().Events(object.GetNamespace()).Create(context.Background(), e.newEvent(group, version, kind, object, eventType, reason, message), meta.CreateOptions{})
if err != nil {
log.Warn().Err(err).
e.logger.Warn().Err(err).
Str("APIVersion", fmt.Sprintf("%s/%s", group, version)).
Str("Kind", kind).
Str("Object", fmt.Sprintf("%s/%s", object.GetNamespace(), object.GetName())).
@ -98,7 +101,7 @@ func (e *eventRecorder) event(group, version, kind string, object meta.Object, e
return
}
log.Info().
e.logger.Info().
Str("APIVersion", fmt.Sprintf("%s/%s", group, version)).
Str("Kind", kind).
Str("Object", fmt.Sprintf("%s/%s", object.GetNamespace(), object.GetName())).

View file

@ -27,6 +27,8 @@ import (
"fmt"
"testing"
"github.com/rs/zerolog/log"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
core "k8s.io/api/core/v1"
@ -39,7 +41,7 @@ func Test_Event_Handler(t *testing.T) {
// Arrange
c := fake.NewSimpleClientset()
recorder := NewEventRecorder("mock", c)
recorder := NewEventRecorder(log.Logger, "mock", c)
group := string(uuid.NewUUID())
version := "v1"

View file

@ -26,13 +26,14 @@ import (
"sync"
"time"
"github.com/rs/zerolog"
"github.com/arangodb/kube-arangodb/pkg/util/errors"
"github.com/arangodb/kube-arangodb/pkg/backup/operator/operation"
"github.com/prometheus/client_golang/prometheus"
"github.com/rs/zerolog/log"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
@ -62,10 +63,11 @@ type Operator interface {
}
// NewOperator creates new operator
func NewOperator(name, namespace string) Operator {
func NewOperator(logger zerolog.Logger, name, namespace string) Operator {
o := &operator{
name: name,
namespace: namespace,
logger: logger,
workqueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), name),
}
@ -80,6 +82,8 @@ type operator struct {
started bool
logger zerolog.Logger
name string
namespace string
@ -192,14 +196,14 @@ func (o *operator) Start(threadiness int, stopCh <-chan struct{}) error {
func (o *operator) start(threadiness int, stopCh <-chan struct{}) error {
// Execute pre checks
log.Info().Msgf("Executing Lifecycle PreStart")
o.logger.Info().Msgf("Executing Lifecycle PreStart")
for _, handler := range o.handlers {
if err := ExecLifecyclePreStart(handler); err != nil {
return err
}
}
log.Info().Msgf("Starting informers")
o.logger.Info().Msgf("Starting informers")
for _, starter := range o.starters {
starter.Start(stopCh)
}
@ -208,12 +212,12 @@ func (o *operator) start(threadiness int, stopCh <-chan struct{}) error {
return err
}
log.Info().Msgf("Starting workers")
o.logger.Info().Msgf("Starting workers")
for i := 0; i < threadiness; i++ {
go wait.Until(o.worker, time.Second, stopCh)
}
log.Info().Msgf("Operator started")
o.logger.Info().Msgf("Operator started")
return nil
}

View file

@ -26,6 +26,8 @@ import (
"context"
"time"
"github.com/rs/zerolog/log"
"github.com/arangodb/kube-arangodb/pkg/backup/operator/operation"
"github.com/stretchr/testify/assert"
@ -43,7 +45,7 @@ import (
func Test_Operator_InformerProcessing(t *testing.T) {
// Arrange
name := string(uuid.NewUUID())
o := NewOperator(name, name)
o := NewOperator(log.Logger, name, name)
size := 64
objects := make([]string, size)
@ -92,7 +94,7 @@ func Test_Operator_InformerProcessing(t *testing.T) {
func Test_Operator_MultipleInformers(t *testing.T) {
// Arrange
name := string(uuid.NewUUID())
o := NewOperator(name, name)
o := NewOperator(log.Logger, name, name)
size := 16
objects := make([]string, size)
@ -153,7 +155,7 @@ func Test_Operator_MultipleInformers(t *testing.T) {
func Test_Operator_MultipleInformers_IgnoredTypes(t *testing.T) {
// Arrange
name := string(uuid.NewUUID())
o := NewOperator(name, name)
o := NewOperator(log.Logger, name, name)
size := 16
objects := make([]string, size)
@ -213,7 +215,7 @@ func Test_Operator_MultipleInformers_IgnoredTypes(t *testing.T) {
func Test_Operator_MultipleInformers_MultipleHandlers(t *testing.T) {
// Arrange
name := string(uuid.NewUUID())
o := NewOperator(name, name)
o := NewOperator(log.Logger, name, name)
size := 16
objects := make([]string, size)
@ -321,7 +323,7 @@ func Test_Operator_MultipleInformers_MultipleHandlers(t *testing.T) {
func Test_Operator_InformerProcessing_Namespaced(t *testing.T) {
// Arrange
name := string(uuid.NewUUID())
o := NewOperator(name, name)
o := NewOperator(log.Logger, name, name)
size := 16
objects := make([]string, size)

View file

@ -25,8 +25,6 @@ package operator
import (
"github.com/arangodb/kube-arangodb/pkg/backup/operator/operation"
"github.com/arangodb/kube-arangodb/pkg/util/errors"
"github.com/rs/zerolog/log"
)
func (o *operator) worker() {
@ -39,7 +37,7 @@ func (o *operator) processNextItem() bool {
defer func() {
// Recover from panic to not shutdown whole operator
if err := recover(); err != nil {
e := log.Error()
e := o.logger.Error()
switch obj := err.(type) {
case error:
@ -65,7 +63,7 @@ func (o *operator) processNextItem() bool {
err := o.processObject(obj)
if err != nil {
log.Error().Err(err).Interface("object", obj).Msgf("Error during object handling")
o.logger.Error().Err(err).Interface("object", obj).Msgf("Error during object handling")
return true
}
@ -98,7 +96,7 @@ func (o *operator) processObject(obj interface{}) error {
o.objectProcessed.Inc()
log.Debug().Msgf("Received Item Action: %s, Type: %s/%s/%s, Namespace: %s, Name: %s",
o.logger.Trace().Msgf("Received Item Action: %s, Type: %s/%s/%s, Namespace: %s, Name: %s",
item.Operation,
item.Group,
item.Version,
@ -111,7 +109,7 @@ func (o *operator) processObject(obj interface{}) error {
return errors.Newf("error syncing '%s': %s, requeuing", key, err.Error())
}
log.Debug().Msgf("Processed Item Action: %s, Type: %s/%s/%s, Namespace: %s, Name: %s",
o.logger.Trace().Msgf("Processed Item Action: %s, Type: %s/%s/%s, Namespace: %s, Name: %s",
item.Operation,
item.Group,
item.Version,

View file

@ -25,6 +25,8 @@ package operator
import (
"testing"
"github.com/rs/zerolog/log"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"k8s.io/apimachinery/pkg/util/uuid"
@ -33,7 +35,7 @@ import (
func Test_Worker_Empty(t *testing.T) {
// Arrange
name := string(uuid.NewUUID())
o := NewOperator(name, name)
o := NewOperator(log.Logger, name, name)
stopCh := make(chan struct{})
@ -53,7 +55,7 @@ func Test_Worker_Empty(t *testing.T) {
func Test_Worker_CatchAll(t *testing.T) {
// Arrange
name := string(uuid.NewUUID())
o := NewOperator(name, name)
o := NewOperator(log.Logger, name, name)
stopCh := make(chan struct{})
@ -82,7 +84,7 @@ func Test_Worker_CatchAll(t *testing.T) {
func Test_Worker_EnsureFirstProcessStopLoop(t *testing.T) {
// Arrange
name := string(uuid.NewUUID())
o := NewOperator(name, name)
o := NewOperator(log.Logger, name, name)
stopCh := make(chan struct{})
@ -116,7 +118,7 @@ func Test_Worker_EnsureFirstProcessStopLoop(t *testing.T) {
func Test_Worker_EnsureObjectIsProcessedBySecondHandler(t *testing.T) {
// Arrange
name := string(uuid.NewUUID())
o := NewOperator(name, name)
o := NewOperator(log.Logger, name, name)
stopCh := make(chan struct{})

49
pkg/logging/const.go Normal file
View file

@ -0,0 +1,49 @@
//
// DISCLAIMER
//
// Copyright 2021 ArangoDB GmbH, Cologne, Germany
//
// 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.
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//
// Author Adam Janikowski
//
package logging
const (
LoggerNameOperator = "operator"
LoggerNameDeployment = "deployment"
LoggerNameKLog = "klog"
LoggerNameServer = "server"
LoggerNameDeploymentReplication = "deployment-replication"
LoggerNameStorage = "storage"
LoggerNameProvisioner = "provisioner"
LoggerNameReconciliation = "reconciliation"
LoggerNameEventRecorder = "event-recorder"
)
func LoggerNames() []string {
return []string{
LoggerNameOperator,
LoggerNameDeployment,
LoggerNameKLog,
LoggerNameServer,
LoggerNameDeploymentReplication,
LoggerNameStorage,
LoggerNameProvisioner,
LoggerNameReconciliation,
LoggerNameEventRecorder,
}
}

View file

@ -118,7 +118,7 @@ func NewOperator(config Config, deps Dependencies) (*Operator, error) {
o := &Operator{
Config: config,
Dependencies: deps,
log: deps.LogService.MustGetLogger("operator"),
log: deps.LogService.MustGetLogger(logging.LoggerNameOperator),
deployments: make(map[string]*deployment.Deployment),
deploymentReplications: make(map[string]*replication.DeploymentReplication),
localStorages: make(map[string]*storage.LocalStorage),
@ -214,7 +214,7 @@ func (o *Operator) onStartBackup(stop <-chan struct{}) {
}
}
operatorName := "arangodb-backup-operator"
operator := backupOper.NewOperator(operatorName, o.Namespace)
operator := backupOper.NewOperator(o.Dependencies.LogService.MustGetLogger(logging.LoggerNameReconciliation), operatorName, o.Namespace)
rand.Seed(time.Now().Unix())
@ -235,7 +235,7 @@ func (o *Operator) onStartBackup(stop <-chan struct{}) {
panic(err)
}
eventRecorder := event.NewEventRecorder(operatorName, kubeClientSet)
eventRecorder := event.NewEventRecorder(o.Dependencies.LogService.MustGetLogger(logging.LoggerNameEventRecorder), operatorName, kubeClientSet)
arangoInformer := arangoInformer.NewSharedInformerFactoryWithOptions(arangoClientSet, 10*time.Second, arangoInformer.WithNamespace(o.Namespace))

View file

@ -24,6 +24,7 @@ package operator
import (
deploymentType "github.com/arangodb/kube-arangodb/pkg/apis/deployment"
"github.com/arangodb/kube-arangodb/pkg/logging"
"github.com/arangodb/kube-arangodb/pkg/util/errors"
kwatch "k8s.io/apimachinery/pkg/watch"
@ -211,7 +212,7 @@ func (o *Operator) makeDeploymentConfigAndDeps(apiObject *api.ArangoDeployment)
Scope: o.Scope,
}
deps := deployment.Dependencies{
Log: o.Dependencies.LogService.MustGetLogger("deployment").With().
Log: o.Dependencies.LogService.MustGetLogger(logging.LoggerNameDeployment).With().
Str("deployment", apiObject.GetName()).
Logger(),
KubeCli: o.Dependencies.KubeCli,

View file

@ -24,6 +24,7 @@ package operator
import (
replication2 "github.com/arangodb/kube-arangodb/pkg/apis/replication"
"github.com/arangodb/kube-arangodb/pkg/logging"
"github.com/arangodb/kube-arangodb/pkg/util/errors"
kwatch "k8s.io/apimachinery/pkg/watch"
@ -205,7 +206,7 @@ func (o *Operator) makeDeploymentReplicationConfigAndDeps(apiObject *api.ArangoD
Namespace: o.Config.Namespace,
}
deps := replication.Dependencies{
Log: o.Dependencies.LogService.MustGetLogger("deployment-replication").With().
Log: o.Dependencies.LogService.MustGetLogger(logging.LoggerNameDeploymentReplication).With().
Str("deployment-replication", apiObject.GetName()).
Logger(),
KubeCli: o.Dependencies.KubeCli,

View file

@ -23,6 +23,7 @@
package operator
import (
"github.com/arangodb/kube-arangodb/pkg/logging"
"github.com/arangodb/kube-arangodb/pkg/util/errors"
kwatch "k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/tools/cache"
@ -205,7 +206,7 @@ func (o *Operator) makeLocalStorageConfigAndDeps(apiObject *api.ArangoLocalStora
ServiceAccount: o.Config.ServiceAccount,
}
deps := storage.Dependencies{
Log: o.Dependencies.LogService.MustGetLogger("storage").With().
Log: o.Dependencies.LogService.MustGetLogger(logging.LoggerNameStorage).With().
Str("localStorage", apiObject.GetName()).
Logger(),
KubeCli: o.Dependencies.KubeCli,

View file

@ -95,7 +95,7 @@ func newProvisionerConfigAndDeps(nodeName string) (service.Config, service.Depen
NodeName: nodeName,
}
deps := service.Dependencies{
Log: logService.MustGetLogger("provisioner"),
Log: logService.MustGetLogger(logging.LoggerNameProvisioner),
}
return cfg, deps