diff --git a/internal/controllers/common/resource_definitions/resource_definitions.go b/internal/controllers/common/resource_definitions/resource_definitions.go index a04805f8..cd3e8036 100644 --- a/internal/controllers/common/resource_definitions/resource_definitions.go +++ b/internal/controllers/common/resource_definitions/resource_definitions.go @@ -988,14 +988,6 @@ func NewCoreContainer(cr *model.CryostatInstance, specs *ServiceSpecs, imageTag Name: "CRYOSTAT_ENABLE_JDP_BROADCAST", Value: "false", }, - { - Name: "CRYOSTAT_DISCOVERY_KUBERNETES_ENABLED", - Value: "true", - }, - { - Name: "CRYOSTAT_DISCOVERY_KUBERNETES_NAMESPACES", - Value: strings.Join(cr.TargetNamespaces, ","), - }, } mounts := []corev1.VolumeMount{ @@ -1116,7 +1108,6 @@ func NewCoreContainer(cr *model.CryostatInstance, specs *ServiceSpecs, imageTag targetCacheSize := "-1" targetCacheTTL := "10" if cr.Spec.JmxCacheOptions != nil { - if cr.Spec.JmxCacheOptions.TargetCacheSize != 0 { targetCacheSize = strconv.Itoa(int(cr.Spec.JmxCacheOptions.TargetCacheSize)) } @@ -1146,37 +1137,43 @@ func NewCoreContainer(cr *model.CryostatInstance, specs *ServiceSpecs, imageTag }, } + k8sDiscoveryEnabled := true + k8sDiscoveryPortNames := "jfr-jmx" + k8sDiscoveryPortNumbers := "9091" if cr.Spec.TargetDiscoveryOptions != nil { - var portNames string + k8sDiscoveryEnabled = !cr.Spec.TargetDiscoveryOptions.BuiltInDiscoveryDisabled + if len(cr.Spec.TargetDiscoveryOptions.DiscoveryPortNames) > 0 { - portNames = strings.Join(cr.Spec.TargetDiscoveryOptions.DiscoveryPortNames[:], ",") + k8sDiscoveryPortNames = strings.Join(cr.Spec.TargetDiscoveryOptions.DiscoveryPortNames[:], ",") } else if cr.Spec.TargetDiscoveryOptions.DisableBuiltInPortNames { - portNames = "-" - } - if len(portNames) > 0 { - envs = append(envs, - corev1.EnvVar{ - Name: "CRYOSTAT_DISCOVERY_K8S_PORT_NAMES", - Value: portNames, - }, - ) + k8sDiscoveryPortNames = "" } - portNumbers := "" if len(cr.Spec.TargetDiscoveryOptions.DiscoveryPortNumbers) > 0 { - portNumbers = strings.Trim(strings.ReplaceAll(fmt.Sprint(cr.Spec.TargetDiscoveryOptions.DiscoveryPortNumbers), " ", ","), "[]") + k8sDiscoveryPortNumbers = strings.Trim(strings.ReplaceAll(fmt.Sprint(cr.Spec.TargetDiscoveryOptions.DiscoveryPortNumbers), " ", ","), "[]") } else if cr.Spec.TargetDiscoveryOptions.DisableBuiltInPortNumbers { - portNumbers = "0" - } - if len(portNumbers) > 0 { - envs = append(envs, - corev1.EnvVar{ - Name: "CRYOSTAT_DISCOVERY_K8S_PORT_NUMBERS", - Value: portNumbers, - }, - ) + k8sDiscoveryPortNumbers = "" } } + envs = append(envs, []corev1.EnvVar{ + { + Name: "CRYOSTAT_DISCOVERY_KUBERNETES_ENABLED", + Value: fmt.Sprintf("%t", k8sDiscoveryEnabled), + }, + { + Name: "CRYOSTAT_DISCOVERY_KUBERNETES_NAMESPACES", + Value: strings.Join(cr.TargetNamespaces, ","), + }, + { + Name: "CRYOSTAT_DISCOVERY_KUBERNETES_PORT_NAMES", + Value: k8sDiscoveryPortNames, + }, + { + Name: "CRYOSTAT_DISCOVERY_KUBERNETES_PORT_NUMBERS", + Value: k8sDiscoveryPortNumbers, + }, + }..., + ) grafanaVars := []corev1.EnvVar{ { diff --git a/internal/controllers/reconciler_test.go b/internal/controllers/reconciler_test.go index 7d8ed213..48461595 100644 --- a/internal/controllers/reconciler_test.go +++ b/internal/controllers/reconciler_test.go @@ -2706,6 +2706,7 @@ func (t *cryostatTestInput) checkMainPodTemplate(deployment *appsv1.Deployment, hasPortConfig := cr.Spec.TargetDiscoveryOptions != nil && len(cr.Spec.TargetDiscoveryOptions.DiscoveryPortNames) > 0 && len(cr.Spec.TargetDiscoveryOptions.DiscoveryPortNumbers) > 0 + builtInDiscoveryDisabled := cr.Spec.TargetDiscoveryOptions != nil && cr.Spec.TargetDiscoveryOptions.BuiltInDiscoveryDisabled builtInPortConfigDisabled := cr.Spec.TargetDiscoveryOptions != nil && cr.Spec.TargetDiscoveryOptions.DisableBuiltInPortNames && cr.Spec.TargetDiscoveryOptions.DisableBuiltInPortNumbers @@ -2715,6 +2716,7 @@ func (t *cryostatTestInput) checkMainPodTemplate(deployment *appsv1.Deployment, cr.Spec.AuthProperties != nil, emptyDir, hasPortConfig, + builtInDiscoveryDisabled, builtInPortConfigDisabled, dbSecretProvided, t.NewCoreContainerResource(cr), t.NewCoreSecurityContext(cr)) @@ -2859,7 +2861,7 @@ func (t *cryostatTestInput) checkDeploymentHasTemplates() { func (t *cryostatTestInput) checkCoreContainer(container *corev1.Container, ingress bool, reportsUrl string, authProps bool, emptyDir bool, - hasPortConfig bool, builtInPortConfigDisabled bool, + hasPortConfig bool, builtInDiscoveryDisabled bool, builtInPortConfigDisabled bool, dbSecretProvided bool, resources *corev1.ResourceRequirements, securityContext *corev1.SecurityContext) { @@ -2870,7 +2872,7 @@ func (t *cryostatTestInput) checkCoreContainer(container *corev1.Container, ingr Expect(container.Image).To(Equal(*t.EnvCoreImageTag)) } Expect(container.Ports).To(ConsistOf(t.NewCorePorts())) - Expect(container.Env).To(ConsistOf(t.NewCoreEnvironmentVariables(reportsUrl, authProps, ingress, emptyDir, hasPortConfig, builtInPortConfigDisabled, dbSecretProvided))) + Expect(container.Env).To(ConsistOf(t.NewCoreEnvironmentVariables(reportsUrl, authProps, ingress, emptyDir, hasPortConfig, builtInDiscoveryDisabled, builtInPortConfigDisabled, dbSecretProvided))) Expect(container.EnvFrom).To(ConsistOf(t.NewCoreEnvFromSource())) Expect(container.VolumeMounts).To(ConsistOf(t.NewCoreVolumeMounts())) Expect(container.LivenessProbe).To(Equal(t.NewCoreLivenessProbe())) diff --git a/internal/test/resources.go b/internal/test/resources.go index fe667e29..619ca762 100644 --- a/internal/test/resources.go +++ b/internal/test/resources.go @@ -1290,7 +1290,7 @@ func (r *TestResources) NewStoragePorts() []corev1.ContainerPort { } func (r *TestResources) NewCoreEnvironmentVariables(reportsUrl string, authProps bool, ingress bool, - emptyDir bool, hasPortConfig bool, builtInPortConfigDisabled bool, dbSecretProvided bool) []corev1.EnvVar { + emptyDir bool, hasPortConfig bool, builtInDiscoveryDisabled bool, builtInPortConfigDisabled bool, dbSecretProvided bool) []corev1.EnvVar { envs := []corev1.EnvVar{ { Name: "QUARKUS_HTTP_HOST", @@ -1388,10 +1388,6 @@ func (r *TestResources) NewCoreEnvironmentVariables(reportsUrl string, authProps Name: "CRYOSTAT_CONNECTIONS_TTL", Value: "10", }, - { - Name: "CRYOSTAT_DISCOVERY_KUBERNETES_ENABLED", - Value: "true", - }, { Name: "CRYOSTAT_DISCOVERY_KUBERNETES_NAMESPACES", Value: strings.Join(r.TargetNamespaces, ","), @@ -1410,7 +1406,7 @@ func (r *TestResources) NewCoreEnvironmentVariables(reportsUrl string, authProps }, } - envs = append(envs, r.NewTargetDiscoveryEnvVar(hasPortConfig, builtInPortConfigDisabled)...) + envs = append(envs, r.NewTargetDiscoveryEnvVars(hasPortConfig, builtInDiscoveryDisabled, builtInPortConfigDisabled)...) optional := false secretName := r.NewDatabaseSecret().Name @@ -1673,29 +1669,45 @@ func (r *TestResources) NewJmxCacheOptionsEnv() []corev1.EnvVar { } } -func (r *TestResources) NewTargetDiscoveryEnvVar(hasPortConfig bool, builtInPortConfigDisabled bool) []corev1.EnvVar { - envs := make([]corev1.EnvVar, 0) +func (r *TestResources) NewTargetDiscoveryEnvVars(hasPortConfig bool, builtInDiscoveryDisabled bool, builtInPortConfigDisabled bool) []corev1.EnvVar { + envs := []corev1.EnvVar{ + { + Name: "CRYOSTAT_DISCOVERY_KUBERNETES_ENABLED", + Value: fmt.Sprintf("%t", !builtInDiscoveryDisabled), + }, + } if hasPortConfig { envs = append(envs, corev1.EnvVar{ - Name: "CRYOSTAT_DISCOVERY_K8S_PORT_NAMES", + Name: "CRYOSTAT_DISCOVERY_KUBERNETES_PORT_NAMES", Value: "custom-port-name,another-custom-port-name", }, corev1.EnvVar{ - Name: "CRYOSTAT_DISCOVERY_K8S_PORT_NUMBERS", + Name: "CRYOSTAT_DISCOVERY_KUBERNETES_PORT_NUMBERS", Value: "9092,9090", }, ) } else if builtInPortConfigDisabled { envs = append(envs, corev1.EnvVar{ - Name: "CRYOSTAT_DISCOVERY_K8S_PORT_NAMES", - Value: "-", + Name: "CRYOSTAT_DISCOVERY_KUBERNETES_PORT_NAMES", + Value: "", + }, + corev1.EnvVar{ + Name: "CRYOSTAT_DISCOVERY_KUBERNETES_PORT_NUMBERS", + Value: "", + }, + ) + } else { + envs = append(envs, + corev1.EnvVar{ + Name: "CRYOSTAT_DISCOVERY_KUBERNETES_PORT_NAMES", + Value: "jfr-jmx", }, corev1.EnvVar{ - Name: "CRYOSTAT_DISCOVERY_K8S_PORT_NUMBERS", - Value: "0", + Name: "CRYOSTAT_DISCOVERY_KUBERNETES_PORT_NUMBERS", + Value: "9091", }, ) }