diff --git a/tests/general/discoverymode/k8s_observer_discovery_test.go b/tests/general/discoverymode/k8s_observer_discovery_test.go index 5f7af5613b..fc3b59c58b 100644 --- a/tests/general/discoverymode/k8s_observer_discovery_test.go +++ b/tests/general/discoverymode/k8s_observer_discovery_test.go @@ -27,7 +27,6 @@ import ( "time" "github.com/stretchr/testify/require" - "go.uber.org/zap" "gopkg.in/yaml.v2" corev1 "k8s.io/api/core/v1" rbacv1 "k8s.io/api/rbac/v1" @@ -49,33 +48,29 @@ func TestK8sObserver(t *testing.T) { cluster.Create() cluster.LoadLocalCollectorImageIfNecessary() - namespace, serviceAccount := createNamespaceAndServiceAccount(cluster) - - configMap, configMapManifest := configToConfigMapManifest(t, "k8s-otlp-exporter-no-internal-prometheus.yaml", namespace) - sout, serr, err := cluster.Apply(configMapManifest) - tc.Logger.Debug("applying ConfigMap", zap.String("stdout", sout.String()), zap.String("stderr", serr.String())) - require.NoError(t, err) - - redisName, redisUID := createRedis(cluster, "target.redis", namespace, serviceAccount) + namespace := manifests.Namespace{Name: "test-namespace"} + serviceAccount := manifests.ServiceAccount{Name: "some.serviceaccount", Namespace: "test-namespace"} + configMap := manifests.ConfigMap{ + Name: "collector.config", Namespace: namespace.Name, + Data: configMapData(t, "k8s-otlp-exporter-no-internal-prometheus.yaml"), + } + sout, serr, err := cluster.Apply(manifests.RenderAll(t, namespace, serviceAccount, configMap)) + require.NoError(t, err, "stdout: %s, stderr: %s", sout, serr) - crManifest, crbManifest := clusterRoleAndBindingManifests(t, namespace, serviceAccount) - sout, serr, err = cluster.Apply(crManifest) - tc.Logger.Debug("applying ClusterRole", zap.String("stdout", sout.String()), zap.String("stderr", serr.String())) - require.NoError(t, err) + redisName, redisUID := createRedis(cluster, "target.redis", namespace.Name, serviceAccount.Name) - sout, serr, err = cluster.Apply(crbManifest) - tc.Logger.Debug("applying ClusterRoleBinding", zap.String("stdout", sout.String()), zap.String("stderr", serr.String())) - require.NoError(t, err) + clusterRole, clusterRoleBinding := clusterRoleAndBinding(namespace.Name, serviceAccount.Name) + sout, serr, err = cluster.Apply(manifests.RenderAll(t, clusterRole, clusterRoleBinding)) + require.NoError(t, err, "stdout: %s, stderr: %s", sout, serr) - daemonSet, dsManifest := daemonSetManifest(cluster, namespace, serviceAccount, configMap) - sout, serr, err = cluster.Apply(dsManifest) - tc.Logger.Debug("applying DaemonSet", zap.String("stdout", sout.String()), zap.String("stderr", serr.String())) - require.NoError(t, err) + ds := daemonSet(cluster, namespace.Name, serviceAccount.Name, configMap.Name) + sout, serr, err = cluster.Apply(ds.Render(t)) + require.NoError(t, err, "stdout: %s, stderr: %s", sout, serr) require.Eventually(t, func() bool { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() - rPod, err := cluster.Clientset.CoreV1().Pods(namespace).Get(ctx, redisName, metav1.GetOptions{}) + rPod, err := cluster.Clientset.CoreV1().Pods(namespace.Name).Get(ctx, redisName, metav1.GetOptions{}) require.NoError(t, err) tc.Logger.Debug(fmt.Sprintf("redis is: %s\n", rPod.Status.Phase)) return rPod.Status.Phase == corev1.PodRunning @@ -85,8 +80,8 @@ func TestK8sObserver(t *testing.T) { require.Eventually(t, func() bool { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() - dsPods, err := cluster.Clientset.CoreV1().Pods(namespace).List(ctx, metav1.ListOptions{ - LabelSelector: fmt.Sprintf("name = %s", daemonSet), + dsPods, err := cluster.Clientset.CoreV1().Pods(namespace.Name).List(ctx, metav1.ListOptions{ + LabelSelector: fmt.Sprintf("name = %s", ds.Name), }) require.NoError(t, err) if len(dsPods.Items) > 0 { @@ -104,7 +99,7 @@ func TestK8sObserver(t *testing.T) { require.NoError(t, tc.OTLPReceiverSink.AssertAllMetricsReceived(t, *expectedMetrics, 30*time.Second)) stdout, stderr, err := cluster.Kubectl( - "exec", "-n", namespace, collectorPodName, "--", "bash", "-c", + "exec", "-n", namespace.Name, collectorPodName, "--", "bash", "-c", `SPLUNK_DEBUG_CONFIG_SERVER=false \ SPLUNK_DISCOVERY_EXTENSIONS_host_observer_ENABLED=false \ SPLUNK_DISCOVERY_EXTENSIONS_docker_observer_ENABLED=false \ @@ -198,28 +193,7 @@ func createRedis(cluster *kubeutils.KindCluster, name, namespace, serviceAccount return redis.Name, string(redis.UID) } -func createNamespaceAndServiceAccount(cluster *kubeutils.KindCluster) (string, string) { - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() - ns, err := cluster.Clientset.CoreV1().Namespaces().Create( - ctx, &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "test-namespace"}}, - metav1.CreateOptions{}, - ) - require.NoError(cluster.Testcase, err) - namespace := ns.Name - - ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() - serviceAccount, err := cluster.Clientset.CoreV1().ServiceAccounts(namespace).Create( - ctx, &corev1.ServiceAccount{ - ObjectMeta: metav1.ObjectMeta{Name: "some.serviceaccount"}, - }, - metav1.CreateOptions{}) - require.NoError(cluster.Testcase, err) - return namespace, serviceAccount.Name -} - -func configToConfigMapManifest(t testing.TB, cfg, namespace string) (name, manifest string) { +func configMapData(t testing.TB, cfg string) string { config, err := os.ReadFile(filepath.Join(".", "testdata", cfg)) require.NoError(t, err) configStore := map[string]any{"config": string(config)} @@ -232,15 +206,10 @@ func configToConfigMapManifest(t testing.TB, cfg, namespace string) (name, manif configYaml, err := yaml.Marshal(configStore) require.NoError(t, err) - - cm := manifests.ConfigMap{ - Name: "collector.config", Namespace: namespace, - Data: string(configYaml), - } - return cm.Name, cm.Render(t) + return string(configYaml) } -func clusterRoleAndBindingManifests(t testing.TB, namespace, serviceAccount string) (string, string) { +func clusterRoleAndBinding(namespace, serviceAccount string) (manifests.ClusterRole, manifests.ClusterRoleBinding) { cr := manifests.ClusterRole{ Name: "cluster-role", Namespace: namespace, @@ -314,10 +283,10 @@ func clusterRoleAndBindingManifests(t testing.TB, namespace, serviceAccount stri ServiceAccountName: serviceAccount, } - return cr.Render(t), crb.Render(t) + return cr, crb } -func daemonSetManifest(cluster *kubeutils.KindCluster, namespace, serviceAccount, configMap string) (name, manifest string) { +func daemonSet(cluster *kubeutils.KindCluster, namespace, serviceAccount, configMap string) manifests.DaemonSet { splat := strings.Split(cluster.Testcase.OTLPEndpoint, ":") port := splat[len(splat)-1] var hostFromContainer string @@ -398,5 +367,5 @@ func daemonSetManifest(cluster *kubeutils.KindCluster, namespace, serviceAccount }, }, } - return ds.Name, ds.Render(cluster.Testcase) + return ds } diff --git a/tests/receivers/discovery/k8s_observer_test.go b/tests/receivers/discovery/k8s_observer_test.go index 0d9862d4ab..120c6943dd 100644 --- a/tests/receivers/discovery/k8s_observer_test.go +++ b/tests/receivers/discovery/k8s_observer_test.go @@ -27,7 +27,6 @@ import ( "time" "github.com/stretchr/testify/require" - "go.uber.org/zap" "gopkg.in/yaml.v2" corev1 "k8s.io/api/core/v1" rbacv1 "k8s.io/api/rbac/v1" @@ -49,11 +48,19 @@ func TestDiscoveryReceiverWithK8sObserverProvidesEndpointLogs(t *testing.T) { cluster.Create() cluster.LoadLocalCollectorImageIfNecessary() - namespace, serviceAccount := createNamespaceAndServiceAccount(cluster) + namespace := manifests.Namespace{Name: "test-namespace"} + serviceAccount := manifests.ServiceAccount{Name: "some.serviceacount", Namespace: namespace.Name} + configMap := manifests.ConfigMap{ + Name: "collector.config", Namespace: namespace.Name, + Data: configMapData(t, "k8s_observer_endpoints_config.yaml"), + } + + sout, serr, err := cluster.Apply(manifests.RenderAll(t, namespace, serviceAccount, configMap)) + require.NoError(t, err, "stdout: %s, stderr: %s", sout, serr) ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute) defer cancel() - _, err := cluster.Clientset.CoreV1().Nodes().Create(ctx, &corev1.Node{ + _, err = cluster.Clientset.CoreV1().Nodes().Create(ctx, &corev1.Node{ ObjectMeta: metav1.ObjectMeta{ Annotations: map[string]string{ "some.annotation": "annotation.value", @@ -70,32 +77,19 @@ func TestDiscoveryReceiverWithK8sObserverProvidesEndpointLogs(t *testing.T) { }, metav1.CreateOptions{}) require.NoError(t, err) - _ = createRedis(cluster, "some.pod", namespace, serviceAccount) + _ = createRedis(cluster, "some.pod", namespace.Name, serviceAccount.Name) - configMap, configMapManifest := configToConfigMapManifest(t, "k8s_observer_endpoints_config.yaml", namespace) - sout, serr, err := cluster.Apply(configMapManifest) - tc.Logger.Debug("applying ConfigMap", zap.String("stdout", sout.String()), zap.String("stderr", serr.String())) - require.NoError(t, err) + clusterRole, clusterRoleBinding := clusterRoleAndBinding(namespace.Name, serviceAccount.Name) + ds := daemonSet(cluster, namespace.Name, serviceAccount.Name, configMap.Name) - crManifest, crbManifest := clusterRoleAndBindingManifests(t, namespace, serviceAccount) - sout, serr, err = cluster.Apply(crManifest) - tc.Logger.Debug("applying ClusterRole", zap.String("stdout", sout.String()), zap.String("stderr", serr.String())) - require.NoError(t, err) - - sout, serr, err = cluster.Apply(crbManifest) - tc.Logger.Debug("applying ClusterRoleBinding", zap.String("stdout", sout.String()), zap.String("stderr", serr.String())) - require.NoError(t, err) - - daemonSet, dsManifest := daemonSetManifest(cluster, namespace, serviceAccount, configMap) - sout, serr, err = cluster.Apply(dsManifest) - tc.Logger.Debug("applying DaemonSet", zap.String("stdout", sout.String()), zap.String("stderr", serr.String())) - require.NoError(t, err) + sout, serr, err = cluster.Apply(manifests.RenderAll(t, clusterRole, clusterRoleBinding, ds)) + require.NoError(t, err, "stdout: %s, stderr: %s", sout, serr) require.Eventually(t, func() bool { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() - dsPods, err := cluster.Clientset.CoreV1().Pods(namespace).List(ctx, metav1.ListOptions{ - LabelSelector: fmt.Sprintf("name = %s", daemonSet), + dsPods, err := cluster.Clientset.CoreV1().Pods(namespace.Name).List(ctx, metav1.ListOptions{ + LabelSelector: fmt.Sprintf("name = %s", ds.Name), }) require.NoError(t, err) if len(dsPods.Items) > 0 { @@ -123,33 +117,28 @@ func TestDiscoveryReceiverWithK8sObserverAndSmartAgentRedisReceiverProvideStatus cluster.Create() cluster.LoadLocalCollectorImageIfNecessary() - namespace, serviceAccount := createNamespaceAndServiceAccount(cluster) + namespace := manifests.Namespace{Name: "test-namespace"} + serviceAccount := manifests.ServiceAccount{Name: "some.serviceacount", Namespace: namespace.Name} + configMap := manifests.ConfigMap{ + Name: "collector.config", Namespace: namespace.Name, + Data: configMapData(t, "k8s_observer_smart_agent_redis_config.yaml"), + } - configMap, configMapManifest := configToConfigMapManifest(t, "k8s_observer_smart_agent_redis_config.yaml", namespace) - sout, serr, err := cluster.Apply(configMapManifest) - tc.Logger.Debug("applying ConfigMap", zap.String("stdout", sout.String()), zap.String("stderr", serr.String())) - require.NoError(t, err) + sout, serr, err := cluster.Apply(manifests.RenderAll(t, namespace, serviceAccount, configMap)) + require.NoError(t, err, "stdout: %s, stderr: %s", sout, serr) - redis := createRedis(cluster, "target.redis", namespace, serviceAccount) + redis := createRedis(cluster, "target.redis", namespace.Name, serviceAccount.Name) - crManifest, crbManifest := clusterRoleAndBindingManifests(t, namespace, serviceAccount) - sout, serr, err = cluster.Apply(crManifest) - tc.Logger.Debug("applying ClusterRole", zap.String("stdout", sout.String()), zap.String("stderr", serr.String())) - require.NoError(t, err) + clusterRole, clusterRoleBinding := clusterRoleAndBinding(namespace.Name, serviceAccount.Name) + ds := daemonSet(cluster, namespace.Name, serviceAccount.Name, configMap.Name) - sout, serr, err = cluster.Apply(crbManifest) - tc.Logger.Debug("applying ClusterRoleBinding", zap.String("stdout", sout.String()), zap.String("stderr", serr.String())) - require.NoError(t, err) - - daemonSet, dsManifest := daemonSetManifest(cluster, namespace, serviceAccount, configMap) - sout, serr, err = cluster.Apply(dsManifest) - tc.Logger.Debug("applying DaemonSet", zap.String("stdout", sout.String()), zap.String("stderr", serr.String())) - require.NoError(t, err) + sout, serr, err = cluster.Apply(manifests.RenderAll(t, clusterRole, clusterRoleBinding, ds)) + require.NoError(t, err, "stdout: %s, stderr: %s", sout, serr) require.Eventually(t, func() bool { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() - rPod, err := cluster.Clientset.CoreV1().Pods(namespace).Get(ctx, redis, metav1.GetOptions{}) + rPod, err := cluster.Clientset.CoreV1().Pods(namespace.Name).Get(ctx, redis, metav1.GetOptions{}) require.NoError(t, err) tc.Logger.Debug(fmt.Sprintf("redis is: %s\n", rPod.Status.Phase)) return rPod.Status.Phase == corev1.PodRunning @@ -158,8 +147,8 @@ func TestDiscoveryReceiverWithK8sObserverAndSmartAgentRedisReceiverProvideStatus require.Eventually(t, func() bool { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() - dsPods, err := cluster.Clientset.CoreV1().Pods(namespace).List(ctx, metav1.ListOptions{ - LabelSelector: fmt.Sprintf("name = %s", daemonSet), + dsPods, err := cluster.Clientset.CoreV1().Pods(namespace.Name).List(ctx, metav1.ListOptions{ + LabelSelector: fmt.Sprintf("name = %s", ds.Name), }) require.NoError(t, err) if len(dsPods.Items) > 0 { @@ -208,40 +197,15 @@ func createRedis(cluster *kubeutils.KindCluster, name, namespace, serviceAccount return redis.Name } -func createNamespaceAndServiceAccount(cluster *kubeutils.KindCluster) (string, string) { - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() - ns, err := cluster.Clientset.CoreV1().Namespaces().Create( - ctx, &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "test-namespace"}}, - metav1.CreateOptions{}, - ) - require.NoError(cluster.Testcase, err) - namespace := ns.Name - - ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() - serviceAccount, err := cluster.Clientset.CoreV1().ServiceAccounts(namespace).Create( - ctx, &corev1.ServiceAccount{ - ObjectMeta: metav1.ObjectMeta{Name: "some.serviceaccount"}, - }, - metav1.CreateOptions{}) - require.NoError(cluster.Testcase, err) - return namespace, serviceAccount.Name -} - -func configToConfigMapManifest(t testing.TB, configPath, namespace string) (name, manifest string) { +func configMapData(t testing.TB, configPath string) string { config, err := os.ReadFile(filepath.Join(".", "testdata", configPath)) configStore := map[string]any{"config": string(config)} configYaml, err := yaml.Marshal(configStore) require.NoError(t, err) - cm := manifests.ConfigMap{ - Name: "collector.config", Namespace: namespace, - Data: string(configYaml), - } - return cm.Name, cm.Render(t) + return string(configYaml) } -func clusterRoleAndBindingManifests(t testing.TB, namespace, serviceAccount string) (string, string) { +func clusterRoleAndBinding(namespace, serviceAccount string) (manifests.ClusterRole, manifests.ClusterRoleBinding) { cr := manifests.ClusterRole{ Name: "cluster-role", Namespace: namespace, @@ -314,10 +278,10 @@ func clusterRoleAndBindingManifests(t testing.TB, namespace, serviceAccount stri ServiceAccountName: serviceAccount, } - return cr.Render(t), crb.Render(t) + return cr, crb } -func daemonSetManifest(cluster *kubeutils.KindCluster, namespace, serviceAccount, configMap string) (name, manifest string) { +func daemonSet(cluster *kubeutils.KindCluster, namespace, serviceAccount, configMap string) manifests.DaemonSet { splat := strings.Split(cluster.Testcase.OTLPEndpoint, ":") port := splat[len(splat)-1] var hostFromContainer string @@ -368,5 +332,5 @@ func daemonSetManifest(cluster *kubeutils.KindCluster, namespace, serviceAccount }, }, } - return ds.Name, ds.Render(cluster.Testcase) + return ds } diff --git a/tests/receivers/smartagent/collectd-mysql/bundled_test.go b/tests/receivers/smartagent/collectd-mysql/bundled_test.go index dc97c66271..d3a70c84b1 100644 --- a/tests/receivers/smartagent/collectd-mysql/bundled_test.go +++ b/tests/receivers/smartagent/collectd-mysql/bundled_test.go @@ -26,8 +26,6 @@ import ( "time" "github.com/stretchr/testify/require" - "go.uber.org/zap" - "gopkg.in/yaml.v3" corev1 "k8s.io/api/core/v1" rbacv1 "k8s.io/api/rbac/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -48,19 +46,40 @@ func TestK8sObserver(t *testing.T) { cluster.Create() cluster.LoadLocalCollectorImageIfNecessary() - namespace, serviceAccount := cluster.createNamespaceAndServiceAccount() - mysqlUID := cluster.createMySQL("target.mysql", namespace, serviceAccount) - cluster.createClusterRoleAndRoleBinding(namespace, serviceAccount) - configMap := cluster.createConfigMap(namespace) - daemonSet := cluster.daemonSetManifest(namespace, serviceAccount, configMap) + namespace := manifests.Namespace{Name: "test-namespace"} + serviceAccount := manifests.ServiceAccount{Name: "some.serviceacount", Namespace: namespace.Name} + configMap := manifests.ConfigMap{ + Name: "collector.config", Namespace: namespace.Name, + Data: `config: | + exporters: + otlp: + endpoint: ${OTLP_ENDPOINT} + tls: + insecure: true + service: + pipelines: + metrics: + exporters: + - otlp +`} + + sout, serr, err := cluster.Apply(manifests.RenderAll(t, namespace, serviceAccount, configMap)) + require.NoError(t, err, "stdout: %s, stderr: %s", sout, serr) + + mysqlUID := cluster.createMySQL("target.mysql", namespace.Name, serviceAccount.Name) + + clusterRole, clusterRoleBinding := clusterRoleAndBinding(namespace.Name, serviceAccount.Name) + ds := cluster.daemonSet(namespace.Name, serviceAccount.Name, configMap.Name) + sout, serr, err = cluster.Apply(manifests.RenderAll(t, clusterRole, clusterRoleBinding, ds)) + require.NoError(t, err, "stdout: %s, stderr: %s", sout, serr) var collectorName string // wait for collector to run require.Eventually(t, func() bool { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() - dsPods, err := cluster.Clientset.CoreV1().Pods(namespace).List(ctx, metav1.ListOptions{ - LabelSelector: fmt.Sprintf("name = %s", daemonSet), + dsPods, err := cluster.Clientset.CoreV1().Pods(namespace.Name).List(ctx, metav1.ListOptions{ + LabelSelector: fmt.Sprintf("name = %s", ds.Name), }) require.NoError(t, err) if len(dsPods.Items) > 0 { @@ -77,7 +96,7 @@ func TestK8sObserver(t *testing.T) { expectedMetrics := tc.ResourceMetrics("all.yaml") require.NoError(t, tc.OTLPReceiverSink.AssertAllMetricsReceived(t, *expectedMetrics, 30*time.Second)) - stdOut, stdErr, err := cluster.Kubectl("logs", "-n", namespace, collectorName) + stdOut, stdErr, err := cluster.Kubectl("logs", "-n", namespace.Name, collectorName) require.NoError(t, err, stdErr.String()) require.Contains( t, stdOut.String(), @@ -179,57 +198,7 @@ func (cluster testCluster) createMySQL(name, namespace, serviceAccount string) s return string(mysql.UID) } -func (cluster testCluster) createNamespaceAndServiceAccount() (string, string) { - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() - ns, err := cluster.Clientset.CoreV1().Namespaces().Create( - ctx, &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "test-namespace"}}, - metav1.CreateOptions{}, - ) - require.NoError(cluster.Testcase, err) - namespace := ns.Name - - ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() - serviceAccount, err := cluster.Clientset.CoreV1().ServiceAccounts(namespace).Create( - ctx, &corev1.ServiceAccount{ - ObjectMeta: metav1.ObjectMeta{Name: "some.serviceaccount"}, - }, - metav1.CreateOptions{}) - require.NoError(cluster.Testcase, err) - return namespace, serviceAccount.Name -} - -func (cluster testCluster) createConfigMap(namespace string) string { - config := map[string]any{ - "config": `exporters: - otlp: - endpoint: ${OTLP_ENDPOINT} - tls: - insecure: true -service: - pipelines: - metrics: - exporters: - - otlp -`, - } - - data, err := yaml.Marshal(config) - require.NoError(cluster.Testcase, err) - - cm := manifests.ConfigMap{ - Namespace: namespace, - Name: "collector.config", - Data: string(data), - } - sout, serr, err := cluster.Apply(cm.Render(cluster.Testcase)) - cluster.Testcase.Logger.Debug("applying ConfigMap", zap.String("stdout", sout.String()), zap.String("stderr", serr.String())) - require.NoError(cluster.Testcase, err) - return cm.Name -} - -func (cluster testCluster) createClusterRoleAndRoleBinding(namespace, serviceAccount string) { +func clusterRoleAndBinding(namespace, serviceAccount string) (manifests.ClusterRole, manifests.ClusterRoleBinding) { cr := manifests.ClusterRole{ Name: "cluster-role", Namespace: namespace, @@ -295,9 +264,6 @@ func (cluster testCluster) createClusterRoleAndRoleBinding(namespace, serviceAcc }, }, } - sout, serr, err := cluster.Apply(cr.Render(cluster.Testcase)) - cluster.Testcase.Logger.Debug("applying ClusterRole", zap.String("stdout", sout.String()), zap.String("stderr", serr.String())) - require.NoError(cluster.Testcase, err) crb := manifests.ClusterRoleBinding{ Namespace: namespace, @@ -305,12 +271,10 @@ func (cluster testCluster) createClusterRoleAndRoleBinding(namespace, serviceAcc ClusterRoleName: cr.Name, ServiceAccountName: serviceAccount, } - sout, serr, err = cluster.Apply(crb.Render(cluster.Testcase)) - cluster.Testcase.Logger.Debug("applying ClusterRoleBinding", zap.String("stdout", sout.String()), zap.String("stderr", serr.String())) - require.NoError(cluster.Testcase, err) + return cr, crb } -func (cluster testCluster) daemonSetManifest(namespace, serviceAccount, configMap string) string { +func (cluster testCluster) daemonSet(namespace, serviceAccount, configMap string) manifests.DaemonSet { splat := strings.Split(cluster.Testcase.OTLPEndpoint, ":") port := splat[len(splat)-1] var hostFromContainer string @@ -321,7 +285,7 @@ func (cluster testCluster) daemonSetManifest(namespace, serviceAccount, configMa } otlpEndpoint := fmt.Sprintf("%s:%s", hostFromContainer, port) - ds := manifests.DaemonSet{ + return manifests.DaemonSet{ Name: "an.agent.daemonset", Namespace: namespace, ServiceAccount: serviceAccount, @@ -373,8 +337,4 @@ func (cluster testCluster) daemonSetManifest(namespace, serviceAccount, configMa }, }, } - sout, serr, err := cluster.Apply(ds.Render(cluster.Testcase)) - cluster.Testcase.Logger.Debug("applying DaemonSet", zap.String("stdout", sout.String()), zap.String("stderr", serr.String())) - require.NoError(cluster.Testcase, err) - return ds.Name } diff --git a/tests/receivers/smartagent/postgresql/bundled_test.go b/tests/receivers/smartagent/postgresql/bundled_test.go index 00ede8be61..b6cb2e850e 100644 --- a/tests/receivers/smartagent/postgresql/bundled_test.go +++ b/tests/receivers/smartagent/postgresql/bundled_test.go @@ -49,19 +49,40 @@ func TestK8sObserver(t *testing.T) { cluster.Create() cluster.LoadLocalCollectorImageIfNecessary() - namespace, serviceAccount := cluster.createNamespaceAndServiceAccount() - postgresUID := cluster.createPostgres("target.postgres", namespace, serviceAccount) - cluster.createClusterRoleAndRoleBinding(namespace, serviceAccount) - configMap := cluster.createConfigMap(namespace) - daemonSet := cluster.daemonSetManifest(namespace, serviceAccount, configMap) + namespace := manifests.Namespace{Name: "test-namespace"} + serviceAccount := manifests.ServiceAccount{Name: "some.serviceacount", Namespace: namespace.Name} + clusterRole, clusterRoleBinding := clusterRoleAndBinding(namespace.Name, serviceAccount.Name) + sout, serr, err := cluster.Apply(manifests.RenderAll(t, namespace, serviceAccount, clusterRole, clusterRoleBinding)) + require.NoError(t, err, "stdout: %s, stderr: %s", sout, serr) + + postgresUID := cluster.createPostgres("target.postgres", namespace.Name, serviceAccount.Name) + + configMap := manifests.ConfigMap{ + Name: "collector.config", Namespace: namespace.Name, + Data: `config: | + exporters: + otlp: + endpoint: ${OTLP_ENDPOINT} + tls: + insecure: true + service: + pipelines: + metrics: + exporters: + - otlp +`} + + ds := cluster.daemonSet(namespace.Name, serviceAccount.Name, configMap.Name) + sout, serr, err = cluster.Apply(manifests.RenderAll(t, configMap, ds)) + require.NoError(t, err, "stdout: %s, stderr: %s", sout, serr) var collectorName string // wait for collector to run require.Eventually(t, func() bool { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() - dsPods, err := cluster.Clientset.CoreV1().Pods(namespace).List(ctx, metav1.ListOptions{ - LabelSelector: fmt.Sprintf("name = %s", daemonSet), + dsPods, err := cluster.Clientset.CoreV1().Pods(namespace.Name).List(ctx, metav1.ListOptions{ + LabelSelector: fmt.Sprintf("name = %s", ds.Name), }) require.NoError(t, err) if len(dsPods.Items) > 0 { @@ -78,7 +99,7 @@ func TestK8sObserver(t *testing.T) { expectedMetrics := tc.ResourceMetrics("all.yaml") require.NoError(t, tc.OTLPReceiverSink.AssertAllMetricsReceived(t, *expectedMetrics, 30*time.Second)) - stdOut, stdErr, err := cluster.Kubectl("logs", "-n", namespace, collectorName) + stdOut, stdErr, err := cluster.Kubectl("logs", "-n", namespace.Name, collectorName) require.NoError(t, err) require.Contains( t, stdOut.String(), @@ -237,57 +258,7 @@ func (cluster testCluster) createPostgres(name, namespace, serviceAccount string return string(postgres.UID) } -func (cluster testCluster) createNamespaceAndServiceAccount() (string, string) { - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() - ns, err := cluster.Clientset.CoreV1().Namespaces().Create( - ctx, &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "test-namespace"}}, - metav1.CreateOptions{}, - ) - require.NoError(cluster.Testcase, err) - namespace := ns.Name - - ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() - serviceAccount, err := cluster.Clientset.CoreV1().ServiceAccounts(namespace).Create( - ctx, &corev1.ServiceAccount{ - ObjectMeta: metav1.ObjectMeta{Name: "some.serviceaccount"}, - }, - metav1.CreateOptions{}) - require.NoError(cluster.Testcase, err) - return namespace, serviceAccount.Name -} - -func (cluster testCluster) createConfigMap(namespace string) string { - config := map[string]any{ - "config": `exporters: - otlp: - endpoint: ${OTLP_ENDPOINT} - tls: - insecure: true -service: - pipelines: - metrics: - exporters: - - otlp -`, - } - - data, err := yaml.Marshal(config) - require.NoError(cluster.Testcase, err) - - cm := manifests.ConfigMap{ - Namespace: namespace, - Name: "collector.config", - Data: string(data), - } - sout, serr, err := cluster.Apply(cm.Render(cluster.Testcase)) - cluster.Testcase.Logger.Debug("applying ConfigMap", zap.String("stdout", sout.String()), zap.String("stderr", serr.String())) - require.NoError(cluster.Testcase, err) - return cm.Name -} - -func (cluster testCluster) createClusterRoleAndRoleBinding(namespace, serviceAccount string) { +func clusterRoleAndBinding(namespace, serviceAccount string) (manifests.ClusterRole, manifests.ClusterRoleBinding) { cr := manifests.ClusterRole{ Name: "cluster-role", Namespace: namespace, @@ -353,9 +324,6 @@ func (cluster testCluster) createClusterRoleAndRoleBinding(namespace, serviceAcc }, }, } - sout, serr, err := cluster.Apply(cr.Render(cluster.Testcase)) - cluster.Testcase.Logger.Debug("applying ClusterRole", zap.String("stdout", sout.String()), zap.String("stderr", serr.String())) - require.NoError(cluster.Testcase, err) crb := manifests.ClusterRoleBinding{ Namespace: namespace, @@ -363,12 +331,10 @@ func (cluster testCluster) createClusterRoleAndRoleBinding(namespace, serviceAcc ClusterRoleName: cr.Name, ServiceAccountName: serviceAccount, } - sout, serr, err = cluster.Apply(crb.Render(cluster.Testcase)) - cluster.Testcase.Logger.Debug("applying ClusterRoleBinding", zap.String("stdout", sout.String()), zap.String("stderr", serr.String())) - require.NoError(cluster.Testcase, err) + return cr, crb } -func (cluster testCluster) daemonSetManifest(namespace, serviceAccount, configMap string) string { +func (cluster testCluster) daemonSet(namespace, serviceAccount, configMap string) manifests.DaemonSet { splat := strings.Split(cluster.Testcase.OTLPEndpoint, ":") port := splat[len(splat)-1] var hostFromContainer string @@ -379,7 +345,7 @@ func (cluster testCluster) daemonSetManifest(namespace, serviceAccount, configMa } otlpEndpoint := fmt.Sprintf("%s:%s", hostFromContainer, port) - ds := manifests.DaemonSet{ + return manifests.DaemonSet{ Name: "an.agent.daemonset", Namespace: namespace, ServiceAccount: serviceAccount, @@ -430,8 +396,4 @@ func (cluster testCluster) daemonSetManifest(namespace, serviceAccount, configMa }, }, } - sout, serr, err := cluster.Apply(ds.Render(cluster.Testcase)) - cluster.Testcase.Logger.Debug("applying DaemonSet", zap.String("stdout", sout.String()), zap.String("stderr", serr.String())) - require.NoError(cluster.Testcase, err) - return ds.Name } diff --git a/tests/testutils/kubeutils/manifests/service_account.go b/tests/testutils/kubeutils/manifests/service_account.go new file mode 100644 index 0000000000..232e5d2323 --- /dev/null +++ b/tests/testutils/kubeutils/manifests/service_account.go @@ -0,0 +1,43 @@ +// Copyright Splunk, Inc. +// +// 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 manifests + +import ( + "testing" +) + +// ServiceAccount is a "k8s.io/api/core/v1.ServiceAccount" convenience type +type ServiceAccount struct { + Name string + Namespace string +} + +const serviceAccountTemplate = `--- +apiVersion: v1 +kind: ServiceAccount +metadata: +{{- if .Name }} + name: {{ .Name }} +{{- end -}} +{{- if .Namespace }} + namespace: {{ .Namespace }} +{{- end }} +` + +var sam = Manifest[ServiceAccount](serviceAccountTemplate) + +func (sa ServiceAccount) Render(t testing.TB) string { + return sam.Render(sa, t) +} diff --git a/tests/testutils/kubeutils/manifests/service_account_test.go b/tests/testutils/kubeutils/manifests/service_account_test.go new file mode 100644 index 0000000000..02996a0062 --- /dev/null +++ b/tests/testutils/kubeutils/manifests/service_account_test.go @@ -0,0 +1,39 @@ +// Copyright Splunk, Inc. +// +// 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. + +//go:build testutils + +package manifests + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestServiceAccount(t *testing.T) { + sa := ServiceAccount{ + Name: "some.service.account", + Namespace: "some.namespace", + } + + require.Equal(t, + `--- +apiVersion: v1 +kind: ServiceAccount +metadata: + name: some.service.account + namespace: some.namespace +`, sa.Render(t)) +}