Skip to content

Commit 8d89c3d

Browse files
committed
Adding lokistack status to console configmap
1 parent 6e94eb0 commit 8d89c3d

File tree

5 files changed

+53
-16
lines changed

5 files changed

+53
-16
lines changed

internal/controller/consoleplugin/config/config.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package config
33
import (
44
_ "embed"
55

6+
lokiv1 "github.com/grafana/loki/operator/apis/loki/v1"
67
"github.com/netobserv/flowlogs-pipeline/pkg/api"
78
flowslatest "github.com/netobserv/network-observability-operator/api/flowcollector/v1beta2"
89
"gopkg.in/yaml.v2"
@@ -24,18 +25,19 @@ type LokiConfig struct {
2425
URL string `yaml:"url" json:"url"`
2526
Labels []string `yaml:"labels" json:"labels"`
2627

27-
StatusURL string `yaml:"statusUrl,omitempty" json:"statusUrl,omitempty"`
28-
Timeout api.Duration `yaml:"timeout,omitempty" json:"timeout,omitempty"`
29-
TenantID string `yaml:"tenantID,omitempty" json:"tenantID,omitempty"`
30-
TokenPath string `yaml:"tokenPath,omitempty" json:"tokenPath,omitempty"`
31-
SkipTLS bool `yaml:"skipTls,omitempty" json:"skipTls,omitempty"`
32-
CAPath string `yaml:"caPath,omitempty" json:"caPath,omitempty"`
33-
StatusSkipTLS bool `yaml:"statusSkipTls,omitempty" json:"statusSkipTls,omitempty"`
34-
StatusCAPath string `yaml:"statusCaPath,omitempty" json:"statusCaPath,omitempty"`
35-
StatusUserCertPath string `yaml:"statusUserCertPath,omitempty" json:"statusUserCertPath,omitempty"`
36-
StatusUserKeyPath string `yaml:"statusUserKeyPath,omitempty" json:"statusUserKeyPath,omitempty"`
37-
UseMocks bool `yaml:"useMocks,omitempty" json:"useMocks,omitempty"`
38-
ForwardUserToken bool `yaml:"forwardUserToken,omitempty" json:"forwardUserToken,omitempty"`
28+
Status *lokiv1.LokiStackStatus `yaml:"status,omitempty" json:"status,omitempty"`
29+
StatusURL string `yaml:"statusUrl,omitempty" json:"statusUrl,omitempty"`
30+
Timeout api.Duration `yaml:"timeout,omitempty" json:"timeout,omitempty"`
31+
TenantID string `yaml:"tenantID,omitempty" json:"tenantID,omitempty"`
32+
TokenPath string `yaml:"tokenPath,omitempty" json:"tokenPath,omitempty"`
33+
SkipTLS bool `yaml:"skipTls,omitempty" json:"skipTls,omitempty"`
34+
CAPath string `yaml:"caPath,omitempty" json:"caPath,omitempty"`
35+
StatusSkipTLS bool `yaml:"statusSkipTls,omitempty" json:"statusSkipTls,omitempty"`
36+
StatusCAPath string `yaml:"statusCaPath,omitempty" json:"statusCaPath,omitempty"`
37+
StatusUserCertPath string `yaml:"statusUserCertPath,omitempty" json:"statusUserCertPath,omitempty"`
38+
StatusUserKeyPath string `yaml:"statusUserKeyPath,omitempty" json:"statusUserKeyPath,omitempty"`
39+
UseMocks bool `yaml:"useMocks,omitempty" json:"useMocks,omitempty"`
40+
ForwardUserToken bool `yaml:"forwardUserToken,omitempty" json:"forwardUserToken,omitempty"`
3941
}
4042

4143
type PrometheusConfig struct {

internal/controller/consoleplugin/consoleplugin_objects.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strconv"
1010
"time"
1111

12+
lokiv1 "github.com/grafana/loki/operator/apis/loki/v1"
1213
osv1 "github.com/openshift/api/console/v1"
1314
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
1415
"gopkg.in/yaml.v2"
@@ -497,7 +498,7 @@ func (b *builder) setFrontendConfig(fconf *cfg.FrontendConfig) error {
497498

498499
// returns a configmap with a digest of its configuration contents, which will be used to
499500
// detect any configuration change
500-
func (b *builder) configMap(ctx context.Context) (*corev1.ConfigMap, string, error) {
501+
func (b *builder) configMap(ctx context.Context, lokiStack *lokiv1.LokiStack) (*corev1.ConfigMap, string, error) {
501502
config := cfg.PluginConfig{
502503
Server: cfg.ServerConfig{
503504
Port: int(*b.advanced.Port),
@@ -513,6 +514,10 @@ func (b *builder) configMap(ctx context.Context) (*corev1.ConfigMap, string, err
513514
// configure loki
514515
var err error
515516
config.Loki, err = b.getLokiConfig()
517+
if lokiStack != nil {
518+
config.Loki.Status = &lokiStack.Status
519+
config.Loki.StatusURL = ""
520+
}
516521
if err != nil {
517522
return nil, "", err
518523
}

internal/controller/consoleplugin/consoleplugin_reconciler.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"k8s.io/apimachinery/pkg/types"
1515
"sigs.k8s.io/controller-runtime/pkg/log"
1616

17+
lokiv1 "github.com/grafana/loki/operator/apis/loki/v1"
1718
flowslatest "github.com/netobserv/network-observability-operator/api/flowcollector/v1beta2"
1819
"github.com/netobserv/network-observability-operator/internal/controller/constants"
1920
"github.com/netobserv/network-observability-operator/internal/controller/reconcilers"
@@ -83,7 +84,7 @@ func (r *CPReconciler) Reconcile(ctx context.Context, desired *flowslatest.FlowC
8384
}
8485
}
8586

86-
cmDigest, err := r.reconcileConfigMap(ctx, &builder)
87+
cmDigest, err := r.reconcileConfigMap(ctx, &builder, &desired.Spec)
8788
if err != nil {
8889
return err
8990
}
@@ -179,8 +180,22 @@ func (r *CPReconciler) reconcilePlugin(ctx context.Context, builder *builder, de
179180
return nil
180181
}
181182

182-
func (r *CPReconciler) reconcileConfigMap(ctx context.Context, builder *builder) (string, error) {
183-
newCM, configDigest, err := builder.configMap(ctx)
183+
func (r *CPReconciler) reconcileConfigMap(ctx context.Context, builder *builder, desired *flowslatest.FlowCollectorSpec) (string, error) {
184+
lokiStack := &lokiv1.LokiStack{}
185+
if desired.Loki.Mode == flowslatest.LokiModeLokiStack {
186+
if r.ClusterInfo.HasLokiStack() {
187+
ns := desired.Loki.LokiStack.Namespace
188+
if ns == "" {
189+
ns = desired.Namespace
190+
}
191+
if err := r.Client.Get(ctx, types.NamespacedName{Name: desired.Loki.LokiStack.Name, Namespace: ns}, lokiStack); err != nil {
192+
lokiStack = nil
193+
log.FromContext(ctx).Info("Could not get the LokiStack resource.")
194+
}
195+
}
196+
}
197+
198+
newCM, configDigest, err := builder.configMap(ctx, lokiStack)
184199
if err != nil {
185200
return "", err
186201
}

internal/controller/flowcollector_controller.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66

7+
lokiv1 "github.com/grafana/loki/operator/apis/loki/v1"
78
osv1 "github.com/openshift/api/console/v1"
89
securityv1 "github.com/openshift/api/security/v1"
910
appsv1 "k8s.io/api/apps/v1"
@@ -12,6 +13,7 @@ import (
1213
ctrl "sigs.k8s.io/controller-runtime"
1314
"sigs.k8s.io/controller-runtime/pkg/client"
1415
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
16+
"sigs.k8s.io/controller-runtime/pkg/handler"
1517
"sigs.k8s.io/controller-runtime/pkg/log"
1618

1719
flowslatest "github.com/netobserv/network-observability-operator/api/flowcollector/v1beta2"
@@ -68,6 +70,11 @@ func Start(ctx context.Context, mgr *manager.Manager) error {
6870
log.Info("CNO not detected: using ovnKubernetes config and reconciler")
6971
}
7072

73+
if mgr.ClusterInfo.HasLokiStack() {
74+
builder.Watches(&lokiv1.LokiStack{}, &handler.EnqueueRequestForObject{})
75+
log.Info("LokiStack CRD detected")
76+
}
77+
7178
ctrl, err := builder.Build(&r)
7279
if err != nil {
7380
return err

internal/pkg/cluster/cluster.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"strings"
88

99
"github.com/coreos/go-semver/semver"
10+
lokiv1 "github.com/grafana/loki/operator/apis/loki/v1"
1011
configv1 "github.com/openshift/api/config/v1"
1112
osv1 "github.com/openshift/api/console/v1"
1213
operatorv1 "github.com/openshift/api/operator/v1"
@@ -31,6 +32,7 @@ var (
3132
svcMonitor = "servicemonitors." + monv1.SchemeGroupVersion.String()
3233
promRule = "prometheusrules." + monv1.SchemeGroupVersion.String()
3334
ocpSecurity = "securitycontextconstraints." + securityv1.SchemeGroupVersion.String()
35+
lokistacks = "lokistacks." + lokiv1.GroupVersion.String()
3436
)
3537

3638
func NewInfo(ctx context.Context, dcl *discovery.DiscoveryClient) (Info, error) {
@@ -58,6 +60,7 @@ func (c *Info) fetchAvailableAPIs(ctx context.Context, client *discovery.Discove
5860
svcMonitor: false,
5961
promRule: false,
6062
ocpSecurity: false,
63+
lokistacks: false,
6164
}
6265
_, resources, err := client.ServerGroupsAndResources()
6366
// We may receive partial data along with an error
@@ -179,3 +182,8 @@ func (c *Info) HasSvcMonitor() bool {
179182
func (c *Info) HasPromRule() bool {
180183
return c.apisMap[promRule]
181184
}
185+
186+
// HasLokiStack returns true if "lokistack" API was found
187+
func (c *Info) HasLokiStack() bool {
188+
return c.apisMap[lokistacks]
189+
}

0 commit comments

Comments
 (0)