All files / modules/75-ce/components/COGatewayConfig GetK8sYamlSchema.ts

100% Statements 9/9
100% Branches 3/3
100% Functions 2/2
100% Lines 7/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215              6x 6x           6x                                                                                                                                                                                                                                                                                                                                             6x               1x 1x                                             6x  
/*
 * Copyright 2021 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Shield 1.0.0 license
 * that can be found in the licenses directory at the root of this repository, also available at
 * https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt.
 */
 
import { isEmpty as _isEmpty } from 'lodash-es'
import { Utils } from '@ce/common/Utils'
 
interface K8sYamlSchemaProps {
  isEdit: boolean
}
 
const getK8sYamlSchema = ({ isEdit }: K8sYamlSchemaProps) => ({
  $schema: 'http://json-schema.org/draft-07/schema',
  $id: 'http://example.com/example.json',
  type: 'object',
  title: 'The root schema',
  required: ['kind', 'apiVersion', 'metadata', 'spec'],
  properties: {
    kind: {
      $id: '#/properties/kind',
      type: 'string',
      title: 'The kind schema'
    },
    apiVersion: {
      $id: '#/properties/apiVersion',
      type: 'string',
      title: 'The apiVersion schema'
    },
    metadata: {
      $id: '#/properties/metadata',
      type: 'object',
      title: 'The metadata schema',
      required: ['name', 'namespace', 'annotations'],
      properties: {
        name: {
          $id: '#/properties/metadata/properties/name',
          type: 'string',
          title: 'The name schema',
          maxLength: 32,
          readOnly: isEdit
        },
        namespace: {
          $id: '#/properties/metadata/properties/namespace',
          type: 'string',
          title: 'The namespace schema'
        },
        annotations: {
          $id: '#/properties/metadata/properties/annotations',
          type: 'object',
          title: 'The annotations schema',
          required: [],
          additionalProperties: true
        }
      },
      additionalProperties: true
    },
    spec: {
      $id: '#/properties/spec',
      type: 'object',
      title: 'The spec schema',
      description: '',
      required: ['hideProgressPage'],
      properties: {
        service: {
          $id: '#/properties/spec/properties/service',
          type: 'object',
          required: ['name', 'port'],
          properties: {
            name: {
              $id: '#/properties/spec/properties/service/properties/name',
              type: 'string'
            },
            port: {
              $id: '#/properties/spec/properties/service/properties/port',
              type: 'integer'
            }
          }
        },
        ingress: {
          $id: '#/properties/spec/properties/ingress',
          type: 'object',
          required: ['name'],
          properties: {
            name: {
              $id: '#/properties/spec/properties/ingress/properties/name',
              type: 'string'
            },
            controllerName: {
              $id: '#/properties/spec/properties/ingress/properties/controllerName',
              type: 'string'
            }
          }
        },
        idleTimeMins: {
          $id: '#/properties/spec/properties/idleTimeMins',
          type: 'integer',
          title: 'The idleTimeMins schema',
          description: '',
          maximum: 600
        },
        hideProgressPage: {
          $id: '#/properties/spec/properties/hideProgressPage',
          type: 'boolean',
          title: 'The hideProgressPage schema',
          description: ''
        },
        dependencies: {
          $id: '#/properties/spec/properties/dependencies',
          type: 'array',
          title: 'The dependencies schema',
          description: '',
          additionalItems: true,
          items: {
            $id: '#/properties/spec/properties/dependencies/items',
            anyOf: [
              {
                $id: '#/properties/spec/properties/dependencies/items/anyOf/0',
                type: 'object',
                title: 'Dependency Object',
                description: '',
                required: ['selector', 'wait'],
                properties: {
                  selector: {
                    $id: '#/properties/spec/properties/dependencies/items/anyOf/0/properties/selector',
                    type: 'object',
                    title: 'The selector schema',
                    description: '',
                    required: ['ruleName'],
                    properties: {
                      ruleName: {
                        $id: '#/properties/spec/properties/dependencies/items/anyOf/0/properties/selector/properties/ruleName',
                        type: 'string',
                        title: 'The ruleName schema',
                        description: ''
                      }
                    },
                    additionalProperties: true
                  },
                  wait: {
                    $id: '#/properties/spec/properties/dependencies/items/anyOf/0/properties/wait',
                    type: 'integer',
                    title: 'The wait schema',
                    description: ''
                  }
                },
                additionalProperties: true
              }
            ]
          }
        },
        serviceName: {
          $id: '#/properties/spec/properties/serviceName',
          type: 'string',
          title: 'The serviceName schema',
          description: ''
        }
      },
      // oneOf: [
      //   {
      //     required: ['ingress']
      //   },
      //   {
      //     required: ['serviceName']
      //   }
      // ],
      additionalProperties: true
    },
    status: {
      $id: '#/properties/status',
      type: 'object',
      title: 'The status schema',
      required: [],
      additionalProperties: true
    }
  },
  additionalProperties: true
})
 
const getK8sIngressTemplate = ({
  name,
  idleTime,
  cloudConnectorId,
  hideProgressPage,
  deps,
  namespace = 'default'
}: Record<string, any>) => {
  const modifiedName = Utils.getHyphenSpacedString(name)
  return {
    apiVersion: 'ccm.harness.io/v1',
    kind: 'AutoStoppingRule',
    metadata: {
      name: modifiedName,
      namespace,
      annotations: {
        'harness.io/cloud-connector-id': cloudConnectorId
      }
    },
    spec: {
      service: { name: '<replace with your service name>', port: 80 },
      ingress: {
        name: '<replace with ingress name>',
        controllerName: 'nginx'
      },
      idleTimeMins: idleTime,
      hideProgressPage: Boolean(hideProgressPage),
      ...(!_isEmpty(deps) && { dependencies: deps })
    }
  }
}
 
export { getK8sYamlSchema, getK8sIngressTemplate }