Skip to content
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

operator: logerr v2 update #5987

Merged
merged 3 commits into from
Apr 26, 2022
Merged
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
1 change: 1 addition & 0 deletions operator/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## Main

- [5987](https://github.com/grafana/loki/pull/5987) **Red-GV**: Update logerr to v2.0.0
- [5907](https://github.com/grafana/loki/pull/5907) **xperimental**: Do not include non-static labels in pod selectors
- [5893](https://github.com/grafana/loki/pull/5893) **periklis**: Align PVC storage size requests for all lokistack t-shirt sizes
- [5884](https://github.com/grafana/loki/pull/5884) **periklis**: Update Loki operand to v2.5.0
Expand Down
2 changes: 1 addition & 1 deletion operator/cmd/loki-broker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"path"
"strings"

"github.com/ViaQ/logerr/log"
"github.com/ViaQ/logerr/v2/log"
"github.com/go-logr/logr"
"github.com/grafana/loki/operator/api/v1beta1"
"github.com/grafana/loki/operator/internal/manifests"
Expand Down
6 changes: 3 additions & 3 deletions operator/cmd/size-calculator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/grafana/loki/operator/internal/sizes"
"github.com/prometheus/common/model"

"github.com/ViaQ/logerr/log"
"github.com/ViaQ/logerr/v2/log"
)

const (
Expand All @@ -29,9 +29,9 @@ const (
sizeOneXMedium string = "1x.medium"
)

var logger = log.NewLogger("size-calculator")

func main() {
logger := log.NewLogger("size-calculator")

logger.Info("starting storage size calculator...")

for {
Expand Down
9 changes: 2 additions & 7 deletions operator/controllers/internal/management/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,16 @@ import (
lokiv1beta1 "github.com/grafana/loki/operator/api/v1beta1"
"github.com/grafana/loki/operator/internal/external/k8s"

"github.com/ViaQ/logerr/kverrors"
"github.com/go-logr/logr"
"github.com/ViaQ/logerr/v2/kverrors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
ctrl "sigs.k8s.io/controller-runtime"
)

// IsManaged checks if the custom resource is configured with ManagementState Managed.
func IsManaged(ctx context.Context, log logr.Logger, req ctrl.Request, k k8s.Client) (bool, error) {
ll := log.WithValues("lokistack", req.NamespacedName)

func IsManaged(ctx context.Context, req ctrl.Request, k k8s.Client) (bool, error) {
var stack lokiv1beta1.LokiStack
if err := k.Get(ctx, req.NamespacedName, &stack); err != nil {
if apierrors.IsNotFound(err) {
// maybe the user deleted it before we could react? Either way this isn't an issue
ll.Error(err, "could not find the requested loki stack", "name", req.NamespacedName)
return false, nil
}
return false, kverrors.Wrap(err, "failed to lookup lokistack", "name", req.NamespacedName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import (
"github.com/grafana/loki/operator/controllers/internal/management/state"
"github.com/grafana/loki/operator/internal/external/k8s/k8sfakes"

"github.com/ViaQ/logerr/kverrors"
"github.com/ViaQ/logerr/log"
"github.com/ViaQ/logerr/v2/kverrors"
"github.com/stretchr/testify/require"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -19,8 +18,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
)

var logger = log.DefaultLogger()

func TestIsManaged(t *testing.T) {
type test struct {
name string
Expand Down Expand Up @@ -76,7 +73,7 @@ func TestIsManaged(t *testing.T) {
k.SetClientObject(object, &tst.stack)
return nil
}
ok, err := state.IsManaged(context.TODO(), logger, r, k)
ok, err := state.IsManaged(context.TODO(), r, k)
require.NoError(t, err)
require.Equal(t, ok, tst.wantOk)
})
Expand Down Expand Up @@ -112,7 +109,7 @@ func TestIsManaged_WhenError_ReturnNotManagedWithError(t *testing.T) {
for _, tst := range table {
t.Run(tst.name, func(t *testing.T) {
k.GetReturns(tst.apierror)
ok, err := state.IsManaged(context.TODO(), logger, r, k)
ok, err := state.IsManaged(context.TODO(), r, k)
require.Equal(t, tst.wantErr, err)
require.False(t, ok)
})
Expand Down
2 changes: 1 addition & 1 deletion operator/controllers/lokistack_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ type LokiStackReconciler struct {
// 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) {
ok, err := state.IsManaged(ctx, r.Log, req, r.Client)
ok, err := state.IsManaged(ctx, req, r.Client)
if err != nil {
return ctrl.Result{
Requeue: true,
Expand Down
11 changes: 5 additions & 6 deletions operator/controllers/lokistack_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import (
"github.com/grafana/loki/operator/internal/external/k8s/k8sfakes"
"github.com/grafana/loki/operator/internal/manifests"

"github.com/ViaQ/logerr/log"
"github.com/ViaQ/logerr/v2/log"
"github.com/go-logr/logr"
routev1 "github.com/openshift/api/route/v1"
"github.com/stretchr/testify/require"
appsv1 "k8s.io/api/apps/v1"
Expand All @@ -25,7 +26,7 @@ import (
)

var (
logger = log.NewLogger("testing")
logger logr.Logger

scheme = runtime.NewScheme()
)
Expand All @@ -34,12 +35,10 @@ func TestMain(m *testing.M) {
testing.Init()
flag.Parse()

sink := log.MustGetSink(logger)
if testing.Verbose() {
// set to the highest for verbose testing
sink.SetVerbosity(5)
logger = log.NewLogger("testing", log.WithVerbosity(5))
} else {
sink.SetOutput(ioutil.Discard)
logger = log.NewLogger("testing", log.WithOutput(ioutil.Discard))
}

// Register the clientgo and CRD schemes
Expand Down
3 changes: 2 additions & 1 deletion operator/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module github.com/grafana/loki/operator
go 1.17

require (
github.com/ViaQ/logerr v1.1.0
github.com/go-logr/logr v1.2.3
github.com/google/uuid v1.1.2
github.com/imdario/mergo v0.3.12
Expand All @@ -21,6 +20,8 @@ require (
sigs.k8s.io/yaml v1.3.0
)

require github.com/ViaQ/logerr/v2 v2.0.0

require (
cloud.google.com/go v0.81.0 // indirect
github.com/Azure/go-autorest v14.2.0+incompatible // indirect
Expand Down
4 changes: 2 additions & 2 deletions operator/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ github.com/PuerkitoBio/purell v1.1.0/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbt
github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
github.com/PuerkitoBio/urlesc v0.0.0-20160726150825-5bd2802263f2/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
github.com/ViaQ/logerr v1.1.0 h1:Jm+WBMbKUcwiDV/aJOIB5Rv5mg0UnyULRs1Jbz5Qq8U=
github.com/ViaQ/logerr v1.1.0/go.mod h1:D0eovRXC5iBP/jW8Nb3mF25JVxg16eAEmwHBsrGCXlI=
github.com/ViaQ/logerr/v2 v2.0.0 h1:5NOOexPjkhaga6E13JJfPa0vRtyW+nIvTYUDrheZETM=
github.com/ViaQ/logerr/v2 v2.0.0/go.mod h1:/qoWLm3YG40Sv5u75s4fvzjZ5p36xINzaxU2L+DJ9uw=
github.com/agnivade/levenshtein v1.0.1/go.mod h1:CURSv5d9Uaml+FovSIICkLbAUZ9S4RqaHDIsdSBg7lM=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package gateway
import (
"context"

"github.com/ViaQ/logerr/kverrors"
"github.com/ViaQ/logerr/v2/kverrors"
lokiv1beta1 "github.com/grafana/loki/operator/api/v1beta1"
"github.com/grafana/loki/operator/internal/external/k8s"
"github.com/grafana/loki/operator/internal/status"
Expand Down
2 changes: 1 addition & 1 deletion operator/internal/handlers/internal/gateway/modes.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package gateway

import (
"github.com/ViaQ/logerr/kverrors"
"github.com/ViaQ/logerr/v2/kverrors"
lokiv1beta1 "github.com/grafana/loki/operator/api/v1beta1"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import (
"github.com/grafana/loki/operator/internal/manifests"
"github.com/grafana/loki/operator/internal/manifests/openshift"

"github.com/ViaQ/logerr/kverrors"
"github.com/go-logr/logr"
"github.com/ViaQ/logerr/v2/kverrors"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/json"
ctrl "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -39,18 +38,16 @@ type openShiftSpec struct {

// GetTenantConfigMapData returns the tenantName, tenantId, cookieSecret
// clusters to auto-create redirect URLs for OpenShift Auth or an error.
func GetTenantConfigMapData(ctx context.Context, log logr.Logger, k k8s.Client, req ctrl.Request) map[string]openshift.TenantData {
func GetTenantConfigMapData(ctx context.Context, k k8s.Client, req ctrl.Request) (map[string]openshift.TenantData, error) {
var tenantConfigMap corev1.ConfigMap
key := client.ObjectKey{Name: manifests.GatewayName(req.Name), Namespace: req.Namespace}
if err := k.Get(ctx, key, &tenantConfigMap); err != nil {
log.Error(err, "couldn't find")
return nil
return nil, kverrors.Wrap(err, "couldn't find tenant configMap.")
}

tcm, err := extractTenantConfigMap(&tenantConfigMap)
if err != nil {
log.Error(err, "error occurred in extracting tenants.yaml configMap.")
return nil
return nil, kverrors.Wrap(err, "error occurred in extracting tenants.yaml configMap.")
}

tcmMap := make(map[string]openshift.TenantData)
Expand All @@ -60,7 +57,7 @@ func GetTenantConfigMapData(ctx context.Context, log logr.Logger, k k8s.Client,
}
}

return tcmMap
return tcmMap, nil
}

// extractTenantConfigMap extracts tenants.yaml data if valid.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/grafana/loki/operator/internal/external/k8s/k8sfakes"
"github.com/grafana/loki/operator/internal/manifests/openshift"

"github.com/ViaQ/logerr/log"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -35,8 +34,6 @@ tenants:
cookieSecret: test789
`)

var logger = log.DefaultLogger()

func TestGetTenantConfigMapData_ConfigMapExist(t *testing.T) {
k := &k8sfakes.FakeClient{}
r := ctrl.Request{
Expand All @@ -61,8 +58,9 @@ func TestGetTenantConfigMapData_ConfigMapExist(t *testing.T) {
return nil
}

ts := GetTenantConfigMapData(context.TODO(), logger, k, r)
ts, err := GetTenantConfigMapData(context.TODO(), k, r)
require.NotNil(t, ts)
require.NoError(t, err)

expected := map[string]openshift.TenantData{
"application": {
Expand Down Expand Up @@ -91,6 +89,7 @@ func TestGetTenantConfigMapData_ConfigMapNotExist(t *testing.T) {
return nil
}

ts := GetTenantConfigMapData(context.TODO(), logger, k, r)
ts, err := GetTenantConfigMapData(context.TODO(), k, r)
require.Nil(t, ts)
require.Error(t, err)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"fmt"

"github.com/ViaQ/logerr/kverrors"
"github.com/ViaQ/logerr/v2/kverrors"

lokiv1beta1 "github.com/grafana/loki/operator/api/v1beta1"
"github.com/grafana/loki/operator/internal/external/k8s"
Expand Down
2 changes: 1 addition & 1 deletion operator/internal/handlers/internal/secrets/secrets.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package secrets

import (
"github.com/ViaQ/logerr/kverrors"
"github.com/ViaQ/logerr/v2/kverrors"
lokiv1beta1 "github.com/grafana/loki/operator/api/v1beta1"
"github.com/grafana/loki/operator/internal/manifests"
"github.com/grafana/loki/operator/internal/manifests/storage"
Expand Down
9 changes: 6 additions & 3 deletions operator/internal/handlers/lokistack_create_or_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/grafana/loki/operator/internal/metrics"
"github.com/grafana/loki/operator/internal/status"

"github.com/ViaQ/logerr/kverrors"
"github.com/ViaQ/logerr/v2/kverrors"
"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
Expand Down Expand Up @@ -111,7 +111,10 @@ func CreateOrUpdateLokiStack(
}

// extract the existing tenant's id, cookieSecret if exists, otherwise create new.
tenantConfigMap = gateway.GetTenantConfigMapData(ctx, log, k, req)
tenantConfigMap, err = gateway.GetTenantConfigMapData(ctx, k, req)
if err != nil {
ll.Error(err, "error in getting tenant config map data")
}
}
}

Expand Down Expand Up @@ -169,7 +172,7 @@ func CreateOrUpdateLokiStack(
}

desired := obj.DeepCopyObject().(client.Object)
mutateFn := manifests.MutateFuncFor(log, obj, desired)
mutateFn := manifests.MutateFuncFor(obj, desired)

op, err := ctrl.CreateOrUpdate(ctx, k, obj, mutateFn)
if err != nil {
Expand Down
11 changes: 5 additions & 6 deletions operator/internal/handlers/lokistack_create_or_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import (
"github.com/grafana/loki/operator/internal/manifests"
"github.com/grafana/loki/operator/internal/status"

"github.com/ViaQ/logerr/log"
"github.com/ViaQ/logerr/v2/log"
"github.com/go-logr/logr"
routev1 "github.com/openshift/api/route/v1"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -32,7 +33,7 @@ import (
)

var (
logger = log.NewLogger("testing")
logger logr.Logger

scheme = runtime.NewScheme()
flags = manifests.FeatureFlags{
Expand Down Expand Up @@ -80,12 +81,10 @@ func TestMain(m *testing.M) {
testing.Init()
flag.Parse()

sink := log.MustGetSink(logger)
if testing.Verbose() {
// set to the highest for verbose testing
sink.SetVerbosity(5)
logger = log.NewLogger("testing", log.WithVerbosity(5))
} else {
sink.SetOutput(ioutil.Discard)
logger = log.NewLogger("testing", log.WithOutput(ioutil.Discard))
}

// Register the clientgo and CRD schemes
Expand Down
2 changes: 1 addition & 1 deletion operator/internal/manifests/build.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package manifests

import (
"github.com/ViaQ/logerr/kverrors"
"github.com/ViaQ/logerr/v2/kverrors"
lokiv1beta1 "github.com/grafana/loki/operator/api/v1beta1"
"github.com/grafana/loki/operator/internal/manifests/internal"

Expand Down
2 changes: 1 addition & 1 deletion operator/internal/manifests/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"path"

"github.com/ViaQ/logerr/kverrors"
"github.com/ViaQ/logerr/v2/kverrors"
"github.com/imdario/mergo"

"github.com/grafana/loki/operator/internal/manifests/internal/gateway"
Expand Down
2 changes: 1 addition & 1 deletion operator/internal/manifests/gateway_tenants.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package manifests

import (
"github.com/ViaQ/logerr/kverrors"
"github.com/ViaQ/logerr/v2/kverrors"
lokiv1beta1 "github.com/grafana/loki/operator/api/v1beta1"
"github.com/grafana/loki/operator/internal/manifests/internal/gateway"
"github.com/grafana/loki/operator/internal/manifests/openshift"
Expand Down
2 changes: 1 addition & 1 deletion operator/internal/manifests/internal/alerts/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"io"
"text/template"

"github.com/ViaQ/logerr/kverrors"
"github.com/ViaQ/logerr/v2/kverrors"
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
"k8s.io/apimachinery/pkg/util/yaml"
)
Expand Down
2 changes: 1 addition & 1 deletion operator/internal/manifests/internal/config/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"io/ioutil"
"text/template"

"github.com/ViaQ/logerr/kverrors"
"github.com/ViaQ/logerr/v2/kverrors"
)

const (
Expand Down
Loading