Skip to content

Commit cc092d7

Browse files
committed
NETOBSERV-2174: check if LokiStack exists
tmp
1 parent 887751b commit cc092d7

File tree

19 files changed

+5144
-0
lines changed

19 files changed

+5144
-0
lines changed

bundle/manifests/netobserv-operator.clusterserviceversion.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,14 @@ spec:
10801080
- get
10811081
- patch
10821082
- update
1083+
- apiGroups:
1084+
- loki.grafana.com
1085+
resources:
1086+
- lokistacks
1087+
verbs:
1088+
- get
1089+
- list
1090+
- watch
10831091
- apiGroups:
10841092
- loki.grafana.com
10851093
resourceNames:

config/rbac/role.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,14 @@ rules:
147147
- get
148148
- patch
149149
- update
150+
- apiGroups:
151+
- loki.grafana.com
152+
resources:
153+
- lokistacks
154+
verbs:
155+
- get
156+
- list
157+
- watch
150158
- apiGroups:
151159
- loki.grafana.com
152160
resourceNames:

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/coreos/go-semver v0.3.1
1010
github.com/go-logr/logr v1.4.2
1111
github.com/google/go-cmp v0.6.0
12+
github.com/grafana/loki/operator/apis/loki v0.0.0-20241021105923-5e970e50b166
1213
github.com/netobserv/flowlogs-pipeline v1.7.0-community.0.20250303144123-5f6d6dcd0bdf
1314
github.com/netobserv/netobserv-ebpf-agent v1.8.0-crc0
1415
github.com/onsi/ginkgo/v2 v2.23.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,8 @@ github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51
456456
github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
457457
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
458458
github.com/gorilla/websocket v0.0.0-20170926233335-4201258b820c/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
459+
github.com/grafana/loki/operator/apis/loki v0.0.0-20241021105923-5e970e50b166 h1:cmG5fwmF+0PsyerLecb7CU4bzNRg5+tDgO3PiNxskKo=
460+
github.com/grafana/loki/operator/apis/loki v0.0.0-20241021105923-5e970e50b166/go.mod h1:QggEReYyQzjnwTlj9hMeRaI2M/w3UPAwrMOXYzIyonc=
459461
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA=
460462
github.com/grpc-ecosystem/go-grpc-middleware v1.0.1-0.20190118093823-f849b5445de4/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
461463
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=

helm/templates/clusterrole.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,14 @@ rules:
146146
- get
147147
- patch
148148
- update
149+
- apiGroups:
150+
- loki.grafana.com
151+
resources:
152+
- lokistacks
153+
verbs:
154+
- get
155+
- list
156+
- watch
149157
- apiGroups:
150158
- loki.grafana.com
151159
resourceNames:

main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"os"
2727

2828
bpfmaniov1alpha1 "github.com/bpfman/bpfman-operator/apis/v1alpha1"
29+
lokiv1 "github.com/grafana/loki/operator/apis/loki/v1"
2930
osv1 "github.com/openshift/api/console/v1"
3031
operatorsv1 "github.com/openshift/api/operator/v1"
3132
securityv1 "github.com/openshift/api/security/v1"
@@ -84,6 +85,7 @@ func init() {
8485
utilruntime.Must(monitoringv1.AddToScheme(scheme))
8586
utilruntime.Must(apiextensionsv1.AddToScheme(scheme))
8687
utilruntime.Must(bpfmaniov1alpha1.Install(scheme))
88+
utilruntime.Must(lokiv1.AddToScheme(scheme))
8789
//+kubebuilder:scaffold:scheme
8890
}
8991

pkg/manager/manager.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
//+kubebuilder:rbac:groups=monitoring.coreos.com,resources=servicemonitors;prometheusrules,verbs=get;create;delete;update;patch;list;watch
3434
//+kubebuilder:rbac:groups=config.openshift.io,resources=clusterversions;networks,verbs=get;list;watch
3535
//+kubebuilder:rbac:groups=loki.grafana.com,resources=network,resourceNames=logs,verbs=create
36+
//+kubebuilder:rbac:groups=loki.grafana.com,resources=lokistacks,verbs=get;list;watch
3637
//+kubebuilder:rbac:groups=metrics.k8s.io,resources=pods,verbs=create
3738
//+kubebuilder:rbac:groups=networking.k8s.io,resources=networkpolicies,verbs=get;list;watch;create;update;patch;delete
3839
//+kubebuilder:rbac:groups=bpfman.io,resources=bpfapplications,verbs=get;list;watch;create;update;patch;delete

pkg/manager/status/status_manager.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@ import (
66
"strings"
77
"sync"
88

9+
lokiv1 "github.com/grafana/loki/operator/apis/loki/v1"
910
flowslatest "github.com/netobserv/network-observability-operator/apis/flowcollector/v1beta2"
1011
"github.com/netobserv/network-observability-operator/controllers/constants"
12+
"github.com/netobserv/network-observability-operator/pkg/helper"
1113
appsv1 "k8s.io/api/apps/v1"
1214
v1 "k8s.io/api/core/v1"
1315
kerr "k8s.io/apimachinery/pkg/api/errors"
1416
"k8s.io/apimachinery/pkg/api/meta"
1517
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
18+
"k8s.io/apimachinery/pkg/types"
1619
"k8s.io/client-go/util/retry"
1720
"sigs.k8s.io/controller-runtime/pkg/client"
1821
"sigs.k8s.io/controller-runtime/pkg/log"
@@ -28,6 +31,7 @@ const (
2831
Monitoring ComponentName = "Monitoring"
2932
NetworkPolicy ComponentName = "NetworkPolicy"
3033
ConditionConfigurationIssue = "ConfigurationIssue"
34+
LokiIssue = "LokiIssue"
3135
)
3236

3337
var allNames = []ComponentName{FlowCollectorLegacy, Monitoring}
@@ -136,6 +140,7 @@ func updateStatus(ctx context.Context, c client.Client, conditions ...metav1.Con
136140
return err
137141
}
138142
conditions = append(conditions, checkValidation(ctx, &fc))
143+
conditions = append(conditions, checkLoki(ctx, c, &fc))
139144
for _, c := range conditions {
140145
meta.SetStatusCondition(&fc.Status.Conditions, c)
141146
}
@@ -173,6 +178,52 @@ func checkValidation(ctx context.Context, fc *flowslatest.FlowCollector) metav1.
173178
}
174179
}
175180

181+
func checkLoki(ctx context.Context, c client.Client, fc *flowslatest.FlowCollector) metav1.Condition {
182+
if !helper.UseLoki(&fc.Spec) {
183+
return metav1.Condition{
184+
Type: LokiIssue,
185+
Reason: "Unused",
186+
Status: metav1.ConditionUnknown,
187+
Message: "Loki is disabled",
188+
}
189+
}
190+
if fc.Spec.Loki.Mode != flowslatest.LokiModeLokiStack {
191+
return metav1.Condition{
192+
Type: LokiIssue,
193+
Reason: "Unused",
194+
Status: metav1.ConditionUnknown,
195+
Message: "Loki is not configured in LokiStack mode",
196+
}
197+
}
198+
lokiStack := &lokiv1.LokiStack{}
199+
nsname := types.NamespacedName{Name: fc.Spec.Loki.LokiStack.Name, Namespace: fc.Spec.Namespace}
200+
if len(fc.Spec.Loki.LokiStack.Namespace) > 0 {
201+
nsname.Namespace = fc.Spec.Loki.LokiStack.Namespace
202+
}
203+
err := c.Get(ctx, nsname, lokiStack)
204+
if err != nil {
205+
if kerr.IsNotFound(err) {
206+
return metav1.Condition{
207+
Type: LokiIssue,
208+
Reason: "LokiStackNotFound",
209+
Status: metav1.ConditionTrue,
210+
Message: fmt.Sprintf("The configured LokiStack reference could not be found [name: %s, namespace: %s]", nsname.Name, nsname.Namespace),
211+
}
212+
}
213+
return metav1.Condition{
214+
Type: LokiIssue,
215+
Reason: "Error",
216+
Status: metav1.ConditionTrue,
217+
Message: fmt.Sprintf("Error while fetching configured LokiStack: %s", err.Error()),
218+
}
219+
}
220+
return metav1.Condition{
221+
Type: LokiIssue,
222+
Reason: "NoIssue",
223+
Status: metav1.ConditionFalse,
224+
}
225+
}
226+
176227
func (s *Manager) ForComponent(cpnt ComponentName) Instance {
177228
return Instance{cpnt: cpnt, s: s}
178229
}

pkg/test/envtest.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
//nolint:revive,stylecheck
1010
. "github.com/onsi/ginkgo/v2"
1111
//nolint:revive,stylecheck
12+
lokiv1 "github.com/grafana/loki/operator/apis/loki/v1"
1213
. "github.com/onsi/gomega"
1314
configv1 "github.com/openshift/api/config/v1"
1415
osv1 "github.com/openshift/api/console/v1"
@@ -115,6 +116,9 @@ func PrepareEnvTest(controllers []manager.Registerer, namespaces []string, baseP
115116
err = securityv1.AddToScheme(scheme.Scheme)
116117
Expect(err).NotTo(HaveOccurred())
117118

119+
err = lokiv1.AddToScheme(scheme.Scheme)
120+
Expect(err).NotTo(HaveOccurred())
121+
118122
k8sClient, err := client.New(cfg, client.Options{Scheme: scheme.Scheme})
119123
Expect(err).NotTo(HaveOccurred())
120124
Expect(k8sClient).NotTo(BeNil())

0 commit comments

Comments
 (0)