Skip to content

Commit

Permalink
Redirect to zerostate if there are only RCs in namespace "kube-system"
Browse files Browse the repository at this point in the history
Show link to RC list in zerostate when there are only RCs in namespace "kube-system"
  • Loading branch information
digitalfishpond committed Feb 23, 2016
1 parent 8b856f6 commit 5761061
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,16 @@ export const stateName = 'replicationcontrollers';

/** Absolute URL of the state. */
export const stateUrl = '/replicationcontrollers';

/**
* Parameters for this state.
*
* All properties are @exported and in sync with URL param names.
*
*/
export class StateParams {
constructor() {
/** @export {boolean} whether there are only RCs in namespace "kube-system" */
this.containsOnlyKubeSystemRCs = false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import {stateName as zerostate} from './zerostate/zerostate_state';
import {stateName as replicationcontrollers} from './replicationcontrollerlist_state';
import {stateUrl as replicationcontrollersUrl} from './replicationcontrollerlist_state';
import {StateParams} from './replicationcontrollerlist_state';
import ReplicationControllerListController from './replicationcontrollerlist_controller';
import ZeroStateController from './zerostate/zerostate_controller';

Expand All @@ -29,6 +30,7 @@ export default function stateConfig($stateProvider) {
controller: ReplicationControllerListController,
controllerAs: 'ctrl',
url: replicationcontrollersUrl,
params: new StateParams(),
resolve: {
'replicationControllers': resolveReplicationControllers,
},
Expand All @@ -47,16 +49,28 @@ export default function stateConfig($stateProvider) {
}

/**
* Avoids entering replication controller list page when there are no replication controllers.
* Used f.e. when last replication controller gets deleted.
* Avoids entering replication controller list page when there are no replication controllers
* or when the only replication controllers are in the kube-system namespace.
* Used f.e. when last replication controller that is not in the kube-system namespace gets
* deleted.
* Transition to: zerostate
* @param {!./replicationcontrollerlist_state.StateParams} $stateParams
* @param {!ui.router.$state} $state
* @param {!angular.$timeout} $timeout
* @param {!backendApi.ReplicationControllerList} replicationControllers
* @ngInject
*/
function redirectIfNeeded($state, $timeout, replicationControllers) {
if (replicationControllers.replicationControllers.length === 0) {
function redirectIfNeeded($state, $stateParams, $timeout, replicationControllers) {
/** @type {boolean} */
let isEmpty = replicationControllers.replicationControllers.length === 0;
// should only display RC list if RCs exist that are not in the kube-system namespace,
// otherwise should redirect to zero state
$stateParams.containsOnlyKubeSystemRCs =
!isEmpty && replicationControllers.replicationControllers.every((rc) => {
return rc.namespace === 'kube-system';
});

if (isEmpty || $stateParams.containsOnlyKubeSystemRCs) {
// allow original state change to finish before redirecting to new state to avoid error
$timeout(() => { $state.go(zerostate); });
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
<md-button ui-sref="deploy" class="md-raised md-primary kd-zerostate-deploy-bt">Deploy an
app
</md-button>
<div></div>
<a class="md-raised md-primary kd-zerostate-deploy-bt" ng-if="ctrl.containsOnlyKubeSystemRCs"
ui-sref="replicationcontrollers">
There are replication controllers in namespace "kube-system" - click to show.
</a>
</md-card-content>
</md-card>
<md-card flex="15" class="kd-zerostate-lm-card">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,19 @@
*/
export default class ZeroStateController {
/**
* @param {!../replicationcontrollerlist_state.StateParams} $stateParams
* @ngInject
*/
constructor() {
constructor($stateParams) {
/** @export {!Array<{title:string, link:string}>} */
this.learnMoreLinks = [
{title: 'Dashboard Tour', link: "#"},
{title: 'Deploying your App', link: "#"},
{title: 'Monitoring your App', link: "#"},
{title: 'Troubleshooting', link: "#"},
];

/** @export {boolean} */
this.containsOnlyKubeSystemRCs = $stateParams.containsOnlyKubeSystemRCs;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@
// limitations under the License.

import ZerostateController from 'replicationcontrollerlist/zerostate/zerostate_controller';
import {StateParams} from 'replicationcontrollerlist/replicationcontrollerlist_state';

describe('Zerostate controller', () => {
let ctrl;

beforeEach(angular.mock.inject(() => { ctrl = new ZerostateController(); }));
/** @type {!StateParams} */
const stateParams = new StateParams();

beforeEach(angular.mock.inject(() => { ctrl = new ZerostateController(stateParams); }));

it('should do something', () => {
expect(ctrl.learnMoreLinks).toEqual([
Expand All @@ -26,5 +30,6 @@ describe('Zerostate controller', () => {
{title: 'Monitoring your App', link: "#"},
{title: 'Troubleshooting', link: "#"},
]);
expect(ctrl.containsOnlyKubeSystemRCs).toEqual(stateParams.containsOnlyKubeSystemRCs);
});
});

0 comments on commit 5761061

Please sign in to comment.