feat(provider/kubernetes): v2 Extract artifact name from reference
Allow for for using a regex to set the artifact name after finding
artifacts in a manifest. This is used to add support in orca for
Currently only done for Docker images, but should be possible for
configmaps and secrets as well.
MODIFY
clouddriver-kubernetes/src/main/groovy/com/netflix/spinnaker/clouddriver/kubernetes/v2/artifact/ArtifactReplacer.java
clouddriver-kubernetes/src/main/groovy/com/netflix/spinnaker/clouddriver/kubernetes/v2/artifact/ArtifactReplacer.java
@@ -42,6 +42,8 @@ import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@@ -104,6 +106,7 @@ public class ArtifactReplacer {
.map(s -> Artifact.builder()
.type(r.getType().toString())
.reference(s)
+ .name(r.getNameFromReference(s))
.build()
);
} catch (Exception e) {
@@ -122,6 +125,8 @@ public class ArtifactReplacer {
public static class Replacer {
private final String replacePath;
private final String findPath;
+ private final Pattern namePattern; // the first group should be the artifact name
+
@Getter
private final ArtifactTypes type;
@@ -142,6 +147,19 @@ public class ArtifactReplacer {
return obj.read(findPath);
}
+ String getNameFromReference(String reference) {
+ if (namePattern == null) {
+ return null;
+ }
+
+ Matcher m = namePattern.matcher(reference);
+ if (m.find() && m.groupCount() > 0 && StringUtils.isNotEmpty(m.group(1))) {
+ return m.group(1);
+ } else {
+ return null;
+ }
+ }
+
boolean replaceIfPossible(DocumentContext obj, Artifact artifact) {
if (artifact == null || StringUtils.isEmpty(artifact.getType())) {
throw new IllegalArgumentException("Artifact and artifact type must be set.");

MODIFY
clouddriver-kubernetes/src/main/groovy/com/netflix/spinnaker/clouddriver/kubernetes/v2/artifact/ArtifactReplacerFactory.java
clouddriver-kubernetes/src/main/groovy/com/netflix/spinnaker/clouddriver/kubernetes/v2/artifact/ArtifactReplacerFactory.java
@@ -19,11 +19,14 @@ package com.netflix.spinnaker.clouddriver.kubernetes.v2.artifact;
import com.netflix.spinnaker.clouddriver.kubernetes.v2.artifact.ArtifactReplacer.Replacer;
+import java.util.regex.Pattern;
+
public class ArtifactReplacerFactory {
public static Replacer dockerImageReplacer() {
return Replacer.builder()
.replacePath("$.spec.template.spec.containers.[?( @.image == \"{%name%}\" )].image")
.findPath("$.spec.template.spec.containers.*.image")
+ .namePattern(Pattern.compile("([0-9A-Za-z./]+).*"))
.type(ArtifactTypes.DOCKER_IMAGE)
.build();
}

feat(provider/kubernetes): v2 Extract artifact name from reference (#2322)

Allow for for using a regex to set the artifact name after finding
artifacts in a manifest. This is used to add support in orca for
https://github.com/spinnaker/spinnaker/issues/2267

Currently only done for Docker images, but should be possible for
configmaps and secrets as well.