11//
22// DISCLAIMER
33//
4- // Copyright 2020 ArangoDB GmbH, Cologne, Germany
4+ // Copyright 2020-2021 ArangoDB GmbH, Cologne, Germany
55//
66// Licensed under the Apache License, Version 2.0 (the "License");
77// you may not use this file except in compliance with the License.
2323package deployment
2424
2525import (
26+ "fmt"
27+ "os"
28+ "path/filepath"
2629 "testing"
2730
28- "github.com/arangodb/kube-arangodb/pkg/util/constants"
29-
30- "github.com/arangodb/kube-arangodb/pkg/util/k8sutil"
31-
3231 "github.com/stretchr/testify/require"
33-
34- "github.com/arangodb/kube-arangodb/pkg/util"
32+ core "k8s.io/api/core/v1"
3533
3634 api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1"
37- core "k8s.io/api/core/v1"
35+ "github.com/arangodb/kube-arangodb/pkg/util"
36+ "github.com/arangodb/kube-arangodb/pkg/util/constants"
37+ "github.com/arangodb/kube-arangodb/pkg/util/k8sutil"
3838)
3939
4040func TestEnsurePod_ArangoDB_Core (t * testing.T ) {
@@ -913,12 +913,14 @@ func TestEnsurePod_ArangoDB_Core(t *testing.T) {
913913 }
914914
915915 testCase .createTestPodData (deployment , api .ServerGroupAgents , firstAgentStatus )
916+ testCase .ExpectedPod .ObjectMeta .Labels [k8sutil .LabelKeyArangoExporter ] = testYes
916917 },
917918 ExpectedEvent : "member agent is created" ,
918919 ExpectedPod : core.Pod {
919920 Spec : core.PodSpec {
920921 Volumes : []core.Volume {
921922 k8sutil .CreateVolumeEmptyDir (k8sutil .ArangodVolumeName ),
923+ k8sutil .CreateVolumeWithSecret (k8sutil .ExporterJWTVolumeName , testExporterToken ),
922924 },
923925 Containers : []core.Container {
924926 {
@@ -934,6 +936,7 @@ func TestEnsurePod_ArangoDB_Core(t *testing.T) {
934936 ImagePullPolicy : core .PullIfNotPresent ,
935937 SecurityContext : securityContext .NewSecurityContext (),
936938 },
939+ testArangodbInternalExporterContainer (false , emptyResources ),
937940 },
938941 RestartPolicy : core .RestartPolicyNever ,
939942 TerminationGracePeriodSeconds : & defaultAgentTerminationTimeout ,
@@ -945,7 +948,7 @@ func TestEnsurePod_ArangoDB_Core(t *testing.T) {
945948 },
946949 },
947950 {
948- Name : "DBserver Pod with metrics exporter" ,
951+ Name : "DBserver Pod with internal metrics exporter" ,
949952 ArangoDeployment : & api.ArangoDeployment {
950953 Spec : api.DeploymentSpec {
951954 Image : util .NewString (testImage ),
@@ -988,7 +991,7 @@ func TestEnsurePod_ArangoDB_Core(t *testing.T) {
988991 ImagePullPolicy : core .PullIfNotPresent ,
989992 SecurityContext : securityContext .NewSecurityContext (),
990993 },
991- testCreateExporterContainer (false , emptyResources ),
994+ testArangodbInternalExporterContainer (false , emptyResources ),
992995 },
993996 RestartPolicy : core .RestartPolicyNever ,
994997 TerminationGracePeriodSeconds : & defaultDBServerTerminationTimeout ,
@@ -1008,7 +1011,7 @@ func TestEnsurePod_ArangoDB_Core(t *testing.T) {
10081011 TLS : noTLS ,
10091012 Metrics : api.MetricsSpec {
10101013 Enabled : util .NewBool (true ),
1011- Image : util .NewString (testExporterImage ),
1014+ Image : util .NewString (testImage ),
10121015 Authentication : api.MetricsAuthenticationSpec {
10131016 JWTTokenSecretName : util .NewString (testExporterToken ),
10141017 },
@@ -1050,7 +1053,7 @@ func TestEnsurePod_ArangoDB_Core(t *testing.T) {
10501053 ImagePullPolicy : core .PullIfNotPresent ,
10511054 SecurityContext : securityContext .NewSecurityContext (),
10521055 },
1053- testCreateExporterContainer (false , k8sutil .ExtractPodResourceRequirement (resourcesUnfiltered )),
1056+ testArangodbInternalExporterContainer (false , k8sutil .ExtractPodResourceRequirement (resourcesUnfiltered )),
10541057 },
10551058 RestartPolicy : core .RestartPolicyNever ,
10561059 TerminationGracePeriodSeconds : & defaultDBServerTerminationTimeout ,
@@ -1123,7 +1126,7 @@ func TestEnsurePod_ArangoDB_Core(t *testing.T) {
11231126 ImagePullPolicy : core .PullIfNotPresent ,
11241127 SecurityContext : securityContext .NewSecurityContext (),
11251128 },
1126- testCreateExporterContainer (false , emptyResources ),
1129+ testArangodbInternalExporterContainer (false , emptyResources ),
11271130 },
11281131 RestartPolicy : core .RestartPolicyNever ,
11291132 TerminationGracePeriodSeconds : & defaultDBServerTerminationTimeout ,
@@ -1195,7 +1198,7 @@ func TestEnsurePod_ArangoDB_Core(t *testing.T) {
11951198 ImagePullPolicy : core .PullIfNotPresent ,
11961199 SecurityContext : securityContext .NewSecurityContext (),
11971200 },
1198- testCreateExporterContainer (false , emptyResources ),
1201+ testArangodbInternalExporterContainer (false , emptyResources ),
11991202 },
12001203 RestartPolicy : core .RestartPolicyNever ,
12011204 TerminationGracePeriodSeconds : & defaultDBServerTerminationTimeout ,
@@ -1289,7 +1292,7 @@ func TestEnsurePod_ArangoDB_Core(t *testing.T) {
12891292 Resources : emptyResources ,
12901293 },
12911294 func () core.Container {
1292- c := testCreateExporterContainer (true , emptyResources )
1295+ c := testArangodbInternalExporterContainer (true , emptyResources )
12931296 c .VolumeMounts = append (c .VolumeMounts , k8sutil .TlsKeyfileVolumeMount ())
12941297 return c
12951298 }(),
@@ -1435,3 +1438,65 @@ func TestEnsurePod_ArangoDB_Core(t *testing.T) {
14351438
14361439 runTestCases (t , testCases ... )
14371440}
1441+
1442+ func testArangodbInternalExporterContainer (secure bool , resources core.ResourceRequirements , ports ... int32 ) core.Container {
1443+
1444+ var port int32 = k8sutil .ArangoExporterPort
1445+ if len (ports ) > 0 {
1446+ port = ports [0 ]
1447+ }
1448+
1449+ return core.Container {
1450+ Name : k8sutil .ExporterContainerName ,
1451+ Image : testImage ,
1452+ Command : createTestInternalExporterCommand (secure , port ),
1453+ Ports : []core.ContainerPort {
1454+ {
1455+ Name : string (api .MetricsModeExporter ),
1456+ ContainerPort : port ,
1457+ Protocol : core .ProtocolTCP ,
1458+ },
1459+ },
1460+ LivenessProbe : createTestExporterLivenessProbe (secure ),
1461+ Resources : resources ,
1462+ ImagePullPolicy : core .PullIfNotPresent ,
1463+ SecurityContext : & core.SecurityContext {
1464+ Capabilities : & core.Capabilities {
1465+ Drop : []core.Capability {
1466+ "ALL" ,
1467+ },
1468+ },
1469+ },
1470+ VolumeMounts : []core.VolumeMount {
1471+ k8sutil .LifecycleVolumeMount (),
1472+ k8sutil .ExporterJWTVolumeMount (),
1473+ },
1474+ }
1475+ }
1476+
1477+ func createTestInternalExporterCommand (secure bool , port int32 ) []string {
1478+ binaryPath , err := os .Executable ()
1479+ if err != nil {
1480+ return []string {}
1481+ }
1482+ exePath := filepath .Join (k8sutil .LifecycleVolumeMountDir , filepath .Base (binaryPath ))
1483+
1484+ args := []string {exePath , "exporter" }
1485+ if secure {
1486+ args = append (args , "--arangodb.endpoint=https://localhost:8529/_admin/metrics" )
1487+ } else {
1488+ args = append (args , "--arangodb.endpoint=http://localhost:8529/_admin/metrics" )
1489+ }
1490+
1491+ args = append (args , "--arangodb.jwt-file=/secrets/exporter/jwt/token" )
1492+
1493+ if port != k8sutil .ArangoExporterPort {
1494+ args = append (args , fmt .Sprintf ("--server.address=:%d" , port ))
1495+ }
1496+
1497+ if secure {
1498+ args = append (args , "--ssl.keyfile=/secrets/tls/tls.keyfile" )
1499+ }
1500+
1501+ return args
1502+ }
0 commit comments