@@ -4,6 +4,11 @@ import (
44 "context"
55 _ "embed"
66 "fmt"
7+ "path/filepath"
8+ "regexp"
9+ "strings"
10+ "time"
11+
712 log "github.com/ViaQ/logerr/v2/log/static"
813 . "github.com/onsi/ginkgo/v2"
914 . "github.com/onsi/gomega"
@@ -13,9 +18,6 @@ import (
1318 framework "github.com/openshift/cluster-logging-operator/test/framework/e2e"
1419 "github.com/openshift/cluster-logging-operator/test/helpers/oc"
1520 "k8s.io/apimachinery/pkg/util/wait"
16- "regexp"
17- "strings"
18- "time"
1921)
2022
2123//go:embed valid.yaml
@@ -47,24 +49,54 @@ var _ = Describe("[e2e][logfilemetricexporter] LogFileMetricsExporter", func() {
4749 Expect (err .Error ()).To (MatchRegexp ("is invalid.*supported values.*instance" ), "exp. the CR to be rejected because it is not THE singleton" )
4850 })
4951
50- It ("should be deployed by the operator and producing metrics " , func () {
52+ It ("should serve metrics to authorized clients providing a valid bearer token " , func () {
5153 e2e .AddCleanup (func () error {
5254 return oc .Literal ().From ("oc -n openshift-logging delete --ignore-not-found logfilemetricexporter instance" ).Output ()
5355 })
56+ metricsReaderRoleName := fmt .Sprintf ("%s-metrics-reader" , constants .ClusterLoggingOperator )
57+ metricsReaderBindingName := fmt .Sprintf ("%s-metrics-reader" , constants .LogfilesmetricexporterName )
58+ metricsAuthRoleName := fmt .Sprintf ("%s-metrics-auth" , constants .LogfilesmetricexporterName )
59+ e2e .AddCleanup (func () error {
60+ return oc .Literal ().From ("oc delete --ignore-not-found clusterrole %s" , metricsReaderRoleName ).Output ()
61+ })
62+ e2e .AddCleanup (func () error {
63+ return oc .Literal ().From ("oc delete --ignore-not-found clusterrolebinding %s" , metricsReaderBindingName ).Output ()
64+ })
65+ // Delete the metrics auth ClusterRoleBinding
66+ // The LFME reconciles the ClusterRoleBinding and ClusterRole for metrics auth
67+ e2e .AddCleanup (func () error {
68+ return oc .Literal ().From ("oc delete --ignore-not-found clusterrolebinding %s" , metricsAuthRoleName ).Output ()
69+ })
70+
5471 err = createLFME (validCR )
5572 Expect (err ).ToNot (HaveOccurred ())
5673 Expect (e2e .WaitForDaemonSet (constants .OpenshiftNS , constants .LogfilesmetricexporterName )).To (Succeed ())
5774
58- args := []string {"-k" , "-s" , fmt .Sprintf ("https://%s.%s.svc:2112/metrics" , constants .LogfilesmetricexporterName , constants .OpenshiftNS )}
75+ By ("creating the metrics-reader ClusterRole" )
76+ roleFilePath , err := filepath .Abs (filepath .Join (".." , ".." , ".." , "config" , "rbac" , "metrics_reader_role.yaml" ))
77+ Expect (err ).ToNot (HaveOccurred (), "Failed to construct role file path" )
78+ _ , err = oc .Literal ().From ("oc apply -f %s" , roleFilePath ).Run ()
79+ Expect (err ).ToNot (HaveOccurred (), "Failed to create metrics-reader ClusterRole" )
80+
81+ By ("creating a ClusterRoleBinding for the service account to allow access to metrics" )
82+ _ , err = oc .Literal ().From ("oc create clusterrolebinding %s --clusterrole=%s --serviceaccount=%s:%s" ,
83+ metricsReaderBindingName , metricsReaderRoleName , constants .OpenshiftNS , constants .LogfilesmetricexporterName ).Run ()
84+ Expect (err ).ToNot (HaveOccurred (), "Failed to create ClusterRoleBinding" )
85+
86+ metricsURL := fmt .Sprintf ("https://%s.%s.svc:2112/metrics" , constants .LogfilesmetricexporterName , constants .OpenshiftNS )
87+ curlCmd := fmt .Sprintf (`curl -s -k -H "Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)" %s` , metricsURL )
88+
5989 err = wait .PollUntilContextTimeout (context .TODO (), time .Second , time .Second * 30 , true , func (context.Context ) (done bool , err error ) {
60- out , err := oc .Exec ().WithNamespace (constants .OpenshiftNS ).Pod (fmt .Sprintf ("ds/%s" , constants .LogfilesmetricexporterName )).WithCmd ("curl" , args ... ).Run ()
90+ out , err := oc .Exec ().WithNamespace (constants .OpenshiftNS ).
91+ Pod (fmt .Sprintf ("ds/%s" , constants .LogfilesmetricexporterName )).
92+ WithCmd ("sh" , "-c" , curlCmd ).Run ()
6193 Expect (err ).ToNot (HaveOccurred (), out )
62- log .V (5 ).Info ("Polling metrics" , "result" , out )
94+ log .V (5 ).Info ("Polling secure metrics" , "result" , out )
6395 if ! strings .Contains (out , "log_logged_bytes_total" ) {
6496 return false , nil
6597 }
6698 return regexp .MatchString (`log_logged_bytes_total{.*} [1-9][0-9]*` , out )
6799 })
68- Expect (err ).ToNot (HaveOccurred (), "Exp. to find log_logged_bytes_total being calculated " )
100+ Expect (err ).ToNot (HaveOccurred (), "Exp. to scrape metrics with a bearer token " )
69101 })
70102})
0 commit comments