-
Notifications
You must be signed in to change notification settings - Fork 809
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(bakery): Making bakery service selectable by parameters (#2973)
- Using kork v2 selector to shard the bakery based on parameters
- Loading branch information
1 parent
3f2095b
commit 74539b0
Showing
10 changed files
with
768 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
orca-bakery/src/main/groovy/com/netflix/spinnaker/orca/bakery/BakerySelector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
/* | ||
* Copyright 2019 Netflix, 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.orca.bakery; | ||
|
||
import static com.netflix.spinnaker.orca.bakery.BakerySelector.ConfigFields.*; | ||
|
||
import com.netflix.spinnaker.kork.web.selector.v2.SelectableService; | ||
import com.netflix.spinnaker.kork.web.selector.v2.SelectableService.Parameter; | ||
import com.netflix.spinnaker.orca.bakery.api.BakeryService; | ||
import com.netflix.spinnaker.orca.bakery.config.BakeryConfigurationProperties; | ||
import com.netflix.spinnaker.orca.pipeline.model.Execution; | ||
import com.netflix.spinnaker.orca.pipeline.model.Stage; | ||
import java.util.*; | ||
import java.util.function.Function; | ||
|
||
public class BakerySelector { | ||
// Temporary flag in stage context allowing to specify if the bakery should be selectable | ||
private static final String SELECT_BAKERY = "selectBakery"; | ||
|
||
private SelectableService<BakeryService> selectableService; | ||
private final BakeryService defaultService; | ||
private final Map<String, Object> defaultConfig; | ||
|
||
public BakerySelector( | ||
BakeryService defaultBakeryService, | ||
BakeryConfigurationProperties bakeryConfigurationProperties, | ||
Function<String, BakeryService> getBakeryServiceByUrlFx) { | ||
this.defaultService = defaultBakeryService; | ||
this.defaultConfig = getDefaultConfig(bakeryConfigurationProperties); | ||
this.selectableService = | ||
getSelectableService(bakeryConfigurationProperties.getBaseUrls(), getBakeryServiceByUrlFx); | ||
} | ||
|
||
/** | ||
* Selects a bakery based on {@link SelectableFields} from the context | ||
* | ||
* @param stage bake stage | ||
* @return a bakery service with associated configuration | ||
*/ | ||
public SelectableService.SelectedService<BakeryService> select(Stage stage) { | ||
if (selectableService == null | ||
|| stage.getContext().get(SELECT_BAKERY) == null | ||
|| !(Boolean) stage.getContext().get(SELECT_BAKERY)) { | ||
return new SelectableService.SelectedService<>(defaultService, defaultConfig, null); | ||
} | ||
|
||
final String application = stage.getExecution().getApplication(); | ||
final String user = | ||
Optional.ofNullable(stage.getExecution().getAuthentication()) | ||
.map(Execution.AuthenticationDetails::getUser) | ||
.orElse("unknown"); | ||
final List<Parameter> parameters = new ArrayList<>(); | ||
|
||
stage | ||
.getContext() | ||
.forEach( | ||
(key, value) -> { | ||
Optional<String> paramName = | ||
SelectableFields.contextFields.stream().filter(f -> f.equals(key)).findFirst(); | ||
paramName.ifPresent( | ||
name -> | ||
parameters.add( | ||
new Parameter() | ||
.withName(name) | ||
.withValues(Collections.singletonList(value)))); | ||
}); | ||
|
||
parameters.add( | ||
new Parameter() | ||
.withName(SelectableFields.authenticatedUser) | ||
.withValues(Collections.singletonList(user))); | ||
parameters.add( | ||
new Parameter() | ||
.withName(SelectableFields.application) | ||
.withValues(Collections.singletonList(application))); | ||
return selectableService.byParameters(parameters); | ||
} | ||
|
||
private Map<String, Object> getDefaultConfig(BakeryConfigurationProperties properties) { | ||
final Map<String, Object> config = new HashMap<>(); | ||
config.put(roscoApisEnabled, properties.isRoscoApisEnabled()); | ||
config.put(allowMissingPackageInstallation, properties.isAllowMissingPackageInstallation()); | ||
config.put(extractBuildDetails, properties.isExtractBuildDetails()); | ||
return config; | ||
} | ||
|
||
private SelectableService<BakeryService> getSelectableService( | ||
List<SelectableService.BaseUrl> baseUrls, | ||
Function<String, BakeryService> getBakeryServiceByUrlFx) { | ||
if (baseUrls == null) { | ||
return null; | ||
} | ||
|
||
return new SelectableService<>( | ||
baseUrls, defaultService, defaultConfig, getBakeryServiceByUrlFx); | ||
} | ||
|
||
interface SelectableFields { | ||
// Allows to shard the bakery based on the current user | ||
String authenticatedUser = "authenticatedUser"; | ||
|
||
// Allows to shard the bakery based on current application | ||
String application = "application"; | ||
|
||
// Allows to shard the bakery based on common bake stage fields | ||
List<String> contextFields = | ||
Arrays.asList("cloudProvider", "region", "baseOS", "cloudProviderType"); | ||
} | ||
|
||
interface ConfigFields { | ||
String roscoApisEnabled = "roscoApisEnabled"; | ||
String allowMissingPackageInstallation = "allowMissingPackageInstallation"; | ||
String extractBuildDetails = "extractBuildDetails"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
...c/main/groovy/com/netflix/spinnaker/orca/bakery/config/BakeryConfigurationProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright 2019 Netflix, 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.orca.bakery.config; | ||
|
||
import com.netflix.spinnaker.kork.web.selector.v2.SelectableService; | ||
import java.util.List; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@ConfigurationProperties(prefix = "bakery") | ||
public class BakeryConfigurationProperties { | ||
private String baseUrl; | ||
private boolean roscoApisEnabled = false; | ||
private boolean extractBuildDetails = false; | ||
private boolean allowMissingPackageInstallation = false; | ||
private List<SelectableService.BaseUrl> baseUrls; | ||
|
||
public String getBaseUrl() { | ||
return baseUrl; | ||
} | ||
|
||
public List<SelectableService.BaseUrl> getBaseUrls() { | ||
return baseUrls; | ||
} | ||
|
||
public boolean isRoscoApisEnabled() { | ||
return roscoApisEnabled; | ||
} | ||
|
||
public void setBaseUrl(String baseUrl) { | ||
this.baseUrl = baseUrl; | ||
} | ||
|
||
public void setRoscoApisEnabled(boolean roscoApisEnabled) { | ||
this.roscoApisEnabled = roscoApisEnabled; | ||
} | ||
|
||
public void setBaseUrls(List<SelectableService.BaseUrl> baseUrls) { | ||
this.baseUrls = baseUrls; | ||
} | ||
|
||
public boolean isExtractBuildDetails() { | ||
return extractBuildDetails; | ||
} | ||
|
||
public void setExtractBuildDetails(boolean extractBuildDetails) { | ||
this.extractBuildDetails = extractBuildDetails; | ||
} | ||
|
||
public boolean isAllowMissingPackageInstallation() { | ||
return allowMissingPackageInstallation; | ||
} | ||
|
||
public void setAllowMissingPackageInstallation(boolean allowMissingPackageInstallation) { | ||
this.allowMissingPackageInstallation = allowMissingPackageInstallation; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.