Skip to content

Commit c922b64

Browse files
authored
feat: add resource name prefix & env var for image name format (#61)
1 parent 8a78f86 commit c922b64

File tree

6 files changed

+62
-19
lines changed

6 files changed

+62
-19
lines changed

docs/configuration.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,17 @@ app:
100100

101101
#### Build and Deployment Configuration
102102

103-
| Property | Environment Variable | Default Value | Required | Applied when | Description |
104-
|--------------------------|-----------------------|-----------------------------------------|------------------------------------------------------|--------------|-------------------------------------------------------------------------------------------------------|
105-
| `app.build-namespace` | `K8S_BUILD_NAMESPACE` | `default` | No (recommended to adjust for target environment) | - | Kubernetes namespace for build operations |
106-
| `app.git-clone-image` | `GIT_CLONE_IMAGE` | `alpine/git:latest` | No (recommended to use a specific tag in production) | - | Docker image for Git cloning in init containers. Must include git and openssh-client for SSH support. |
107-
| `app.builder-image` | - | `gcr.io/kaniko-project/executor:latest` | No (recommended to use a specific tag in production) | - | Docker image for building containers |
108-
| `app.analyser-image` | - | `anchore/syft:latest` | No (recommended to use a specific tag in production) | - | Docker image used for analyzing container images |
109-
| `app.copy-image` | - | `quay.io/skopeo/stable:latest` | No (recommended to use a specific tag in production) | - | Docker image for copying images |
110-
| `app.docker-config-path` | - | `/kaniko/.docker/config.json` | No | - | Path to the location where the Docker config file is mounted for build containers. |
111-
| `app.cilium-network-policies-enabled` | `CILIUM_NETWORK_POLICIES_ENABLED` | `false` | No | - | Flag that allows to enable Cilium network policies for image build and deployments. |
103+
| Property | Environment Variable | Default Value | Required | Applied when | Description |
104+
|-----------------------------------------|-----------------------------------|-----------------------------------------|------------------------------------------------------|--------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
105+
| `app.build-namespace` | `K8S_BUILD_NAMESPACE` | `default` | No (recommended to adjust for target environment) | - | Kubernetes namespace for build operations |
106+
| `app.git-clone-image` | `GIT_CLONE_IMAGE` | `alpine/git:latest` | No (recommended to use a specific tag in production) | - | Docker image for Git cloning in init containers. Must include git and openssh-client for SSH support. |
107+
| `app.builder-image` | - | `gcr.io/kaniko-project/executor:latest` | No (recommended to use a specific tag in production) | - | Docker image for building containers |
108+
| `app.analyser-image` | - | `anchore/syft:latest` | No (recommended to use a specific tag in production) | - | Docker image used for analyzing container images |
109+
| `app.copy-image` | - | `quay.io/skopeo/stable:latest` | No (recommended to use a specific tag in production) | - | Docker image for copying images |
110+
| `app.docker-config-path` | - | `/kaniko/.docker/config.json` | No | - | Path to the location where the Docker config file is mounted for build containers. |
111+
| `app.cilium-network-policies-enabled` | `CILIUM_NETWORK_POLICIES_ENABLED` | `false` | No | - | Flag that allows to enable Cilium network policies for image build and deployments. |
112+
| `app.image-name-format` | `IMAGE_NAME_FORMAT` | `app-%s` | No | - | Name format for images that are built using Deployment Manager. Must contain `%s` that will be replaced by image definition ID. |
113+
| `app.resource-name-prefix` | `RESOURCE_NAME_PREFIX` | - | No | - | Prefix that will be added to all resources that image build and deployments produce. Important note: do not change this value on exising setups, otherwise existing images and K8s resources will be lost. |
112114

113115
#### MCP Proxy Configuration
114116

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.epam.aidial.deployment.manager.configuration;
2+
3+
import com.epam.aidial.deployment.manager.utils.K8sNamingUtils;
4+
import org.springframework.beans.factory.annotation.Value;
5+
import org.springframework.context.annotation.Configuration;
6+
7+
@Configuration
8+
public class NamingUtilsConfig {
9+
10+
@Value("${app.resource-name-prefix}")
11+
public void initNamingUtils(String value) {
12+
K8sNamingUtils.setResourceNamePrefix(value);
13+
}
14+
}

src/main/java/com/epam/aidial/deployment/manager/service/pipeline/specification/CiliumNetworkPolicyCreator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.epam.aidial.deployment.manager.service.pipeline.specification;
22

33
import com.epam.aidial.deployment.manager.configuration.logging.LogExecution;
4+
import com.epam.aidial.deployment.manager.utils.K8sNamingUtils;
45
import io.cilium.v2.CiliumNetworkPolicy;
56
import io.cilium.v2.CiliumNetworkPolicySpec;
67
import io.cilium.v2.ciliumnetworkpolicyspec.Egress;
@@ -24,7 +25,6 @@
2425
@LogExecution
2526
public class CiliumNetworkPolicyCreator {
2627

27-
private static final String POLICY_NAME_TEMPLATE = "restrict-egress-%s";
2828
private static final String UDP_DNS_PATTERN_ALL = "*";
2929
private static final String TCP_PORT = "443";
3030
private static final String UDP_PORT = "53";
@@ -94,6 +94,6 @@ public CiliumNetworkPolicy create(String namespace, String matchLabelName, Strin
9494
}
9595

9696
public static String getPolicyName(String matchLabelValue) {
97-
return POLICY_NAME_TEMPLATE.formatted(matchLabelValue);
97+
return K8sNamingUtils.generateName(matchLabelValue);
9898
}
9999
}

src/main/java/com/epam/aidial/deployment/manager/utils/K8sNamingUtils.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.epam.aidial.deployment.manager.utils;
22

33
import io.fabric8.kubernetes.api.model.HasMetadata;
4+
import lombok.Setter;
45
import lombok.experimental.UtilityClass;
56
import lombok.extern.slf4j.Slf4j;
67
import org.apache.commons.lang3.RandomStringUtils;
8+
import org.apache.commons.lang3.StringUtils;
79

810
@Slf4j
911
@UtilityClass
@@ -12,6 +14,9 @@ public class K8sNamingUtils {
1214
public static final String MCP_PREFIX = "mcp";
1315
public static final String DM_PREFIX = "dm";
1416

17+
@Setter
18+
private static String resourceNamePrefix;
19+
1520
public String extractName(HasMetadata k8sObject) {
1621
return k8sObject.getMetadata().getName();
1722
}
@@ -25,7 +30,8 @@ public String extractId(String name) {
2530
return extractId(DM_PREFIX, name);
2631
}
2732

28-
private String extractId(String prefix, String name) {
33+
private String extractId(String defaultPrefix, String name) {
34+
String prefix = getResourceNamePrefixOrDefaultPrefix(defaultPrefix);
2935
try {
3036
String fullPrefix = prefix + "-";
3137
if (name.startsWith(fullPrefix)) {
@@ -40,26 +46,33 @@ private String extractId(String prefix, String name) {
4046
}
4147
}
4248

43-
4449
@Deprecated
4550
public String generateMcpPrefixedName(String name) {
46-
return "%s-%s".formatted(MCP_PREFIX, name);
51+
String prefix = getResourceNamePrefixOrDefaultPrefix(MCP_PREFIX);
52+
return "%s-%s".formatted(prefix, name);
4753
}
4854

4955
public String generateName(String name) {
50-
return "%s-%s".formatted(DM_PREFIX, name);
56+
String prefix = getResourceNamePrefixOrDefaultPrefix(DM_PREFIX);
57+
return "%s-%s".formatted(prefix, name);
5158
}
5259

5360
public String generateName(String type, String name) {
54-
return "%s-%s-%s".formatted(DM_PREFIX, type, name);
61+
String prefix = getResourceNamePrefixOrDefaultPrefix(DM_PREFIX);
62+
return "%s-%s-%s".formatted(prefix, type, name);
5563
}
5664

5765
public String generateUniqueName(String type, String name) {
58-
return "%s-%s-%s-%s".formatted(DM_PREFIX, type, name, randomStr(6));
66+
String prefix = getResourceNamePrefixOrDefaultPrefix(DM_PREFIX);
67+
return "%s-%s-%s-%s".formatted(prefix, type, name, randomStr(6));
5968
}
6069

6170
private String randomStr(int length) {
6271
return RandomStringUtils.secure().nextAlphabetic(length).toLowerCase();
6372
}
6473

74+
private String getResourceNamePrefixOrDefaultPrefix(String defaultPrefix) {
75+
return StringUtils.isNotBlank(resourceNamePrefix) ? resourceNamePrefix : defaultPrefix;
76+
}
77+
6578
}

src/main/resources/application.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ app:
9696
startup-timeout: ${K8S_KSERVE_DEPLOYMENT_STARTUP_TIMEOUT_SEC:3600}
9797
informer-resync-interval: ${K8S_KSERVE_DEPLOYMENT_INFORMER_RESYNC_INTERVAL_SEC:60}
9898
use-cluster-internal-url: ${K8S_KSERVE_DEPLOYMENT_USE_CLUSTER_INTERNAL_URL:true}
99-
image-name-format: app-%s
99+
image-name-format: ${IMAGE_NAME_FORMAT:app-%s}
100100
image-label: latest
101101
image-build-timeout-sec: 300
102102
image-build-logs-size-limit: 1000
@@ -121,6 +121,7 @@ app:
121121
watcher-failure-reset-interval-ms: ${WATCHER_FAILURE_RESET_INTERVAL_MS:600000}
122122
deployment-reserved-env-names: ${DEPLOYMENT_RESERVED_ENV_NAMES:PORT,K_SERVICE,K_CONFIGURATION,K_REVISION}
123123
cilium-network-policies-enabled: ${CILIUM_NETWORK_POLICIES_ENABLED:false}
124+
resource-name-prefix: ${RESOURCE_NAME_PREFIX:}
124125
knative-service-config:
125126
apiVersion: serving.knative.dev/v1
126127
kind: Service

src/test/java/com/epam/aidial/deployment/manager/service/deployment/KnativeDeploymentManagerTest.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.epam.aidial.deployment.manager.service.manifest.KnativeManifestGenerator;
2424
import com.epam.aidial.deployment.manager.service.manifest.ManifestGenerator;
2525
import com.epam.aidial.deployment.manager.service.pipeline.specification.CiliumNetworkPolicyCreator;
26+
import com.epam.aidial.deployment.manager.utils.K8sNamingUtils;
2627
import io.cilium.v2.CiliumNetworkPolicy;
2728
import io.fabric8.knative.serving.v1.Service;
2829
import io.fabric8.kubernetes.api.model.Container;
@@ -35,7 +36,9 @@
3536
import io.fabric8.kubernetes.api.model.PodStatus;
3637
import io.fabric8.kubernetes.client.dsl.ContainerResource;
3738
import io.fabric8.kubernetes.client.dsl.PodResource;
39+
import org.junit.jupiter.api.AfterAll;
3840
import org.junit.jupiter.api.AfterEach;
41+
import org.junit.jupiter.api.BeforeAll;
3942
import org.junit.jupiter.api.BeforeEach;
4043
import org.junit.jupiter.api.Test;
4144
import org.junit.jupiter.api.extension.ExtendWith;
@@ -71,7 +74,7 @@ class KnativeDeploymentManagerTest {
7174
private static final int UNDEPLOY_TIMEOUT = 300;
7275

7376
private static final String SERVICE_URL = "https://service-url.example.com";
74-
private static final String SERVICE_NAME = "mcp-" + DEPLOYMENT_ID;
77+
private static final String SERVICE_NAME = "test-" + DEPLOYMENT_ID;
7578
private static final String SERVICE_CONTAINER = "user-container";
7679
private static final String IMAGE_NAME = "test-image:latest";
7780
private static final String NAMESPACE = "test-namespace";
@@ -106,6 +109,16 @@ class KnativeDeploymentManagerTest {
106109

107110
private KnativeDeploymentManager knativeDeploymentManager;
108111

112+
@BeforeAll
113+
static void setPrefix() {
114+
K8sNamingUtils.setResourceNamePrefix("test");
115+
}
116+
117+
@AfterAll
118+
static void dropPrefix() {
119+
K8sNamingUtils.setResourceNamePrefix("");
120+
}
121+
109122
@BeforeEach
110123
void setUp() {
111124
var knativeDeployProperties = new KnativeDeployProperties();

0 commit comments

Comments
 (0)