|
| 1 | +/** |
| 2 | + * Copyright (C) 2024 Hedera Hashgraph, LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the ""License""); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an ""AS IS"" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + */ |
| 17 | +import * as constants from '../../constants.js'; |
| 18 | +import {SoloError} from '../../errors.js'; |
| 19 | + |
| 20 | +import type {K8} from '../../k8.js'; |
| 21 | +import type {ComponentsDataWrapper} from './components_data_wrapper.js'; |
| 22 | +import {type BaseComponent} from './components/base_component.js'; |
| 23 | + |
| 24 | +/** |
| 25 | + * Static class is used to validate that components in the remote config |
| 26 | + * are present in the kubernetes cluster, and throw errors if there is mismatch. |
| 27 | + */ |
| 28 | +export class RemoteConfigValidator { |
| 29 | + /** |
| 30 | + * Gathers together and handles validation of all components. |
| 31 | + * |
| 32 | + * @param components - components which to validate. |
| 33 | + * @param k8 - to validate the elements. |
| 34 | + * TODO: Make compatible with multi-cluster K8 implementation |
| 35 | + */ |
| 36 | + public static async validateComponents(components: ComponentsDataWrapper, k8: K8): Promise<void> { |
| 37 | + await Promise.all([ |
| 38 | + ...RemoteConfigValidator.validateRelays(components, k8), |
| 39 | + ...RemoteConfigValidator.validateHaProxies(components, k8), |
| 40 | + ...RemoteConfigValidator.validateMirrorNodes(components, k8), |
| 41 | + ...RemoteConfigValidator.validateEnvoyProxies(components, k8), |
| 42 | + ...RemoteConfigValidator.validateConsensusNodes(components, k8), |
| 43 | + ...RemoteConfigValidator.validateMirrorNodeExplorers(components, k8), |
| 44 | + ]); |
| 45 | + } |
| 46 | + |
| 47 | + private static validateRelays(components: ComponentsDataWrapper, k8: K8): Promise<void>[] { |
| 48 | + return Object.values(components.relays).map(async component => { |
| 49 | + try { |
| 50 | + const pods = await k8.getPodsByLabel([constants.SOLO_RELAY_LABEL]); |
| 51 | + |
| 52 | + // to return the generic error message |
| 53 | + if (!pods.length) throw new Error('Pod not found'); |
| 54 | + } catch (e) { |
| 55 | + RemoteConfigValidator.throwValidationError('Relay', component, e); |
| 56 | + } |
| 57 | + }); |
| 58 | + } |
| 59 | + |
| 60 | + private static validateHaProxies(components: ComponentsDataWrapper, k8: K8): Promise<void>[] { |
| 61 | + return Object.values(components.haProxies).map(async component => { |
| 62 | + try { |
| 63 | + const pod = await k8.getPodByName(component.name); |
| 64 | + |
| 65 | + // to return the generic error message |
| 66 | + if (!pod) throw new Error('Pod not found'); |
| 67 | + } catch (e) { |
| 68 | + RemoteConfigValidator.throwValidationError('HaProxy', component, e); |
| 69 | + } |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + private static validateMirrorNodes(components: ComponentsDataWrapper, k8: K8): Promise<void>[] { |
| 74 | + return Object.values(components.mirrorNodes).map(async component => { |
| 75 | + try { |
| 76 | + const pods = await k8.getPodsByLabel(constants.SOLO_HEDERA_MIRROR_IMPORTER); |
| 77 | + |
| 78 | + // to return the generic error message |
| 79 | + if (!pods.length) throw new Error('Pod not found'); |
| 80 | + } catch (e) { |
| 81 | + RemoteConfigValidator.throwValidationError('Mirror node', component, e); |
| 82 | + } |
| 83 | + }); |
| 84 | + } |
| 85 | + |
| 86 | + private static validateEnvoyProxies(components: ComponentsDataWrapper, k8: K8): Promise<void>[] { |
| 87 | + return Object.values(components.envoyProxies).map(async component => { |
| 88 | + try { |
| 89 | + const pod = await k8.getPodByName(component.name); |
| 90 | + |
| 91 | + // to return the generic error message |
| 92 | + if (!pod) throw new Error('Pod not found'); |
| 93 | + } catch (e) { |
| 94 | + RemoteConfigValidator.throwValidationError('Envoy proxy', component, e); |
| 95 | + } |
| 96 | + }); |
| 97 | + } |
| 98 | + |
| 99 | + private static validateConsensusNodes(components: ComponentsDataWrapper, k8: K8): Promise<void>[] { |
| 100 | + return Object.values(components.consensusNodes).map(async component => { |
| 101 | + try { |
| 102 | + const pod = await k8.getPodByName(component.name); |
| 103 | + |
| 104 | + // to return the generic error message |
| 105 | + if (!pod) throw new Error('Pod not found'); |
| 106 | + } catch (e) { |
| 107 | + RemoteConfigValidator.throwValidationError('Consensus node', component, e); |
| 108 | + } |
| 109 | + }); |
| 110 | + } |
| 111 | + |
| 112 | + private static validateMirrorNodeExplorers(components: ComponentsDataWrapper, k8: K8): Promise<void>[] { |
| 113 | + return Object.values(components.mirrorNodeExplorers).map(async component => { |
| 114 | + try { |
| 115 | + const pods = await k8.getPodsByLabel([constants.SOLO_HEDERA_EXPLORER_LABEL]); |
| 116 | + |
| 117 | + // to return the generic error message |
| 118 | + if (!pods.length) throw new Error('Pod not found'); |
| 119 | + } catch (e) { |
| 120 | + RemoteConfigValidator.throwValidationError('Mirror node explorer', component, e); |
| 121 | + } |
| 122 | + }); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Generic handler that throws errors. |
| 127 | + * |
| 128 | + * @param type - name to display in error message |
| 129 | + * @param component - component which is not found in the cluster |
| 130 | + * @param e - original error for the kube client |
| 131 | + */ |
| 132 | + private static throwValidationError(type: string, component: BaseComponent, e: Error | unknown): never { |
| 133 | + throw new SoloError( |
| 134 | + `${type} in remote config with name ${component.name} ` + |
| 135 | + `was not found in namespace: ${component.namespace}, cluster: ${component.cluster}`, |
| 136 | + e, |
| 137 | + {component: component.toObject()}, |
| 138 | + ); |
| 139 | + } |
| 140 | +} |
0 commit comments