Skip to content

Commit

Permalink
ui: Add pods reducer typing
Browse files Browse the repository at this point in the history
Refs: #2999
  • Loading branch information
JBWatenbergScality committed Jan 18, 2021
1 parent f46450a commit 1420c1f
Showing 1 changed file with 28 additions and 7 deletions.
35 changes: 28 additions & 7 deletions ui/src/ducks/app/pods.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { call, put, takeLatest } from 'redux-saga/effects';
//@flow
import { V1ContainerStatus, V1Pod, V1PodList } from '@kubernetes/client-node/dist/gen/model/models';
import { call, put, takeLatest, Effect } from 'redux-saga/effects';
import * as CoreApi from '../../services/k8s/core';
import type {APIResult} from '../../types';

// Actions
const FETCH_PODS = 'FETCH_PODS';
Expand All @@ -10,7 +13,25 @@ const defaultState = {
list: [],
};

export default function reducer(state = defaultState, action = {}) {
export type Pod = {
name: string,
namespace: string,
nodeName: string,
status: string,
startTime: string,
restartCount: number,
volumes: {
name: string,
persistentVolumeClaim: string,
}[],
containerStatuses: V1ContainerStatus[],
}

export type PodsState = {
list: Pod[]
}

export default function reducer(state: PodsState = defaultState, action: any = {}) {
switch (action.type) {
case SET_PODS:
return { ...state, list: action.payload };
Expand All @@ -24,17 +45,17 @@ export const fetchPodsAction = () => {
return { type: FETCH_PODS };
};

export const setPodsAction = (payload) => {
export const setPodsAction = (payload: Pod[]) => {
return { type: SET_PODS, payload };
};

// Sagas
export function* fetchPods() {
export function* fetchPods(): Generator<Effect, void, APIResult<V1PodList>> {
const result = yield call(CoreApi.getPods);
if (!result.error) {
yield put(
setPodsAction(
result.body.items.map((pod) => ({
result.body.items.map((pod: V1Pod) => ({
name: pod.metadata.name,
namespace: pod.metadata.namespace,
nodeName: pod.spec.nodeName,
Expand All @@ -44,7 +65,7 @@ export function* fetchPods() {
pod.status.containerStatuses && pod.status.containerStatuses.length
? pod.status.containerStatuses[0].restartCount
: 0,
volumes: pod?.spec?.volumes?.map((volume) => ({
volumes: (pod?.spec?.volumes ?? []).map((volume) => ({
name: volume.name,
persistentVolumeClaim: volume?.persistentVolumeClaim?.claimName,
})),
Expand All @@ -55,6 +76,6 @@ export function* fetchPods() {
}
}

export function* podsSaga() {
export function* podsSaga(): Generator<void, void, void> {
yield takeLatest(FETCH_PODS, fetchPods);
}

0 comments on commit 1420c1f

Please sign in to comment.