def FetchConfigMap(parser_args):
    _CoreV1Api = globalvars.get_value('KubCoreV1Api')
    if not _CoreV1Api:
        factory.Factory_InitKubeClient(parser_args.configtype,
                                       parser_args.configfile)
        _CoreV1Api = globalvars.get_value('KubCoreV1Api')
    #print("Fetch config map %s" % parser_args.name)
    try:
        api_response = _CoreV1Api.list_namespaced_config_map(
            parser_args.namespace)
        #print(api_response)
        config_map_items = api_response.items
        for config_map in config_map_items:
            #print("check config_map %s and compare with %s" % (config_map.metadata.name,parser_args.tenant+parser_args.env+parser_args.envtype+"-"+parser_args.name))
            if (config_map.metadata.name == parser_args.tenant +
                    parser_args.env + parser_args.envtype + "-" +
                    parser_args.name) or (config_map.metadata.name
                                          == parser_args.name):
                _output = ""
                for key in config_map.data.keys():
                    if key.find('properties') == -1 and key.find(
                            'xml') == -1 and key.find(
                                'Dockerfile') == -1 and key.find('yaml') == -1:
                        _output = _output + key + "=" + config_map.data[
                            key] + "\n"
                    else:
                        _output = _output + config_map.data[key]
                print(_output)
                return _output
        return ''
    except ApiException as e:
        print(
            "Exception when calling CoreV1Api->list_namespaced_config_map: %s\n"
            % e)
Esempio n. 2
0
def DeleteKubePVC(parser_args):
    if parser_args.component == "search":
        # {tenantName}-{environment}-search-master-gluster-volume
        if parser_args.envtype == "auth":
            pvc_name = parser_args.tenant + parser_args.env + "-search-master-" + "volume"
        elif parser_args.envtype == "live":
            pvc_name = parser_args.tenant + parser_args.env + "-search-repeater-" + "volume"

        # Below config boby still need refine to accept more flexible extension
        body = client.V1DeleteOptions()
        try:
            apiInstance = globalvars.get_value('KubCoreV1Api')
            if not apiInstance:
                factory.Factory_InitKubeClient(parser_args.configtype,
                                               parser_args.configfile)
                apiInstance = globalvars.get_value('KubCoreV1Api')
            api_response = apiInstance.delete_namespaced_persistent_volume_claim(
                pvc_name, parser_args.namespace, body)
            print(api_response)
        except ApiException as e:
            print(
                "Exception when calling CoreV1Api->delete_namespaced_persistent_volume_claim: %s\n"
                % e)
    else:
        print("Compoennt %s should not have any persistent volumes" %
              (parser_args.component))
def DeleteConfigMap(parser_args):
    _CoreV1Api = globalvars.get_value('KubCoreV1Api')
    if not _CoreV1Api:
        factory.Factory_InitKubeClient(parser_args.configtype,
                                       parser_args.configfile)
        _CoreV1Api = globalvars.get_value('KubCoreV1Api')
    print("delete config map")
    if listConfigMap(parser_args, _CoreV1Api):
        # create an instance of the API class
        name = parser_args.name  # str | name of the ConfigMap
        namespace = parser_args.namespace  # str | object name and auth scope, such as for teams and projects
        body = client.V1DeleteOptions()  # V1DeleteOptions |
        pretty = 'pretty_example'  # str | If 'true', then the output is pretty printed. (optional)

        try:
            api_response = _CoreV1Api.delete_namespaced_config_map(
                parser_args.tenant + parser_args.env + parser_args.envtype +
                "-" + name,
                namespace,
                body,
                pretty=pretty)
            print(api_response)
        except ApiException as e:
            print(
                "Exception when calling CoreV1Api->delete_namespaced_config_map: %s\n"
                % e)
    else:
        print("Not Found target cofigMap ojbect %s exist" % parser_args.name)
Esempio n. 4
0
def CheckAndCreate(parser_args):

    # Core Logic For Quick Testing
    # _CoreV1Api = globalvars.get_value('KubCoreV1Api')
    # _createRawCerts(args)
    # _createKubeSecret(args, _CoreV1Api)

    _targetCertName=parser_args.tenant+parser_args.env+parser_args.envtype+"-certificate"
    _targetPublicKeyName=parser_args.tenant+parser_args.env+parser_args.envtype+"-dhparam"

    _targetCertExist = False
    _targetPublicKeyExist = False

    _secretCheckPass=False

    if parser_args.namespace !=None and parser_args.namespace !="":
        namespace=parser_args.namespace
    else:
        namespace='default'

    _CoreV1Api = globalvars.get_value('KubCoreV1Api')
    if not _CoreV1Api:
        factory.Factory_InitKubeClient(parser_args.configtype, parser_args.configfile)
        _CoreV1Api = globalvars.get_value('KubCoreV1Api')
    if _CoreV1Api:
     try:
        api_response = _CoreV1Api.list_namespaced_secret(namespace=namespace,pretty=True,watch=False)

        print("try to find certification %s and publickey %s" %(_targetCertName,_targetPublicKeyName))
        for item in api_response.items:
           if _targetCertExist and _targetPublicKeyExist:
              _secretCheckPass = True
              break
           if item.metadata.name == _targetCertName:
              if parser_args.replace == "true":
                _deleteSecrete(_targetCertName,_CoreV1Api,parser_args.namespace)
                continue
              _targetCertExist = True
           elif item.metadata.name == _targetPublicKeyName:
               if parser_args.replace == "true":
                   _deleteSecrete(_targetPublicKeyName,_CoreV1Api,parser_args.namespace)
                   continue
               _targetPublicKeyExist = True
           else:
               print("secret %s is no match" %(item.metadata.name))
               continue

        if not _secretCheckPass:
           print("need to create secret")
           _createRawCerts(parser_args)
           _createKubeSecret(parser_args, _CoreV1Api)
        else:
           print("secret check pass !")

     except ApiException as e:
        print("Exception when calling CoreV1Api->list_namespaced_secret: %s\n" % e)
Esempio n. 5
0
def CreateConfigMap(parser_args):
    _CoreV1Api = globalvars.get_value('KubCoreV1Api')
    if not _CoreV1Api:
        factory.Factory_InitKubeClient(parser_args.configtype, parser_args.configfile)
        _CoreV1Api = globalvars.get_value('KubCoreV1Api')
    if listConfigMap(parser_args, _CoreV1Api):
        print("find target config map %s" % (parser_args.tenant+parser_args.env+parser_args.envtype+"-"+parser_args.name))
        if parser_args.forcecreate == "true":
                deleteConfigMap(parser_args,_CoreV1Api)
                createConfigMap(parser_args,_CoreV1Api)
        else:
                patchConfigMap(parser_args,_CoreV1Api)
    else:
        print("create new config map %s" % (parser_args.name))
        createConfigMap(parser_args, _CoreV1Api)
Esempio n. 6
0
def DeleteSecret(parser_args):
    _targetCertName = parser_args.tenant + parser_args.env + parser_args.envtype + "-certificate"
    _targetPublicKeyName = parser_args.tenant + parser_args.env + parser_args.envtype + "-dhparam"

    _targetSecretList=[_targetCertName,_targetPublicKeyName]

    _CoreV1Api = globalvars.get_value('KubCoreV1Api')
    if not _CoreV1Api:
        factory.Factory_InitKubeClient(parser_args.configtype, parser_args.configfile)
        _CoreV1Api = globalvars.get_value('KubCoreV1Api')
    if parser_args.namespace !=None and parser_args.namespace !="":
        namespace=parser_args.namespace
    else:
        namespace='default'

    api_response = _CoreV1Api.list_namespaced_secret(namespace=namespace, pretty=True, watch=False)

    for item in api_response.items:
        if item.metadata.name in _targetSecretList:
            _deleteSecrete(item.metadata.name, _CoreV1Api, namespace)
Esempio n. 7
0
def CreateKubePVC(parser_args):
    '''
    1.check if there have the spcified storage class exist, if not , exit(1)
    2.check if there have the specified PVC exist
      IF not exist create
      IF exist ignore
    '''
    _CoreV1Api = globalvars.get_value('KubCoreV1Api')
    if not _CoreV1Api:
        factory.Factory_InitKubeClient(parser_args.configtype,
                                       parser_args.configfile)
        _CoreV1Api = globalvars.get_value('KubCoreV1Api')
    is_pvcExist = _checkKubePVCStatus(parser_args, _CoreV1Api)

    if is_pvcExist:
        if parser_args.force == "true":
            DeleteKubePVC(parser_args)
        else:
            print("Reuse Exist Persistent Volume Chaim In Kubernetes")
            exit(0)
    _createKubePVC(parser_args, _CoreV1Api)
Esempio n. 8
0
def ListPods_by_Label(parser_args):

    _CoreV1Api = globalvars.get_value('KubCoreV1Api')
    if not _CoreV1Api:
        factory.Factory_InitKubeClient(parser_args.configtype,
                                       parser_args.configfile)
        _CoreV1Api = globalvars.get_value('KubCoreV1Api')

    candaditePods = []

    #sample for label and field_selector: label_selector="group=demoqalive,component=demoqalivecrs-app"  field_selector="status.phase=Running"
    pod_list = _CoreV1Api.list_namespaced_pod(
        namespace=parser_args.namespace_name,
        label_selector=parser_args.label_selector,
        field_selector=parser_args.field_selector)

    for item in pod_list.items:
        if item.status.container_statuses[0].state.running != 'None':
            candaditePods.append(item.metadata.name)
            #print(item.status.container_statuses[0].state.running)
            print(item.metadata.name + ",")
def CreateSampleConf(parser_args):
    _CoreV1Api = globalvars.get_value('KubCoreV1Api')
    if not _CoreV1Api:
        factory.Factory_InitKubeClient(parser_args.configtype,
                                       parser_args.configfile)
        _CoreV1Api = globalvars.get_value('KubCoreV1Api')

    _configMapKV = {}
    try:
        print("Call kube_log to get Vault Token")
        _, _vaultToken = logs.CheckContainerLog(
            _CoreV1Api, "vault.consul", "vault", parser_args.namespace,
            "^(.*)(\S{8}-\S{4}-\S{4}-\S{4}-\S{12})(.*)$", 2)
        _configMapKV["VAULT_TOKEN"] = _vaultToken

        _configMapKV["DBHOST"] = parser_args.dbhost
        _configMapKV["DBNAME"] = parser_args.dbname
        _configMapKV["DBPASS"] = parser_args.dbpass
        _configMapKV["DBPORT"] = parser_args.dbport
        _configMapKV["DBTYPE"] = parser_args.dbtype
        _configMapKV["DBUSER"] = parser_args.dbuser
        _configMapKV["DBAUSER"] = parser_args.dbauser
        _configMapKV["DBAPASSENCRYPT"] = parser_args.dbapwdencrypt
        _configMapKV["DBPASSENCRYPT"] = parser_args.dbpwdencrypt

        # print(_configMapKV)
        # print("Create config.properties file for key-value")
        createPropertiesFile(parser_args.configfiles, _configMapKV)
        # print("Call kube_configmap to create ConfigMap")
        CreateConfigMap(parser_args)

    except ApiException as e:
        print(
            "Exception when calling create sample configmap for quick deploy: %s\n"
            % e)

    print("Create ConfigMap %s for sample deploy on namespace %s success" %
          (parser_args.tenant + parser_args.env + parser_args.envtype +
           "-config.properties", parser_args.namespace))
Esempio n. 10
0
import kube_buildIndex
import kube_configmap


def KubeCli():
    return "This is KubeCli"


if __name__ == "__main__":
    # This is the unified interface to accept parameters
    # Python model argparse be used to parse the input parameter
    # Subparser has been defined in git_handler.py which including what subcmd and related parameters should be used
    # IF you want to extenstion more cmd please following this coding pattern

    parser = globalvars.get_value('RootCMDParser')
    args = parser.parse_args()
    #print(args)

    # config and group are global config used to initial gitlab object
    config, configtype, context, apiversion = None, None, None, None
    if hasattr(args, 'config'):
        config = args.config
    if hasattr(args, 'configtype'):
        configtype = args.configtype

    # based on input parameter to initial the kubernetes api object
    factory.Factory_InitKubeClient(configtype, config, context, apiversion)

    # args will based on the function which bind on command to launch that function
    args.func(args)