Skip to content

Commit

Permalink
Merge pull request #29 from evenh/skip-loki-update
Browse files Browse the repository at this point in the history
new flag: onlyReconcileRules
  • Loading branch information
BrunoTarijon committed Mar 18, 2024
2 parents 7b6ab3c + ffae60a commit d37f36d
Show file tree
Hide file tree
Showing 7 changed files with 345 additions and 265 deletions.
2 changes: 1 addition & 1 deletion deploy/helm/charts/loki-rule-operator/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type: application
# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: "0.7.4"
version: "0.7.5"

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ spec:
- -loki-rule-mount-path={{ .Values.lokiRuleOperator.lokiRuleMountPath }}
- -loki-url={{ .Values.lokiRuleOperator.lokiURL }}
{{- range .Values.lokiRuleOperator.lokiHeaders }}
- -loki-header="{{ . }}"
- -loki-header={{ . }}
{{- end }}
{{- if .Values.lokiRuleOperator.logLevel }}
- -log-level={{ .Values.lokiRuleOperator.logLevel }}
Expand All @@ -53,6 +53,7 @@ spec:
{{- if .Values.lokiRuleOperator.leaderElection.id }}
- -leader-election-id={{ .Values.lokiRuleOperator.leaderElection.id }}
{{- end }}
- -only-reconcile-rules={{ .Values.lokiRuleOperator.onlyReconcileRules | default false }}
livenessProbe:
httpGet:
path: /healthz
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ tests:
- "-leader-elect=true"
- "-leader-election-namespace=helm-test"
- "-leader-election-id=my-id"
- "-only-reconcile-rules=false"
- it: should configure app options with custom headers
values:
- ./minimal_values.yaml
Expand Down Expand Up @@ -131,14 +132,15 @@ tests:
- '-loki-namespace=loki'
- '-loki-rule-mount-path=/var/loki'
- '-loki-url=loki.url'
- '-loki-header="X-Foo=Bar"'
- '-loki-header="X-Bar=Foo"'
- '-loki-header=X-Foo=Bar'
- '-loki-header=X-Bar=Foo'
- '-log-level=debug'
- '-metrics-bind-address=:9090'
- '-health-probe-bind-address=:9091'
- '-leader-elect=true'
- '-leader-election-namespace=helm-test'
- '-leader-election-id=my-id'
- "-only-reconcile-rules=false"
- it: should configure globalOptions
values:
- ./minimal_values.yaml
Expand Down
1 change: 1 addition & 0 deletions deploy/helm/charts/loki-rule-operator/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,5 @@ lokiRuleOperator:
lokiURL: ""
# Extra HTTP headers specified as HeaderName=Value which will be passed on to Loki
lokiHeaders: []
onlyReconcileRules: false
keepCrds: false
12 changes: 11 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func main() {
var lokiRuleMountPath string
var lokiURL string
var lokiHeaders flags.ArrayFlags
var onlyReconcileRules bool

flag.BoolVar(
&enableLeaderElection,
Expand Down Expand Up @@ -138,6 +139,14 @@ func main() {
"loki-header",
"Extra header that will be sent to Loki. Format KEY=VALUE. May be repeated.",
)
flag.BoolVar(
&onlyReconcileRules,
"only-reconcile-rules",
false,
"When enabled the operator will only reconcile LokiRule's into the ConfigMap. "+
"It will skip updating the DaemonSet volume, volumeMounts and annotation hash, "+
"efficiently avoiding restarts of Loki.",
)

flag.Parse()

Expand Down Expand Up @@ -184,6 +193,7 @@ func main() {
LokiNamespace: lokiNamespace,
LokiRuleConfigMapName: "loki-rule-cfg",
LokiURL: lokiURL,
UpdateLoki: !onlyReconcileRules,
}).SetupWithManager(mgr); err != nil {
log.Error(err, "unable to create controller", "controller", "LokiRule")
os.Exit(1)
Expand All @@ -198,7 +208,7 @@ func main() {
os.Exit(1)
}

log.Info("starting manager")
log.Info("starting manager", "onlyReconcileRules", onlyReconcileRules)
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
log.Error(err, "problem running manager")
os.Exit(1)
Expand Down
89 changes: 47 additions & 42 deletions pkg/controllers/lokirule_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type LokiRuleReconciler struct {
LokiNamespace string
LokiRuleConfigMapName string
LokiURL string
UpdateLoki bool
}

func (r *LokiRuleReconciler) newRuleHandler(
Expand Down Expand Up @@ -163,16 +164,6 @@ func handleByEventType(r *LokiRuleReconciler) predicate.Predicate {
deletedInstance.Name,
)

lokiStatefulset, err := getLokiStatefulSet(
r.Client,
r.LokiLabelSelector,
r.LokiNamespace,
r.Logger,
)
if err != nil {
r.Logger.Error(err, "Failed to get Loki statefulSet")
}

configMapFileToRemove, err := lokirule.GenerateRuleConfigMapFile(deletedInstance)
if err != nil {
r.Logger.Error(err, "Failed to generate rule groups")
Expand All @@ -189,17 +180,29 @@ func handleByEventType(r *LokiRuleReconciler) predicate.Predicate {
r.Logger.Error(err, "Failed to ensure configMap exists")
}

err = k8sutils.MountConfigMap(
r.Client,
r.LokiNamespace,
r.LokiRuleConfigMapName,
r.LokiRulesPath,
lokiStatefulset,
options,
)

if err != nil {
r.Logger.Error(err, "ConfigMap not attached")
if r.UpdateLoki {
lokiStatefulset, err := getLokiStatefulSet(
r.Client,
r.LokiLabelSelector,
r.LokiNamespace,
r.Logger,
)
if err != nil {
r.Logger.Error(err, "Failed to get Loki statefulSet")
}

err = k8sutils.MountConfigMap(
r.Client,
r.LokiNamespace,
r.LokiRuleConfigMapName,
r.LokiRulesPath,
lokiStatefulset,
options,
)

if err != nil {
r.Logger.Error(err, "ConfigMap not attached")
}
}

r.Logger.Info("LokiRule Reconciled")
Expand Down Expand Up @@ -238,28 +241,30 @@ func (r *LokiRuleReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
r.Logger.Error(err, "Failed to handle LokiRule")
}

lokiStatefulset, err := getLokiStatefulSet(
r.Client,
r.LokiLabelSelector,
r.LokiNamespace,
r.Logger,
)
if err != nil {
r.Logger.Error(err, "Failed to get loki statefulSet")
return reconcile.Result{}, err
}
if r.UpdateLoki {
lokiStatefulset, err := getLokiStatefulSet(
r.Client,
r.LokiLabelSelector,
r.LokiNamespace,
r.Logger,
)
if err != nil {
r.Logger.Error(err, "Failed to get loki statefulSet")
return reconcile.Result{}, err
}

err = k8sutils.MountConfigMap(
r.Client,
r.LokiNamespace,
r.LokiRuleConfigMapName,
r.LokiRulesPath,
lokiStatefulset,
options,
)
if err != nil {
r.Logger.Error(err, "ConfigMap not attached")
return reconcile.Result{}, err
err = k8sutils.MountConfigMap(
r.Client,
r.LokiNamespace,
r.LokiRuleConfigMapName,
r.LokiRulesPath,
lokiStatefulset,
options,
)
if err != nil {
r.Logger.Error(err, "ConfigMap not attached")
return reconcile.Result{}, err
}
}

r.Logger.Info("LokiRule Reconciled")
Expand Down
Loading

0 comments on commit d37f36d

Please sign in to comment.