Skip to content

Commit

Permalink
Fix OpenShift deployment name
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean Francois Denise committed Apr 25, 2024
1 parent feb8a6e commit a3e078a
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 1 deletion.
5 changes: 5 additions & 0 deletions openshift-deployment/api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,10 @@
<groupId>io.fabric8</groupId>
<artifactId>openshift-client</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,49 @@ private static List<Deployer> getEnabledDeployers(Set<String> disabledDeployers)
return deployers;
}

static final String generateValidName(String name) {
name = name.toLowerCase();
StringBuilder validName = new StringBuilder();
char[] array = name.toCharArray();
for (int i = 0; i < array.length; i++) {
char c = array[i];
// start with an alphabetic character
if (i == 0) {
if (c <= 97 || c >= 122) {
validName.append("app-");
}
validName.append(c);
} else {
// end with an alphabetic character
if (i == array.length - 1) {
if ((c >= 48 && c <= 57) || (c >= 97 && c <= 122)) {
validName.append(c);
} else {
validName.append('0');
}
} else {
// - allowed in the middle
if (c == '-') {
validName.append(c);
} else {
// a-z or 0-9
if ((c >= 48 && c <= 57) || (c >= 97 && c <= 122)) {
validName.append(c);
} else {
// Other character are replaced by -
validName.append('-');
}
}
}
}
}
String ret = validName.toString();
if (ret.length() > 63) {
ret = ret.substring(0, 63);
}
return ret;
}

public static void deploy(List<Path> deployments,
String defaultName,
GlowMessageWriter writer,
Expand All @@ -312,8 +355,9 @@ public static void deploy(List<Path> deployments,
Files.createDirectories(deploymentsDir);
for (Path p : deployments) {
Files.copy(p, deploymentsDir.resolve(p.getFileName()));
int ext = p.getFileName().toString().indexOf(".");
int ext = p.getFileName().toString().lastIndexOf(".");
appName += p.getFileName().toString().substring(0, ext);
appName = generateValidName(appName);
}
if (appName.isEmpty()) {
appName = defaultName;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2024 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.wildfly.glow.deployment.openshift.api;

import org.junit.Assert;
import org.junit.Test;

/**
*
* @author jdenise
*/
public class OpenShiftSupportTestCase {

@Test
public void testAppName() {
{
String name = "foo";
Assert.assertEquals(name, OpenShiftSupport.generateValidName(name));
}
{
String name = "web-1.0";
Assert.assertEquals("web-1-0", OpenShiftSupport.generateValidName(name));
}
{
String name = "990-web-1.0";
Assert.assertEquals("app-990-web-1-0", OpenShiftSupport.generateValidName(name));
}
{
String name = "-web-1.0-foo";
Assert.assertEquals("app--web-1-0-foo", OpenShiftSupport.generateValidName(name));
}
{
String name = "web$970*";
Assert.assertEquals("web-9700", OpenShiftSupport.generateValidName(name));
}
{
String name = "web_970_456_foo";
Assert.assertEquals("web-970-456-foo", OpenShiftSupport.generateValidName(name));
}
{
String name = "ROOT";
Assert.assertEquals("root", OpenShiftSupport.generateValidName(name));
}
{
String name = "0123456789012345678901234567890123456789012345678901234567890123456789";
Assert.assertEquals("app-01234567890123456789012345678901234567890123456789012345678", OpenShiftSupport.generateValidName(name));
}
}
}

0 comments on commit a3e078a

Please sign in to comment.