Skip to content

Commit

Permalink
Merge branches 'w/2.9/bugfix/salt-may-return-false' and 'q/3116/2.8/b…
Browse files Browse the repository at this point in the history
…ugfix/salt-may-return-false' into tmp/octopus/q/2.9
  • Loading branch information
bert-e committed Mar 4, 2021
3 parents 1d46f2a + 2c6a811 + 85d740f commit 09d07d6
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 20 deletions.
2 changes: 0 additions & 2 deletions ui/src/containers/NodePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ import { refreshNodesAction, stopRefreshNodesAction } from '../ducks/app/nodes';
import { useRefreshEffect } from '../services/utils';
import NodePageContent from './NodePageContent';
import { PageContainer } from '../components/style/CommonLayoutStyle';
import { fetchNodesIPsInterfaceAction } from '../ducks/app/nodes';
import { fetchAlertsAlertmanagerAction } from '../ducks/app/alerts';
import { getNodeListData } from '../services/NodeUtils';

const NodePage = (props) => {
const dispatch = useDispatch();
useEffect(() => {
dispatch(fetchNodesIPsInterfaceAction());
dispatch(fetchAlertsAlertmanagerAction());
}, [dispatch]);
useRefreshEffect(refreshNodesAction, stopRefreshNodesAction);
Expand Down
17 changes: 10 additions & 7 deletions ui/src/ducks/app/nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,6 @@ export const stopRefreshNodesAction = () => {
return { type: STOP_REFRESH_NODES };
};

export const fetchNodesIPsInterfaceAction = () => {
return { type: FETCH_NODES_IPS_INTERFACES };
};

export const updateNodesIPsInterfacesAction = (payload) => {
return { type: UPDATE_NODES_IPS_INTERFACES, payload };
};
Expand All @@ -261,7 +257,7 @@ export const updateNodeObjectAction = (payload) => {
export const clusterVersionSelector = (state) => state.app.nodes.clusterVersion;
export const nodesRefreshingSelector = (state) => state.app.nodes.isRefreshing;
export const isSaltAPIAuthenticatedSelector = (state) => state.login.salt;

const nodeListSelector = (state) => state.app.nodes.list;
// Sagas
export function* fetchClusterVersion() {
const result = yield call(CoreApi.getKubeSystemNamespace);
Expand Down Expand Up @@ -341,6 +337,7 @@ export function* fetchNodes() {
}
yield delay(1000); // To make sure that the loader is visible for at least 1s
yield put(updateNodesAction({ isLoading: false }));
yield call(fetchNodesIPsInterface);
return result;
}

Expand Down Expand Up @@ -484,19 +481,25 @@ export function* stopRefreshNodes() {

export function* fetchNodesIPsInterface() {
let result;

const nodeList = yield select(nodeListSelector);
const nodeNames = nodeList.map((node) => {
return node.name;
});

// Check if Salt API is already authenticated
// If not, wait for the CONNECT_SALT_API action.
const isSaltAPIAuthenticated = yield select(isSaltAPIAuthenticatedSelector);
if (isSaltAPIAuthenticated) {
result = yield call(ApiSalt.getNodesIPsInterfaces);
result = yield call(ApiSalt.getNodesIPsInterfaces, nodeNames);
} else {
// eslint-disable-next-line no-unused-vars
const { success, failure, timeout } = yield race({
success: take(CONNECT_SALT_API),
timeout: delay(5000),
});
if (success) {
result = yield call(ApiSalt.getNodesIPsInterfaces);
result = yield call(ApiSalt.getNodesIPsInterfaces, nodeNames);
}
//TODO: We're missing proper error-handling here.
}
Expand Down
2 changes: 2 additions & 0 deletions ui/src/ducks/app/nodes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
CREATE_NODE_FAILED,
clusterVersionSelector,
nodesRefreshingSelector,
fetchNodesIPsInterface,
} from './nodes';
import { allJobsSelector } from './salt';
import {
Expand Down Expand Up @@ -127,6 +128,7 @@ describe('`fetchNodes` saga', () => {
expect(_gen.next().value).toEqual(
put({ type: UPDATE_NODES, payload: { isLoading: false } }),
);
expect(_gen.next().value).toEqual(call(fetchNodesIPsInterface));
expect(_gen.next().done).toBe(true);
};

Expand Down
19 changes: 16 additions & 3 deletions ui/src/services/NodeUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
API_STATUS_DEPLOYING,
} from '../constants';
import { compareHealth } from './utils';
import type { IPInterfaces } from './salt/api';

const METALK8S_CONTROL_PLANE_IP = 'metalk8s:control_plane_ip';
const METALK8S_WORKLOAD_PLANE_IP = 'metalk8s:workload_plane_ip';
Expand Down Expand Up @@ -138,10 +139,22 @@ export const getNodeListData = createSelector(
// }
// Return
// {
// control_plane: { ip: '10.0.1.42', interface: 'eth1'}
// workload_plane: { ip: '10.100.0.2', interface: 'eth3'},
// controlPlane: { ip: '10.0.1.42', interface: 'eth1'}
// workloadPlane: { ip: '10.100.0.2', interface: 'eth3'},
// }
export const nodesCPWPIPsInterface = (IPsInterfacesObject) => {
export const nodesCPWPIPsInterface = (
IPsInterfacesObject: IPInterfaces | boolean,
): {
controlPlane: { ip: string, interface: string },
workloadPlane: { ip: string, interface: string },
} => {
if (!IPsInterfacesObject) {
return {
controlPlane: { ip: '', interface: '' },
workloadPlane: { ip: '', interface: '' },
};
}

return {
controlPlane: {
ip: IPsInterfacesObject[METALK8S_CONTROL_PLANE_IP],
Expand Down
43 changes: 35 additions & 8 deletions ui/src/services/salt/api.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//@flow
import { User } from 'oidc-client';
import ApiClient from '../ApiClient';

let saltApiClient = null;
Expand All @@ -6,7 +8,7 @@ export function getClient() {
return saltApiClient;
}

export function initialize(apiUrl) {
export function initialize(apiUrl: string) {
saltApiClient = new ApiClient({ apiUrl });
}

Expand All @@ -23,16 +25,22 @@ export type SaltToken = {
],
};

export function authenticate(user): Promise<SaltToken> {
var payload = {
export async function authenticate(user: User): Promise<SaltToken> {
if (!saltApiClient) {
throw new Error('Salt api client should be defined.');
}
const payload = {
eauth: 'kubernetes_rbac',
username: `oidc:${user.profile.email}`,
token: user.id_token,
};
return saltApiClient.post('/login', payload);
}

export async function deployNode(node, version) {
export async function deployNode(node: string, version: string) {
if (!saltApiClient) {
throw new Error('Salt api client should be defined.');
}
return saltApiClient.post('/', {
client: 'runner_async',
fun: 'state.orchestrate',
Expand All @@ -44,15 +52,21 @@ export async function deployNode(node, version) {
});
}

export async function printJob(jid) {
export async function printJob(jid: string) {
if (!saltApiClient) {
throw new Error('Salt api client should be defined.');
}
return saltApiClient.post('/', {
client: 'runner',
fun: 'jobs.print_job',
arg: [jid],
});
}

export async function prepareEnvironment(environment, version) {
export async function prepareEnvironment(environment: string, version: string) {
if (!saltApiClient) {
throw new Error('Salt api client should be defined.');
}
return saltApiClient.post('/', {
client: 'runner_async',
fun: 'state.orchestrate',
Expand All @@ -64,10 +78,23 @@ export async function prepareEnvironment(environment, version) {
});
}

export async function getNodesIPsInterfaces() {
export type IPInterfaces = {
'metalk8s:control_plane_ip': string,
'metalk8s:workload_plane_ip': string,
ip_interfaces: { [interface: string]: string[] },
};
export async function getNodesIPsInterfaces(
nodeNames: string[],
): Promise<{
return: [{ [nodeName: string]: boolean | IPInterfaces }],
}> {
if (!saltApiClient) {
throw new Error('Salt api client should be defined.');
}
return saltApiClient.post('/', {
client: 'local',
tgt: '*',
tgt: nodeNames.join(','),
tgt_type: 'list',
fun: 'grains.item',
arg: [
'metalk8s:control_plane_ip',
Expand Down

0 comments on commit 09d07d6

Please sign in to comment.