sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package main |
| 18 | |
| 19 | import ( |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 20 | "errors" |
David K. Bainbridge | f430cd5 | 2019-05-28 15:00:35 -0700 | [diff] [blame] | 21 | "flag" |
Kent Hagerman | 334a8ce | 2019-05-16 16:50:33 -0400 | [diff] [blame] | 22 | "fmt" |
Kent Hagerman | 9266146 | 2019-06-04 18:22:05 -0400 | [diff] [blame] | 23 | "google.golang.org/grpc/connectivity" |
| 24 | "google.golang.org/grpc/keepalive" |
Kent Hagerman | 334a8ce | 2019-05-16 16:50:33 -0400 | [diff] [blame] | 25 | "math" |
| 26 | "os" |
David K. Bainbridge | f430cd5 | 2019-05-28 15:00:35 -0700 | [diff] [blame] | 27 | "path" |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 28 | "regexp" |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 29 | "strconv" |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 30 | "time" |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 31 | |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 32 | "github.com/golang/protobuf/ptypes" |
Kent Hagerman | 334a8ce | 2019-05-16 16:50:33 -0400 | [diff] [blame] | 33 | "github.com/golang/protobuf/ptypes/empty" |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 34 | "github.com/opencord/voltha-go/common/log" |
David K. Bainbridge | f430cd5 | 2019-05-28 15:00:35 -0700 | [diff] [blame] | 35 | "github.com/opencord/voltha-go/common/version" |
Kent Hagerman | 334a8ce | 2019-05-16 16:50:33 -0400 | [diff] [blame] | 36 | "github.com/opencord/voltha-go/kafka" |
William Kurkian | daa6bb2 | 2019-03-07 12:26:28 -0500 | [diff] [blame] | 37 | pb "github.com/opencord/voltha-protos/go/afrouter" |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 38 | cmn "github.com/opencord/voltha-protos/go/common" |
William Kurkian | daa6bb2 | 2019-03-07 12:26:28 -0500 | [diff] [blame] | 39 | ic "github.com/opencord/voltha-protos/go/inter_container" |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 40 | vpb "github.com/opencord/voltha-protos/go/voltha" |
| 41 | "golang.org/x/net/context" |
| 42 | "google.golang.org/grpc" |
| 43 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 44 | "k8s.io/client-go/kubernetes" |
| 45 | "k8s.io/client-go/rest" |
Kent Hagerman | e566c2e | 2019-06-03 17:56:42 -0400 | [diff] [blame] | 46 | "k8s.io/client-go/tools/clientcmd" |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 47 | ) |
| 48 | |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 49 | type volthaPod struct { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 50 | name string |
| 51 | ipAddr string |
| 52 | node string |
| 53 | devIds map[string]struct{} |
| 54 | cluster string |
| 55 | backend string |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 56 | connection string |
| 57 | } |
| 58 | |
| 59 | type podTrack struct { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 60 | pod *volthaPod |
| 61 | dn bool |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 62 | } |
| 63 | |
David K. Bainbridge | f430cd5 | 2019-05-28 15:00:35 -0700 | [diff] [blame] | 64 | type Configuration struct { |
| 65 | DisplayVersionOnly *bool |
| 66 | } |
| 67 | |
Kent Hagerman | 334a8ce | 2019-05-16 16:50:33 -0400 | [diff] [blame] | 68 | var ( |
Kent Hagerman | e566c2e | 2019-06-03 17:56:42 -0400 | [diff] [blame] | 69 | // if k8s variables are undefined, will attempt to use in-cluster config |
| 70 | k8sApiServer = getStrEnv("K8S_API_SERVER", "") |
| 71 | k8sKubeConfigPath = getStrEnv("K8S_KUBE_CONFIG_PATH", "") |
| 72 | |
Kent Hagerman | 334a8ce | 2019-05-16 16:50:33 -0400 | [diff] [blame] | 73 | podNamespace = getStrEnv("POD_NAMESPACE", "voltha") |
| 74 | podGrpcPort = uint64(getIntEnv("POD_GRPC_PORT", 0, math.MaxUint16, 50057)) |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 75 | |
Kent Hagerman | 334a8ce | 2019-05-16 16:50:33 -0400 | [diff] [blame] | 76 | numRWPods = getIntEnv("NUM_RW_PODS", 1, math.MaxInt32, 6) |
| 77 | numROPods = getIntEnv("NUM_RO_PODS", 1, math.MaxInt32, 3) |
| 78 | |
Kent Hagerman | 9266146 | 2019-06-04 18:22:05 -0400 | [diff] [blame] | 79 | afrouterApiAddress = getStrEnv("AFROUTER_API_ADDRESS", "localhost:55554") |
| 80 | |
Kent Hagerman | 334a8ce | 2019-05-16 16:50:33 -0400 | [diff] [blame] | 81 | afrouterRouterName = getStrEnv("AFROUTER_ROUTER_NAME", "vcore") |
| 82 | afrouterRWClusterName = getStrEnv("AFROUTER_RW_CLUSTER_NAME", "vcore") |
| 83 | afrouterROClusterName = getStrEnv("AFROUTER_RO_CLUSTER_NAME", "ro_vcore") |
| 84 | |
| 85 | kafkaTopic = getStrEnv("KAFKA_TOPIC", "AffinityRouter") |
| 86 | kafkaClientType = getStrEnv("KAFKA_CLIENT_TYPE", "sarama") |
| 87 | kafkaHost = getStrEnv("KAFKA_HOST", "kafka") |
| 88 | kafkaPort = getIntEnv("KAFKA_PORT", 0, math.MaxUint16, 9092) |
| 89 | kafkaInstanceID = getStrEnv("KAFKA_INSTANCE_ID", "arouterd") |
| 90 | ) |
| 91 | |
| 92 | func getIntEnv(key string, min, max, defaultValue int) int { |
| 93 | if val, have := os.LookupEnv(key); have { |
| 94 | num, err := strconv.Atoi(val) |
| 95 | if err != nil || !(min <= num && num <= max) { |
| 96 | panic(fmt.Errorf("%s must be a number in the range [%d, %d]; default: %d", key, min, max, defaultValue)) |
| 97 | } |
| 98 | return num |
| 99 | } |
| 100 | return defaultValue |
| 101 | } |
| 102 | |
| 103 | func getStrEnv(key, defaultValue string) string { |
| 104 | if val, have := os.LookupEnv(key); have { |
| 105 | return val |
| 106 | } |
| 107 | return defaultValue |
| 108 | } |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 109 | |
| 110 | func newKafkaClient(clientType string, host string, port int, instanceID string) (kafka.Client, error) { |
| 111 | |
| 112 | log.Infow("kafka-client-type", log.Fields{"client": clientType}) |
| 113 | switch clientType { |
| 114 | case "sarama": |
| 115 | return kafka.NewSaramaClient( |
| 116 | kafka.Host(host), |
| 117 | kafka.Port(port), |
| 118 | kafka.ConsumerType(kafka.GroupCustomer), |
| 119 | kafka.ProducerReturnOnErrors(true), |
| 120 | kafka.ProducerReturnOnSuccess(true), |
| 121 | kafka.ProducerMaxRetries(6), |
| 122 | kafka.NumPartitions(3), |
| 123 | kafka.ConsumerGroupName(instanceID), |
| 124 | kafka.ConsumerGroupPrefix(instanceID), |
| 125 | kafka.AutoCreateTopic(false), |
| 126 | kafka.ProducerFlushFrequency(5), |
| 127 | kafka.ProducerRetryBackoff(time.Millisecond*30)), nil |
| 128 | } |
| 129 | return nil, errors.New("unsupported-client-type") |
| 130 | } |
| 131 | |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 132 | func k8sClientSet() *kubernetes.Clientset { |
Kent Hagerman | e566c2e | 2019-06-03 17:56:42 -0400 | [diff] [blame] | 133 | var config *rest.Config |
| 134 | if k8sApiServer != "" || k8sKubeConfigPath != "" { |
| 135 | // use combination of URL & local kube-config file |
| 136 | c, err := clientcmd.BuildConfigFromFlags(k8sApiServer, k8sKubeConfigPath) |
| 137 | if err != nil { |
| 138 | panic(err) |
| 139 | } |
| 140 | config = c |
| 141 | } else { |
| 142 | // use in-cluster config |
| 143 | c, err := rest.InClusterConfig() |
| 144 | if err != nil { |
| 145 | log.Errorf("Unable to load in-cluster config. Try setting K8S_API_SERVER and K8S_KUBE_CONFIG_PATH?") |
| 146 | panic(err) |
| 147 | } |
| 148 | config = c |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 149 | } |
| 150 | // creates the clientset |
| 151 | clientset, err := kubernetes.NewForConfig(config) |
| 152 | if err != nil { |
Kent Hagerman | e566c2e | 2019-06-03 17:56:42 -0400 | [diff] [blame] | 153 | panic(err) |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | return clientset |
| 157 | } |
| 158 | |
Kent Hagerman | 9266146 | 2019-06-04 18:22:05 -0400 | [diff] [blame] | 159 | func connect(ctx context.Context, addr string) (*grpc.ClientConn, error) { |
| 160 | log.Debugf("Trying to connect to %s", addr) |
| 161 | conn, err := grpc.DialContext(ctx, addr, |
| 162 | grpc.WithInsecure(), |
| 163 | grpc.WithBlock(), |
| 164 | grpc.WithBackoffMaxDelay(time.Second*5), |
| 165 | grpc.WithKeepaliveParams(keepalive.ClientParameters{Time: time.Second * 10, Timeout: time.Second * 5})) |
| 166 | if err == nil { |
| 167 | log.Debugf("Connection succeeded") |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 168 | } |
Kent Hagerman | 9266146 | 2019-06-04 18:22:05 -0400 | [diff] [blame] | 169 | return conn, err |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 170 | } |
| 171 | |
Kent Hagerman | 9266146 | 2019-06-04 18:22:05 -0400 | [diff] [blame] | 172 | func getVolthaPods(cs *kubernetes.Clientset, coreFilter *regexp.Regexp) ([]*volthaPod, error) { |
Kent Hagerman | 334a8ce | 2019-05-16 16:50:33 -0400 | [diff] [blame] | 173 | pods, err := cs.CoreV1().Pods(podNamespace).List(metav1.ListOptions{}) |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 174 | if err != nil { |
Kent Hagerman | 9266146 | 2019-06-04 18:22:05 -0400 | [diff] [blame] | 175 | return nil, err |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 176 | } |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 177 | //log.Debugf("There are a total of %d pods in the cluster\n", len(pods.Items)) |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 178 | |
Kent Hagerman | 9266146 | 2019-06-04 18:22:05 -0400 | [diff] [blame] | 179 | var rtrn []*volthaPod |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 180 | for _, v := range pods.Items { |
Kent Hagerman | 334a8ce | 2019-05-16 16:50:33 -0400 | [diff] [blame] | 181 | if coreFilter.MatchString(v.Name) { |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 182 | log.Debugf("Namespace: %s, PodName: %s, PodIP: %s, Host: %s\n", v.Namespace, v.Name, |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 183 | v.Status.PodIP, v.Spec.NodeName) |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 184 | // Only add the pod if it has an IP address. If it doesn't then it likely crashed and |
| 185 | // and is still in the process of getting re-started. |
| 186 | if v.Status.PodIP != "" { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 187 | rtrn = append(rtrn, &volthaPod{name: v.Name, ipAddr: v.Status.PodIP, node: v.Spec.NodeName, |
| 188 | devIds: make(map[string]struct{}), backend: "", connection: ""}) |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 189 | } |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 190 | } |
| 191 | } |
Kent Hagerman | 9266146 | 2019-06-04 18:22:05 -0400 | [diff] [blame] | 192 | return rtrn, nil |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 193 | } |
| 194 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 195 | func reconcilePodDeviceIds(pod *volthaPod, ids map[string]struct{}) bool { |
Kent Hagerman | 9266146 | 2019-06-04 18:22:05 -0400 | [diff] [blame] | 196 | ctx, _ := context.WithTimeout(context.Background(), time.Second*5) |
| 197 | conn, err := connect(ctx, fmt.Sprintf("%s:%d", pod.ipAddr, podGrpcPort)) |
sslobodr | 6c1689c | 2019-01-24 07:31:15 -0500 | [diff] [blame] | 198 | if err != nil { |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 199 | log.Debugf("Could not query devices from %s, could not connect", pod.name) |
| 200 | return false |
| 201 | } |
Kent Hagerman | 9266146 | 2019-06-04 18:22:05 -0400 | [diff] [blame] | 202 | defer conn.Close() |
Kent Hagerman | 334a8ce | 2019-05-16 16:50:33 -0400 | [diff] [blame] | 203 | |
| 204 | var idList cmn.IDs |
| 205 | for k := range ids { |
| 206 | idList.Items = append(idList.Items, &cmn.ID{Id: k}) |
| 207 | } |
| 208 | |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 209 | client := vpb.NewVolthaServiceClient(conn) |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 210 | _, err = client.ReconcileDevices(context.Background(), &idList) |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 211 | if err != nil { |
| 212 | log.Error(err) |
| 213 | return false |
| 214 | } |
| 215 | |
| 216 | return true |
| 217 | } |
| 218 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 219 | func queryPodDeviceIds(pod *volthaPod) map[string]struct{} { |
Kent Hagerman | 334a8ce | 2019-05-16 16:50:33 -0400 | [diff] [blame] | 220 | var rtrn = make(map[string]struct{}) |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 221 | // Open a connection to the pod |
Kent Hagerman | 9266146 | 2019-06-04 18:22:05 -0400 | [diff] [blame] | 222 | ctx, _ := context.WithTimeout(context.Background(), time.Second*5) |
| 223 | conn, err := connect(ctx, fmt.Sprintf("%s:%d", pod.ipAddr, podGrpcPort)) |
sslobodr | 6c1689c | 2019-01-24 07:31:15 -0500 | [diff] [blame] | 224 | if err != nil { |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 225 | log.Debugf("Could not query devices from %s, could not connect", pod.name) |
| 226 | return rtrn |
| 227 | } |
| 228 | defer conn.Close() |
| 229 | client := vpb.NewVolthaServiceClient(conn) |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 230 | devs, err := client.ListDeviceIds(context.Background(), &empty.Empty{}) |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 231 | if err != nil { |
| 232 | log.Error(err) |
| 233 | return rtrn |
| 234 | } |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 235 | for _, dv := range devs.Items { |
| 236 | rtrn[dv.Id] = struct{}{} |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | return rtrn |
| 240 | } |
| 241 | |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 242 | func queryDeviceIds(pods []*volthaPod) { |
Kent Hagerman | 334a8ce | 2019-05-16 16:50:33 -0400 | [diff] [blame] | 243 | for pk := range pods { |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 244 | // Keep the old Id list if a new list is not returned |
| 245 | if idList := queryPodDeviceIds(pods[pk]); len(idList) != 0 { |
| 246 | pods[pk].devIds = idList |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 247 | } |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 251 | func allEmpty(pods []*volthaPod) bool { |
Kent Hagerman | 334a8ce | 2019-05-16 16:50:33 -0400 | [diff] [blame] | 252 | for k := range pods { |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 253 | if len(pods[k].devIds) != 0 { |
| 254 | return false |
| 255 | } |
| 256 | } |
| 257 | return true |
| 258 | } |
| 259 | |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 260 | func rmPod(pods []*volthaPod, idx int) []*volthaPod { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 261 | return append(pods[:idx], pods[idx+1:]...) |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 262 | } |
| 263 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 264 | func groupIntersectingPods1(pods []*volthaPod, podCt int) ([][]*volthaPod, []*volthaPod) { |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 265 | var rtrn [][]*volthaPod |
| 266 | var out []*volthaPod |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 267 | |
| 268 | for { |
| 269 | if len(pods) == 0 { |
| 270 | break |
| 271 | } |
| 272 | if len(pods[0].devIds) == 0 { // Ignore pods with no devices |
| 273 | ////log.Debugf("%s empty pod", pd[k].pod.name) |
| 274 | out = append(out, pods[0]) |
| 275 | pods = rmPod(pods, 0) |
| 276 | continue |
| 277 | } |
| 278 | // Start a pod group with this pod |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 279 | var grp []*volthaPod |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 280 | grp = append(grp, pods[0]) |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 281 | pods = rmPod(pods, 0) |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 282 | //log.Debugf("Creating new group %s", pd[k].pod.name) |
| 283 | // Find the peer pod based on device overlap |
| 284 | // It's ok if one isn't found, an empty one will be used instead |
Kent Hagerman | 334a8ce | 2019-05-16 16:50:33 -0400 | [diff] [blame] | 285 | for k := range pods { |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 286 | if len(pods[k].devIds) == 0 { // Skip pods with no devices |
| 287 | //log.Debugf("%s empty pod", pd[k1].pod.name) |
| 288 | continue |
| 289 | } |
Kent Hagerman | 334a8ce | 2019-05-16 16:50:33 -0400 | [diff] [blame] | 290 | if intersect(grp[0].devIds, pods[k].devIds) { |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 291 | //log.Debugf("intersection found %s:%s", pd[k].pod.name, pd[k1].pod.name) |
| 292 | if grp[0].node == pods[k].node { |
| 293 | // This should never happen |
| 294 | log.Errorf("pods %s and %s intersect and are on the same server!! Not pairing", |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 295 | grp[0].name, pods[k].name) |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 296 | continue |
| 297 | } |
| 298 | grp = append(grp, pods[k]) |
| 299 | pods = rmPod(pods, k) |
| 300 | break |
| 301 | |
| 302 | } |
| 303 | } |
| 304 | rtrn = append(rtrn, grp) |
| 305 | //log.Debugf("Added group %s", grp[0].name) |
| 306 | // Check if the number of groups = half the pods, if so all groups are started. |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 307 | if len(rtrn) == podCt>>1 { |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 308 | // Append any remaining pods to out |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 309 | out = append(out, pods[0:]...) |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 310 | break |
| 311 | } |
| 312 | } |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 313 | return rtrn, out |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 314 | } |
| 315 | |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 316 | func unallocPodCount(pd []*podTrack) int { |
| 317 | var rtrn int = 0 |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 318 | for _, v := range pd { |
Kent Hagerman | 334a8ce | 2019-05-16 16:50:33 -0400 | [diff] [blame] | 319 | if !v.dn { |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 320 | rtrn++ |
| 321 | } |
| 322 | } |
| 323 | return rtrn |
| 324 | } |
| 325 | |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 326 | func sameNode(pod *volthaPod, grps [][]*volthaPod) bool { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 327 | for _, v := range grps { |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 328 | if v[0].node == pod.node { |
| 329 | return true |
| 330 | } |
| 331 | if len(v) == 2 && v[1].node == pod.node { |
| 332 | return true |
| 333 | } |
| 334 | } |
| 335 | return false |
| 336 | } |
| 337 | |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 338 | func startRemainingGroups1(grps [][]*volthaPod, pods []*volthaPod, podCt int) ([][]*volthaPod, []*volthaPod) { |
| 339 | var grp []*volthaPod |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 340 | |
Kent Hagerman | 334a8ce | 2019-05-16 16:50:33 -0400 | [diff] [blame] | 341 | for k := range pods { |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 342 | if sameNode(pods[k], grps) { |
| 343 | continue |
| 344 | } |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 345 | grp = []*volthaPod{} |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 346 | grp = append(grp, pods[k]) |
| 347 | pods = rmPod(pods, k) |
| 348 | grps = append(grps, grp) |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 349 | if len(grps) == podCt>>1 { |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 350 | break |
| 351 | } |
| 352 | } |
| 353 | return grps, pods |
| 354 | } |
| 355 | |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 356 | func hasSingleSecondNode(grp []*volthaPod) bool { |
Kent Hagerman | 334a8ce | 2019-05-16 16:50:33 -0400 | [diff] [blame] | 357 | var servers = make(map[string]struct{}) |
| 358 | for k := range grp { |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 359 | if k == 0 { |
| 360 | continue // Ignore the first item |
| 361 | } |
Kent Hagerman | 334a8ce | 2019-05-16 16:50:33 -0400 | [diff] [blame] | 362 | servers[grp[k].node] = struct{}{} |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 363 | } |
Kent Hagerman | 334a8ce | 2019-05-16 16:50:33 -0400 | [diff] [blame] | 364 | if len(servers) == 1 { |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 365 | return true |
| 366 | } |
| 367 | return false |
| 368 | } |
| 369 | |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 370 | func addNode(grps [][]*volthaPod, idx *volthaPod, item *volthaPod) [][]*volthaPod { |
Kent Hagerman | 334a8ce | 2019-05-16 16:50:33 -0400 | [diff] [blame] | 371 | for k := range grps { |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 372 | if grps[k][0].name == idx.name { |
| 373 | grps[k] = append(grps[k], item) |
| 374 | return grps |
| 375 | } |
| 376 | } |
| 377 | // TODO: Error checking required here. |
| 378 | return grps |
| 379 | } |
| 380 | |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 381 | func removeNode(grps [][]*volthaPod, item *volthaPod) [][]*volthaPod { |
Kent Hagerman | 334a8ce | 2019-05-16 16:50:33 -0400 | [diff] [blame] | 382 | for k := range grps { |
| 383 | for k1 := range grps[k] { |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 384 | if grps[k][k1].name == item.name { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 385 | grps[k] = append(grps[k][:k1], grps[k][k1+1:]...) |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 386 | break |
| 387 | } |
| 388 | } |
| 389 | } |
| 390 | return grps |
| 391 | } |
| 392 | |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 393 | func groupRemainingPods1(grps [][]*volthaPod, pods []*volthaPod) [][]*volthaPod { |
| 394 | var lgrps [][]*volthaPod |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 395 | // All groups must be started when this function is called. |
| 396 | // Copy incomplete groups |
Kent Hagerman | 334a8ce | 2019-05-16 16:50:33 -0400 | [diff] [blame] | 397 | for k := range grps { |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 398 | if len(grps[k]) != 2 { |
| 399 | lgrps = append(lgrps, grps[k]) |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | // Add all pairing candidates to each started group. |
Kent Hagerman | 334a8ce | 2019-05-16 16:50:33 -0400 | [diff] [blame] | 404 | for k := range pods { |
| 405 | for k2 := range lgrps { |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 406 | if lgrps[k2][0].node != pods[k].node { |
| 407 | lgrps[k2] = append(lgrps[k2], pods[k]) |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | //TODO: If any member of lgrps doesn't have at least 2 |
| 413 | // nodes something is wrong. Check for that here |
| 414 | |
| 415 | for { |
| 416 | for { // Address groups with only a single server choice |
| 417 | var ssn bool = false |
| 418 | |
Kent Hagerman | 334a8ce | 2019-05-16 16:50:33 -0400 | [diff] [blame] | 419 | for k := range lgrps { |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 420 | // Now if any of the groups only have a single |
| 421 | // node as the choice for the second member |
| 422 | // address that one first. |
Kent Hagerman | 334a8ce | 2019-05-16 16:50:33 -0400 | [diff] [blame] | 423 | if hasSingleSecondNode(lgrps[k]) { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 424 | ssn = true |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 425 | // Add this pairing to the groups |
| 426 | grps = addNode(grps, lgrps[k][0], lgrps[k][1]) |
| 427 | // Since this node is now used, remove it from all |
| 428 | // remaining tenative groups |
| 429 | lgrps = removeNode(lgrps, lgrps[k][1]) |
| 430 | // Now remove this group completely since |
| 431 | // it's been addressed |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 432 | lgrps = append(lgrps[:k], lgrps[k+1:]...) |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 433 | break |
| 434 | } |
| 435 | } |
Kent Hagerman | 334a8ce | 2019-05-16 16:50:33 -0400 | [diff] [blame] | 436 | if !ssn { |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 437 | break |
| 438 | } |
| 439 | } |
Kent Hagerman | 334a8ce | 2019-05-16 16:50:33 -0400 | [diff] [blame] | 440 | // Now address one of the remaining groups |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 441 | if len(lgrps) == 0 { |
| 442 | break // Nothing left to do, exit the loop |
| 443 | } |
| 444 | grps = addNode(grps, lgrps[0][0], lgrps[0][1]) |
| 445 | lgrps = removeNode(lgrps, lgrps[0][1]) |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 446 | lgrps = append(lgrps[:0], lgrps[1:]...) |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 447 | } |
| 448 | return grps |
| 449 | } |
| 450 | |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 451 | func groupPods1(pods []*volthaPod) [][]*volthaPod { |
| 452 | var rtrn [][]*volthaPod |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 453 | var podCt int = len(pods) |
| 454 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 455 | rtrn, pods = groupIntersectingPods1(pods, podCt) |
| 456 | // There are several outcomes here |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 457 | // 1) All pods have been paired and we're done |
| 458 | // 2) Some un-allocated pods remain |
| 459 | // 2.a) All groups have been started |
| 460 | // 2.b) Not all groups have been started |
| 461 | if len(pods) == 0 { |
| 462 | return rtrn |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 463 | } else if len(rtrn) == podCt>>1 { // All groupings started |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 464 | // Allocate the remaining (presumably empty) pods to the started groups |
| 465 | return groupRemainingPods1(rtrn, pods) |
| 466 | } else { // Some groupings started |
| 467 | // Start empty groups with remaining pods |
| 468 | // each grouping is on a different server then |
| 469 | // allocate remaining pods. |
| 470 | rtrn, pods = startRemainingGroups1(rtrn, pods, podCt) |
| 471 | return groupRemainingPods1(rtrn, pods) |
| 472 | } |
| 473 | } |
| 474 | |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 475 | func intersect(d1 map[string]struct{}, d2 map[string]struct{}) bool { |
Kent Hagerman | 334a8ce | 2019-05-16 16:50:33 -0400 | [diff] [blame] | 476 | for k := range d1 { |
| 477 | if _, ok := d2[k]; ok { |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 478 | return true |
| 479 | } |
| 480 | } |
| 481 | return false |
| 482 | } |
| 483 | |
Kent Hagerman | 9266146 | 2019-06-04 18:22:05 -0400 | [diff] [blame] | 484 | func setConnection(ctx context.Context, client pb.ConfigurationClient, cluster string, backend string, connection string, addr string, port uint64) { |
sslobodr | 360c8d7 | 2019-02-05 12:47:56 -0500 | [diff] [blame] | 485 | log.Debugf("Configuring backend %s : connection %s in cluster %s\n\n", |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 486 | backend, connection, cluster) |
| 487 | cnf := &pb.Conn{Server: "grpc_command", Cluster: cluster, Backend: backend, |
| 488 | Connection: connection, Addr: addr, |
| 489 | Port: port} |
Kent Hagerman | 9266146 | 2019-06-04 18:22:05 -0400 | [diff] [blame] | 490 | if res, err := client.SetConnection(ctx, cnf); err != nil { |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 491 | log.Debugf("failed SetConnection RPC call: %s", err) |
| 492 | } else { |
| 493 | log.Debugf("Result: %v", res) |
| 494 | } |
| 495 | } |
| 496 | |
Kent Hagerman | 9266146 | 2019-06-04 18:22:05 -0400 | [diff] [blame] | 497 | func setAffinity(ctx context.Context, client pb.ConfigurationClient, ids map[string]struct{}, backend string) { |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 498 | log.Debugf("Configuring backend %s : affinities \n", backend) |
Kent Hagerman | 334a8ce | 2019-05-16 16:50:33 -0400 | [diff] [blame] | 499 | aff := &pb.Affinity{Router: afrouterRouterName, Route: "dev_manager", Cluster: afrouterRWClusterName, Backend: backend} |
| 500 | for k := range ids { |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 501 | log.Debugf("Setting affinity for id %s", k) |
| 502 | aff.Id = k |
Kent Hagerman | 9266146 | 2019-06-04 18:22:05 -0400 | [diff] [blame] | 503 | if res, err := client.SetAffinity(ctx, aff); err != nil { |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 504 | log.Debugf("failed affinity RPC call: %s", err) |
| 505 | } else { |
| 506 | log.Debugf("Result: %v", res) |
| 507 | } |
| 508 | } |
| 509 | } |
| 510 | |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 511 | func getBackendForCore(coreId string, coreGroups [][]*volthaPod) string { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 512 | for _, v := range coreGroups { |
| 513 | for _, v2 := range v { |
sslobodr | 38afd0d | 2019-01-21 12:31:46 -0500 | [diff] [blame] | 514 | if v2.name == coreId { |
| 515 | return v2.backend |
| 516 | } |
| 517 | } |
| 518 | } |
| 519 | log.Errorf("No backend found for core %s\n", coreId) |
| 520 | return "" |
| 521 | } |
| 522 | |
Kent Hagerman | 9266146 | 2019-06-04 18:22:05 -0400 | [diff] [blame] | 523 | func monitorDiscovery(ctx context.Context, |
| 524 | client pb.ConfigurationClient, |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 525 | ch <-chan *ic.InterContainerMessage, |
Kent Hagerman | 9266146 | 2019-06-04 18:22:05 -0400 | [diff] [blame] | 526 | coreGroups [][]*volthaPod, |
| 527 | doneCh chan<- struct{}) { |
| 528 | defer close(doneCh) |
| 529 | |
Kent Hagerman | 334a8ce | 2019-05-16 16:50:33 -0400 | [diff] [blame] | 530 | var id = make(map[string]struct{}) |
sslobodr | 38afd0d | 2019-01-21 12:31:46 -0500 | [diff] [blame] | 531 | |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 532 | select { |
Kent Hagerman | 9266146 | 2019-06-04 18:22:05 -0400 | [diff] [blame] | 533 | case <-ctx.Done(): |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 534 | case msg := <-ch: |
| 535 | log.Debugf("Received a device discovery notification") |
sslobodr | 38afd0d | 2019-01-21 12:31:46 -0500 | [diff] [blame] | 536 | device := &ic.DeviceDiscovered{} |
| 537 | if err := ptypes.UnmarshalAny(msg.Body, device); err != nil { |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 538 | log.Errorf("Could not unmarshal received notification %v", msg) |
| 539 | } else { |
sslobodr | 38afd0d | 2019-01-21 12:31:46 -0500 | [diff] [blame] | 540 | // Set the affinity of the discovered device. |
| 541 | if be := getBackendForCore(device.Id, coreGroups); be != "" { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 542 | id[device.Id] = struct{}{} |
Kent Hagerman | 9266146 | 2019-06-04 18:22:05 -0400 | [diff] [blame] | 543 | setAffinity(ctx, client, id, be) |
sslobodr | 38afd0d | 2019-01-21 12:31:46 -0500 | [diff] [blame] | 544 | } else { |
| 545 | log.Error("Cant use an empty string as a backend name") |
| 546 | } |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 547 | } |
| 548 | break |
| 549 | } |
| 550 | } |
| 551 | |
Kent Hagerman | 9266146 | 2019-06-04 18:22:05 -0400 | [diff] [blame] | 552 | func startDiscoveryMonitor(ctx context.Context, |
| 553 | client pb.ConfigurationClient, |
| 554 | coreGroups [][]*volthaPod) (<-chan struct{}, error) { |
| 555 | doneCh := make(chan struct{}) |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 556 | var ch <-chan *ic.InterContainerMessage |
| 557 | // Connect to kafka for discovery events |
Kent Hagerman | 334a8ce | 2019-05-16 16:50:33 -0400 | [diff] [blame] | 558 | topic := &kafka.Topic{Name: kafkaTopic} |
| 559 | kc, err := newKafkaClient(kafkaClientType, kafkaHost, kafkaPort, kafkaInstanceID) |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 560 | kc.Start() |
Kent Hagerman | 9266146 | 2019-06-04 18:22:05 -0400 | [diff] [blame] | 561 | defer kc.Stop() |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 562 | |
| 563 | if ch, err = kc.Subscribe(topic); err != nil { |
Kent Hagerman | 334a8ce | 2019-05-16 16:50:33 -0400 | [diff] [blame] | 564 | log.Errorf("Could not subscribe to the '%s' channel, discovery disabled", kafkaTopic) |
Kent Hagerman | 9266146 | 2019-06-04 18:22:05 -0400 | [diff] [blame] | 565 | close(doneCh) |
| 566 | return doneCh, err |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 567 | } |
Kent Hagerman | 9266146 | 2019-06-04 18:22:05 -0400 | [diff] [blame] | 568 | |
| 569 | go monitorDiscovery(ctx, client, ch, coreGroups, doneCh) |
| 570 | return doneCh, nil |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 571 | } |
| 572 | |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 573 | // Determines which items in core groups |
| 574 | // have changed based on the list provided |
| 575 | // and returns a coreGroup with only the changed |
| 576 | // items and a pod list with the new items |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 577 | func getAddrDiffs(coreGroups [][]*volthaPod, rwPods []*volthaPod) ([][]*volthaPod, []*volthaPod) { |
| 578 | var nList []*volthaPod |
Kent Hagerman | 334a8ce | 2019-05-16 16:50:33 -0400 | [diff] [blame] | 579 | var rtrn = make([][]*volthaPod, numRWPods>>1) |
| 580 | var ipAddrs = make(map[string]struct{}) |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 581 | |
| 582 | log.Debug("Get addr diffs") |
| 583 | |
| 584 | // Start with an empty array |
Kent Hagerman | 334a8ce | 2019-05-16 16:50:33 -0400 | [diff] [blame] | 585 | for k := range rtrn { |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 586 | rtrn[k] = make([]*volthaPod, 2) |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | // Build a list with only the new items |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 590 | for _, v := range rwPods { |
Kent Hagerman | 334a8ce | 2019-05-16 16:50:33 -0400 | [diff] [blame] | 591 | if !hasIpAddr(coreGroups, v.ipAddr) { |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 592 | nList = append(nList, v) |
| 593 | } |
| 594 | ipAddrs[v.ipAddr] = struct{}{} // for the search below |
| 595 | } |
| 596 | |
| 597 | // Now build the coreGroups with only the changed items |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 598 | for k1, v1 := range coreGroups { |
| 599 | for k2, v2 := range v1 { |
Kent Hagerman | 334a8ce | 2019-05-16 16:50:33 -0400 | [diff] [blame] | 600 | if _, ok := ipAddrs[v2.ipAddr]; !ok { |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 601 | rtrn[k1][k2] = v2 |
| 602 | } |
| 603 | } |
| 604 | } |
| 605 | return rtrn, nList |
| 606 | } |
| 607 | |
| 608 | // Figure out where best to put the new pods |
| 609 | // in the coreGroup array based on the old |
| 610 | // pods being replaced. The criteria is that |
| 611 | // the new pod be on the same server as the |
| 612 | // old pod was. |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 613 | func reconcileAddrDiffs(coreGroupDiffs [][]*volthaPod, rwPodDiffs []*volthaPod) [][]*volthaPod { |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 614 | var srvrs map[string][]*volthaPod = make(map[string][]*volthaPod) |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 615 | |
| 616 | log.Debug("Reconciling diffs") |
| 617 | log.Debug("Building server list") |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 618 | for _, v := range rwPodDiffs { |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 619 | log.Debugf("Adding %v to the server list", *v) |
| 620 | srvrs[v.node] = append(srvrs[v.node], v) |
| 621 | } |
| 622 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 623 | for k1, v1 := range coreGroupDiffs { |
| 624 | log.Debugf("k1:%v, v1:%v", k1, v1) |
| 625 | for k2, v2 := range v1 { |
| 626 | log.Debugf("k2:%v, v2:%v", k2, v2) |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 627 | if v2 == nil { // Nothing to do here |
| 628 | continue |
| 629 | } |
Kent Hagerman | 334a8ce | 2019-05-16 16:50:33 -0400 | [diff] [blame] | 630 | if _, ok := srvrs[v2.node]; ok { |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 631 | coreGroupDiffs[k1][k2] = srvrs[v2.node][0] |
| 632 | if len(srvrs[v2.node]) > 1 { // remove one entry from the list |
| 633 | srvrs[v2.node] = append(srvrs[v2.node][:0], srvrs[v2.node][1:]...) |
| 634 | } else { // Delete the endtry from the map |
| 635 | delete(srvrs, v2.node) |
| 636 | } |
| 637 | } else { |
| 638 | log.Error("This should never happen, node appears to have changed names") |
| 639 | // attempt to limp along by keeping this old entry |
| 640 | } |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | return coreGroupDiffs |
| 645 | } |
| 646 | |
Kent Hagerman | 9266146 | 2019-06-04 18:22:05 -0400 | [diff] [blame] | 647 | func applyAddrDiffs(ctx context.Context, client pb.ConfigurationClient, coreList interface{}, nPods []*volthaPod) { |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 648 | var newEntries [][]*volthaPod |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 649 | |
| 650 | log.Debug("Applying diffs") |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 651 | switch cores := coreList.(type) { |
| 652 | case [][]*volthaPod: |
| 653 | newEntries = reconcileAddrDiffs(getAddrDiffs(cores, nPods)) |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 654 | |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 655 | // Now replace the information in coreGropus with the new |
| 656 | // entries and then reconcile the device ids on the core |
| 657 | // that's in the new entry with the device ids of it's |
| 658 | // active-active peer. |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 659 | for k1, v1 := range cores { |
| 660 | for k2, v2 := range v1 { |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 661 | if newEntries[k1][k2] != nil { |
| 662 | // TODO: Missing is the case where bothe the primary |
| 663 | // and the secondary core crash and come back. |
| 664 | // Pull the device ids from the active-active peer |
| 665 | ids := queryPodDeviceIds(cores[k1][k2^1]) |
| 666 | if len(ids) != 0 { |
Kent Hagerman | 334a8ce | 2019-05-16 16:50:33 -0400 | [diff] [blame] | 667 | if !reconcilePodDeviceIds(newEntries[k1][k2], ids) { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 668 | log.Errorf("Attempt to reconcile ids on pod %v failed", newEntries[k1][k2]) |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 669 | } |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 670 | } |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 671 | // Send the affininty router new connection information |
Kent Hagerman | 9266146 | 2019-06-04 18:22:05 -0400 | [diff] [blame] | 672 | setConnection(ctx, client, afrouterRWClusterName, v2.backend, v2.connection, newEntries[k1][k2].ipAddr, podGrpcPort) |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 673 | // Copy the new entry information over |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 674 | cores[k1][k2].ipAddr = newEntries[k1][k2].ipAddr |
| 675 | cores[k1][k2].name = newEntries[k1][k2].name |
| 676 | cores[k1][k2].devIds = ids |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 677 | } |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 678 | } |
| 679 | } |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 680 | case []*volthaPod: |
| 681 | var mia []*volthaPod |
| 682 | var found bool |
| 683 | // TODO: Break this using functions to simplify |
| 684 | // reading of the code. |
| 685 | // Find the core(s) that have changed addresses |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 686 | for k1, v1 := range cores { |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 687 | found = false |
| 688 | for _, v2 := range nPods { |
| 689 | if v1.ipAddr == v2.ipAddr { |
| 690 | found = true |
| 691 | break |
| 692 | } |
| 693 | } |
Kent Hagerman | 334a8ce | 2019-05-16 16:50:33 -0400 | [diff] [blame] | 694 | if !found { |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 695 | mia = append(mia, cores[k1]) |
| 696 | } |
| 697 | } |
| 698 | // Now plug in the new addresses and set the connection |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 699 | for _, v1 := range nPods { |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 700 | found = false |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 701 | for _, v2 := range cores { |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 702 | if v1.ipAddr == v2.ipAddr { |
| 703 | found = true |
| 704 | break |
| 705 | } |
| 706 | } |
Kent Hagerman | 334a8ce | 2019-05-16 16:50:33 -0400 | [diff] [blame] | 707 | if found { |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 708 | continue |
| 709 | } |
| 710 | mia[0].ipAddr = v1.ipAddr |
| 711 | mia[0].name = v1.name |
Kent Hagerman | 9266146 | 2019-06-04 18:22:05 -0400 | [diff] [blame] | 712 | setConnection(ctx, client, afrouterROClusterName, mia[0].backend, mia[0].connection, v1.ipAddr, podGrpcPort) |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 713 | // Now get rid of the mia entry just processed |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 714 | mia = append(mia[:0], mia[1:]...) |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 715 | } |
| 716 | default: |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 717 | log.Error("Internal: Unexpected type in call to applyAddrDiffs") |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 718 | } |
| 719 | } |
| 720 | |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 721 | func updateDeviceIds(coreGroups [][]*volthaPod, rwPods []*volthaPod) { |
Kent Hagerman | 334a8ce | 2019-05-16 16:50:33 -0400 | [diff] [blame] | 722 | var byName = make(map[string]*volthaPod) |
sslobodr | cd37bc5 | 2019-01-24 11:47:16 -0500 | [diff] [blame] | 723 | |
Kent Hagerman | 334a8ce | 2019-05-16 16:50:33 -0400 | [diff] [blame] | 724 | // Convenience |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 725 | for _, v := range rwPods { |
sslobodr | cd37bc5 | 2019-01-24 11:47:16 -0500 | [diff] [blame] | 726 | byName[v.name] = v |
| 727 | } |
| 728 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 729 | for k1, v1 := range coreGroups { |
Kent Hagerman | 334a8ce | 2019-05-16 16:50:33 -0400 | [diff] [blame] | 730 | for k2 := range v1 { |
sslobodr | cd37bc5 | 2019-01-24 11:47:16 -0500 | [diff] [blame] | 731 | coreGroups[k1][k2].devIds = byName[v1[k2].name].devIds |
| 732 | } |
| 733 | } |
| 734 | } |
| 735 | |
Kent Hagerman | 9266146 | 2019-06-04 18:22:05 -0400 | [diff] [blame] | 736 | func startCoreMonitor(ctx context.Context, |
| 737 | client pb.ConfigurationClient, |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 738 | clientset *kubernetes.Clientset, |
| 739 | rwCoreFltr *regexp.Regexp, |
| 740 | roCoreFltr *regexp.Regexp, |
| 741 | coreGroups [][]*volthaPod, |
Kent Hagerman | 9266146 | 2019-06-04 18:22:05 -0400 | [diff] [blame] | 742 | oRoPods []*volthaPod) { |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 743 | // Now that initial allocation has been completed, monitor the pods |
| 744 | // for IP changes |
| 745 | // The main loop needs to do the following: |
| 746 | // 1) Periodically query the pods and filter out |
| 747 | // the vcore ones |
| 748 | // 2) Validate that the pods running are the same |
| 749 | // as the previous check |
| 750 | // 3) Validate that the IP addresses are the same |
| 751 | // as the last check. |
| 752 | // If the pod name(s) ha(s/ve) changed then remove |
| 753 | // the unused pod names and add in the new pod names |
| 754 | // maintaining the cluster/backend information. |
| 755 | // If an IP address has changed (which shouldn't |
| 756 | // happen unless a pod is re-started) it should get |
| 757 | // caught by the pod name change. |
Kent Hagerman | 9266146 | 2019-06-04 18:22:05 -0400 | [diff] [blame] | 758 | loop: |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 759 | for { |
Kent Hagerman | 9266146 | 2019-06-04 18:22:05 -0400 | [diff] [blame] | 760 | select { |
| 761 | case <-ctx.Done(): |
| 762 | // if we're done, exit |
| 763 | break loop |
| 764 | case <-time.After(10 * time.Second): //wait a while |
| 765 | } |
| 766 | |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 767 | // Get the rw core list from k8s |
Kent Hagerman | 9266146 | 2019-06-04 18:22:05 -0400 | [diff] [blame] | 768 | rwPods, err := getVolthaPods(clientset, rwCoreFltr) |
| 769 | if err != nil { |
| 770 | log.Error(err) |
| 771 | continue |
| 772 | } |
| 773 | |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 774 | queryDeviceIds(rwPods) |
sslobodr | cd37bc5 | 2019-01-24 11:47:16 -0500 | [diff] [blame] | 775 | updateDeviceIds(coreGroups, rwPods) |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 776 | // If we didn't get 2n+1 pods then wait since |
| 777 | // something is down and will hopefully come |
| 778 | // back up at some point. |
Kent Hagerman | 334a8ce | 2019-05-16 16:50:33 -0400 | [diff] [blame] | 779 | if len(rwPods) != numRWPods { |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 780 | continue |
| 781 | } |
| 782 | // We have all pods, check if any IP addresses |
| 783 | // have changed. |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 784 | for _, v := range rwPods { |
Kent Hagerman | 334a8ce | 2019-05-16 16:50:33 -0400 | [diff] [blame] | 785 | if !hasIpAddr(coreGroups, v.ipAddr) { |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 786 | log.Debug("Address has changed...") |
Kent Hagerman | 9266146 | 2019-06-04 18:22:05 -0400 | [diff] [blame] | 787 | applyAddrDiffs(ctx, client, coreGroups, rwPods) |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 788 | break |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 789 | } |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 790 | } |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 791 | |
Kent Hagerman | 9266146 | 2019-06-04 18:22:05 -0400 | [diff] [blame] | 792 | roPods, err := getVolthaPods(clientset, roCoreFltr) |
| 793 | if err != nil { |
| 794 | log.Error(err) |
| 795 | continue |
| 796 | } |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 797 | |
Kent Hagerman | 334a8ce | 2019-05-16 16:50:33 -0400 | [diff] [blame] | 798 | if len(roPods) != numROPods { |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 799 | continue |
| 800 | } |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 801 | for _, v := range roPods { |
Kent Hagerman | 334a8ce | 2019-05-16 16:50:33 -0400 | [diff] [blame] | 802 | if !hasIpAddr(oRoPods, v.ipAddr) { |
Kent Hagerman | 9266146 | 2019-06-04 18:22:05 -0400 | [diff] [blame] | 803 | applyAddrDiffs(ctx, client, oRoPods, roPods) |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 804 | break |
| 805 | } |
| 806 | } |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 807 | } |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 808 | } |
| 809 | |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 810 | func hasIpAddr(coreList interface{}, ipAddr string) bool { |
| 811 | switch cores := coreList.(type) { |
| 812 | case []*volthaPod: |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 813 | for _, v := range cores { |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 814 | if v.ipAddr == ipAddr { |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 815 | return true |
| 816 | } |
| 817 | } |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 818 | case [][]*volthaPod: |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 819 | for _, v1 := range cores { |
| 820 | for _, v2 := range v1 { |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 821 | if v2.ipAddr == ipAddr { |
| 822 | return true |
| 823 | } |
| 824 | } |
| 825 | } |
| 826 | default: |
| 827 | log.Error("Internal: Unexpected type in call to hasIpAddr") |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 828 | } |
| 829 | return false |
| 830 | } |
| 831 | |
Kent Hagerman | 9266146 | 2019-06-04 18:22:05 -0400 | [diff] [blame] | 832 | // endOnClose cancels the context when the connection closes |
| 833 | func connectionActiveContext(conn *grpc.ClientConn) context.Context { |
| 834 | ctx, disconnected := context.WithCancel(context.Background()) |
| 835 | go func() { |
| 836 | for state := conn.GetState(); state != connectivity.TransientFailure && state != connectivity.Shutdown; state = conn.GetState() { |
| 837 | if !conn.WaitForStateChange(context.Background(), state) { |
| 838 | break |
| 839 | } |
| 840 | } |
| 841 | log.Infof("Connection to afrouter lost") |
| 842 | disconnected() |
| 843 | }() |
| 844 | return ctx |
| 845 | } |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 846 | |
Kent Hagerman | 9266146 | 2019-06-04 18:22:05 -0400 | [diff] [blame] | 847 | func main() { |
David K. Bainbridge | f430cd5 | 2019-05-28 15:00:35 -0700 | [diff] [blame] | 848 | config := &Configuration{} |
| 849 | cmdParse := flag.NewFlagSet(path.Base(os.Args[0]), flag.ContinueOnError) |
| 850 | config.DisplayVersionOnly = cmdParse.Bool("version", false, "Print version information and exit") |
| 851 | |
Kent Hagerman | 9266146 | 2019-06-04 18:22:05 -0400 | [diff] [blame] | 852 | if err := cmdParse.Parse(os.Args[1:]); err != nil { |
David K. Bainbridge | f430cd5 | 2019-05-28 15:00:35 -0700 | [diff] [blame] | 853 | fmt.Printf("Error: %v\n", err) |
| 854 | os.Exit(1) |
| 855 | } |
| 856 | |
| 857 | if *config.DisplayVersionOnly { |
| 858 | fmt.Println("VOLTHA API Server (afrouterd)") |
| 859 | fmt.Println(version.VersionInfo.String(" ")) |
| 860 | return |
| 861 | } |
| 862 | |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 863 | // Set up logging |
| 864 | if _, err := log.SetDefaultLogger(log.JSON, 0, nil); err != nil { |
| 865 | log.With(log.Fields{"error": err}).Fatal("Cannot setup logging") |
| 866 | } |
| 867 | |
| 868 | // Set up kubernetes api |
| 869 | clientset := k8sClientSet() |
| 870 | |
Kent Hagerman | 9266146 | 2019-06-04 18:22:05 -0400 | [diff] [blame] | 871 | for { |
| 872 | // Connect to the affinity router |
| 873 | conn, err := connect(context.Background(), afrouterApiAddress) // This is a sidecar container so communicating over localhost |
| 874 | if err != nil { |
| 875 | panic(err) |
| 876 | } |
| 877 | |
| 878 | // monitor the connection status, end context if connection is lost |
| 879 | ctx := connectionActiveContext(conn) |
| 880 | |
| 881 | // set up the client |
| 882 | client := pb.NewConfigurationClient(conn) |
| 883 | |
| 884 | // determine config & repopulate the afrouter |
| 885 | generateAndMaintainConfiguration(ctx, client, clientset) |
| 886 | |
| 887 | conn.Close() |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | // generateAndMaintainConfiguration does the pod-reconciliation work, |
| 892 | // it only returns once all sub-processes have completed |
| 893 | func generateAndMaintainConfiguration(ctx context.Context, client pb.ConfigurationClient, clientset *kubernetes.Clientset) { |
| 894 | // Set up the regular expression to identify the voltha cores |
| 895 | rwCoreFltr := regexp.MustCompile(`rw-core[0-9]-`) |
| 896 | roCoreFltr := regexp.MustCompile(`ro-core-`) |
| 897 | |
| 898 | // Get the voltha rw-core podes |
| 899 | rwPods, err := getVolthaPods(clientset, rwCoreFltr) |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 900 | if err != nil { |
Kent Hagerman | e566c2e | 2019-06-03 17:56:42 -0400 | [diff] [blame] | 901 | panic(err) |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 902 | } |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 903 | |
| 904 | // Fetch the devices held by each running core |
sslobodr | e7ce71d | 2019-01-22 16:21:45 -0500 | [diff] [blame] | 905 | queryDeviceIds(rwPods) |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 906 | |
| 907 | // For debugging... comment out l8r |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 908 | for _, v := range rwPods { |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 909 | log.Debugf("Pod list %v", *v) |
| 910 | } |
| 911 | |
| 912 | coreGroups := groupPods1(rwPods) |
| 913 | |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 914 | // Assign the groupings to the the backends and connections |
Kent Hagerman | 334a8ce | 2019-05-16 16:50:33 -0400 | [diff] [blame] | 915 | for k := range coreGroups { |
| 916 | for k1 := range coreGroups[k] { |
| 917 | coreGroups[k][k1].cluster = afrouterRWClusterName |
| 918 | coreGroups[k][k1].backend = afrouterRWClusterName + strconv.Itoa(k+1) |
| 919 | coreGroups[k][k1].connection = afrouterRWClusterName + strconv.Itoa(k+1) + strconv.Itoa(k1+1) |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 920 | } |
| 921 | } |
Kent Hagerman | 334a8ce | 2019-05-16 16:50:33 -0400 | [diff] [blame] | 922 | log.Info("Core grouping completed") |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 923 | |
| 924 | // TODO: Debugging code, comment out for production |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 925 | for k, v := range coreGroups { |
| 926 | for k2, v2 := range v { |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 927 | log.Debugf("Core group %d,%d: %v", k, k2, v2) |
| 928 | } |
| 929 | } |
sslobodr | cd37bc5 | 2019-01-24 11:47:16 -0500 | [diff] [blame] | 930 | log.Info("Setting affinities") |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 931 | // Now set the affinities for exising devices in the cores |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 932 | for _, v := range coreGroups { |
Kent Hagerman | 9266146 | 2019-06-04 18:22:05 -0400 | [diff] [blame] | 933 | setAffinity(ctx, client, v[0].devIds, v[0].backend) |
| 934 | setAffinity(ctx, client, v[1].devIds, v[1].backend) |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 935 | } |
sslobodr | cd37bc5 | 2019-01-24 11:47:16 -0500 | [diff] [blame] | 936 | log.Info("Setting connections") |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 937 | // Configure the backeds based on the calculated core groups |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 938 | for _, v := range coreGroups { |
Kent Hagerman | 9266146 | 2019-06-04 18:22:05 -0400 | [diff] [blame] | 939 | setConnection(ctx, client, afrouterRWClusterName, v[0].backend, v[0].connection, v[0].ipAddr, podGrpcPort) |
| 940 | setConnection(ctx, client, afrouterRWClusterName, v[1].backend, v[1].connection, v[1].ipAddr, podGrpcPort) |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | // Process the read only pods |
Kent Hagerman | 9266146 | 2019-06-04 18:22:05 -0400 | [diff] [blame] | 944 | roPods, err := getVolthaPods(clientset, roCoreFltr) |
| 945 | if err != nil { |
| 946 | panic(err) |
| 947 | } |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 948 | for k, v := range roPods { |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 949 | log.Debugf("Processing ro_pod %v", v) |
Kent Hagerman | 334a8ce | 2019-05-16 16:50:33 -0400 | [diff] [blame] | 950 | vN := afrouterROClusterName + strconv.Itoa(k+1) |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 951 | log.Debugf("Setting connection %s, %s, %s", vN, vN+"1", v.ipAddr) |
Kent Hagerman | 334a8ce | 2019-05-16 16:50:33 -0400 | [diff] [blame] | 952 | roPods[k].cluster = afrouterROClusterName |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 953 | roPods[k].backend = vN |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 954 | roPods[k].connection = vN + "1" |
Kent Hagerman | 9266146 | 2019-06-04 18:22:05 -0400 | [diff] [blame] | 955 | setConnection(ctx, client, afrouterROClusterName, v.backend, v.connection, v.ipAddr, podGrpcPort) |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 956 | } |
| 957 | |
sslobodr | cd37bc5 | 2019-01-24 11:47:16 -0500 | [diff] [blame] | 958 | log.Info("Starting discovery monitoring") |
Kent Hagerman | 9266146 | 2019-06-04 18:22:05 -0400 | [diff] [blame] | 959 | doneCh, _ := startDiscoveryMonitor(ctx, client, coreGroups) |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 960 | |
sslobodr | cd37bc5 | 2019-01-24 11:47:16 -0500 | [diff] [blame] | 961 | log.Info("Starting core monitoring") |
Kent Hagerman | 9266146 | 2019-06-04 18:22:05 -0400 | [diff] [blame] | 962 | startCoreMonitor(ctx, client, clientset, rwCoreFltr, roCoreFltr, coreGroups, roPods) |
| 963 | |
| 964 | //ensure the discovery monitor to quit |
| 965 | <-doneCh |
sslobodr | 16e41bc | 2019-01-18 16:22:21 -0500 | [diff] [blame] | 966 | } |