Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020-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 core |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "encoding/json" |
| 22 | "fmt" |
| 23 | "github.com/opencord/bbsim-sadis-server/internal/utils" |
| 24 | "github.com/opencord/voltha-lib-go/v4/pkg/log" |
| 25 | v1 "k8s.io/api/core/v1" |
| 26 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
Matteo Scandolo | abf872d | 2020-12-14 08:22:06 -1000 | [diff] [blame] | 27 | "k8s.io/apimachinery/pkg/watch" |
Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 28 | "k8s.io/client-go/kubernetes" |
| 29 | "net/http" |
| 30 | "sync" |
Matteo Scandolo | 5b6eb8d | 2020-12-15 12:21:33 -1000 | [diff] [blame] | 31 | "time" |
Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 32 | ) |
| 33 | |
Matteo Scandolo | 5b6eb8d | 2020-12-15 12:21:33 -1000 | [diff] [blame] | 34 | const attemptLimit = 10 |
| 35 | |
Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 36 | type Watcher struct { |
| 37 | client *kubernetes.Clientset |
| 38 | store *Store |
| 39 | config *utils.ConfigFlags |
| 40 | } |
| 41 | |
| 42 | func NewWatcher(client *kubernetes.Clientset, store *Store, cf *utils.ConfigFlags) *Watcher { |
| 43 | return &Watcher{ |
| 44 | client: client, |
| 45 | store: store, |
| 46 | config: cf, |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | func (w *Watcher) Watch(ctx context.Context, wg *sync.WaitGroup) { |
| 51 | defer wg.Done() |
Matteo Scandolo | abf872d | 2020-12-14 08:22:06 -1000 | [diff] [blame] | 52 | |
| 53 | // we need to watch for PODs, services can't respond to requests if the backend is not there |
| 54 | // note that when this container starts we receive notifications for all of the existing pods |
| 55 | |
| 56 | watcher, err := w.client.CoreV1().Pods("").Watch(context.TODO(), metav1.ListOptions{LabelSelector: "app=bbsim"}) |
| 57 | if err != nil { |
| 58 | logger.Fatalw(ctx, "error-while-watching-pods", log.Fields{"err": err}) |
| 59 | } |
| 60 | |
| 61 | ch := watcher.ResultChan() |
| 62 | for event := range ch { |
| 63 | |
| 64 | pod, ok := event.Object.(*v1.Pod) |
| 65 | if !ok { |
| 66 | logger.Fatalw(ctx, "unexpected-type-while-watching-pod", log.Fields{"object": event.Object}) |
Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 67 | } |
Matteo Scandolo | abf872d | 2020-12-14 08:22:06 -1000 | [diff] [blame] | 68 | |
| 69 | logger.Debugw(ctx, "received-pod-event", log.Fields{"object": event.Type, "pod": pod.Name}) |
| 70 | if event.Type == watch.Deleted { |
| 71 | // TODO remove sadis entries |
| 72 | logger.Debug(ctx, "pod-has-been-removed") |
| 73 | } |
| 74 | |
| 75 | if event.Type == watch.Added || event.Type == watch.Modified { |
| 76 | // fetch the sadis information and store them |
| 77 | |
| 78 | // the pod is ready only if all the containers in it are ready, |
| 79 | // for now the BBSim pod only has 1 container, but things may change in the future, so keep the loop |
| 80 | ready := true |
| 81 | |
| 82 | if len(pod.Status.ContainerStatuses) == 0 { |
| 83 | // if there are no containers in the pod, then it's not ready |
| 84 | ready = false |
| 85 | } |
| 86 | |
| 87 | for _, containerStatus := range pod.Status.ContainerStatuses { |
| 88 | if !containerStatus.Ready { |
| 89 | // if one of the container is not ready, then the entire pod is not ready |
| 90 | ready = false |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | logger.Debugw(ctx, "received-event-for-bbsim-pod", log.Fields{"pod": pod.Name, "namespace": pod.Namespace, |
| 95 | "release": pod.Labels["release"], "ready": ready}) |
| 96 | |
| 97 | // as soon as the pod is ready cache the sadis entries |
| 98 | if ready { |
| 99 | // note that we should one service |
| 100 | labelSelector := fmt.Sprintf("app=bbsim,release=%s", pod.Labels["release"]) |
| 101 | services, err := w.client.CoreV1().Services(pod.Namespace).List(context.TODO(), metav1.ListOptions{LabelSelector: labelSelector}) |
| 102 | |
| 103 | if err != nil { |
| 104 | logger.Fatalw(ctx, "error-while-listing-services", log.Fields{"err": err}) |
| 105 | } |
| 106 | |
| 107 | w.handleServices(ctx, services) |
| 108 | } |
| 109 | } |
| 110 | |
Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
| 114 | func (w *Watcher) handleServices(ctx context.Context, services *v1.ServiceList) { |
| 115 | // TODO if a service is removed we'll want to remove the related entries |
| 116 | for _, service := range services.Items { |
Matteo Scandolo | 5b6eb8d | 2020-12-15 12:21:33 -1000 | [diff] [blame] | 117 | go func(service v1.Service) { |
| 118 | if err := w.queryService(ctx, service, 0); err != nil { |
| 119 | logger.Errorw(ctx, "error-while-reading-from-service", log.Fields{"error": err.Error()}) |
| 120 | } |
| 121 | }(service) |
Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
Matteo Scandolo | 5b6eb8d | 2020-12-15 12:21:33 -1000 | [diff] [blame] | 125 | func (w *Watcher) queryService(ctx context.Context, service v1.Service, attempt int) error { |
Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 126 | endpoint := fmt.Sprintf("%s.%s.svc:%d", service.Name, service.Namespace, w.config.BBsimSadisPort) |
| 127 | logger.Infow(ctx, "querying-service", log.Fields{"endpoint": endpoint}) |
| 128 | |
| 129 | res, err := http.Get(fmt.Sprintf("http://%s/v2/static", endpoint)) |
| 130 | |
| 131 | if err != nil { |
Matteo Scandolo | 5b6eb8d | 2020-12-15 12:21:33 -1000 | [diff] [blame] | 132 | if attempt < attemptLimit { |
| 133 | logger.Warnw(ctx, "error-while-reading-from-service-retrying", log.Fields{"error": err.Error()}) |
| 134 | // if there is an error and we have attempt left just retry later |
| 135 | time.Sleep(1 * time.Second) |
| 136 | return w.queryService(ctx, service, attempt+1) |
| 137 | } |
| 138 | |
Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 139 | return err |
| 140 | } |
| 141 | |
| 142 | if res.Body != nil { |
| 143 | defer res.Body.Close() |
| 144 | } |
| 145 | |
| 146 | var result SadisConfig |
| 147 | |
| 148 | decoder := json.NewDecoder(res.Body) |
| 149 | if err := decoder.Decode(&result); err != nil { |
| 150 | logger.Errorw(ctx, "cannot-decode-sadis-response", log.Fields{"error": err.Error()}) |
| 151 | return err |
| 152 | } |
| 153 | |
| 154 | logger.Debugw(ctx, "fetched-sadis-config", log.Fields{ |
| 155 | "endpoint": endpoint, |
| 156 | "entries": len(result.Sadis.Entries), |
| 157 | "bandwidthProfiles": len(result.BandwidthProfile.Entries), |
| 158 | }) |
| 159 | |
| 160 | //for _, entry := range result.Sadis.Entries { |
| 161 | // switch entry.(type) { |
| 162 | // case SadisOltEntry: |
| 163 | // case *SadisOltEntry: |
| 164 | // logger.Infow(ctx, "olt-entry", log.Fields{"entry": entry}) |
| 165 | // case SadisOnuEntryV2: |
| 166 | // case *SadisOnuEntryV2: |
| 167 | // logger.Infow(ctx, "onu-entry", log.Fields{"entry": entry}) |
| 168 | // default: |
| 169 | // logger.Warnw(ctx, "unknown-entity", log.Fields{"entry": entry}) |
| 170 | // } |
| 171 | //} |
| 172 | |
| 173 | for _, entry := range result.Sadis.Entries { |
| 174 | if entry.HardwareIdentifier != "" { |
| 175 | e := SadisOltEntry{ |
| 176 | ID: entry.ID, |
| 177 | HardwareIdentifier: entry.HardwareIdentifier, |
| 178 | IPAddress: entry.IPAddress, |
| 179 | NasID: entry.NasID, |
| 180 | UplinkPort: entry.UplinkPort, |
| 181 | } |
| 182 | w.store.addOlt(ctx, e) |
| 183 | continue |
| 184 | } |
| 185 | if len(entry.UniTagList) != 0 { |
| 186 | e := SadisOnuEntryV2{ |
| 187 | ID: entry.ID, |
| 188 | NasPortID: entry.NasPortID, |
| 189 | CircuitID: entry.CircuitID, |
| 190 | RemoteID: entry.RemoteID, |
| 191 | UniTagList: entry.UniTagList, |
| 192 | } |
| 193 | w.store.addOnu(ctx, e) |
| 194 | continue |
| 195 | } |
| 196 | logger.Warnw(ctx, "unknown-entity", log.Fields{"entry": entry}) |
| 197 | } |
| 198 | |
| 199 | for _, bp := range result.BandwidthProfile.Entries { |
| 200 | w.store.addBp(ctx, *bp) |
| 201 | } |
| 202 | |
| 203 | logger.Infow(ctx, "stored-sadis-config", log.Fields{"endpoint": endpoint}) |
| 204 | |
| 205 | return nil |
| 206 | } |