summaryrefslogtreecommitdiffstats
path: root/clover/clovisor/libclovisor/clovisor_cfg.go
blob: 9c552da40bb77caa88ac5305824febb99dfca3c4 (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
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
// 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

package clovisor

import (
    "bytes"
    "encoding/binary"
    "fmt"
    "io"
    "net"
    "strconv"
    "strings"

    "github.com/go-redis/redis"
    opentracing "github.com/opentracing/opentracing-go"
    jaeger "github.com/uber/jaeger-client-go"
    jaeger_config "github.com/uber/jaeger-client-go/config"
)

var redisServer string = "redis.clover-system"
var jaegerCollector string = "jaeger-collector.clover-system:14268"
var jaegerAgent string = "jaeger-agent.clover-system:6831"
var ProtoCfg string = "clovisor_proto_cfg"
var protoPluginCfgChan string = "clovisor_proto_plugin_cfg_chan"

/*
 * redisConnect: redis client connecting to redis server
 */
func redisConnect() *redis.Client {
    client := redis.NewClient(&redis.Options{
        Addr:       fmt.Sprintf("%s:6379", redisServer),
        Password:   "",
        DB:         0,
    })
    return client
}

func get_cfg_labels(node_name string) ([]string, error) {
    client := redisConnect()
    defer client.Close()
    labels_list, err := client.LRange("clovisor_labels", 0, -1).Result()
    if err != nil {
        fmt.Println(err.Error())
        return nil, err
    }

    return labels_list, err
}

func get_egress_match_list(pod_name string) ([]egress_match_t) {
    client := redisConnect()
    defer client.Close()
    egress_cfg_list, err := client.LRange("clovior_egress_match", 0, -1).Result()
    if err != nil {
        fmt.Println(err.Error())
        return nil
    }
    ret_list := make([]egress_match_t, 0, len(egress_cfg_list))
    for _, em_cfg_str := range(egress_cfg_list) {
        fmt.Printf("egress match cfg == %v\n", em_cfg_str)
        em_cfg_slice := strings.Split(em_cfg_str, ":")
        if len(em_cfg_slice) < 2 {
            fmt.Printf("egress match config requires at least two fields [%v]\n", em_cfg_slice)
            continue
        } else if len(em_cfg_slice) == 3 {
            if strings.Contains(pod_name, em_cfg_slice[2]) {
                fmt.Printf("%v != %v, filtering out this config for pod %v\n",
                           em_cfg_slice[2], pod_name, pod_name)
                continue
            }
        }
        var ip uint32 = 0
        if em_cfg_slice[0] != "0" {
            ip = ip2Long(em_cfg_slice[0])
        }
        port_32, _ := strconv.Atoi(em_cfg_slice[1])
        port := uint16(port_32)
        ret_list = append(ret_list,  egress_match_t{ip, port})
    }
    return ret_list
}

// following function comes from
// https://www.socketloop.com/tutorials/golang-convert-ip-address-string-to-long-unsigned-32-bit-integer
func ip2Long(ip string) uint32 {
    var long uint32
    //binary.Read(bytes.NewBuffer(net.ParseIP(ip).To4()), binary.LittleEndian, &long)
    binary.Read(bytes.NewBuffer(net.ParseIP(ip).To4()), binary.BigEndian, &long)
    return long
}

func backtoIP4(ipInt int64) string {
    // need to do two bit shifting and “0xff” masking
    b0 := strconv.FormatInt((ipInt>>24)&0xff, 10)
    b1 := strconv.FormatInt((ipInt>>16)&0xff, 10)
    b2 := strconv.FormatInt((ipInt>>8)&0xff, 10)
    b3 := strconv.FormatInt((ipInt & 0xff), 10)
    return b0 + "." + b1 + "." + b2 + "." + b3
}

func get_cfg_session_match() ([]egress_match_cfg, error) {
    var ret_list []egress_match_cfg
    client := redisConnect()
    defer client.Close()
    keys, err := client.HKeys("clovisor_session_match").Result()
    if err != nil {
        fmt.Println(err.Error())
        return nil, err
    }
    for _, key := range keys {
        value, err := client.HGet("clovisor_session_match", key).Result()
        if err != nil {
            fmt.Println(err.Error())
            continue
        }
        match_slice := strings.Split(key, "-")
        dst_ip := ip2Long(match_slice[0])
        dst_port, _ := strconv.Atoi(match_slice[1])
        egress_match := egress_match_t{
                dst_ip:     dst_ip,
                dst_port:   uint16(dst_port),
        }
        // organize into internally understandable struct
        ret_list = append(ret_list, egress_match_cfg{
                                        egress_match:   egress_match,
                                        action:         value,
                                    })
    }
    return ret_list, nil
}

func initJaeger(service string) (opentracing.Tracer, io.Closer) {
    cfg := &jaeger_config.Configuration{
        Sampler: &jaeger_config.SamplerConfig{
            Type:  "const",
            Param: 1,
        },
        Reporter: &jaeger_config.ReporterConfig{
            LogSpans: true,
            CollectorEndpoint: fmt.Sprintf("http://%s/api/traces", jaegerCollector),
            LocalAgentHostPort: fmt.Sprintf("%s", jaegerAgent),
        },
    }
    tracer, closer, err := cfg.New(service, jaeger_config.Logger(jaeger.StdLogger))
    if err != nil {
        panic(fmt.Sprintf("ERROR: cannot init Jaeger: %v\n", err))
    }
    return tracer, closer
}

func get_jaeger_server() (string, error) {
    client := redisConnect()
    defer client.Close()
    return client.Get("clovisor_jaeger_server").Result()
}

func Monitor_proto_plugin_cfg() {
    client := redisConnect()
    //defer client.Close()

    pubsub := client.Subscribe(protoPluginCfgChan)
    //defer pubsub.Close()

    go func() {
        for {
            msg, err := pubsub.ReceiveMessage()
            if err != nil {
                fmt.Printf("Error getting protocol plugin configuration: %v\n", err)
                return
            }

            fmt.Printf("Update on protocol %v notification received\n", msg.Payload)
            loadProtoParser(msg.Payload, true)
        }
    }()
}