-
Notifications
You must be signed in to change notification settings - Fork 331
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Bence Csati <[email protected]>
- Loading branch information
Showing
3 changed files
with
260 additions
and
1 deletion.
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
253 changes: 253 additions & 0 deletions
253
e2e/metrics-servicemonitors/metrics_servicemonitors_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,253 @@ | ||
// Copyright © 2021 Cisco Systems, Inc. and/or its affiliates | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package metrics_servicemonitors_test | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
v1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1" | ||
appsv1 "k8s.io/api/apps/v1" | ||
batchv1 "k8s.io/api/batch/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
rbacv1 "k8s.io/api/rbac/v1" | ||
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/cluster" | ||
|
||
"github.com/kube-logging/logging-operator/pkg/sdk/logging/api/v1beta1" | ||
|
||
"github.com/kube-logging/logging-operator/e2e/common" | ||
"github.com/kube-logging/logging-operator/e2e/common/setup" | ||
"sigs.k8s.io/e2e-framework/third_party/helm" | ||
) | ||
|
||
var serviceMonitorNames = map[string]bool{ | ||
"metrics-test-syslog-ng-metrics": true, | ||
"metrics-test-syslog-ng-buffer-metrics": true, | ||
"metrics-test-fluentd-metrics": true, | ||
"metrics-test-fluentbit-metrics": true, | ||
} | ||
|
||
var TestTempDir string | ||
|
||
func init() { | ||
var ok bool | ||
TestTempDir, ok = os.LookupEnv("PROJECT_DIR") | ||
if !ok { | ||
TestTempDir = "../.." | ||
} | ||
TestTempDir = filepath.Join(TestTempDir, "build/_test") | ||
err := os.MkdirAll(TestTempDir, os.FileMode(0755)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func TestMetrics_ServiceMonitors(t *testing.T) { | ||
common.Initialize(t) | ||
ns := "test" | ||
releaseNameOverride := "e2e" | ||
common.WithCluster("syslog-ng-metrics", t, func(t *testing.T, c common.Cluster) { | ||
setup.LoggingOperator(t, c, setup.LoggingOperatorOptionFunc(func(options *setup.LoggingOperatorOptions) { | ||
options.Namespace = ns | ||
options.NameOverride = releaseNameOverride | ||
})) | ||
|
||
ctx := context.Background() | ||
|
||
err := installPrometheusOperator(c) | ||
common.RequireNoError(t, err) | ||
|
||
logging := v1beta1.Logging{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "metrics-test", | ||
Namespace: ns, | ||
}, | ||
Spec: v1beta1.LoggingSpec{ | ||
ControlNamespace: ns, | ||
FluentbitSpec: &v1beta1.FluentbitSpec{ | ||
Metrics: &v1beta1.Metrics{ | ||
ServiceMonitor: true, | ||
}, | ||
}, | ||
SyslogNGSpec: &v1beta1.SyslogNGSpec{ | ||
Metrics: &v1beta1.Metrics{ | ||
ServiceMonitor: true, | ||
}, | ||
BufferVolumeMetrics: &v1beta1.BufferMetrics{ | ||
Metrics: v1beta1.Metrics{ | ||
ServiceMonitor: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
common.RequireNoError(t, c.GetClient().Create(ctx, &logging)) | ||
|
||
common.RequireNoError(t, checkServiceMonitorAvailability(t, ctx, c, ns)) | ||
|
||
defer startPortForwardingToService(t, "metrics-test-fluentbit-metrics", ns, "2020:2020") | ||
rawOut, err := getRawOutputFromService(c, "2020", "/api/v1/metrics/prometheus") | ||
common.RequireNoError(t, err) | ||
|
||
fmt.Print(rawOut) | ||
|
||
defer startPortForwardingToService(t, "metrics-test-syslog-ng-metrics", ns, "9577:9577") | ||
rawOut, err = getRawOutputFromService(c, "9577", "/metrics") | ||
common.RequireNoError(t, err) | ||
|
||
fmt.Print(rawOut) | ||
|
||
common.RequireNoError(t, c.GetClient().Delete(ctx, &logging)) | ||
|
||
loggingPatch := v1beta1.Logging{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "metrics-test", | ||
Namespace: ns, | ||
}, | ||
Spec: v1beta1.LoggingSpec{ | ||
ControlNamespace: ns, | ||
FluentbitSpec: &v1beta1.FluentbitSpec{ | ||
Metrics: &v1beta1.Metrics{ | ||
ServiceMonitor: true, | ||
}, | ||
}, | ||
FluentdSpec: &v1beta1.FluentdSpec{ | ||
Metrics: &v1beta1.Metrics{ | ||
ServiceMonitor: true, | ||
}, | ||
}, | ||
}, | ||
} | ||
common.RequireNoError(t, c.GetClient().Create(ctx, &loggingPatch)) | ||
|
||
defer startPortForwardingToService(t, "metrics-test-fluentd-metrics", ns, "24231:24231") | ||
rawOut, err = getRawOutputFromService(c, "24231", "/metrics") | ||
common.RequireNoError(t, err) | ||
|
||
fmt.Print(rawOut) | ||
|
||
}, func(t *testing.T, c common.Cluster) error { | ||
path := filepath.Join(TestTempDir, fmt.Sprintf("cluster-%s.log", t.Name())) | ||
t.Logf("Printing cluster logs to %s", path) | ||
err := c.PrintLogs(common.PrintLogConfig{ | ||
Namespaces: []string{ns, "default"}, | ||
FilePath: path, | ||
Limit: 100 * 1000, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
loggingOperatorName := "logging-operator-" + releaseNameOverride | ||
t.Logf("Collecting coverage files from logging-operator: %s/%s", ns, loggingOperatorName) | ||
err = c.CollectTestCoverageFiles(ns, loggingOperatorName) | ||
if err != nil { | ||
t.Logf("Failed collecting coverage files: %s", err) | ||
} | ||
return err | ||
|
||
}, func(o *cluster.Options) { | ||
if o.Scheme == nil { | ||
o.Scheme = runtime.NewScheme() | ||
} | ||
common.RequireNoError(t, v1beta1.AddToScheme(o.Scheme)) | ||
common.RequireNoError(t, apiextensionsv1.AddToScheme(o.Scheme)) | ||
common.RequireNoError(t, appsv1.AddToScheme(o.Scheme)) | ||
common.RequireNoError(t, batchv1.AddToScheme(o.Scheme)) | ||
common.RequireNoError(t, corev1.AddToScheme(o.Scheme)) | ||
common.RequireNoError(t, rbacv1.AddToScheme(o.Scheme)) | ||
}) | ||
} | ||
|
||
func installPrometheusOperator(c common.Cluster) error { | ||
manager := helm.New(c.KubeConfigFilePath()) | ||
|
||
err := manager.RunRepo(helm.WithArgs("add", "prometheus-community", "https://prometheus-community.github.io/helm-charts")) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = manager.RunInstall( | ||
helm.WithName("prometheus"), | ||
helm.WithChart("prometheus-community/kube-prometheus-stack"), | ||
helm.WithArgs("--create-namespace"), | ||
helm.WithNamespace("monitoring"), | ||
helm.WithArgs("--set", "prometheus.prometheusSpec.serviceMonitorSelectorNilUsesHelmValues=false"), | ||
helm.WithArgs("--set", "prometheus.prometheusSpec.podMonitorSelectorNilUsesHelmValues=false"), | ||
helm.WithWait(), | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func checkServiceMonitorAvailability(t *testing.T, ctx context.Context, c common.Cluster, ns string) error { | ||
serviceMonitors := &v1.ServiceMonitorList{} | ||
common.RequireNoError(t, c.GetClient().List(ctx, serviceMonitors)) | ||
|
||
for _, sm := range serviceMonitors.Items { | ||
if sm.Namespace == ns { | ||
delete(serviceMonitorNames, sm.Name) | ||
} | ||
} | ||
|
||
var errorMsg strings.Builder | ||
for name := range serviceMonitorNames { | ||
errorMsg.WriteString(fmt.Sprintf("ServiceMonitor %s not found\n", name)) | ||
} | ||
|
||
if errorMsg.Len() > 0 { | ||
return errors.New(errorMsg.String()) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func startPortForwardingToService(t *testing.T, svcName, ns, portMapping string) func() { | ||
args := []string{"port-forward", fmt.Sprintf("svc/%s", svcName), portMapping, "-n", ns} | ||
cmd := exec.Command("kubectl", args...) | ||
cmd.Stderr = os.Stderr // Redirect stderr to test output | ||
err := cmd.Start() | ||
common.RequireNoError(t, err) | ||
|
||
// Wait for port forwarding to be established | ||
time.Sleep(5 * time.Second) | ||
|
||
return func() { | ||
_ = cmd.Process.Kill() | ||
} | ||
} | ||
|
||
func getRawOutputFromService(c common.Cluster, port, path string) ([]byte, error) { | ||
cmd := common.CmdEnv(exec.Command("curl", fmt.Sprintf("localhost:%s%s", port, path)), c) | ||
rawOut, err := cmd.Output() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return rawOut, nil | ||
} |