Skip to content

Commit

Permalink
refactor(provider/cf): use custom implementation of DeploymentStrateg…
Browse files Browse the repository at this point in the history
…ySelector
  • Loading branch information
Jammy Louie authored and jkschneider committed Nov 26, 2018
1 parent e19c832 commit c8b8456
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 5 deletions.
4 changes: 1 addition & 3 deletions app/scripts/modules/cloudfoundry/src/cf.module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { module } from 'angular';

import { CloudProviderRegistry, DeploymentStrategyRegistry } from '@spinnaker/core';
import { CloudProviderRegistry } from '@spinnaker/core';

import { CLOUD_FOUNDRY_INSTANCE_DETAILS_CTRL } from './instance/details/details.controller';
import { CLOUD_FOUNDRY_LOAD_BALANCER_MODULE } from './loadBalancer/loadBalancer.module';
Expand Down Expand Up @@ -84,5 +84,3 @@ module(CLOUD_FOUNDRY_MODULE, [
},
});
});

DeploymentStrategyRegistry.registerProvider('cloudfoundry', ['redblack']);
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { DeploymentStrategySelector, IDeploymentStrategySelectorState } from '@spinnaker/core';
import { IRedBlackCommand } from 'cloudfoundry/deploymentStrategy/strategies/redblack/redblack.strategy';
import { defaultsDeep, unset } from 'lodash';
import { AdditionalFields } from './strategies/redblack/AdditionalFields';

export class CloudFoundryDeploymentStrategySelector extends DeploymentStrategySelector {
public state: IDeploymentStrategySelectorState = {
strategies: [
{
label: 'None',
description: 'Creates the next server group with no impact on existing server groups',
key: '',
},
{
label: 'Highlander',
description:
'Destroys <i>all</i> previous server groups in the cluster as soon as new server group passes health checks',
key: 'highlander',
},
{
label: 'Red/Black',
description:
'Disables <i>all</i> previous server groups in the cluster as soon as new server group passes health checks',
key: 'redblack',
additionalFields: ['maxRemainingAsgs'],
AdditionalFieldsComponent: AdditionalFields,
initializationMethod: (command: IRedBlackCommand) => {
defaultsDeep(command, {
rollback: {
onFailure: false,
},
maxRemainingAsgs: 2,
delayBeforeDisableSec: 0,
delayBeforeScaleDownSec: 0,
scaleDown: false,
});
},
},
],
currentStrategy: null,
AdditionalFieldsComponent: undefined,
};

public selectStrategy(strategy: string): void {
const { command, onStrategyChange } = this.props;
const oldStrategy = this.state.strategies.find(s => s.key === this.state.currentStrategy);
const newStrategy = this.state.strategies.find(s => s.key === strategy);

if (oldStrategy && oldStrategy.additionalFields) {
oldStrategy.additionalFields.forEach(field => {
if (!newStrategy || !newStrategy.additionalFields || !newStrategy.additionalFields.includes(field)) {
unset(command, field);
}
});
}

let AdditionalFieldsComponent;
if (newStrategy) {
AdditionalFieldsComponent = newStrategy.AdditionalFieldsComponent;
if (newStrategy.initializationMethod) {
newStrategy.initializationMethod(command);
}
}
command.strategy = strategy;
if (onStrategyChange && newStrategy) {
onStrategyChange(command, newStrategy);
}
this.setState({ currentStrategy: strategy, AdditionalFieldsComponent });
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import * as React from 'react';
import { set } from 'lodash';

import { HelpField, IDeploymentStrategyAdditionalFieldsProps } from '@spinnaker/core';
import { IRedBlackCommand } from 'cloudfoundry/deploymentStrategy/strategies/redblack/redblack.strategy';

export interface IRedBlackStrategyAdditionalFieldsProps extends IDeploymentStrategyAdditionalFieldsProps {
command: IRedBlackCommand;
}

export class AdditionalFields extends React.Component<IRedBlackStrategyAdditionalFieldsProps> {
private handleChange = (key: string, value: any) => {
set(this.props.command, key, value);
this.forceUpdate();
};

public render() {
const { command } = this.props;
return (
<div className="form-group">
<div className="col-md-12 checkbox">
<label>
<input
type="checkbox"
checked={command.rollback.onFailure}
onChange={e => this.handleChange('rollback.onFailure', e.target.checked)}
/>
Rollback to previous server group if deployment fails <HelpField id="strategy.redblack.rollback" />
</label>
</div>
<div className="col-md-12 form-inline">
<label>
Maximum number of server groups to leave
<HelpField id="strategy.redblack.maxRemainingAsgs" />
</label>
<input
className="form-control input-sm"
style={{ width: '50px' }}
type="number"
value={command.maxRemainingAsgs}
onChange={e => this.handleChange('maxRemainingAsgs', e.target.value)}
min="2"
/>
</div>
<div className="col-md-12 form-inline">
<label>
Wait Before Disable
<HelpField content="Time to wait before disabling old server group" />
</label>
<input
className="form-control input-sm"
style={{ width: '60px' }}
min="0"
type="number"
value={command.delayBeforeDisableSec}
onChange={e => this.handleChange('delayBeforeDisableSec', e.target.value)}
placeholder="0"
/>
seconds
</div>
</div>
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { IServerGroupCommand } from '@spinnaker/core';

export interface IRedBlackCommand extends IServerGroupCommand {
maxRemainingAsgs: number;
delayBeforeDisableSec: number;
delayBeforeScaleDownSec: number;
rollback: {
onFailure: boolean;
};
scaleDown: boolean;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { Field, FormikErrors } from 'formik';
import {
AccountSelectField,
AccountService,
DeploymentStrategySelector,
IAccount,
IRegion,
IWizardPageProps,
Expand All @@ -16,6 +15,7 @@ import {
} from '@spinnaker/core';

import { ICloudFoundryCreateServerGroupCommand } from '../../serverGroupConfigurationModel.cf';
import { CloudFoundryDeploymentStrategySelector } from 'cloudfoundry/deploymentStrategy/DeploymentStrategySelector';

export type ICloudFoundryServerGroupBasicSettingsProps = IWizardPageProps<ICloudFoundryCreateServerGroupCommand>;

Expand Down Expand Up @@ -122,7 +122,7 @@ class BasicSettingsImpl extends React.Component<
</div>
</div>
{(values.viewState.mode === 'editPipeline' || values.viewState.mode === 'createPipeline') && (
<DeploymentStrategySelector
<CloudFoundryDeploymentStrategySelector
onFieldChange={this.onStrategyFieldChange}
onStrategyChange={this.strategyChanged}
command={values}
Expand Down

0 comments on commit c8b8456

Please sign in to comment.