/**
 * 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 { ResourceCollection , Deployment , ClusterRoleBinding , ServiceAccount , Secret } from "@opstrace/kubernetes" ; import { KubeConfig } from "@kubernetes/client-node" ; import { generateSecretValue } from "../helpers" ; import { getImagePullSecrets , dockerHubCredsSecret } from "./dockerhub" ; export const CONTROLLER_NAME = "opstrace-controller" ; export const CONTROLLER_NAMESPACE = "kube-system" ; export function ControllerResources ( { controllerImage , opstraceClusterName , kubeConfig } ) { const collection = new ResourceCollection ( ) ; const namespace = CONTROLLER_NAMESPACE ; const name = CONTROLLER_NAME ; const controllerCmdlineArgs = [ `${ opstraceClusterName }` ] ; collection . add ( dockerHubCredsSecret ( kubeConfig ) ) ; const hasuraAdminSecret = new Secret ( { apiVersion : "v1" , data : { HASURA_ADMIN_SECRET : Buffer . from ( generateSecretValue ( ) ) . toString ( "base64" ) } , kind : "Secret" , metadata : { name : "hasura-admin-secret" , namespace : namespace } } , kubeConfig ) ; hasuraAdminSecret . setImmutable ( ) ; collection . add ( hasuraAdminSecret ) ; collection . add ( new Deployment ( { apiVersion : "apps/v1" , kind : "Deployment" , metadata : { name , namespace , labels : { "k8s-app" : "opstrace-controller" } } , spec : { replicas : 1 , strategy : { type : "Recreate" } , selector : { matchLabels : { name } } , template : { metadata : { labels : { name } } , spec : { imagePullSecrets : getImagePullSecrets ( ) , serviceAccountName : "opstrace-controller" , containers : [ { name : "opstrace-controller" , image : `${ controllerImage }` , imagePullPolicy : "IfNotPresent" , command : [ "node" , "./cmd.js" ] , args : controllerCmdlineArgs , resources : { limits : { cpu : "1" , memory : "1Gi" } , requests : { cpu : "0.5" , memory : "500Mi" } } , ports : [ { name : "metrics" , containerPort : 8900 } , { name : "readiness" , containerPort : 9000 } ] , env : [ { name : "HASURA_GRAPHQL_ADMIN_SECRET" , valueFrom : { secretKeyRef : { name : "hasura-admin-secret" , key : "HASURA_ADMIN_SECRET" } } } , { name : "GRAPHQL_ENDPOINT" , value : "http://graphql.application.svc.cluster.local:8080/v1/graphql" } , { name : "ALERTMANAGER_ENDPOINT" , value : "http://alertmanager.cortex.svc.cluster.local/api/v1/alerts" } , { name : "RULER_ENDPOINT" , value : "http://ruler.cortex.svc.cluster.local/api/v1/rules" } ] , readinessProbe : { httpGet : { path : "/ready" , port : "readiness" as any , scheme : "HTTP" } } } ] } } } } , kubeConfig ) ) ; collection . add ( new ServiceAccount ( { apiVersion : "v1" , kind : "ServiceAccount" , metadata : { name : "opstrace-controller" , namespace } } , kubeConfig ) ) ; collection . add ( new ClusterRoleBinding ( { apiVersion : "rbac.authorization.k8s.io/v1" , kind : "ClusterRoleBinding" , metadata : { name : "opstrace-controller-clusteradmin-binding" } , roleRef : { apiGroup : "rbac.authorization.k8s.io" , kind : "ClusterRole" , name : "cluster-admin" } , subjects : [ { kind : "ServiceAccount" , name : "opstrace-controller" , namespace } ] } , kubeConfig ) ) ; return collection ; }