summaryrefslogtreecommitdiffstats
path: root/multisite-init-container/buildconf.py
blob: 774f407b88a1e13789cdf4ab2973178f26c26188 (plain)
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
# ================================================================================
# Copyright (c) 2019 AT&T Intellectual Property. All rights reserved.
# ================================================================================
# 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.
# ============LICENSE_END=========================================================
#
# Extract the API address and credentials provided
# to the container by Kubernetes and push it into a
# configmap that can be shared by other components
# and that can be augmented with addresses and credentials
# of remote Kubernetes clusters.

from kubernetes import client, config
import yaml
import string
import base64

# Default values for parameters
K8S_CENTRAL_LOCATION_ID = "central"                 # Used as cluster and context name in kubeconfig
K8S_USER = "user00"                                 # Used as user name in kubeconfig
CONFIG_MAP_NAME = "multisite-kubeconfig-configmap"    # Name of the existing ConfigMap that receives the kubeconfig
CONFIG_MAP_KEY = "kubeconfig"                       # Key in ConfigMap where kubeconfig is stored

def _get_config():
    ''' Get API access configuration as provided by k8s '''
    config.load_incluster_config()
    cfg = client.Configuration._default

    token = cfg.api_key['authorization'].split(' ')[1]
    server = cfg.host
    with open(cfg.ssl_ca_cert, 'r') as f:
        ca_cert = f.read().strip()

    ca_cert_string = base64.standard_b64encode(ca_cert.encode('utf-8'))

    return token, server, ca_cert_string

def _build_kubeconfig(location, kuser):
    ''' Build content of a kubeconfig file using the access info provided by k8s '''

    token, server, ca_cert = _get_config()
    cluster = {"name": location, "cluster": {"server": server, "certificate-authority-data": ca_cert}}
    user = {"name": kuser, "user": {"token": token}}
    context = {"name": location, "context": {"cluster": location, "user": kuser}}

    kubeconfig = {"apiVersion": "v1", "kind": "Config", "preferences": {}, "current-context": location}
    kubeconfig["clusters"] = [cluster]
    kubeconfig["users"] = [user]
    kubeconfig["contexts"] = [context]

    return kubeconfig

def update_kubeconfig_config_map(namespace, config_map_name, key, kubeconfig):
    body = client.V1ConfigMap(data={key: yaml.safe_dump(kubeconfig)})
    client.CoreV1Api().patch_namespaced_config_map(config_map_name, namespace, body)

def create_kubeconfig_file(location, user, config_file):
    ''' Write a kubeconfig file using the API access info provided by k8s '''
    with open(config_file, 'w') as f:
        yaml.safe_dump(_build_kubeconfig(location, user), f)

if __name__ == "__main__":
    from optparse import OptionParser
    parser = OptionParser()
    parser.add_option("-l", "--location", dest="location", help="Name of central location", default=K8S_CENTRAL_LOCATION_ID)
    parser.add_option("-u", "--user", dest="user", help="Username", default=K8S_USER)
    parser.add_option("-n", "--namespace", dest="namespace", help="Target namespace")
    parser.add_option("-c", "--configmap", dest="configmap", help= "ConfigMap name", default=CONFIG_MAP_NAME)
    parser.add_option("-k", "--key", dest="key", help="ConfigMap key (filename when ConfigMap is mounted)", default=CONFIG_MAP_KEY)
    (options, args) = parser.parse_args()
    kubeconfig = _build_kubeconfig(options.location, options.user)
    update_kubeconfig_config_map(options.namespace,options.configmap,options.key, kubeconfig)
} /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */ }
# ================================================================================
# Copyright (c) 2020 Nokia. All rights reserved.
# ================================================================================
# 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.
# ============LICENSE_END=========================================================
#
swagger: '2.0'
info:
  description: Api Documentation
  version: '1.5.4'
  title: Api Documentation
  termsOfService: 'urn:tos'
  contact: {}
  license:
    name: Apache 2.0
    url: 'http://www.apache.org/licenses/LICENSE-2.0'
host: 'localhost:8443'
basePath: /
tags:
  - name: basic-error-controller
    description: Basic Error Controller
  - name: ves-rest-controller
    description: Ves Rest Controller
paths:
  /:
    get:
      tags:
        - ves-rest-controller
      summary: mainPage
      operationId: mainPageUsingGET
      produces:
        - '*/*'
      responses:
        '200':
          description: OK
          schema:
            type: string
        '401':
          description: Unauthorized
        '403':
          description: Forbidden
        '404':
          description: Not Found
  /error:
    get:
      tags:
        - basic-error-controller
      summary: errorHtml
      operationId: errorHtmlUsingGET
      produces:
        - text/html
      responses:
        '200':
          description: OK
          schema:
            $ref: '#/definitions/ModelAndView'
        '401':
          description: Unauthorized
        '403':
          description: Forbidden
        '404':
          description: Not Found
    head:
      tags:
        - basic-error-controller
      summary: errorHtml
      operationId: errorHtmlUsingHEAD
      consumes:
        - application/json
      produces:
        - text/html
      responses:
        '200':
          description: OK
          schema:
            $ref: '#/definitions/ModelAndView'
        '204':
          description: No Content
        '401':
          description: Unauthorized
        '403':
          description: Forbidden
    post:
      tags:
        - basic-error-controller
      summary: errorHtml
      operationId: errorHtmlUsingPOST
      consumes:
        - application/json
      produces:
        - text/html
      responses:
        '200':
          description: OK
          schema:
            $ref: '#/definitions/ModelAndView'
        '202':
          description: Accepted
        '401':
          description: Unauthorized
        '403':
          description: Forbidden
        '404':
          description: Not Found
    put:
      tags:
        - basic-error-controller
      summary: errorHtml
      operationId: errorHtmlUsingPUT
      consumes:
        - application/json
      produces:
        - text/html
      responses:
        '200':
          description: OK
          schema:
            $ref: '#/definitions/ModelAndView'
        '202':
          description: Accepted
        '401':
          description: Unauthorized
        '403':
          description: Forbidden
        '404':
          description: Not Found
    delete:
      tags:
        - basic-error-controller
      summary: errorHtml
      operationId: errorHtmlUsingDELETE
      produces:
        - text/html
      responses:
        '200':
          description: OK
          schema:
            $ref: '#/definitions/ModelAndView'
        '204':
          description: No Content
        '401':
          description: Unauthorized
        '403':
          description: Forbidden
    options:
      tags:
        - basic-error-controller
      summary: errorHtml
      operationId: errorHtmlUsingOPTIONS
      consumes:
        - application/json
      produces:
        - text/html
      responses:
        '200':
          description: OK
          schema:
            $ref: '#/definitions/ModelAndView'
        '204':
          description: No Content
        '401':
          description: Unauthorized
        '403':
          description: Forbidden
    patch:
      tags:
        - basic-error-controller
      summary: errorHtml
      operationId: errorHtmlUsingPATCH
      consumes:
        - application/json
      produces:
        - text/html
      responses:
        '200':
          description: OK
          schema:
            $ref: '#/definitions/ModelAndView'
        '204':
          description: No Content
        '401':
          description: Unauthorized
        '403':
          description: Forbidden
  /eventListener/v1:
    post:
      tags:
        - ves-rest-controller
      summary: receiveEvent
      operationId: receiveEventUsingPOST
      consumes:
        - application/json
      produces:
        - '*/*'
      parameters:
        - in: body
          name: jsonPayload
          description: jsonPayload
          required: true
          schema:
            type: string
      responses:
        '200':
          description: OK
          schema:
            type: string
        '202':
          description: Accepted
        '401':
          description: Unauthorized
        '403':
          description: Forbidden
        '404':
          description: Not Found
  /eventListener/v1/eventBatch:
    post:
      tags:
        - ves-rest-controller
      summary: receiveEvent
      operationId: receiveEventUsingPOST_1
      consumes:
        - application/json
      produces:
        - '*/*'
      parameters:
        - in: body
          name: jsonPayload
          description: jsonPayload
          required: true
          schema:
            type: string
      responses:
        '200':
          description: OK
          schema:
            type: string
        '202':
          description: Accepted
        '401':
          description: Unauthorized
        '403':
          description: Forbidden
        '404':
          description: Not Found
  /eventListener/v2:
    post:
      tags:
        - ves-rest-controller
      summary: receiveEvent
      operationId: receiveEventUsingPOST_2
      consumes:
        - application/json
      produces:
        - '*/*'
      parameters:
        - in: body
          name: jsonPayload
          description: jsonPayload
          required: true
          schema:
            type: string
      responses:
        '200':
          description: OK
          schema:
            type: string
        '202':
          description: Accepted
        '401':
          description: Unauthorized
        '403':
          description: Forbidden
        '404':
          description: Not Found
  /eventListener/v2/eventBatch:
    post:
      tags:
        - ves-rest-controller
      summary: receiveEvent
      operationId: receiveEventUsingPOST_3
      consumes:
        - application/json
      produces:
        - '*/*'
      parameters:
        - in: body
          name: jsonPayload
          description: jsonPayload
          required: true
          schema:
            type: string
      responses:
        '200':
          description: OK
          schema:
            type: string
        '202':
          description: Accepted
        '401':
          description: Unauthorized
        '403':
          description: Forbidden
        '404':
          description: Not Found
  /eventListener/v3:
    post:
      tags:
        - ves-rest-controller
      summary: receiveEvent
      operationId: receiveEventUsingPOST_4
      consumes:
        - application/json
      produces:
        - '*/*'
      parameters:
        - in: body
          name: jsonPayload
          description: jsonPayload
          required: true
          schema:
            type: string
      responses:
        '200':
          description: OK
          schema:
            type: string
        '202':
          description: Accepted
        '401':
          description: Unauthorized
        '403':
          description: Forbidden
        '404':
          description: Not Found
  /eventListener/v3/eventBatch:
    post:
      tags:
        - ves-rest-controller
      summary: receiveEvent
      operationId: receiveEventUsingPOST_5
      consumes:
        - application/json
      produces:
        - '*/*'
      parameters:
        - in: body
          name: jsonPayload
          description: jsonPayload
          required: true
          schema:
            type: string
      responses:
        '200':
          description: OK
          schema:
            type: string
        '202':
          description: Accepted
        '401':
          description: Unauthorized
        '403':
          description: Forbidden
        '404':
          description: Not Found
  /eventListener/v4:
    post:
      tags:
        - ves-rest-controller
      summary: receiveEvent
      operationId: receiveEventUsingPOST_6
      consumes:
        - application/json
      produces:
        - '*/*'
      parameters:
        - in: body
          name: jsonPayload
          description: jsonPayload
          required: true
          schema:
            type: string
      responses:
        '200':
          description: OK
          schema:
            type: string
        '202':
          description: Accepted
        '401':
          description: Unauthorized
        '403':
          description: Forbidden
        '404':
          description: Not Found
  /eventListener/v4/eventBatch:
    post:
      tags:
        - ves-rest-controller
      summary: receiveEvent
      operationId: receiveEventUsingPOST_7
      consumes:
        - application/json
      produces:
        - '*/*'
      parameters:
        - in: body
          name: jsonPayload
          description: jsonPayload
          required: true
          schema:
            type: string
      responses:
        '200':
          description: OK
          schema:
            type: string
        '202':
          description: Accepted
        '401':
          description: Unauthorized
        '403':
          description: Forbidden
        '404':
          description: Not Found
  /eventListener/v5:
    post:
      tags:
        - ves-rest-controller
      summary: receiveEvent
      operationId: receiveEventUsingPOST_8
      consumes:
        - application/json
      produces:
        - '*/*'
      parameters:
        - in: body
          name: jsonPayload
          description: jsonPayload
          required: true
          schema:
            type: string
      responses:
        '200':
          description: OK
          schema:
            type: string
        '202':
          description: Accepted
        '401':
          description: Unauthorized
        '403':
          description: Forbidden
        '404':
          description: Not Found
  /eventListener/v5/eventBatch:
    post:
      tags:
        - ves-rest-controller
      summary: receiveEvent
      operationId: receiveEventUsingPOST_9
      consumes:
        - application/json
      produces:
        - '*/*'
      parameters:
        - in: body
          name: jsonPayload
          description: jsonPayload
          required: true
          schema:
            type: string
      responses:
        '200':
          description: OK
          schema:
            type: string
        '202':
          description: Accepted
        '401':
          description: Unauthorized
        '403':
          description: Forbidden
        '404':
          description: Not Found
  /eventListener/v7:
    post:
      tags:
        - ves-rest-controller
      summary: receiveEvent
      operationId: receiveEventUsingPOST_10
      consumes:
        - application/json
      produces:
        - '*/*'
      parameters:
        - in: body
          name: jsonPayload
          description: jsonPayload
          required: true
          schema:
            type: string
      responses:
        '200':
          description: OK
          schema:
            type: string
        '202':
          description: Accepted
        '400':
          description: Bad Request
        '401':
          description: Unauthorized
        '403':
          description: Forbidden
        '404':
          description: Not Found
        '500':
          description: Internal Server Error
  /eventListener/v7/eventBatch:
    post:
      tags:
        - ves-rest-controller
      summary: receiveEvent
      operationId: receiveEventUsingPOST_11
      consumes:
        - application/json
      produces:
        - '*/*'
      parameters:
        - in: body
          name: jsonPayload
          description: jsonPayload
          required: true
          schema:
            type: string
      responses:
        '200':
          description: OK
          schema:
            type: string
        '202':
          description: Accepted
        '400':
          description: Bad Request
        '401':
          description: Unauthorized
        '403':
          description: Forbidden
        '404':
          description: Not Found
        '500':
          description: Internal Server Error
definitions:
  ModelAndView:
    type: object
    properties:
      empty:
        type: boolean
      model:
        type: object
      modelMap:
        type: object
        additionalProperties:
          type: object
      reference:
        type: boolean
      status:
        type: string
        enum:
          - '100'
          - '101'
          - '102'
          - '103'
          - '200'
          - '201'
          - '202'
          - '203'
          - '204'
          - '205'
          - '206'
          - '207'
          - '208'
          - '226'
          - '300'
          - '301'
          - '302'
          - '303'
          - '304'
          - '305'
          - '307'
          - '308'
          - '400'
          - '401'
          - '402'
          - '403'
          - '404'
          - '405'
          - '406'
          - '407'
          - '408'
          - '409'
          - '410'
          - '411'
          - '412'
          - '413'
          - '414'
          - '415'
          - '416'
          - '417'
          - '418'
          - '419'
          - '420'
          - '421'
          - '422'
          - '423'
          - '424'
          - '426'
          - '428'
          - '429'
          - '431'
          - '451'
          - '500'
          - '501'
          - '502'
          - '503'
          - '504'
          - '505'
          - '506'
          - '507'
          - '508'
          - '509'
          - '510'
          - '511'
      view:
        $ref: '#/definitions/View'
      viewName:
        type: string
    title: ModelAndView
  View:
    type: object
    properties:
      contentType:
        type: string
    title: View