Skip to content

Commit

Permalink
operator: Refactor CreateOrUpdateLokiStack handler (#11592)
Browse files Browse the repository at this point in the history
Co-authored-by: Robert Jacob <[email protected]>
  • Loading branch information
periklis and xperimental authored Jan 12, 2024
1 parent edba360 commit 0065fd6
Show file tree
Hide file tree
Showing 22 changed files with 1,781 additions and 1,208 deletions.
5 changes: 2 additions & 3 deletions operator/internal/handlers/internal/gateway/base_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,18 @@ import (
"github.com/ViaQ/logerr/v2/kverrors"
configv1 "github.com/openshift/api/config/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

lokiv1 "github.com/grafana/loki/operator/apis/loki/v1"
"github.com/grafana/loki/operator/internal/external/k8s"
"github.com/grafana/loki/operator/internal/status"
)

// GetOpenShiftBaseDomain returns the cluster DNS base domain on OpenShift
// getOpenShiftBaseDomain returns the cluster DNS base domain on OpenShift
// clusters to auto-create redirect URLs for OpenShift Auth or an error.
// If the config.openshift.io/DNS object is not found the whole lokistack
// resoure is set to a degraded state.
func GetOpenShiftBaseDomain(ctx context.Context, k k8s.Client, req ctrl.Request) (string, error) {
func getOpenShiftBaseDomain(ctx context.Context, k k8s.Client) (string, error) {
var cluster configv1.DNS
key := client.ObjectKey{Name: "cluster"}
if err := k.Get(ctx, key, &cluster); err != nil {
Expand Down
87 changes: 87 additions & 0 deletions operator/internal/handlers/internal/gateway/gateway.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package gateway

import (
"context"
"fmt"

"github.com/go-logr/logr"

configv1 "github.com/grafana/loki/operator/apis/config/v1"
lokiv1 "github.com/grafana/loki/operator/apis/loki/v1"
"github.com/grafana/loki/operator/internal/external/k8s"
"github.com/grafana/loki/operator/internal/handlers/internal/openshift"
"github.com/grafana/loki/operator/internal/manifests"
"github.com/grafana/loki/operator/internal/status"
)

// BuildOptions returns the options needed to generate Kubernetes resource
// manifests for the lokistack-gateway.
// The returned error can be a status.DegradedError in the following cases:
// - The tenants spec is missing.
// - The tenants spec is invalid.
func BuildOptions(ctx context.Context, log logr.Logger, k k8s.Client, stack *lokiv1.LokiStack, fg configv1.FeatureGates) (string, manifests.Tenants, error) {
var (
err error
baseDomain string
secrets []*manifests.TenantSecrets
configs map[string]manifests.TenantConfig
tenants manifests.Tenants
)

if !fg.LokiStackGateway {
return "", tenants, nil
}

if stack.Spec.Tenants == nil {
return "", tenants, &status.DegradedError{
Message: "Invalid tenants configuration: TenantsSpec cannot be nil when gateway flag is enabled",
Reason: lokiv1.ReasonInvalidTenantsConfiguration,
Requeue: false,
}
}

if err = validateModes(stack); err != nil {
return "", tenants, &status.DegradedError{
Message: fmt.Sprintf("Invalid tenants configuration: %s", err),
Reason: lokiv1.ReasonInvalidTenantsConfiguration,
Requeue: false,
}
}

switch stack.Spec.Tenants.Mode {
case lokiv1.OpenshiftLogging, lokiv1.OpenshiftNetwork:
baseDomain, err = getOpenShiftBaseDomain(ctx, k)
if err != nil {
return "", tenants, err
}

if stack.Spec.Proxy == nil {
// If the LokiStack has no proxy set but there is a cluster-wide proxy setting,
// set the LokiStack proxy to that.
ocpProxy, proxyErr := openshift.GetProxy(ctx, k)
if proxyErr != nil {
return "", tenants, proxyErr
}

stack.Spec.Proxy = ocpProxy
}
default:
secrets, err = getTenantSecrets(ctx, k, stack)
if err != nil {
return "", tenants, err
}
}

// extract the existing tenant's id, cookieSecret if exists, otherwise create new.
configs, err = getTenantConfigFromSecret(ctx, k, stack)
if err != nil {
log.Error(err, "error in getting tenant secret data")
}

tenants = manifests.Tenants{
Secrets: secrets,
Configs: configs,
}

return baseDomain, tenants, nil
}
Loading

0 comments on commit 0065fd6

Please sign in to comment.