Skip to content

Commit

Permalink
feat(gcs): allow reading from GCS to be disabled (#936)
Browse files Browse the repository at this point in the history
  • Loading branch information
lwander authored May 8, 2018
1 parent c5806c0 commit f6dcf25
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@
import com.netflix.spinnaker.halyard.config.model.v1.security.Security;
import com.netflix.spinnaker.halyard.config.problem.v1.ConfigProblemSetBuilder;
import com.netflix.spinnaker.halyard.config.services.v1.VersionsService;
import com.netflix.spinnaker.halyard.core.registry.v1.Versions;
import com.netflix.spinnaker.halyard.core.registry.v1.Versions.Version;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -119,6 +122,11 @@ public DeploymentConfiguration() {}

protected List<String> versionOptions(ConfigProblemSetBuilder psBuilder) {
VersionsService service = psBuilder.getContext().getBean(VersionsService.class);
return service.getVersions().getVersions().stream().map(Version::getVersion).collect(Collectors.toList());
Versions versions = service.getVersions();
if (versions == null) {
return Collections.emptyList();
} else {
return versions.getVersions().stream().map(Version::getVersion).collect(Collectors.toList());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,13 @@ public BillOfMaterials getBillOfMaterials(String version) {
public String getLatestHalyardVersion() {
String result = concurrentMap.get(latestHalyardKey);
if (result == null) {
result = getVersions().getLatestHalyard();
concurrentMap.put(latestHalyardKey, result);
Versions versions = getVersions();
if (versions != null) {
result = versions.getLatestHalyard();
concurrentMap.put(latestHalyardKey, result);
} else {
result = "0.0.0-UNKNOWN";
}
}

return result;
Expand All @@ -99,7 +104,12 @@ public String getRunningHalyardVersion() {
public String getLatestSpinnakerVersion() {
String result = concurrentMap.get(latestSpinnakerKey);
if (result == null) {
result = getVersions().getLatestSpinnaker();
Versions versions = getVersions();
if (versions != null) {
result = versions.getLatestSpinnaker();
} else {
result = "0.0.0-UNKNOWN";
}
concurrentMap.put(latestSpinnakerKey, result);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.netflix.spinnaker.halyard.config.model.v1.node.DeploymentEnvironment;
import com.netflix.spinnaker.halyard.config.model.v1.node.DeploymentEnvironment.DeploymentType;
import com.netflix.spinnaker.halyard.config.model.v1.node.Validator;
import com.netflix.spinnaker.halyard.config.problem.v1.ConfigProblemBuilder;
import com.netflix.spinnaker.halyard.config.problem.v1.ConfigProblemSetBuilder;
import com.netflix.spinnaker.halyard.config.services.v1.VersionsService;
import com.netflix.spinnaker.halyard.core.error.v1.HalException;
Expand Down Expand Up @@ -49,9 +50,16 @@ public void validate(ConfigProblemSetBuilder p, DeploymentConfiguration n) {
p.addProblem(Problem.Severity.ERROR, "Timezone " + timezone + " does not match any known canonical timezone ID")
.setRemediation("Pick a timezone from those listed here: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones");
}
}

String version = n.getVersion();
private void validateVersions(ConfigProblemSetBuilder p, DeploymentConfiguration n) {
Versions versions = versionsService.getVersions();
if (versions == null) {
return;
}

String version = n.getVersion();

boolean localGit = n.getDeploymentEnvironment().getType() == DeploymentType.LocalGit;

if (StringUtils.isEmpty(version)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.netflix.spinnaker.halyard.core.provider.v1.google.GoogleCredentials;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.yaml.snakeyaml.Yaml;
Expand All @@ -40,6 +41,7 @@

@Component
@Slf4j
@ConditionalOnProperty("spinnaker.config.input.gcs.enabled")
public class GoogleProfileReader implements ProfileReader {
@Autowired
String spinconfigBucket;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
package com.netflix.spinnaker.halyard.core.registry.v1;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.netflix.spinnaker.halyard.core.error.v1.HalException;
import com.netflix.spinnaker.halyard.core.problem.v1.Problem;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
Expand All @@ -29,7 +31,7 @@
@Component
@Slf4j
public class ProfileRegistry {
@Autowired
@Autowired(required = false)
GoogleProfileReader googleProfileReader;

@Autowired
Expand All @@ -53,8 +55,12 @@ public BillOfMaterials readBom(String version) throws IOException {
}

public Versions readVersions() throws IOException {
// git can't store these
return googleProfileReader.readVersions();
if (googleProfileReader == null) {
return null;
} else {
// git can't store these
return googleProfileReader.readVersions();
}
}

public InputStream readArchiveProfile(String artifactName, String version, String profileName) throws IOException {
Expand All @@ -66,8 +72,10 @@ private ProfileReader pickProfileReader(String version) {
return gitProfileReader;
} else if (Versions.isLocal(version)) {
return localDiskProfileReader;
} else {
} else if (googleProfileReader != null) {
return googleProfileReader;
} else {
throw new HalException(Problem.Severity.FATAL, "No profile reader exists to read '" + version + "'. Consider setting 'spinnaker.config.input.gcs.enabled: true' in /opt/spinnaker/config/halyard.yml");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
import com.netflix.spinnaker.halyard.config.services.v1.VersionsService;
import com.netflix.spinnaker.halyard.core.error.v1.HalException;
import com.netflix.spinnaker.halyard.core.registry.v1.BillOfMaterials;
import com.netflix.spinnaker.halyard.core.registry.v1.GoogleWriteableProfileRegistry;
import com.netflix.spinnaker.halyard.core.registry.v1.Versions;
import com.netflix.spinnaker.halyard.core.registry.v1.Versions.Version;
import com.netflix.spinnaker.halyard.core.registry.v1.GoogleWriteableProfileRegistry;
import com.netflix.spinnaker.halyard.deploy.spinnaker.v1.SpinnakerArtifact;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -87,13 +87,20 @@ private void deleteVersion(Versions versionsCollection, String version) {
.collect(Collectors.toList()));
}

private static final String BAD_CONFIG_FORMAT = "You need to set '%s: true' in /opt/spinnaker/config/halyard-local.yml to perform this admin feature";
private static final String NO_WRITER_ENABLED = String.format(BAD_CONFIG_FORMAT, "spinnaker.config.input.writerEnabled");
private static final String NO_GCS_ENABLED = String.format(BAD_CONFIG_FORMAT, "spinnaker.config.input.gcs.enabled");

public void deprecateVersion(Version version, String illegalReason) {
if (googleWriteableProfileRegistry == null) {
throw new HalException(new ConfigProblemBuilder(FATAL,
"You need to set the \"spinnaker.config.input.writerEnabled\" property to \"true\" to modify your halconfig bucket contents.").build());
throw new HalException(FATAL, NO_WRITER_ENABLED);
}

Versions versionsCollection = versionsService.getVersions();
if (versionsCollection == null) {
throw new HalException(FATAL, NO_GCS_ENABLED);
}

deleteVersion(versionsCollection, version.getVersion());

if (!StringUtils.isEmpty(illegalReason)) {
Expand All @@ -111,11 +118,13 @@ public void deprecateVersion(Version version, String illegalReason) {

public void publishVersion(Version version) {
if (googleWriteableProfileRegistry == null) {
throw new HalException(new ConfigProblemBuilder(FATAL,
"You need to set the \"spinnaker.config.input.writerEnabled\" property to \"true\" to modify your halconfig bucket contents.").build());
throw new HalException(FATAL, NO_WRITER_ENABLED);
}

Versions versionsCollection = versionsService.getVersions();
if (versionsCollection == null) {
throw new HalException(FATAL, NO_GCS_ENABLED);
}
deleteVersion(versionsCollection, version.getVersion());
versionsCollection.getVersions().add(version);

Expand All @@ -124,11 +133,13 @@ public void publishVersion(Version version) {

public void publishLatestSpinnaker(String latestSpinnaker) {
if (googleWriteableProfileRegistry == null) {
throw new HalException(new ConfigProblemBuilder(FATAL,
"You need to set the \"spinnaker.config.input.writerEnabled\" property to \"true\" to modify BOM contents.").build());
throw new HalException(FATAL, NO_WRITER_ENABLED);
}

Versions versionsCollection = versionsService.getVersions();
if (versionsCollection == null) {
throw new HalException(FATAL, NO_GCS_ENABLED);
}
boolean hasLatest = versionsCollection.getVersions().stream().anyMatch(v -> v.getVersion().equals(latestSpinnaker));
if (!hasLatest) {
throw new HalException(FATAL, "Version " + latestSpinnaker + " does not exist in the list of published versions");
Expand All @@ -141,11 +152,13 @@ public void publishLatestSpinnaker(String latestSpinnaker) {

public void publishLatestHalyard(String latestHalyard) {
if (googleWriteableProfileRegistry == null) {
throw new HalException(new ConfigProblemBuilder(FATAL,
"You need to set the \"spinnaker.config.input.writerEnabled\" property to \"true\" to modify BOM contents.").build());
throw new HalException(FATAL, NO_WRITER_ENABLED);
}

Versions versionsCollection = versionsService.getVersions();
if (versionsCollection == null) {
throw new HalException(FATAL, NO_GCS_ENABLED);
}

versionsCollection.setLatestHalyard(latestHalyard);

Expand All @@ -154,8 +167,7 @@ public void publishLatestHalyard(String latestHalyard) {

public void writeBom(String bomPath) {
if (googleWriteableProfileRegistry == null) {
throw new HalException(new ConfigProblemBuilder(FATAL,
"You need to set the \"spinnaker.config.input.writerEnabled\" property to \"true\" to modify BOM contents.").build());
throw new HalException(FATAL, NO_WRITER_ENABLED);
}

BillOfMaterials bom;
Expand Down Expand Up @@ -183,8 +195,7 @@ public void writeBom(String bomPath) {

public void writeArtifactConfig(String bomPath, String artifactName, String profilePath) {
if (googleWriteableProfileRegistry == null) {
throw new HalException(new ConfigProblemBuilder(FATAL,
"You need to set the \"spinnaker.config.input.writerEnabled\" property to \"true\" to modify base-profiles.").build());
throw new HalException(FATAL, NO_WRITER_ENABLED);
}

BillOfMaterials bom;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,13 @@ protected void setProfile(Profile profile, DeploymentConfiguration deploymentCon
bindings.put("timezone", deploymentConfiguration.getTimezone());
bindings.put("version", deploymentConfiguration.getVersion());

Optional<Versions.Version> validatedVersion = versionsService.getVersions().getVersion(version);
Versions versions = versionsService.getVersions();
Optional<Versions.Version> validatedVersion;
if (versions != null) {
validatedVersion = versions.getVersion(version);
} else {
validatedVersion = Optional.empty();
}

if (validatedVersion.isPresent()) {
String changelog = validatedVersion.get().getChangelog();
Expand Down
2 changes: 2 additions & 0 deletions halyard-web/config/halyard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ spinnaker:
docker: gcr.io/spinnaker-marketplace
config:
input:
gcs:
enabled: true
writerEnabled: false
bucket: halconfig

Expand Down

0 comments on commit f6dcf25

Please sign in to comment.