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

fix(operator): Add validation on empty per-tenant limits config #12199

Closed
wants to merge 29 commits into from
Closed
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
829c417
add validation on empty per-tenant limits config
btaani Mar 13, 2024
8c521ee
fix lint issues
btaani Mar 13, 2024
c0d2abd
fix unit tests
btaani Mar 13, 2024
57aff57
Merge branch 'main' into per-tenant-limits-validation
btaani Mar 13, 2024
ac502f0
modify CHANGELOG.md
btaani Mar 13, 2024
29d0cd9
Merge branch 'main' into per-tenant-limits-validation
btaani Mar 14, 2024
5dd0327
update docs
btaani Mar 14, 2024
d7785bd
Update operator/apis/loki/v1/lokistack_types.go
btaani Mar 14, 2024
d23db84
Update operator/internal/handlers/lokistack_create_or_update.go
btaani Mar 14, 2024
f3e110f
Merge branch 'main' into per-tenant-limits-validation
btaani Mar 14, 2024
9cd1274
Merge branch 'main' into per-tenant-limits-validation
btaani Mar 15, 2024
d316196
update docs
btaani Mar 15, 2024
305ace7
Merge branch 'main' into per-tenant-limits-validation
btaani Mar 15, 2024
dd42df8
Merge branch 'main' into per-tenant-limits-validation
btaani Mar 25, 2024
c3e7d96
Merge branch 'main' into per-tenant-limits-validation
btaani Mar 26, 2024
8e55069
move validation to a separate function
btaani Mar 27, 2024
8f4683f
Merge branch 'main' into per-tenant-limits-validation
btaani Mar 27, 2024
a9377bd
Update operator/internal/manifests/options.go
btaani Apr 2, 2024
6338b70
Update operator/internal/manifests/options.go
btaani Apr 2, 2024
844912b
Update operator/internal/manifests/options.go
btaani Apr 2, 2024
dd0e2da
Merge branch 'main' into per-tenant-limits-validation
btaani Apr 2, 2024
f746500
fix errors
btaani Apr 3, 2024
dbe5547
Merge branch 'main' into per-tenant-limits-validation
btaani Apr 3, 2024
b31aaee
Merge branch 'main' into per-tenant-limits-validation
btaani Apr 4, 2024
270007d
Merge branch 'main' into per-tenant-limits-validation
btaani Apr 4, 2024
b792120
Simplify implementation
xperimental Apr 3, 2024
7ffcfa2
Fix typo
xperimental Apr 4, 2024
6bd8f18
modify docs
btaani Apr 4, 2024
c272f4d
Merge branch 'main' into per-tenant-limits-validation
btaani Apr 5, 2024
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

- [12199](https://github.com/grafana/loki/pull/12199) **btaani**: Add validation on empty per-tenant limits config
- [12333](https://github.com/grafana/loki/pull/12333) **periklis**: Bump max OpenShift version to next release

## 0.6.0 (2024-03-19)
Expand Down
2 changes: 2 additions & 0 deletions operator/apis/loki/v1/lokistack_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,8 @@ const (
ReasonFailedCertificateRotation LokiStackConditionReason = "FailedCertificateRotation"
// ReasonQueryTimeoutInvalid when the QueryTimeout can not be parsed.
ReasonQueryTimeoutInvalid LokiStackConditionReason = "ReasonQueryTimeoutInvalid"
// ReasonInvalidPerTenantLimitsConfig when the per-tenant limits configuration is invalid.
ReasonInvalidPerTenantLimitsConfig LokiStackConditionReason = "ReasonInvalidPerTenantLimitsConfig"
// ReasonZoneAwareNodesMissing when the cluster does not contain any nodes with the labels needed for zone-awareness.
ReasonZoneAwareNodesMissing LokiStackConditionReason = "ReasonZoneAwareNodesMissing"
// ReasonZoneAwareEmptyLabel when the node-label used for zone-awareness has an empty value.
Expand Down
3 changes: 3 additions & 0 deletions operator/docs/operator/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1769,6 +1769,9 @@ PodStatusMap
</tr><tr><td><p>&#34;InvalidObjectStorageSecret&#34;</p></td>
<td><p>ReasonInvalidObjectStorageSecret when the format of the secret is invalid.</p>
</td>
</tr><tr><td><p>&#34;ReasonInvalidPerTenantLimitsConfig&#34;</p></td>
<td><p>ReasonInvalidPerTenantLimitsConfig when the per-tenant limits configuration is invalid.</p>
</td>
</tr><tr><td><p>&#34;InvalidReplicationConfiguration&#34;</p></td>
<td><p>ReasonInvalidReplicationConfiguration when the configurated replication factor is not valid
with the select cluster size.</p>
Expand Down
13 changes: 13 additions & 0 deletions operator/internal/handlers/lokistack_create_or_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package handlers

import (
"context"
"errors"
"fmt"
"os"

Expand Down Expand Up @@ -82,6 +83,18 @@ func CreateOrUpdateLokiStack(
certRotationRequiredAt = stack.Annotations[manifests.AnnotationCertRotationRequiredAt]
}

err = manifests.ValidatePerTenantConfig(stack.Spec.Limits)
if err != nil {
if errors.Is(err, manifests.ErrPerTenantConfigInvalid) {
ll.Error(err, "invalid per-tenant config")
return "", &status.DegradedError{
Message: fmt.Sprintf("Invalid per-tenant limits config: %s", err),
Reason: lokiv1.ReasonInvalidPerTenantLimitsConfig,
Requeue: false,
}
}
}

timeoutConfig, err := manifests.NewTimeoutConfig(stack.Spec.Limits)
if err != nil {
ll.Error(err, "failed to parse query timeout")
Expand Down
70 changes: 70 additions & 0 deletions operator/internal/handlers/lokistack_create_or_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -740,3 +740,73 @@ func TestCreateOrUpdateLokiStack_WhenInvalidQueryTimeout_SetDegraded(t *testing.
require.Error(t, err)
require.Equal(t, degradedErr, err)
}

func TestCreateOrUpdateLokiStack_WhenInvalidPerTenantLimits_SetDegraded(t *testing.T) {
sw := &k8sfakes.FakeStatusWriter{}
k := &k8sfakes.FakeClient{}
r := ctrl.Request{
NamespacedName: types.NamespacedName{
Name: "my-stack",
Namespace: "some-ns",
},
}

degradedErr := &status.DegradedError{
Message: "Invalid per-tenant limits config: per-tenant config cannot contain empty keys: invalid",
Reason: lokiv1.ReasonInvalidPerTenantLimitsConfig,
Requeue: false,
}

stack := lokiv1.LokiStack{
TypeMeta: metav1.TypeMeta{
Kind: "LokiStack",
},
ObjectMeta: metav1.ObjectMeta{
Name: "my-stack",
Namespace: "some-ns",
UID: "b23f9a38-9672-499f-8c29-15ede74d3ece",
},
Spec: lokiv1.LokiStackSpec{
Size: lokiv1.SizeOneXExtraSmall,
Storage: lokiv1.ObjectStorageSpec{
Schemas: []lokiv1.ObjectStorageSchema{
{
Version: lokiv1.ObjectStorageSchemaV11,
EffectiveDate: "2020-10-11",
},
},
Secret: lokiv1.ObjectStorageSecretSpec{
Name: defaultSecret.Name,
Type: lokiv1.ObjectStorageSecretS3,
},
},
Tenants: &lokiv1.TenantsSpec{
Mode: "openshift",
},
Limits: &lokiv1.LimitsSpec{
Tenants: map[string]lokiv1.PerTenantLimitsTemplateSpec{
"invalid": {},
},
},
},
}

// Create looks up the CR first, so we need to return our fake stack
k.GetStub = func(_ context.Context, name types.NamespacedName, object client.Object, _ ...client.GetOption) error {
_, isLokiStack := object.(*lokiv1.LokiStack)
if r.Name == name.Name && r.Namespace == name.Namespace && isLokiStack {
k.SetClientObject(object, &stack)
}
if defaultSecret.Name == name.Name {
k.SetClientObject(object, &defaultSecret)
}
return nil
}

k.StatusStub = func() client.StatusWriter { return sw }

_, err := CreateOrUpdateLokiStack(context.TODO(), logger, r, k, scheme, featureGates)

require.Error(t, err)
require.Equal(t, degradedErr, err)
}
16 changes: 16 additions & 0 deletions operator/internal/manifests/options.go
btaani marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package manifests

import (
"errors"
"fmt"
"strings"
"time"

Expand Down Expand Up @@ -126,6 +128,8 @@ type TLSProfileSpec struct {
MinTLSVersion string
}

var ErrPerTenantConfigInvalid error = errors.New("per-tenant config cannot contain empty keys")

// TLSCipherSuites transforms TLSProfileSpec.Ciphers from a slice
// to a string of elements joined with a comma.
func (o Options) TLSCipherSuites() string {
Expand Down Expand Up @@ -195,3 +199,15 @@ func calculateHTTPTimeouts(queryTimeout time.Duration) TimeoutConfig {
},
}
}

func ValidatePerTenantConfig(s *lokiv1.LimitsSpec) error {
if s != nil && s.Tenants != nil {
for key, config := range s.Tenants {
c := lokiv1.PerTenantLimitsTemplateSpec{}
if config == c {
return fmt.Errorf("%w: %s", ErrPerTenantConfigInvalid, key)
}
}
}
return nil
}
Loading