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 main |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "github.com/opencord/bbsim-sadis-server/internal/core" |
| 22 | "github.com/opencord/bbsim-sadis-server/internal/utils" |
| 23 | "github.com/opencord/voltha-lib-go/v4/pkg/log" |
| 24 | "k8s.io/client-go/kubernetes" |
| 25 | "k8s.io/client-go/rest" |
| 26 | "k8s.io/client-go/tools/clientcmd" |
| 27 | "sync" |
| 28 | ) |
| 29 | |
| 30 | var ( |
| 31 | logger log.CLogger |
| 32 | cf *utils.ConfigFlags |
| 33 | ) |
| 34 | |
| 35 | func init() { |
| 36 | ctx := context.Background() |
| 37 | |
| 38 | cf = utils.NewConfigFlags() |
| 39 | cf.ParseCommandArguments() |
| 40 | |
| 41 | // Setup this package so that it's log level can be modified at run time |
| 42 | var err error |
| 43 | logger, err = log.RegisterPackage(cf.LogFormat, log.ErrorLevel, log.Fields{}) |
| 44 | if err != nil { |
| 45 | panic(err) |
| 46 | } |
| 47 | |
| 48 | // Set the instance ID as the hostname |
| 49 | instanceID := utils.GetHostName() |
| 50 | |
| 51 | logLevel, err := log.StringToLogLevel(cf.LogLevel) |
| 52 | |
| 53 | if err != nil { |
| 54 | logger.Errorw(ctx, "provided-log-level-is-not-valid", log.Fields{"err": err, "providedLevel": cf.LogLevel}) |
| 55 | } |
| 56 | |
| 57 | //Setup default logger - applies for packages that do not have specific logger set |
| 58 | if _, err := log.SetDefaultLogger(cf.LogFormat, logLevel, log.Fields{"instanceId": instanceID}); err != nil { |
| 59 | logger.With(log.Fields{"error": err}).Fatal(ctx, "Cannot setup logging") |
| 60 | } |
| 61 | |
| 62 | // Update all loggers (provisioned via init) with a common field |
| 63 | if err := log.UpdateAllLoggers(log.Fields{"instanceId": instanceID}); err != nil { |
| 64 | logger.With(log.Fields{"error": err}).Fatal(ctx, "Cannot setup logging") |
| 65 | } |
| 66 | |
| 67 | // Update all loggers to log level specified as input parameter |
| 68 | log.SetAllLogLevel(logLevel) |
| 69 | |
| 70 | core.SetupLogger(logLevel, cf.LogFormat) |
| 71 | } |
| 72 | |
| 73 | func main() { |
| 74 | ctx := context.Background() |
| 75 | logger.Info(ctx, "bbsim-sadis-server-started") |
| 76 | |
| 77 | var config *rest.Config |
| 78 | var err error |
| 79 | |
| 80 | // if kubeconfig is provided use that, otherwise assume we're running within the cluster |
| 81 | if cf.Kubeconfig != "" { |
| 82 | config, err = clientcmd.BuildConfigFromFlags("", cf.Kubeconfig) |
| 83 | } else { |
| 84 | config, err = rest.InClusterConfig() |
| 85 | } |
| 86 | if err != nil { |
| 87 | panic(err.Error()) |
| 88 | } |
| 89 | |
| 90 | clientset, err := kubernetes.NewForConfig(config) |
| 91 | if err != nil { |
| 92 | panic(err.Error()) |
| 93 | } |
| 94 | |
| 95 | store := core.NewStore() |
| 96 | |
| 97 | watcher := core.NewWatcher(clientset, store, cf) |
| 98 | server := core.NewServer(store) |
| 99 | |
| 100 | wg := sync.WaitGroup{} |
| 101 | |
| 102 | wg.Add(2) |
| 103 | |
| 104 | go watcher.Watch(ctx, &wg) |
| 105 | go server.StartSadisServer(&wg) |
| 106 | |
| 107 | wg.Wait() |
| 108 | } |