generated from giantswarm/template-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
template grafana-agent-config (#273)
* template grafana-agent-config * fix incorrect grafana agent secret namespace * add common format scraped namespaces
- Loading branch information
1 parent
b24f44c
commit 5319466
Showing
15 changed files
with
375 additions
and
185 deletions.
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
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,98 @@ | ||
package eventsloggerconfig | ||
|
||
import ( | ||
"bytes" | ||
_ "embed" | ||
"fmt" | ||
"text/template" | ||
|
||
"github.com/Masterminds/sprig/v3" | ||
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 ( | ||
grafanaAgentConfigName = "grafana-agent-config" | ||
) | ||
|
||
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-config.grafanaagent.yaml").Funcs(sprig.FuncMap()).Parse(grafanaAgentConfig)) | ||
} | ||
|
||
// configMeta returns metadata for the grafana-agent-config | ||
func configMeta(lc loggedcluster.Interface) metav1.ObjectMeta { | ||
metadata := metav1.ObjectMeta{ | ||
Name: getGrafanaAgentConfigName(lc), | ||
Namespace: lc.GetAppsNamespace(), | ||
Labels: map[string]string{}, | ||
} | ||
|
||
common.AddCommonLabels(metadata.Labels) | ||
return metadata | ||
} | ||
|
||
// generateGrafanaAgentConfig returns a configmap for | ||
// the grafana-agent extra-config | ||
func generateGrafanaAgentConfig(lc loggedcluster.Interface, defaultWorkloadClusterNamespaces []string) (string, error) { | ||
var values bytes.Buffer | ||
|
||
grafanaAgentInnerConfig, err := generateGrafanaAgentInnerConfig(lc, defaultWorkloadClusterNamespaces) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
data := struct { | ||
GrafanaAgentInnerConfig string | ||
}{ | ||
GrafanaAgentInnerConfig: grafanaAgentInnerConfig, | ||
} | ||
|
||
err = grafanaAgentTemplate.Execute(&values, data) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return values.String(), nil | ||
} | ||
|
||
func generateGrafanaAgentInnerConfig(lc loggedcluster.Interface, defaultWorkloadClusterNamespaces []string) (string, error) { | ||
var values bytes.Buffer | ||
|
||
data := struct { | ||
ClusterID string | ||
Installation string | ||
InsecureSkipVerify string | ||
SecretName string | ||
ScrapedNamespaces string | ||
}{ | ||
ClusterID: lc.GetClusterName(), | ||
Installation: lc.GetInstallationName(), | ||
InsecureSkipVerify: fmt.Sprintf("%t", lc.IsInsecureCA()), | ||
SecretName: fmt.Sprintf("%s-%s", lc.GetClusterName(), common.GrafanaAgentExtraSecretName()), | ||
ScrapedNamespaces: common.FormatScrapedNamespaces(lc, defaultWorkloadClusterNamespaces), | ||
} | ||
|
||
err := grafanaAgentConfigTemplate.Execute(&values, data) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return values.String(), nil | ||
} | ||
|
||
func getGrafanaAgentConfigName(lc loggedcluster.Interface) string { | ||
return fmt.Sprintf("%s-%s", lc.GetClusterName(), grafanaAgentConfigName) | ||
} |
77 changes: 77 additions & 0 deletions
77
pkg/resource/events-logger-config/grafana-agent-config_test.go
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,77 @@ | ||
package eventsloggerconfig | ||
|
||
import ( | ||
_ "embed" | ||
"flag" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
capi "sigs.k8s.io/cluster-api/api/v1beta1" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
|
||
loggedcluster "github.com/giantswarm/logging-operator/pkg/logged-cluster" | ||
"github.com/giantswarm/logging-operator/pkg/logged-cluster/capicluster" | ||
) | ||
|
||
var ( | ||
update = flag.Bool("update", false, "update .golden files") | ||
) | ||
|
||
func TestGenerateGrafanaAgentConfig(t *testing.T) { | ||
testCases := []struct { | ||
goldenFile string | ||
defaultNamespaces []string | ||
installationName string | ||
clusterName string | ||
}{ | ||
{ | ||
goldenFile: "grafana-agent/test/events-logger-config.grafanaagent.MC.yaml", | ||
installationName: "test-installation", | ||
clusterName: "test-installation", | ||
}, | ||
{ | ||
goldenFile: "grafana-agent/test/events-logger-config.grafanaagent.WC.yaml", | ||
installationName: "test-installation", | ||
clusterName: "test-cluster", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(filepath.Base(tc.goldenFile), func(t *testing.T) { | ||
golden, err := os.ReadFile(tc.goldenFile) | ||
if err != nil { | ||
t.Fatalf("Failed to read golden file: %v", err) | ||
} | ||
|
||
loggedCluster := &capicluster.Object{ | ||
Object: &capi.Cluster{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: tc.clusterName, | ||
}, | ||
}, | ||
Options: loggedcluster.Options{ | ||
InstallationName: tc.installationName, | ||
}, | ||
} | ||
|
||
config, err := generateGrafanaAgentConfig(loggedCluster, []string{"kube-system", "giantswarm"}) | ||
if err != nil { | ||
t.Fatalf("Failed to generate grafana-agent config: %v", err) | ||
} | ||
|
||
if string(golden) != config { | ||
t.Logf("Generated config differs from %s, diff:\n%s", tc.goldenFile, cmp.Diff(string(golden), config)) | ||
t.Fail() | ||
if *update { | ||
//nolint:gosec | ||
if err := os.WriteFile(tc.goldenFile, []byte(config), 0644); err != nil { | ||
t.Fatalf("Failed to update golden file: %v", err) | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...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,35 @@ | ||
logging { | ||
level = "info" | ||
format = "logfmt" | ||
} | ||
|
||
loki.source.kubernetes_events "local" { | ||
namespaces = {{ .ScrapedNamespaces }} | ||
forward_to = [loki.write.default.receiver] | ||
} | ||
|
||
remote.kubernetes.secret "credentials" { | ||
namespace = "kube-system" | ||
name = "{{ .SecretName }}" | ||
} | ||
|
||
loki.write "default" { | ||
endpoint { | ||
url = nonsensitive(remote.kubernetes.secret.credentials.data["logging-url"]) | ||
tenant_id = nonsensitive(remote.kubernetes.secret.credentials.data["logging-tenant-id"]) | ||
|
||
basic_auth { | ||
username = nonsensitive(remote.kubernetes.secret.credentials.data["logging-username"]) | ||
password = remote.kubernetes.secret.credentials.data["logging-password"] | ||
} | ||
|
||
tls_config { | ||
insecure_skip_verify = {{ .InsecureSkipVerify }} | ||
} | ||
} | ||
external_labels = { | ||
installation = "{{ .Installation }}", | ||
cluster_id = "{{ .ClusterID }}", | ||
scrape_job = "kubernetes-events", | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
pkg/resource/events-logger-config/grafana-agent/events-logger.grafanaagent.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,17 @@ | ||
# 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 | nindent 8 }} | ||
extraArgs: | ||
- --disable-reporting | ||
controller: | ||
replicas: 1 | ||
type: deployment | ||
crds: | ||
create: false |
52 changes: 52 additions & 0 deletions
52
...esource/events-logger-config/grafana-agent/test/events-logger-config.grafanaagent.MC.yaml
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,52 @@ | ||
# 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: |- | ||
logging { | ||
level = "info" | ||
format = "logfmt" | ||
} | ||
loki.source.kubernetes_events "local" { | ||
namespaces = [] | ||
forward_to = [loki.write.default.receiver] | ||
} | ||
remote.kubernetes.secret "credentials" { | ||
namespace = "kube-system" | ||
name = "test-installation-grafana-agent-secret" | ||
} | ||
loki.write "default" { | ||
endpoint { | ||
url = nonsensitive(remote.kubernetes.secret.credentials.data["logging-url"]) | ||
tenant_id = nonsensitive(remote.kubernetes.secret.credentials.data["logging-tenant-id"]) | ||
basic_auth { | ||
username = nonsensitive(remote.kubernetes.secret.credentials.data["logging-username"]) | ||
password = remote.kubernetes.secret.credentials.data["logging-password"] | ||
} | ||
tls_config { | ||
insecure_skip_verify = false | ||
} | ||
} | ||
external_labels = { | ||
installation = "test-installation", | ||
cluster_id = "test-installation", | ||
scrape_job = "kubernetes-events", | ||
} | ||
} | ||
extraArgs: | ||
- --disable-reporting | ||
controller: | ||
replicas: 1 | ||
type: deployment | ||
crds: | ||
create: false |
Oops, something went wrong.