UNPKG

@openshift-assisted/ui-lib

Version:

React component library for the Assisted Installer UI

317 lines (315 loc) 11.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.onEnableCIM = exports.isIngressController = void 0; const tslib_1 = require("tslib"); const react_core_1 = require("@patternfly/react-core"); const utils_1 = require("../../../../common/utils"); const types_1 = require("../../../types"); const utils_2 = require("./utils"); const ASSISTED_IMAGE_SERVICE_ROUTE_PREFIX = 'assisted-image-service-multicluster-engine'; // Since we do not know MCE's namespace, query all routes, // Find assisted-image-service's one and parse domain from there. const getAssistedImageServiceRoute = (t, setError, listResources) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { var _a; let allRoutes; try { allRoutes = (yield listResources({ kind: 'Route', apiVersion: 'route.openshift.io/v1', })); } catch (e) { // console.error('Failed to read all routes: ', allRoutes); setError({ title: t('ai:Failed to save configuration'), message: t('ai:Can not query routes.'), }); return undefined; } const assistedImageServiceRoute = allRoutes === null || allRoutes === void 0 ? void 0 : allRoutes.find((r) => { var _a; return ((_a = r.metadata) === null || _a === void 0 ? void 0 : _a.name) === 'assisted-image-service'; }); if (!((_a = assistedImageServiceRoute === null || assistedImageServiceRoute === void 0 ? void 0 : assistedImageServiceRoute.spec) === null || _a === void 0 ? void 0 : _a.host)) { setError({ title: t('ai:Failed to save configuration'), message: t('ai:Can not find host of the assisted-image-service route'), }); return undefined; } return assistedImageServiceRoute; }); // There are many ways how to get the domain name, let's follow the documentation. const getClusterDomain = (t, setError, assistedImageServiceRoute) => { var _a; const prefix = `${ASSISTED_IMAGE_SERVICE_ROUTE_PREFIX}.`; const host = ((_a = assistedImageServiceRoute.spec) === null || _a === void 0 ? void 0 : _a.host) || ''; const appsDomain = host.substring(host.indexOf(prefix) + prefix.length); // the appsDomain can be prefixed either by "apps." or "nlb-apps." so search for first dot const domain = appsDomain.substring(appsDomain.indexOf('.') + 1); if (!domain) { // It must be present // console.error('Can not find domain in assistedImageServiceRoute: ', assistedImageServiceRoute); setError({ title: t('ai:Can not find cluster domain.'), }); } return domain; }; // Keep this function in sync with getClusterDomain() const patchAssistedImageServiceRoute = (t, setError, patchResource, assistedImageServiceRoute, domain) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { var _b, _c; const newHost = `${ASSISTED_IMAGE_SERVICE_ROUTE_PREFIX}.nlb-apps.${domain}`; const labels = ((_b = assistedImageServiceRoute.metadata) === null || _b === void 0 ? void 0 : _b.labels) || {}; labels['router-type'] = 'nlb'; const patches = [ { op: 'replace', path: '/spec/host', value: newHost, }, { op: ((_c = assistedImageServiceRoute.metadata) === null || _c === void 0 ? void 0 : _c.labels) ? 'replace' : 'add', path: '/metadata/labels', value: labels, }, ]; try { yield patchResource((0, types_1.convertOCPtoCIMResourceHeader)(assistedImageServiceRoute), patches); } catch (e) { // console.error('Failed to patch assisted-image-service route: ', e, patches); setError({ title: t('ai:Failed to patch assisted-image-service route for new domain.'), }); return false; } return true; }); const isIngressController = (getResource) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { try { yield getResource({ apiVersion: 'operator.openshift.io/v1', kind: 'IngressController', metadata: { name: 'ingress-controller-with-nlb', namespace: 'openshift-ingress-operator', }, }); return true; } catch (_d) { return false; } }); exports.isIngressController = isIngressController; const createIngressController = (t, setError, createResource, domain) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { // Assumption: the IngressController resource is not present - ensured at higher level const ingressController = { apiVersion: 'operator.openshift.io/v1', kind: 'IngressController', metadata: { name: 'ingress-controller-with-nlb', namespace: 'openshift-ingress-operator', }, spec: { domain: `nlb-apps.${domain}`, routeSelector: { matchLabels: { 'router-type': 'nlb', }, }, endpointPublishingStrategy: { type: 'LoadBalancerService', loadBalancer: { scope: 'External', providerParameters: { type: 'AWS', aws: { type: 'NLB', }, }, }, }, }, }; try { yield createResource(ingressController); return true; } catch (e) { // console.error('Create IngressController error: ', e); setError({ title: t('ai:Failed to create IngressController'), }); } return false; }); const patchProvisioningConfiguration = ({ t, setError, patchResource, getResource, }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { var _e; try { const provisioning = (yield getResource({ kind: 'Provisioning', apiVersion: 'metal3.io/v1alpha1', metadata: { name: 'provisioning-configuration', }, })); const patches = [ { op: ((_e = provisioning.spec) === null || _e === void 0 ? void 0 : _e.watchAllNamespaces) ? 'replace' : 'add', path: '/spec/watchAllNamespaces', value: true, }, ]; yield patchResource((0, types_1.convertOCPtoCIMResourceHeader)(provisioning), patches); } catch (e) { // console.error('Failed to patch provisioning-configuration: ', e); setError({ title: t('ai:Failed to configure provisioning to enable registering hosts via BMC.'), message: (0, utils_1.getErrorMessage)(e), variant: react_core_1.AlertVariant.warning, }); } }); const createAgentServiceConfig = ({ t, setError, createResource, dbVolSizeGiB, fsVolSizeGiB, imgVolSizeGiB, }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { try { const agentServiceConfig = { apiVersion: 'agent-install.openshift.io/v1beta1', kind: 'AgentServiceConfig', metadata: { name: 'agent', }, spec: { databaseStorage: { accessModes: ['ReadWriteOnce'], resources: { requests: { storage: `${dbVolSizeGiB}Gi`, }, }, }, filesystemStorage: { accessModes: ['ReadWriteOnce'], resources: { requests: { storage: `${fsVolSizeGiB}Gi`, }, }, }, imageStorage: { accessModes: ['ReadWriteOnce'], resources: { requests: { storage: `${imgVolSizeGiB}Gi`, }, }, }, }, }; yield createResource(agentServiceConfig); return true; } catch (e) { // console.error('Failed to create AgentServiceConfig: ', e); setError({ title: t('ai:Failed to create AgentServiceConfig'), message: (0, utils_1.getErrorMessage)(e), }); return false; } }); /* Following functions are tested but recently not used. const patchAgentServiceConfig = async ({ t, setError, patchResource, agentServiceConfig, imgVolSizeGB, }: { t: TFunction; setError: SetErrorFuncType; patchResource: PatchResourceFuncType; agentServiceConfig: AgentServiceConfigK8sResource; imgVolSizeGB: number; }): Promise<boolean> => { try { const patches: ResourcePatch[] = [ { op: 'replace', path: '/spec/imageStorage/resources/requests/storage', value: `${imgVolSizeGB}G`, }, ]; await patchResource(agentServiceConfig, patches); return true; } catch (e) { console.error('Failed to patch AgentServiceConfig: ', e); setError({ title: t('ai:Failed to update the AgentServiceConfig'), }); return false; } }; export const onDeleteCimConfig = async ({ deleteResource, }: { deleteResource: DeleteResourceFuncType; }) => { try { await deleteResource({ apiVersion: 'agent-install.openshift.io/v1beta1', kind: 'AgentServiceConfig', metadata: { name: 'agent', // cluster-scoped resource }, }); } catch (e) { console.error('Failed to delete AgentServiceConfig: ', e); } }; */ // https://access.redhat.com/documentation/en-us/red_hat_advanced_cluster_management_for_kubernetes/2.6/html/multicluster_engine/multicluster_engine_overview#enable-cim const onEnableCIM = ({ t, setError, createResource, getResource, listResources, patchResource, agentServiceConfig, platform, dbVolSizeGiB, fsVolSizeGiB, imgVolSizeGiB, configureLoadBalancer, }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { if (['none', 'baremetal', 'openstack', 'vsphere'].includes(platform.toLocaleLowerCase())) { yield patchProvisioningConfiguration({ t, setError, patchResource, getResource }); } if (agentServiceConfig) { // console.log('The AgentServiceConfig recently can not be patched. Delete and create instead.'); } else { if (!(yield createAgentServiceConfig({ t, setError, createResource, dbVolSizeGiB, fsVolSizeGiB, imgVolSizeGiB, }))) { return false; } } // see isCIMConfigProgressing() window.localStorage.setItem(utils_2.LOCAL_STORAGE_ID_LAST_UPDATE_TIMESTAMP, new Date().toISOString()); if (configureLoadBalancer) { // Recently No to Yes only (since we do not delete the ingress controller) if (yield (0, exports.isIngressController)(getResource)) { // console.log('IngressController already present, we do not patch it.'); return true /* Not an error */; } const assistedImageServiceRoute = yield getAssistedImageServiceRoute(t, setError, listResources); if (!assistedImageServiceRoute) { return false; } const domain = getClusterDomain(t, setError, assistedImageServiceRoute); if (!domain) { return false; } if (!(yield createIngressController(t, setError, createResource, domain)) || !(yield patchAssistedImageServiceRoute(t, setError, patchResource, assistedImageServiceRoute, domain))) { return false; } } return true; }); exports.onEnableCIM = onEnableCIM; //# sourceMappingURL=persist.js.map