blob: eb328e1ff7c49ea0e22ef39fdd77513416b16a77 [file] [log] [blame]
Matteo Scandoloa4285862020-12-01 18:10:10 -08001/*
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
17package core
18
19import (
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 Scandoloabf872d2020-12-14 08:22:06 -100027 "k8s.io/apimachinery/pkg/watch"
Matteo Scandoloa4285862020-12-01 18:10:10 -080028 "k8s.io/client-go/kubernetes"
29 "net/http"
30 "sync"
Matteo Scandolo5b6eb8d2020-12-15 12:21:33 -100031 "time"
Matteo Scandoloa4285862020-12-01 18:10:10 -080032)
33
Matteo Scandolo5b6eb8d2020-12-15 12:21:33 -100034const attemptLimit = 10
35
Matteo Scandoloa4285862020-12-01 18:10:10 -080036type Watcher struct {
37 client *kubernetes.Clientset
38 store *Store
39 config *utils.ConfigFlags
40}
41
42func 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
50func (w *Watcher) Watch(ctx context.Context, wg *sync.WaitGroup) {
51 defer wg.Done()
Matteo Scandoloabf872d2020-12-14 08:22:06 -100052
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 Scandoloa4285862020-12-01 18:10:10 -080067 }
Matteo Scandoloabf872d2020-12-14 08:22:06 -100068
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 Scandoloa4285862020-12-01 18:10:10 -0800111 }
112}
113
114func (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 Scandolo5b6eb8d2020-12-15 12:21:33 -1000117 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 Scandoloa4285862020-12-01 18:10:10 -0800122 }
123}
124
Matteo Scandolo5b6eb8d2020-12-15 12:21:33 -1000125func (w *Watcher) queryService(ctx context.Context, service v1.Service, attempt int) error {
Matteo Scandoloa4285862020-12-01 18:10:10 -0800126 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 Scandolo5b6eb8d2020-12-15 12:21:33 -1000132 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 Scandoloa4285862020-12-01 18:10:10 -0800139 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}