From 0ad533b8eeb5e7486bcd51f261095214e77e4d86 Mon Sep 17 00:00:00 2001 From: lukasz-jarocki-sonarsource Date: Mon, 23 Sep 2024 16:55:10 +0200 Subject: [PATCH] putting the value of IS_HELM_AUTOSCALING_ENABLED env variable into telemetry and support info file --- .../legacy/TelemetryDataLoaderImplIT.java | 2 +- .../legacy/CloudUsageDataProvider.java | 1 + .../sonar/telemetry/legacy/TelemetryData.java | 4 +-- .../legacy/TelemetryDataJsonWriterTest.java | 2 +- .../server/platform/ContainerSupport.java | 1 + .../server/platform/ContainerSupportImpl.java | 6 ++++ .../cluster/GlobalSystemSection.java | 1 + .../platform/ContainerSupportImplTest.java | 32 +++++++++++++------ .../cluster/GlobalSystemSectionTest.java | 7 ++++ 9 files changed, 43 insertions(+), 13 deletions(-) diff --git a/server/sonar-telemetry/src/it/java/org/sonar/telemetry/legacy/TelemetryDataLoaderImplIT.java b/server/sonar-telemetry/src/it/java/org/sonar/telemetry/legacy/TelemetryDataLoaderImplIT.java index 65f434b29a5..18a19365cf6 100644 --- a/server/sonar-telemetry/src/it/java/org/sonar/telemetry/legacy/TelemetryDataLoaderImplIT.java +++ b/server/sonar-telemetry/src/it/java/org/sonar/telemetry/legacy/TelemetryDataLoaderImplIT.java @@ -691,7 +691,7 @@ public class TelemetryDataLoaderImplIT { @Test public void load_shouldContainCloudUsage() { - CloudUsage cloudUsage = new CloudUsage(true, "1.27", "linux/amd64", "5.4.181-99.354.amzn2.x86_64", "10.1.0", false, "docker", false); + CloudUsage cloudUsage = new CloudUsage(true, "1.27", "linux/amd64", "5.4.181-99.354.amzn2.x86_64", "10.1.0", false, false, "docker", false); when(cloudUsageDataProvider.getCloudUsage()).thenReturn(cloudUsage); TelemetryData data = commercialUnderTest.load(); diff --git a/server/sonar-telemetry/src/main/java/org/sonar/telemetry/legacy/CloudUsageDataProvider.java b/server/sonar-telemetry/src/main/java/org/sonar/telemetry/legacy/CloudUsageDataProvider.java index 28229274208..32d61d6b10b 100644 --- a/server/sonar-telemetry/src/main/java/org/sonar/telemetry/legacy/CloudUsageDataProvider.java +++ b/server/sonar-telemetry/src/main/java/org/sonar/telemetry/legacy/CloudUsageDataProvider.java @@ -116,6 +116,7 @@ public class CloudUsageDataProvider { getKubernetesProvider(), getOfficialHelmChartVersion(), containerSupport.isRunningOnHelmOpenshift(), + containerSupport.isHelmAutoscalingEnabled(), containerSupport.getContainerContext(), isOfficialImageUsed()); diff --git a/server/sonar-telemetry/src/main/java/org/sonar/telemetry/legacy/TelemetryData.java b/server/sonar-telemetry/src/main/java/org/sonar/telemetry/legacy/TelemetryData.java index 59a9c6d95a6..649a34710dc 100644 --- a/server/sonar-telemetry/src/main/java/org/sonar/telemetry/legacy/TelemetryData.java +++ b/server/sonar-telemetry/src/main/java/org/sonar/telemetry/legacy/TelemetryData.java @@ -385,8 +385,8 @@ public class TelemetryData { } record CloudUsage(boolean kubernetes, @Nullable String kubernetesVersion, @Nullable String kubernetesPlatform, - @Nullable String kubernetesProvider, - @Nullable String officialHelmChart, boolean isOnOpenshift, @Nullable String containerRuntime, boolean officialImage) { + @Nullable String kubernetesProvider, @Nullable String officialHelmChart, boolean isOnOpenshift, + boolean isHelmAutoscalingEnabled, @Nullable String containerRuntime, boolean officialImage) { } public static class ProjectStatistics { diff --git a/server/sonar-telemetry/src/test/java/org/sonar/telemetry/legacy/TelemetryDataJsonWriterTest.java b/server/sonar-telemetry/src/test/java/org/sonar/telemetry/legacy/TelemetryDataJsonWriterTest.java index 48ec921ad5a..80f72fd2c2b 100644 --- a/server/sonar-telemetry/src/test/java/org/sonar/telemetry/legacy/TelemetryDataJsonWriterTest.java +++ b/server/sonar-telemetry/src/test/java/org/sonar/telemetry/legacy/TelemetryDataJsonWriterTest.java @@ -696,7 +696,7 @@ class TelemetryDataJsonWriterTest { .setMessageSequenceNumber(1L) .setPlugins(Collections.emptyMap()) .setManagedInstanceInformation(new TelemetryData.ManagedInstanceInformation(false, null)) - .setCloudUsage(new TelemetryData.CloudUsage(true, "1.27", "linux/amd64", "5.4.181-99.354.amzn2.x86_64", "10.1.0", false, "docker", false)) + .setCloudUsage(new TelemetryData.CloudUsage(true, "1.27", "linux/amd64", "5.4.181-99.354.amzn2.x86_64", "10.1.0", false, false, "docker", false)) .setDatabase(new TelemetryData.Database("H2", "11")) .setNcdId(NCD_ID); } diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/ContainerSupport.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/ContainerSupport.java index 8968cd60b47..879f965fb26 100644 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/ContainerSupport.java +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/ContainerSupport.java @@ -32,4 +32,5 @@ public interface ContainerSupport { boolean isRunningOnHelmOpenshift(); + boolean isHelmAutoscalingEnabled(); } diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/ContainerSupportImpl.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/ContainerSupportImpl.java index a836029918c..5c2e2055f47 100644 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/ContainerSupportImpl.java +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/ContainerSupportImpl.java @@ -39,6 +39,7 @@ public class ContainerSupportImpl implements ContainerSupport { private static final String CONTAINER_D = "containerd"; private static final String GENERAL_CONTAINER = "general_container"; private static final String IS_HELM_OPENSHIFT_ENABLED = "IS_HELM_OPENSHIFT_ENABLED"; + private static final String IS_HELM_AUTOSCALING_ENABLED = "IS_HELM_AUTOSCALING_ENABLED"; private static final String[] MOUNT_GREP_COMMAND = {"bash", "-c", "mount | grep 'overlay on /'"}; private static final String[] CAT_COMMAND = {"bash", "-c", "cat /run/.containerenv"}; @@ -86,6 +87,11 @@ public class ContainerSupportImpl implements ContainerSupport { return "true".equals(system2.envVariable(IS_HELM_OPENSHIFT_ENABLED)); } + @Override + public boolean isHelmAutoscalingEnabled() { + return "true".equals(system2.envVariable(IS_HELM_AUTOSCALING_ENABLED)); + } + private boolean isDocker() { return getMountOverlays().contains("/docker") && paths2.exists("/.dockerenv"); } diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/cluster/GlobalSystemSection.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/cluster/GlobalSystemSection.java index cdd591ee3db..c3985bef70d 100644 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/cluster/GlobalSystemSection.java +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/cluster/GlobalSystemSection.java @@ -61,6 +61,7 @@ public class GlobalSystemSection implements SystemInfoSection, Global { setAttribute(protobuf, NCLOC.getName() ,statisticsSupport.getLinesOfCode()); setAttribute(protobuf, "Container", containerSupport.isRunningInContainer()); setAttribute(protobuf, "Running on OpenShift", containerSupport.isRunningOnHelmOpenshift()); + setAttribute(protobuf, "Helm autoscaling", containerSupport.isHelmAutoscalingEnabled()); setAttribute(protobuf, "High Availability", true); setAttribute(protobuf, "External Users and Groups Provisioning", commonSystemInformation.getManagedInstanceProviderName()); diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/ContainerSupportImplTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/ContainerSupportImplTest.java index 6ec91832ed7..c53cdf22b08 100644 --- a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/ContainerSupportImplTest.java +++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/ContainerSupportImplTest.java @@ -59,6 +59,7 @@ public class ContainerSupportImplTest { "podman" : ""); when(underTest.getMountOverlays()).thenReturn(containerContext.mountOverlays); when(system2.envVariable("IS_HELM_OPENSHIFT_ENABLED")).thenReturn(containerContext.isRunningOnOpenShiftEnvVariable); + when(system2.envVariable("IS_HELM_AUTOSCALING_ENABLED")).thenReturn(containerContext.isHelmAutoscalingEnabledEnvVariable); underTest.populateCache(); } @@ -85,15 +86,23 @@ public class ContainerSupportImplTest { .isEqualTo(containerContext.expectedRunningOnOpenShift); } + @Test + public void testIsHelmAutoscalingEnabled() { + assertThat(underTest.isHelmAutoscalingEnabled()) + .isEqualTo(containerContext.expectedHelmAutoscaling); + } + public enum ContainerEnvConfig { - DOCKER("docker", false, true, false, false, "/docker", "false", false), - PODMAN("podman", true, false, true, false, "", "false", false), - BUILDAH("buildah", true, false, false, true, "", "false", false), - CONTAINER_D("containerd", false, false, false, false, "/containerd", "false", false), - GENERAL_CONTAINER("general_container", true, false, false, false, "", "false", false), - NONE(null, false, false, false, false, "", null, false), - OPENSHIFT(null, false, false, false, false, "", "true", true), - OPENSHIFT_SET_TO_FALSE(null, false, false, false, false, "", "false", false); + DOCKER("docker", false, true, false, false, "/docker", "false", false, "false", false), + PODMAN("podman", true, false, true, false, "", "false", false, "false", false), + BUILDAH("buildah", true, false, false, true, "", "false", false, "false", false), + CONTAINER_D("containerd", false, false, false, false, "/containerd", "false", false, "false", false), + GENERAL_CONTAINER("general_container", true, false, false, false, "", "false", false, "false", false), + NONE(null, false, false, false, false, "", null, false, "false", false), + OPENSHIFT(null, false, false, false, false, "", "true", true, "false", false), + OPENSHIFT_SET_TO_FALSE(null, false, false, false, false, "", "false", false, "false", false), + HELM_AUTOSCALING_ENABLED(null, false, false, false, false, "", "true", true, "true", true); + final String expectedContainerContext; final boolean hasContainerenvFile; final boolean hasDockerEnvFile; @@ -102,10 +111,13 @@ public class ContainerSupportImplTest { final String mountOverlays; final String isRunningOnOpenShiftEnvVariable; final boolean expectedRunningOnOpenShift; + final String isHelmAutoscalingEnabledEnvVariable; + final boolean expectedHelmAutoscaling; ContainerEnvConfig(@Nullable String expectedContainerContext, boolean hasContainerenvFile, boolean hasDockerEnvFile, boolean hasPodmanEnvVariable, - boolean hasBuildahContainerenv, String mountOverlays, @Nullable String isRunningOnOpenShiftEnvVariable, boolean expectedRunningOnOpenShift) { + boolean hasBuildahContainerenv, String mountOverlays, @Nullable String isRunningOnOpenShiftEnvVariable, boolean expectedRunningOnOpenShift, + String isHelmAutoscalingEnabledEnvVariable, boolean expectedHelmAutoscaling) { this.expectedContainerContext = expectedContainerContext; this.hasContainerenvFile = hasContainerenvFile; this.hasDockerEnvFile = hasDockerEnvFile; @@ -114,6 +126,8 @@ public class ContainerSupportImplTest { this.mountOverlays = mountOverlays; this.isRunningOnOpenShiftEnvVariable = isRunningOnOpenShiftEnvVariable; this.expectedRunningOnOpenShift = expectedRunningOnOpenShift; + this.isHelmAutoscalingEnabledEnvVariable = isHelmAutoscalingEnabledEnvVariable; + this.expectedHelmAutoscaling = expectedHelmAutoscaling; } } diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/cluster/GlobalSystemSectionTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/cluster/GlobalSystemSectionTest.java index a72a0d76511..c0c6df539ec 100644 --- a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/cluster/GlobalSystemSectionTest.java +++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/cluster/GlobalSystemSectionTest.java @@ -139,6 +139,13 @@ public class GlobalSystemSectionTest { assertThatAttributeIs(protobuf, "Running on OpenShift", true); } + @Test + public void toProtobuf_whenHelmAutoscalingEnabled_returnIt() { + when(containerSupport.isHelmAutoscalingEnabled()).thenReturn(true); + ProtobufSystemInfo.Section protobuf = underTest.toProtobuf(); + assertThatAttributeIs(protobuf, "Helm autoscaling", true); + } + @Test public void return_Lines_of_Codes_from_StatisticsSupport(){ when(statisticsSupport.getLinesOfCode()).thenReturn(17752L); -- 2.39.5