Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(artifacts): Add docker and kubernetes artifact types #3424

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2019 Pivotal, Inc.
*
* 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 com.netflix.spinnaker.clouddriver.artifacts.docker;

import com.netflix.spinnaker.clouddriver.artifacts.config.ArtifactAccount;

public class DockerArtifactAccount implements ArtifactAccount {
@Override
public String getName() {
return "docker-registry";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2019 Pivotal, Inc.
*
* 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 com.netflix.spinnaker.clouddriver.artifacts.docker;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Collections;
import java.util.List;

@Configuration
@ConditionalOnProperty("kubernetes.enabled")
@RequiredArgsConstructor
@Slf4j
public class DockerArtifactConfiguration {
@Bean
List<? extends DockerArtifactCredentials> dockerArtifactCredentials() {
return Collections.singletonList(new DockerArtifactCredentials(new DockerArtifactAccount()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2019 Pivotal, Inc.
*
* 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 com.netflix.spinnaker.clouddriver.artifacts.docker;

import com.netflix.spinnaker.clouddriver.artifacts.config.ArtifactCredentials;
import com.netflix.spinnaker.kork.artifacts.model.Artifact;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.io.InputStream;
import java.util.Collections;
import java.util.List;

@Slf4j
@Data
public class DockerArtifactCredentials implements ArtifactCredentials {
public static final String TYPE = "docker/image";

private final String name;
private final List<String> types = Collections.singletonList(TYPE);

public DockerArtifactCredentials(DockerArtifactAccount account) {
this.name = account.getName();
}

public InputStream download(Artifact artifact) {
throw new UnsupportedOperationException("Docker references are passed on to cloud platforms who retrieve images directly");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2019 Pivotal, Inc.
*
* 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 com.netflix.spinnaker.clouddriver.artifacts.kubernetes;

import com.netflix.spinnaker.clouddriver.artifacts.config.ArtifactAccount;

public class KubernetesArtifactAccount implements ArtifactAccount {
@Override
public String getName() {
return "kubernetes";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2019 Pivotal, Inc.
*
* 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 com.netflix.spinnaker.clouddriver.artifacts.kubernetes;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Collections;
import java.util.List;

@Configuration
@ConditionalOnProperty("kubernetes.enabled")
@RequiredArgsConstructor
@Slf4j
public class KubernetesArtifactConfiguration {
@Bean
List<? extends KubernetesArtifactCredentials> kubernetesArtifactAccounts() {
return Collections.singletonList(new KubernetesArtifactCredentials(new KubernetesArtifactAccount()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2019 Pivotal, Inc.
*
* 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 com.netflix.spinnaker.clouddriver.artifacts.kubernetes;

import com.netflix.spinnaker.clouddriver.artifacts.config.ArtifactCredentials;
import com.netflix.spinnaker.clouddriver.artifacts.docker.DockerArtifactCredentials;
import com.netflix.spinnaker.kork.artifacts.model.Artifact;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Data
public class KubernetesArtifactCredentials implements ArtifactCredentials {
private final String name;
private final List<String> types;

public KubernetesArtifactCredentials(KubernetesArtifactAccount account) {
this.name = account.getName();
this.types = Arrays.stream(KubernetesArtifactType.values())
.map(KubernetesArtifactType::getType)
.collect(Collectors.toList());
types.remove(DockerArtifactCredentials.TYPE);
}

public InputStream download(Artifact artifact) {
throw new UnsupportedOperationException("Kubernetes artifacts are retrieved by kubernetes directly");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2019 Pivotal, Inc.
*
* 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 com.netflix.spinnaker.clouddriver.artifacts.kubernetes;

import com.netflix.spinnaker.clouddriver.artifacts.docker.DockerArtifactCredentials;

public enum KubernetesArtifactType {
DockerImage(DockerArtifactCredentials.TYPE),
ConfigMap("kubernetes/configMap"),
Deployment("kubernetes/deployment"),
ReplicaSet("kubernetes/replicaSet"),
Secret("kubernetes/secret");

private final String type;

KubernetesArtifactType(String type) {
this.type = type;
}

public String getType() {
return type;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.jayway.jsonpath.PathNotFoundException;
import com.jayway.jsonpath.spi.json.JacksonJsonNodeJsonProvider;
import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider;
import com.netflix.spinnaker.clouddriver.artifacts.kubernetes.KubernetesArtifactType;
import com.netflix.spinnaker.clouddriver.kubernetes.v2.description.manifest.KubernetesManifest;
import com.netflix.spinnaker.kork.artifacts.model.Artifact;
import lombok.AllArgsConstructor;
Expand Down Expand Up @@ -145,7 +146,7 @@ public Set<Artifact> findAll(KubernetesManifest input) {
String name = nameFromReference == null ? s : nameFromReference;
if (r.namePattern == null || nameFromReference != null) {
return Artifact.builder()
.type(r.getType().toString())
.type(r.getType().getType())
.reference(s)
.name(name)
.build();
Expand Down Expand Up @@ -174,7 +175,7 @@ public static class Replacer {
private final Function<String, String> nameFromReference;

@Getter
private final ArtifactTypes type;
private final KubernetesArtifactType type;

private static String substituteField(String result, String fieldName, String field) {
field = field == null ? "" : field;
Expand Down Expand Up @@ -213,7 +214,7 @@ boolean replaceIfPossible(DocumentContext obj, Artifact artifact) {
throw new IllegalArgumentException("Artifact and artifact type must be set.");
}

if (!artifact.getType().equals(type.toString())) {
if (!artifact.getType().equals(type.getType())) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package com.netflix.spinnaker.clouddriver.kubernetes.v2.artifact;

import com.netflix.spinnaker.clouddriver.artifacts.kubernetes.KubernetesArtifactType;
import com.netflix.spinnaker.clouddriver.kubernetes.v2.artifact.ArtifactReplacer.Replacer;

import java.util.regex.Pattern;
Expand Down Expand Up @@ -62,71 +63,71 @@ public static Replacer dockerImageReplacer() {
// https://github.com/docker/distribution/blob/95daa793b83a21656fe6c13e6d5cf1c3999108c7/reference/regexp.go#L34
return ref.substring(0, lastColonIndex);
})
.type(ArtifactTypes.DOCKER_IMAGE)
.type(KubernetesArtifactType.DockerImage)
.build();
}

public static Replacer configMapVolumeReplacer() {
return Replacer.builder()
.replacePath("$..spec.template.spec.volumes.[?( @.configMap.name == \"{%name%}\" )].configMap.name")
.findPath("$..spec.template.spec.volumes.*.configMap.name")
.type(ArtifactTypes.KUBERNETES_CONFIG_MAP)
.type(KubernetesArtifactType.ConfigMap)
.build();
}

public static Replacer secretVolumeReplacer() {
return Replacer.builder()
.replacePath("$..spec.template.spec.volumes.[?( @.secret.secretName == \"{%name%}\" )].secret.secretName")
.findPath("$..spec.template.spec.volumes.*.secret.secretName")
.type(ArtifactTypes.KUBERNETES_SECRET)
.type(KubernetesArtifactType.Secret)
.build();
}

public static Replacer configMapKeyValueFromReplacer() {
return Replacer.builder()
.replacePath("$..spec.template.spec['containers', 'initContainers'].*.env.[?( @.valueFrom.configMapKeyRef.name == \"{%name%}\" )].valueFrom.configMapKeyRef.name")
.findPath("$..spec.template.spec['containers', 'initContainers'].*.env.*.valueFrom.configMapKeyRef.name")
.type(ArtifactTypes.KUBERNETES_CONFIG_MAP)
.type(KubernetesArtifactType.ConfigMap)
.build();
}

public static Replacer secretKeyValueFromReplacer() {
return Replacer.builder()
.replacePath("$..spec.template.spec['containers', 'initContainers'].*.env.[?( @.valueFrom.secretKeyRef.name == \"{%name%}\" )].valueFrom.secretKeyRef.name")
.findPath("$..spec.template.spec['containers', 'initContainers'].*.env.*.valueFrom.secretKeyRef.name")
.type(ArtifactTypes.KUBERNETES_SECRET)
.type(KubernetesArtifactType.Secret)
.build();
}

public static Replacer configMapEnvFromReplacer() {
return Replacer.builder()
.replacePath("$..spec.template.spec['containers', 'initContainers'].*.envFrom.[?( @.configMapRef.name == \"{%name%}\" )].configMapRef.name")
.findPath("$..spec.template.spec['containers', 'initContainers'].*.envFrom.*.configMapRef.name")
.type(ArtifactTypes.KUBERNETES_CONFIG_MAP)
.type(KubernetesArtifactType.ConfigMap)
.build();
}

public static Replacer secretEnvFromReplacer() {
return Replacer.builder()
.replacePath("$..spec.template.spec['containers', 'initContainers'].*.envFrom.[?( @.secretRef.name == \"{%name%}\" )].secretRef.name")
.findPath("$..spec.template.spec['containers', 'initContainers'].*.envFrom.*.secretRef.name")
.type(ArtifactTypes.KUBERNETES_SECRET)
.type(KubernetesArtifactType.Secret)
.build();
}

public static Replacer hpaDeploymentReplacer() {
return Replacer.builder()
.replacePath("$[?( (@.spec.scaleTargetRef.kind == \"Deployment\" || @.spec.scaleTargetRef.kind == \"deployment\") && @.spec.scaleTargetRef.name == \"{%name%}\" )].spec.scaleTargetRef.name")
.findPath("$[?( @.spec.scaleTargetRef.kind == \"Deployment\" || @.spec.scaleTargetRef.kind == \"deployment\" )].spec.scaleTargetRef.name")
.type(ArtifactTypes.KUBERNETES_DEPLOYMENT)
.type(KubernetesArtifactType.Deployment)
.build();
}

public static Replacer hpaReplicaSetReplacer() {
return Replacer.builder()
.replacePath("$[?( (@.spec.scaleTargetRef.kind == \"ReplicaSet\" || @.spec.scaleTargetRef.kind == \"replicaSet\") && @.spec.scaleTargetRef.name == \"{%name%}\" )].spec.scaleTargetRef.name")
.findPath("$[?( @.spec.scaleTargetRef.kind == \"ReplicaSet\" || @.spec.scaleTargetRef.kind == \"replicaSet\" )].spec.scaleTargetRef.name")
.type(ArtifactTypes.KUBERNETES_REPLICA_SET)
.type(KubernetesArtifactType.ReplicaSet)
.build();
}
}
Loading