Skip to content

Commit 6815b9b

Browse files
committed
Adding lokistack status to console configmap
1 parent 6e94eb0 commit 6815b9b

File tree

6 files changed

+65
-28
lines changed

6 files changed

+65
-28
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/consoleplugin/consoleplugin_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func getAutoScalerSpecs() (ascv2.HorizontalPodAutoscaler, flowslatest.FlowCollec
110110
func getBuilder(spec *flowslatest.FlowCollectorSpec, lk *helper.LokiConfig) builder {
111111
info := reconcilers.Common{Namespace: testNamespace, Loki: lk, ClusterInfo: &cluster.Info{}}
112112
b := newBuilder(info.NewInstance(map[reconcilers.ImageRef]string{reconcilers.MainImage: testImage}, status.Instance{}), spec, constants.PluginName)
113-
_, _, _ = b.configMap(context.Background()) // build configmap to update builder's volumes
113+
_, _, _ = b.configMap(context.Background(), nil) // build configmap to update builder's volumes
114114
return b
115115
}
116116

@@ -223,8 +223,8 @@ func TestConfigMapUpdateCheck(t *testing.T) {
223223
}
224224
spec := flowslatest.FlowCollectorSpec{ConsolePlugin: plugin}
225225
builder := getBuilder(&spec, &loki)
226-
old, _, _ := builder.configMap(context.Background())
227-
nEw, _, _ := builder.configMap(context.Background())
226+
old, _, _ := builder.configMap(context.Background(), nil)
227+
nEw, _, _ := builder.configMap(context.Background(), nil)
228228
assert.Equal(old.Data, nEw.Data)
229229

230230
// update loki
@@ -239,15 +239,15 @@ func TestConfigMapUpdateCheck(t *testing.T) {
239239
}},
240240
}
241241
builder = getBuilder(&spec, &loki)
242-
nEw, _, _ = builder.configMap(context.Background())
242+
nEw, _, _ = builder.configMap(context.Background(), nil)
243243
assert.NotEqual(old.Data, nEw.Data)
244244
old = nEw
245245

246246
// set status url and enable default tls
247247
loki.LokiManualParams.StatusURL = "http://loki.status:3100/"
248248
loki.LokiManualParams.StatusTLS.Enable = true
249249
builder = getBuilder(&spec, &loki)
250-
nEw, _, _ = builder.configMap(context.Background())
250+
nEw, _, _ = builder.configMap(context.Background(), nil)
251251
assert.NotEqual(old.Data, nEw.Data)
252252
old = nEw
253253

@@ -258,7 +258,7 @@ func TestConfigMapUpdateCheck(t *testing.T) {
258258
CertFile: "status-ca.crt",
259259
}
260260
builder = getBuilder(&spec, &loki)
261-
nEw, _, _ = builder.configMap(context.Background())
261+
nEw, _, _ = builder.configMap(context.Background(), nil)
262262
assert.NotEqual(old.Data, nEw.Data)
263263
old = nEw
264264

@@ -270,7 +270,7 @@ func TestConfigMapUpdateCheck(t *testing.T) {
270270
CertKey: "tls.key",
271271
}
272272
builder = getBuilder(&spec, &loki)
273-
nEw, _, _ = builder.configMap(context.Background())
273+
nEw, _, _ = builder.configMap(context.Background(), nil)
274274
assert.NotEqual(old.Data, nEw.Data)
275275
}
276276

@@ -286,8 +286,8 @@ func TestConfigMapUpdateWithLokistackMode(t *testing.T) {
286286
loki := helper.NewLokiConfig(&lokiSpec, "any")
287287
spec := flowslatest.FlowCollectorSpec{ConsolePlugin: plugin, Loki: lokiSpec}
288288
builder := getBuilder(&spec, &loki)
289-
old, _, _ := builder.configMap(context.Background())
290-
nEw, _, _ := builder.configMap(context.Background())
289+
old, _, _ := builder.configMap(context.Background(), nil)
290+
nEw, _, _ := builder.configMap(context.Background(), nil)
291291
assert.Equal(old.Data, nEw.Data)
292292

293293
// update lokistack name
@@ -296,7 +296,7 @@ func TestConfigMapUpdateWithLokistackMode(t *testing.T) {
296296

297297
spec = flowslatest.FlowCollectorSpec{ConsolePlugin: plugin, Loki: lokiSpec}
298298
builder = getBuilder(&spec, &loki)
299-
nEw, _, _ = builder.configMap(context.Background())
299+
nEw, _, _ = builder.configMap(context.Background(), nil)
300300
assert.NotEqual(old.Data, nEw.Data)
301301
old = nEw
302302

@@ -306,7 +306,7 @@ func TestConfigMapUpdateWithLokistackMode(t *testing.T) {
306306

307307
spec = flowslatest.FlowCollectorSpec{ConsolePlugin: plugin, Loki: lokiSpec}
308308
builder = getBuilder(&spec, &loki)
309-
nEw, _, _ = builder.configMap(context.Background())
309+
nEw, _, _ = builder.configMap(context.Background(), nil)
310310
assert.NotEqual(old.Data, nEw.Data)
311311
}
312312

@@ -331,7 +331,7 @@ func TestConfigMapContent(t *testing.T) {
331331
Processor: flowslatest.FlowCollectorFLP{SubnetLabels: flowslatest.SubnetLabels{OpenShiftAutoDetect: ptr.To(false)}},
332332
}
333333
builder := getBuilder(&spec, &loki)
334-
cm, _, err := builder.configMap(context.Background())
334+
cm, _, err := builder.configMap(context.Background(), nil)
335335
assert.NotNil(cm)
336336
assert.Nil(err)
337337

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)