Пример #1
0
def main(argv):
    parser = argparse.ArgumentParser(
        "kubectl.py",
        description=
        "Run kubectl commands on cluster, any arguments not parsed here will be passed directly to kubectl"
    )
    parser.add_argument("--id",
                        default=None,
                        help="Context ID of cluster to operate on")
    parser.add_argument("-u", "--user", default=None)
    parser.add_argument("-n", "--namespace", default=None)
    args, remargs = parser.parse_known_args(argv)

    (args.id, args.user) = getCtx(args.id, args.user, None)

    # move default config as it may conflict, register to remove
    moveExistConfig()

    cfgdir = "{0}/.kcluster/{1}".format(utils.getHome(), args.id)
    serverInfo = utils.loadYaml("{0}/servers.yaml".format(cfgdir))
    servers = serverInfo["Servers"]
    servers = [
        re.sub('(.*):(.*)', '\g<1>:{0}'.format(serverInfo["k8sport"]), s)
        for s in servers
    ]
    (queryParams, _) = argsToQuery(user=args.user)
    webutils.tryServers(
        servers, partial(doOper, args.user, args.id, remargs, args.namespace),
        partial(getServers, queryParams))
Пример #2
0
def replicate(specupdate=None, workcfgupdate=None):
    (queryParams, data) = argsToQuery(None)
    suffix = "-" + utils.random_string(10)
    #spec = apiOperWork("get", "work/{0}".format(os.environ["KC_WORKNAME"]), queryParams, data).json()["spec"]
    #spec['metadata']['name'] += suffix
    origspec = yaml.safe_load(utils.b64d(os.environ["KC_ORIGSPEC"]))
    if specupdate is not None:
        origspec.update(specupdate)  # modify original
    spec = copy.deepcopy(origspec)
    modSpec(spec, {}, suffix)
    workfilecontent = utils.b64d(os.environ["KC_WORKFILE"])  # raw
    if "KC_WORKCFG" in os.environ:
        workcfgcontent = yaml.safe_load(utils.b64d(
            os.environ["KC_WORKCFG"]))  # a map
    else:
        workcfgcontent = None
    if workcfgcontent is not None and workcfgupdate is not None:
        workcfgcontent.update(workcfgupdate)
    (queryParams, data) = argsToQuery(None, spec, None, workcfgcontent,
                                      workfilecontent, origspec, None)
    apiOperWork("create", "work", queryParams, data)
Пример #3
0
def apiOperWork(verb, noun, queryParams=None, data=None):
    home = utils.getHome()
    if queryParams is None:
        (queryParams, _) = argsToQuery()
    id = os.environ["KC_CLUSTERID"]
    if os.path.exists('{0}/.{1}/{2}/servers.yaml'.format(home, "kcluster",
                                                         id)):
        servers = None  # read from file
    else:
        servers = os.environ["KC_APISERVERS"].split(
            ",")  # initial list of servers
    return doAPIOper(servers, id, verb, noun, queryParams, data)
Пример #4
0
def specIter(user, status, workcfg, workfile, workuser):
    if workcfg is not None:
        template = Template(workfile)
        specC = template.render(cnf=workcfg)
    else:
        specC = workfile
    specs = utils.loadMYamlC(specC)
    #print(specC)
    selectors = getServiceSelectors(specs)
    suffix = "-" + utils.random_string(10)
    for spec in specs:
        origspec = copy.deepcopy(spec)
        modSpec(spec, selectors, suffix)
        (queryParams, data) = argsToQuery(user, spec, status, workcfg,
                                          workfile, origspec, workuser)
        yield (queryParams, data, spec)
Пример #5
0
    if args.setcontext:
        setCtx(args.id, args.user)
        exit()

    if args.verb == 'checktoken':
        print(webutils.decodeOIDCToken("kcluster", args.user))
        exit()

    kcapi.sslVerify = not args.noverify
    kcapi.ctxName = args.ctxname

    if args.verb == "login":
        if args.noun == "google" or args.noun == "msft":
            (_, user) = oidclogin.login(args.noun, "kcluster")
            # after login, obtain userinfo and usercreds
            (queryParams, data) = argsToQuery(user=user)
            doAPIOper(args.server, args.id, "get", "user", queryParams, data)
            doAPIOper(args.server, args.id, "get", "kubecred", queryParams,
                      data)
    else:
        if args.verb == "create":
            args.noun = "work"

        complete = False
        if not complete and args.verb == "get" and args.noun.startswith(
                "endpt/"):
            #print("GETSVC: {0}".format(args.noun.split('/')[1]))
            svcDesc, _ = kubeclient.doKubeOper(
                args.user, args.id,
                "get svc {0} -o yaml".format(args.noun.split('/')[1]).split())
            #print(yaml.safe_load(svcDesc))