summaryrefslogtreecommitdiffstats
path: root/samples/services/nginx/docker/grpc/nginx_client.py
blob: dfefb08daf3bca2d97152643578e01aa1d4968e0 (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
83
84
85
86
87
# Copyright (c) Authors of Clover
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0

from __future__ import print_function
from kubernetes import client, config

import grpc
import argparse
import pickle

import nginx_pb2
import nginx_pb2_grpc


def run(args):
    # get pod ip for grpc
    pod_ip = get_podip(args['service_name'])
    if pod_ip == '':
        return "Cant find service with name: {}".format(args['service_name'])
    nginx_grpc = pod_ip + ':50054'
    channel = grpc.insecure_channel(nginx_grpc)
    stub = nginx_pb2_grpc.ControllerStub(channel)

    # modify config
    if args['service_type'] == 'lb':
        modify_lb(stub)
    elif args['service_type'] == 'proxy':
        modify_proxy(stub)
    elif args['service_type'] == 'server':
        modify_server(stub)
    else:
        return "Invalid service type: {}".format(args['service_type'])
    return "Modification complete"


def get_podip(pod_name):
    config.load_kube_config()
    v1 = client.CoreV1Api()
    ret = v1.list_pod_for_all_namespaces(watch=False)
    ip = ''
    for i in ret.items:
        if i.metadata.name.lower().find(pod_name.lower()) != -1:
            print(i.status.pod_ip)
            ip = i.status.pod_ip
    return str(ip)


def modify_proxy(stub):
    response = stub.ModifyProxy(nginx_pb2.ConfigProxy(
            server_port='9180', server_name='http-proxy',
            location_path='/', proxy_path='http://clover-server:9180',
            mirror_path='http://snort-ids:80'))
    print(response.message)


def modify_server(stub):
    response = stub.ModifyServer(nginx_pb2.ConfigServer(
            server_port='9180', server_name='clover-server',
            site_root='/var/www/html', site_index='index.nginx-debian.html'))
    print(response.message)


def modify_lb(stub):
    slb_list = pickle.dumps(
                    ['clover-server1', 'clover-server2', 'clover-server3'])
    response = stub.ModifyLB(nginx_pb2.ConfigLB(
            server_port='9188', server_name='http-lb',
            slb_list=slb_list,
            slb_group='cloverlb', lb_path='/'))
    print(response.message)


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument(
            '--service_type', required=True,
            help='The service to reconfigure')
    parser.add_argument(
            '--service_name', required=True,
            help='The service to reconfigure')

    args = parser.parse_args()
    print(run(vars(args)))