|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2024 ArangoDB GmbH, Cologne, Germany |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +// Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 19 | +// |
| 20 | + |
| 21 | +package integrations |
| 22 | + |
| 23 | +import ( |
| 24 | + "fmt" |
| 25 | + "sort" |
| 26 | + "sync" |
| 27 | + |
| 28 | + "github.com/spf13/cobra" |
| 29 | + |
| 30 | + "github.com/arangodb/kube-arangodb/pkg/util/errors" |
| 31 | + "github.com/arangodb/kube-arangodb/pkg/util/shutdown" |
| 32 | + "github.com/arangodb/kube-arangodb/pkg/util/svc" |
| 33 | +) |
| 34 | + |
| 35 | +var ( |
| 36 | + lock sync.Mutex |
| 37 | + registered []Factory |
| 38 | +) |
| 39 | + |
| 40 | +func register(i Factory) { |
| 41 | + lock.Lock() |
| 42 | + defer lock.Unlock() |
| 43 | + |
| 44 | + registered = append(registered, i) |
| 45 | +} |
| 46 | + |
| 47 | +func Register(cmd *cobra.Command) error { |
| 48 | + var c configuration |
| 49 | + |
| 50 | + return c.Register(cmd) |
| 51 | +} |
| 52 | + |
| 53 | +type configuration struct { |
| 54 | + registered []Integration |
| 55 | + |
| 56 | + health struct { |
| 57 | + shutdownEnabled bool |
| 58 | + |
| 59 | + config svc.Configuration |
| 60 | + } |
| 61 | + |
| 62 | + services struct { |
| 63 | + config svc.Configuration |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func (c *configuration) Register(cmd *cobra.Command) error { |
| 68 | + lock.Lock() |
| 69 | + defer lock.Unlock() |
| 70 | + |
| 71 | + c.registered = make([]Integration, len(registered)) |
| 72 | + for id := range registered { |
| 73 | + c.registered[id] = registered[id]() |
| 74 | + } |
| 75 | + |
| 76 | + sort.Slice(c.registered, func(i, j int) bool { |
| 77 | + return c.registered[i].Name() < c.registered[j].Name() |
| 78 | + }) |
| 79 | + |
| 80 | + subCommand := &cobra.Command{ |
| 81 | + Use: "integration", |
| 82 | + RunE: c.run, |
| 83 | + } |
| 84 | + |
| 85 | + f := subCommand.Flags() |
| 86 | + |
| 87 | + f.StringVar(&c.health.config.Address, "health.address", "0.0.0.0:9091", "Address to expose health service") |
| 88 | + f.BoolVar(&c.health.shutdownEnabled, "health.shutdown.enabled", true, "Determines if shutdown service should be enabled and exposed") |
| 89 | + f.StringVar(&c.services.config.Address, "services.address", "127.0.0.1:9092", "Address to expose services") |
| 90 | + |
| 91 | + for _, service := range c.registered { |
| 92 | + prefix := fmt.Sprintf("integration.%s", service.Name()) |
| 93 | + |
| 94 | + f.Bool(prefix, false, service.Description()) |
| 95 | + |
| 96 | + if err := service.Register(subCommand, func(name string) string { |
| 97 | + return fmt.Sprintf("%s.%s", prefix, name) |
| 98 | + }); err != nil { |
| 99 | + return errors.Wrapf(err, "Unable to register service %s", service.Name()) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + cmd.AddCommand(subCommand) |
| 104 | + return nil |
| 105 | +} |
| 106 | + |
| 107 | +func (c *configuration) run(cmd *cobra.Command, args []string) error { |
| 108 | + handlers := make([]svc.Handler, 0, len(c.registered)) |
| 109 | + |
| 110 | + for _, handler := range c.registered { |
| 111 | + if ok, err := cmd.Flags().GetBool(fmt.Sprintf("integration.%s", handler.Name())); err != nil { |
| 112 | + return err |
| 113 | + } else { |
| 114 | + logger.Str("service", handler.Name()).Bool("enabled", ok).Info("Service discovered") |
| 115 | + if ok { |
| 116 | + if svc, err := handler.Handler(shutdown.Context()); err != nil { |
| 117 | + return err |
| 118 | + } else { |
| 119 | + handlers = append(handlers, svc) |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + var healthServices []svc.Handler |
| 126 | + |
| 127 | + if c.health.shutdownEnabled { |
| 128 | + healthServices = append(healthServices, shutdown.NewGlobalShutdownServer()) |
| 129 | + } |
| 130 | + |
| 131 | + health := svc.NewHealthService(c.health.config, svc.Readiness, healthServices...) |
| 132 | + |
| 133 | + healthHandler := health.Start(shutdown.Context()) |
| 134 | + |
| 135 | + logger.Str("address", healthHandler.Address()).Info("Health handler started") |
| 136 | + |
| 137 | + s := svc.NewService(c.services.config, handlers...).StartWithHealth(shutdown.Context(), health) |
| 138 | + |
| 139 | + logger.Str("address", s.Address()).Info("Service handler started") |
| 140 | + |
| 141 | + return s.Wait() |
| 142 | +} |
0 commit comments