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