generated from giantswarm/template-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add k8s events logging to alloy #263
Open
QuantumEnigmaa
wants to merge
30
commits into
main
Choose a base branch
from
alloy-log-events
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+420
−228
Open
Changes from 21 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
b276673
add k8s events logging to alloy
QuantumEnigmaa d1ecc0e
changelog
QuantumEnigmaa 73c3a71
create the events-logger resource : either alloy or grafana-agent
QuantumEnigmaa c39115a
handle secret for authentication
QuantumEnigmaa 3cfb909
merge with main and resolve conflicts
QuantumEnigmaa 33cce97
fix alloy config property name
QuantumEnigmaa dedd9ad
merge with main
QuantumEnigmaa f49f6f3
merge with main
QuantumEnigmaa 2e6c67f
merge with main
QuantumEnigmaa f09f504
fix build errors
QuantumEnigmaa 9a8454c
rename k8s-events-config package to events-logger-config
QuantumEnigmaa a62b968
Apply suggestions from code review
QuantumEnigmaa 8e78389
fix error log
QuantumEnigmaa 6776f50
return correct configmap name depending on the events logger
QuantumEnigmaa 27f1aef
return correct secret name depending on the events logger
QuantumEnigmaa 83c572b
add scrapedNamespaces to alloy config create related function in common
QuantumEnigmaa ab44bc4
add tls config for alloy
QuantumEnigmaa ad0e906
fix formating in grafana-agent template
QuantumEnigmaa 1469dff
hardcode controller.type and controller.replicas in alloyEvents template
QuantumEnigmaa 25c8cb4
remove unused observabilityBundleVersion parameters and variable in e…
QuantumEnigmaa e0cb4a7
remove grafanaAgentSecretName and eventsLoggerSecretName from securit…
QuantumEnigmaa aa6917a
rename variables in events-logger-config's reconciler to a neutral name
QuantumEnigmaa 049d2cc
make several functions private
QuantumEnigmaa ec83211
hardcode several fields in grafana-agent config template
QuantumEnigmaa ad971d0
rename comments and logs in agents-toggle's reconciler
QuantumEnigmaa 194d1cc
enable events logger in main.go
QuantumEnigmaa b3e196e
fix formating in events-logger templates
QuantumEnigmaa cdc361c
remove unused field in alloy generateAlloyConfig
QuantumEnigmaa df3a66e
fix alloy-events secret name
QuantumEnigmaa 5d04402
merge with main
QuantumEnigmaa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package eventsloggerconfig | ||
|
||
import ( | ||
"bytes" | ||
_ "embed" | ||
"fmt" | ||
"text/template" | ||
|
||
"github.com/Masterminds/sprig/v3" | ||
|
||
"github.com/giantswarm/logging-operator/pkg/common" | ||
loggedcluster "github.com/giantswarm/logging-operator/pkg/logged-cluster" | ||
eventsloggersecret "github.com/giantswarm/logging-operator/pkg/resource/events-logger-secret" | ||
loggingsecret "github.com/giantswarm/logging-operator/pkg/resource/logging-secret" | ||
) | ||
|
||
var ( | ||
//go:embed alloy/events-logger.alloy.template | ||
alloyEvents string | ||
alloyEventsTemplate *template.Template | ||
|
||
//go:embed alloy/events-logger-config.alloy.yaml.template | ||
alloyEventsConfig string | ||
alloyEventsConfigTemplate *template.Template | ||
) | ||
|
||
func init() { | ||
alloyEventsTemplate = template.Must(template.New("events-logger.alloy").Funcs(sprig.FuncMap()).Parse(alloyEvents)) | ||
alloyEventsConfigTemplate = template.Must(template.New("events-logger.alloy.yaml").Funcs(sprig.FuncMap()).Parse(alloyEventsConfig)) | ||
} | ||
|
||
func GenerateAlloyEventsConfig(lc loggedcluster.Interface, defaultNamespaces []string) (string, error) { | ||
var values bytes.Buffer | ||
|
||
alloyConfig, err := generateAlloyConfig(lc, defaultNamespaces) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
data := struct { | ||
AlloyConfig string | ||
SecretName string | ||
}{ | ||
AlloyConfig: alloyConfig, | ||
SecretName: eventsloggersecret.GetEventsLoggerSecretName(lc), | ||
} | ||
|
||
err = alloyEventsConfigTemplate.Execute(&values, data) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return values.String(), nil | ||
} | ||
|
||
func generateAlloyConfig(lc loggedcluster.Interface, defaultNamespaces []string) (string, error) { | ||
var values bytes.Buffer | ||
|
||
data := struct { | ||
ClusterID string | ||
Installation string | ||
InsecureSkipVerify string | ||
DefaultWorkloadClusterNamespaces []string | ||
MaxBackoffPeriod string | ||
LokiURLEnvVarName string | ||
TenantIDEnvVarName string | ||
BasicAuthUsernameEnvVarName string | ||
BasicAuthPasswordEnvVarName string | ||
ScrapedNamespaces string | ||
}{ | ||
ClusterID: lc.GetClusterName(), | ||
Installation: lc.GetInstallationName(), | ||
InsecureSkipVerify: fmt.Sprintf("%t", lc.IsInsecureCA()), | ||
DefaultWorkloadClusterNamespaces: defaultNamespaces, | ||
QuantumEnigmaa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
MaxBackoffPeriod: common.MaxBackoffPeriod, | ||
LokiURLEnvVarName: loggingsecret.AlloyLokiURLEnvVarName, | ||
TenantIDEnvVarName: loggingsecret.AlloyTenantIDEnvVarName, | ||
BasicAuthUsernameEnvVarName: loggingsecret.AlloyBasicAuthUsernameEnvVarName, | ||
BasicAuthPasswordEnvVarName: loggingsecret.AlloyBasicAuthPasswordEnvVarName, | ||
ScrapedNamespaces: common.FormatScrapedNamespaces(lc, defaultNamespaces), | ||
QuentinBisson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
err := alloyEventsTemplate.Execute(&values, data) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return values.String(), nil | ||
} |
28 changes: 28 additions & 0 deletions
28
pkg/resource/events-logger-config/alloy/events-logger-config.alloy.yaml.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# This file was generated by logging-operator. | ||
# It configures Alloy to be used as events logger. | ||
# - configMap is generated from events-logger.alloy.template and passed as a string | ||
# here and will be created by Alloy's chart. | ||
# - Alloy runs as a deployment, with only 1 replica. | ||
alloy: | ||
alloy: | ||
configMap: | ||
create: true | ||
content: |- | ||
{{ .AlloyConfig | indent 8 }} | ||
QuantumEnigmaa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
envFrom: | ||
- secretRef: | ||
name: {{ .SecretName }} | ||
securityContext: | ||
allowPrivilegeEscalation: false | ||
capabilities: | ||
drop: | ||
- ALL | ||
readOnlyRootFilesystem: false | ||
runAsUser: 10 | ||
runAsGroup: 10 | ||
runAsNonRoot: true | ||
seccompProfile: | ||
type: RuntimeDefault | ||
controller: | ||
type: deployment | ||
replicas: 1 |
31 changes: 31 additions & 0 deletions
31
pkg/resource/events-logger-config/alloy/events-logger.alloy.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,31 @@ | ||||||||
loki.source.kubernetes_events "local" { | ||||||||
namespaces = {{ .ScrapedNamespaces }} | ||||||||
forward_to = [loki.write.default.receiver] | ||||||||
} | ||||||||
|
||||||||
// Loki target configuration | ||||||||
loki.write "default" { | ||||||||
endpoint { | ||||||||
url = env("{{ .LokiURLEnvVarName }}") | ||||||||
max_backoff_period = "{{ .MaxBackoffPeriod }}" | ||||||||
tenant_id = env("{{ .TenantIDEnvVarName }}") | ||||||||
|
||||||||
basic_auth { | ||||||||
username = env("{{ .BasicAuthUsernameEnvVarName }}") | ||||||||
password = env("{{ .BasicAuthPasswordEnvVarName }}") | ||||||||
} | ||||||||
|
||||||||
tls_config { | ||||||||
insecure_skip_verify = {{ .InsecureSkipVerify }} | ||||||||
} | ||||||||
} | ||||||||
external_labels = { | ||||||||
cluster_id = "{{ .ClusterID }}", | ||||||||
installation = "{{ .Installation }}", | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To match with grafana agent |
||||||||
} | ||||||||
} | ||||||||
|
||||||||
logging { | ||||||||
level = "info" | ||||||||
QuantumEnigmaa marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
format = "logfmt" | ||||||||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package eventsloggerconfig | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/pkg/errors" | ||
|
||
v1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
"github.com/giantswarm/logging-operator/pkg/common" | ||
loggedcluster "github.com/giantswarm/logging-operator/pkg/logged-cluster" | ||
) | ||
|
||
const ( | ||
eventsLogggerConfigName = "events-logger-config" | ||
grafanaAgentConfigName = "grafana-agent-config" | ||
) | ||
|
||
func GenerateEventsLoggerConfig(lc loggedcluster.Interface, defaultNamespaces []string) (v1.ConfigMap, error) { | ||
var values string | ||
var err error | ||
|
||
switch lc.GetKubeEventsLogger() { | ||
case common.EventsLoggerGrafanaAgent: | ||
values, err = GenerateGrafanaAgentConfig(lc, defaultNamespaces) | ||
if err != nil { | ||
return v1.ConfigMap{}, err | ||
} | ||
case common.EventsLoggerAlloy: | ||
values, err = GenerateAlloyEventsConfig(lc, defaultNamespaces) | ||
if err != nil { | ||
return v1.ConfigMap{}, err | ||
} | ||
default: | ||
return v1.ConfigMap{}, errors.Errorf("unsupported events logger %q", lc.GetKubeEventsLogger()) | ||
} | ||
|
||
configmap := v1.ConfigMap{ | ||
ObjectMeta: ConfigMeta(lc), | ||
Data: map[string]string{ | ||
"values": values, | ||
}, | ||
} | ||
|
||
return configmap, nil | ||
} | ||
|
||
// ConfigMeta returns metadata for the logging-config | ||
func ConfigMeta(lc loggedcluster.Interface) metav1.ObjectMeta { | ||
metadata := metav1.ObjectMeta{ | ||
Name: getEventsLoggerConfigName(lc), | ||
Namespace: lc.GetAppsNamespace(), | ||
Labels: map[string]string{}, | ||
} | ||
|
||
common.AddCommonLabels(metadata.Labels) | ||
return metadata | ||
} | ||
|
||
func getEventsLoggerConfigName(lc loggedcluster.Interface) string { | ||
switch lc.GetKubeEventsLogger() { | ||
case common.EventsLoggerGrafanaAgent: | ||
return fmt.Sprintf("%s-%s", lc.GetClusterName(), grafanaAgentConfigName) | ||
default: | ||
return fmt.Sprintf("%s-%s", lc.GetClusterName(), eventsLogggerConfigName) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package eventsloggerconfig | ||
|
||
import ( | ||
"bytes" | ||
_ "embed" | ||
"fmt" | ||
"text/template" | ||
|
||
"github.com/Masterminds/sprig/v3" | ||
|
||
"github.com/giantswarm/logging-operator/pkg/common" | ||
loggedcluster "github.com/giantswarm/logging-operator/pkg/logged-cluster" | ||
eventsloggersecret "github.com/giantswarm/logging-operator/pkg/resource/events-logger-secret" | ||
) | ||
|
||
var ( | ||
//go:embed grafana-agent/events-logger.grafanaagent.template | ||
grafanaAgent string | ||
grafanaAgentTemplate *template.Template | ||
|
||
//go:embed grafana-agent/events-logger-config.grafanaagent.yaml.template | ||
grafanaAgentConfig string | ||
grafanaAgentConfigTemplate *template.Template | ||
) | ||
|
||
func init() { | ||
grafanaAgentTemplate = template.Must(template.New("events-logger.grafanaagent").Funcs(sprig.FuncMap()).Parse(grafanaAgent)) | ||
grafanaAgentConfigTemplate = template.Must(template.New("events-logger.grafanaagent.yaml").Funcs(sprig.FuncMap()).Parse(grafanaAgentConfig)) | ||
} | ||
|
||
// GenerateGrafanaAgentConfig returns a configmap for | ||
// the grafana-agent extra-config | ||
func GenerateGrafanaAgentConfig(lc loggedcluster.Interface, defaultNamespaces []string) (string, error) { | ||
var values bytes.Buffer | ||
|
||
grafanaAgentInnerConfig, err := generateGrafanaAgentInnerConfig(lc, defaultNamespaces) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
data := struct { | ||
GrafanaAgentInnerConfig string | ||
Replicas int | ||
Type string | ||
Create string | ||
}{ | ||
GrafanaAgentInnerConfig: grafanaAgentInnerConfig, | ||
Replicas: 1, | ||
Type: "deployment", | ||
Create: "false", | ||
} | ||
|
||
err = grafanaAgentConfigTemplate.Execute(&values, data) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return values.String(), nil | ||
} | ||
|
||
func generateGrafanaAgentInnerConfig(lc loggedcluster.Interface, defaultNamespaces []string) (string, error) { | ||
var values bytes.Buffer | ||
|
||
data := struct { | ||
ClusterID string | ||
Installation string | ||
InsecureSkipVerify string | ||
SecretName string | ||
SecretNamespace string | ||
ScrapedNamespaces string | ||
}{ | ||
ClusterID: lc.GetClusterName(), | ||
Installation: lc.GetInstallationName(), | ||
InsecureSkipVerify: fmt.Sprintf("%t", lc.IsInsecureCA()), | ||
SecretName: eventsloggersecret.GetEventsLoggerSecretName(lc), | ||
SecretNamespace: lc.GetAppsNamespace(), | ||
ScrapedNamespaces: common.FormatScrapedNamespaces(lc, defaultNamespaces), | ||
} | ||
|
||
err := grafanaAgentTemplate.Execute(&values, data) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return values.String(), nil | ||
} |
15 changes: 15 additions & 0 deletions
15
...source/events-logger-config/grafana-agent/events-logger-config.grafanaagent.yaml.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# This file was generated by logging-operator. | ||
# It configures the Grafana-agent to be used as events logger. | ||
# - configMap is generated from events-logger.grafanaagent.template and passed as a string | ||
# here and will be created by Grafana-agent's chart. | ||
# - Grafana-agent runs as a deployment, with only 1 replica. | ||
grafana-agent: | ||
agent: | ||
configMap: | ||
content: |- | ||
{{ .GrafanaAgentInnerConfig | indent 8 }} | ||
QuantumEnigmaa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
controller: | ||
replicas: {{ .Replicas }} | ||
type: {{ .Type }} | ||
crds: | ||
create: {{ .Create }} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can remove this change I guess :D