Skip to content

Commit

Permalink
fix(discovery): correct k8s API discovery configuration (#804)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewazores authored May 3, 2024
1 parent b9b0365 commit c1c350c
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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))
}
Expand Down Expand Up @@ -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{
{
Expand Down
6 changes: 4 additions & 2 deletions internal/controllers/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
Expand Down Expand Up @@ -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) {
Expand All @@ -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()))
Expand Down
40 changes: 26 additions & 14 deletions internal/test/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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, ","),
Expand All @@ -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
Expand Down Expand Up @@ -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",
},
)
}
Expand Down

0 comments on commit c1c350c

Please sign in to comment.