-
Notifications
You must be signed in to change notification settings - Fork 41
Add phase-configs support to Hook model for target-app resolution #1812
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
Open
stiv03
wants to merge
10
commits into
cloudfoundry:master
Choose a base branch
from
stiv03:feature/hook-phase-target-app
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b272617
Add phase-configs target-app resolution in ExecuteTaskStep for blue-g…
stiv03 8e768cd
Fix phase-configs target-app resolution for multi-phase hooks
stiv03 ceef42a
Fix phase-configs target-app routing when live app has no color suffix
stiv03 eab5ad4
Add phases-config hook parameter support with duplicate phase validation
stiv03 9165d29
Refactoring
stiv03 aab7ff0
Extract hook task target app resolution into dedicated step
stiv03 695aea6
Fix phases-config target app resolution for cf deploy --strategy blue…
stiv03 26e0d65
Add unit tests for ResolveHookTaskTargetAppStep
stiv03 80b634a
Fix comments
stiv03 13a5600
Log a warning when hooks use the unreachable before-unmap-routes.idle…
stiv03 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
132 changes: 132 additions & 0 deletions
132
...ava/org/cloudfoundry/multiapps/controller/process/steps/ResolveHookTaskTargetAppStep.java
This file contains hidden or 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,132 @@ | ||
| package org.cloudfoundry.multiapps.controller.process.steps; | ||
|
|
||
| import java.text.MessageFormat; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| import jakarta.inject.Named; | ||
| import org.cloudfoundry.multiapps.controller.client.lib.domain.CloudApplicationExtended; | ||
| import org.cloudfoundry.multiapps.controller.core.model.ApplicationColor; | ||
| import org.cloudfoundry.multiapps.controller.core.model.BlueGreenApplicationNameSuffix; | ||
| import org.cloudfoundry.multiapps.controller.core.model.HookPhaseProcessType; | ||
| import org.cloudfoundry.multiapps.controller.core.model.SupportedParameters; | ||
| import org.cloudfoundry.multiapps.controller.process.Messages; | ||
| import org.cloudfoundry.multiapps.controller.process.variables.Variables; | ||
| import org.cloudfoundry.multiapps.mta.model.Hook; | ||
| import org.springframework.beans.factory.config.BeanDefinition; | ||
| import org.springframework.context.annotation.Scope; | ||
|
|
||
| @Named("resolveHookTaskTargetAppStep") | ||
| @Scope(BeanDefinition.SCOPE_PROTOTYPE) | ||
| public class ResolveHookTaskTargetAppStep extends SyncFlowableStep { | ||
|
|
||
| private static final String PHASE_KEY = "phase"; | ||
| private static final String TARGET_APP_KEY = "target-app"; | ||
|
|
||
| @Override | ||
| protected StepPhase executeStep(ProcessContext context) { | ||
| CloudApplicationExtended app = context.getVariable(Variables.APP_TO_PROCESS); | ||
|
|
||
| String resolvedAppName = resolveTargetAppName(context, app); | ||
| if (resolvedAppName == null) { | ||
| context.setVariable(Variables.TASKS_TO_EXECUTE, List.of()); | ||
| return StepPhase.DONE; | ||
| } | ||
| if (!resolvedAppName.equals(app.getName())) { | ||
| context.setVariable(Variables.APP_TO_PROCESS, buildAppWithName(app, resolvedAppName)); | ||
| } | ||
|
|
||
| return StepPhase.DONE; | ||
| } | ||
|
|
||
| private String resolveTargetAppName(ProcessContext context, CloudApplicationExtended app) { | ||
| Hook hook = context.getVariable(Variables.HOOK_FOR_EXECUTION); | ||
| if (hook == null) { | ||
| return app.getName(); | ||
| } | ||
|
|
||
| List<Map<String, String>> phasesConfig = getPhasesConfig(hook); | ||
| if (phasesConfig.isEmpty()) { | ||
| return app.getName(); | ||
| } | ||
|
|
||
| String currentPhase = buildCurrentPhaseString(context, hook); | ||
| String targetApp = phasesConfig.stream() | ||
| .filter(config -> currentPhase.equals(config.get(PHASE_KEY))) | ||
| .map(config -> config.get(TARGET_APP_KEY)) | ||
| .findFirst() | ||
| .orElse(null); | ||
|
|
||
| if (targetApp == null) { | ||
| return app.getName(); | ||
| } | ||
|
|
||
| return resolveAppNameForTarget(context, app, targetApp); | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| private List<Map<String, String>> getPhasesConfig(Hook hook) { | ||
| Object phasesConfigValue = hook.getParameters() | ||
| .get(SupportedParameters.PHASES_CONFIG); | ||
| if (phasesConfigValue instanceof List) { | ||
| return (List<Map<String, String>>) phasesConfigValue; | ||
| } | ||
| return List.of(); | ||
| } | ||
|
|
||
| private String buildCurrentPhaseString(ProcessContext context, Hook hook) { | ||
| List<String> hookExecutionPhases = context.getVariable(Variables.HOOK_EXECUTION_PHASES); | ||
| return hook.getPhases() | ||
| .stream() | ||
| .filter(hookExecutionPhases::contains) | ||
| .findFirst() | ||
| .orElse(""); | ||
| } | ||
|
|
||
| private String resolveAppNameForTarget(ProcessContext context, CloudApplicationExtended app, String targetApp) { | ||
| if (HookPhaseProcessType.HookProcessPhase.LIVE.getType() | ||
| .equals(targetApp)) { | ||
| if (isInitialDeploy(context)) { | ||
| getStepLogger().warn(Messages.SKIPPING_HOOK_TASK_NO_LIVE_APP, app.getName()); | ||
| return null; | ||
| } | ||
| ApplicationColor liveColor = context.getVariable(Variables.LIVE_MTA_COLOR); | ||
| String suffix = liveColor != null ? liveColor.asSuffix() : BlueGreenApplicationNameSuffix.LIVE.asSuffix(); | ||
| return BlueGreenApplicationNameSuffix.removeSuffix(app.getName()) + suffix; | ||
| } | ||
| if (HookPhaseProcessType.HookProcessPhase.IDLE.getType() | ||
| .equals(targetApp)) { | ||
| String baseName = BlueGreenApplicationNameSuffix.removeSuffix(app.getName()); | ||
| ApplicationColor idleColor = context.getVariable(Variables.IDLE_MTA_COLOR); | ||
| if (idleColor != null) { | ||
| return baseName + idleColor.asSuffix(); | ||
| } | ||
| if (isAfterRenamePhase(context)) { | ||
| return baseName; | ||
| } | ||
| return baseName + BlueGreenApplicationNameSuffix.IDLE.asSuffix(); | ||
| } | ||
| return app.getName(); | ||
| } | ||
|
|
||
| private boolean isAfterRenamePhase(ProcessContext context) { | ||
| return context.getVariable(Variables.PHASE) != null; | ||
| } | ||
|
|
||
| private boolean isInitialDeploy(ProcessContext context) { | ||
| return context.getVariable(Variables.DEPLOYED_MTA) == null; | ||
| } | ||
|
|
||
| private CloudApplicationExtended buildAppWithName(CloudApplicationExtended app, String resolvedAppName) { | ||
| return org.cloudfoundry.multiapps.controller.client.lib.domain.ImmutableCloudApplicationExtended.copyOf(app) | ||
| .withName(resolvedAppName); | ||
| } | ||
|
|
||
| @Override | ||
| protected String getStepErrorMessage(ProcessContext context) { | ||
| return MessageFormat.format(Messages.ERROR_EXECUTING_HOOK, | ||
| context.getVariable(Variables.HOOK_FOR_EXECUTION) | ||
| .getName()); | ||
| } | ||
|
|
||
| } | ||
62 changes: 62 additions & 0 deletions
62
...in/java/org/cloudfoundry/multiapps/controller/process/util/HookPhasesConfigValidator.java
This file contains hidden or 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,62 @@ | ||
| package org.cloudfoundry.multiapps.controller.process.util; | ||
|
|
||
| import java.text.MessageFormat; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
|
|
||
| import org.cloudfoundry.multiapps.common.SLException; | ||
| import org.cloudfoundry.multiapps.controller.core.model.HookPhase; | ||
| import org.cloudfoundry.multiapps.controller.core.model.SupportedParameters; | ||
| import org.cloudfoundry.multiapps.controller.process.Messages; | ||
| import org.cloudfoundry.multiapps.mta.model.DeploymentDescriptor; | ||
| import org.cloudfoundry.multiapps.mta.model.Hook; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| public class HookPhasesConfigValidator { | ||
|
|
||
| private static final Logger LOGGER = LoggerFactory.getLogger(HookPhasesConfigValidator.class); | ||
| private static final String PHASE_KEY = "phase"; | ||
|
|
||
| public void validate(DeploymentDescriptor descriptor) { | ||
| descriptor.getModules() | ||
| .stream() | ||
| .flatMap(module -> module.getHooks().stream()) | ||
| .forEach(hook -> { | ||
| warnIfDeprecatedPhaseUsed(hook); | ||
| validateHookHasNoDuplicatePhaseConfigs(hook); | ||
| }); | ||
| } | ||
|
|
||
| private void warnIfDeprecatedPhaseUsed(Hook hook) { | ||
| boolean usesDeprecatedPhase = hook.getPhases() | ||
| .stream() | ||
| .anyMatch(phase -> HookPhase.BLUE_GREEN_APPLICATION_BEFORE_UNMAP_ROUTES_IDLE.getValue() | ||
| .equals(phase)); | ||
| if (usesDeprecatedPhase) { | ||
| LOGGER.warn(Messages.HOOK_PHASE_BLUE_GREEN_BEFORE_UNMAP_ROUTES_IDLE_USED, hook.getName()); | ||
| } | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| private void validateHookHasNoDuplicatePhaseConfigs(Hook hook) { | ||
| Object phasesConfigValue = hook.getParameters().get(SupportedParameters.PHASES_CONFIG); | ||
| if (phasesConfigValue == null) { | ||
| return; | ||
| } | ||
| if (!(phasesConfigValue instanceof List)) { | ||
| throw new SLException(MessageFormat.format(Messages.INVALID_PHASES_CONFIG_NOT_A_LIST, hook.getName())); | ||
| } | ||
| List<Map<String, String>> phasesConfig = (List<Map<String, String>>) phasesConfigValue; | ||
| Set<String> seenPhases = new HashSet<>(); | ||
| for (Map<String, String> phaseConfig : phasesConfig) { | ||
| String phase = phaseConfig.get(PHASE_KEY); | ||
| if (phase != null && !seenPhases.add(phase)) { | ||
| throw new SLException(MessageFormat.format(Messages.DUPLICATE_PHASE_IN_PHASES_CONFIG, phase, hook.getName())); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What will happen if we using hook:
blue-green.application.before-start.idle
with target-app: live
During initial deployment when live app still does not exist?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With the current implementation we hit 404. What’s the right approach here- should the process fail or just log a warning? Also, should the combination of phase: blue-green.application.before-start.idle and target-app: live even be allowed during an initial deployment?