/**
 * Copyright 2020 Opstrace, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */ import yaml from "js-yaml" ; import { State } from "./reducer" ; import { LatestControllerConfigType } from "@opstrace/controller-config" ; import { Tenant , Tenants } from "@opstrace/tenants" ; import { log } from "@opstrace/utils" ; import { ConfigMap } from "@opstrace/kubernetes" ; export { generateSecretValue } from "@opstrace/controller-config" ; export const DEVELOPMENT = "development" ; /**
 * currentVersion returns the version of this controller.
 * If running with --development, currentVersion will return "development"
 */ export const currentVersion = ( ) : string => process . env . STACK_VERSION || DEVELOPMENT ; /**
 * iAmDesiredVersion will return true if the version passed in is equal to the version of this controller instance.
 * If running in development, currentVersion will return DEVELOPMENT
 */ export const iAmDesiredVersion = ( version ) : boolean => { if ( currentVersion ( ) === DEVELOPMENT ) { return true ; } return version === currentVersion ( ) ; } ; export const getControllerConfig = ( state ) : LatestControllerConfigType => { if ( state . config . config === undefined ) { throw Error ( "Controller configmap is not present or missing fields" ) ; } return state . config . config ; } ; export type ControllerOverrides = { [ key ] : any ; } ; export function getControllerOverrides ( cm ) { const o = { } ; if ( ! cm . spec . data ) { return o ; } for ( const key of Object . keys ( cm . spec . data ) ) { try { o [ key ] = yaml . load ( cm . spec . data [ key ] ) ; } catch ( e ) { log . warning ( `failed to parse controller config override key=${ key } value=${ cm . spec . data [ key ] }: ${ e }` ) ; } } return o ; } function getControllerConfigOverrides ( state , key ) { if ( state . config . config === undefined ) { throw Error ( "Controller configmap is not present or missing fields" ) ; } const cm = state . kubernetes . cluster . ConfigMaps . resources . find ( cm => cm . namespace === "default" && cm . name === "opstrace-controller-config-overrides" ) ; try { return yaml . load ( cm ?. spec . data ?. [ key ] ?? "" ) ; } catch ( e ) { log . warning ( `failed to parse config overrides: ${ e . message }` ) ; return { } ; } } export const getControllerCortexConfigOverrides = ( state ) : any => { return getControllerConfigOverrides ( state , "cortex" ) ; } ; export const getControllerLokiConfigOverrides = ( state ) : any => { return getControllerConfigOverrides ( state , "loki" ) ; } ; export const toTenantNamespace = ( tenantName ) : string => `${ tenantName }-tenant` ; /**
 * Searches tenants for the matching name, returns the ID.
 */ export const toTenantId = ( tenantName , tenants ) : string | null => { const matches = tenants . filter ( t => t . name === tenantName ) ; if ( matches . length == 1 ) { const match = matches [ 0 ] ; return match . id ? match . id : null ; } else if ( matches . length > 1 ) { log . warning ( "found two tenants with same name=%s: %s" , tenantName , matches ) ; } return null ; } ; /**
 * Searches tenants for the matching ID, returns the name.
 */ export const toTenantName = ( tenantId , tenants ) : string | null => { const matches = tenants . filter ( t => t . id === tenantId ) ; if ( matches . length == 1 ) { return matches [ 0 ] . name ; } else if ( matches . length > 1 ) { log . warning ( "found two tenants with same id=%s: %s" , tenantId , matches ) ; } return null ; } ; export const getTenantNamespace = ( tenant ) : string => toTenantNamespace ( tenant . name ) ; /**
 * Get the queue endpoint.
 */ export const getQueueEndpoint = ( ) : string => "kafka-kafka-bootstrap.kafka.svc.cluster.local:9092" ; param state export const getDomain = ( state ) : string => { const stack = getControllerConfig ( state ) ; if ( stack . custom_dns_name !== undefined ) { return stack . custom_dns_name ; } return `${ stack . name }.${ stack . dnsName . replace ( /\.$/ , "" ) }` ; } ; export const getTenantDomain = ( tenant , state ) : string => `${ tenant . name }.${ getDomain ( state ) }` ; export const getApiDomain = ( api , tenant , state ) : string => `${ api }.${ getTenantDomain ( tenant , state ) }` ; export const getNodeCount = ( state ) : number => state . kubernetes . cluster . Nodes . resources . length ; export const getPrometheusName = ( tenant ) : string => `${ tenant . name }-prometheus` ;