Skip to content

Commit 6ea6950

Browse files
authored
[Feature] Extract Integration Service (#1591)
1 parent 61cdd68 commit 6ea6950

File tree

23 files changed

+743
-259
lines changed

23 files changed

+743
-259
lines changed

.golangci.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ linters-settings:
4949
alias: mlSharedTests
5050
- pkg: github.com/arangodb/kube-arangodb/pkg/apis/ml/v1alpha1
5151
alias: mlApi
52+
- pkg: github.com/arangodb/kube-arangodb/integrations/shutdown/v1/definition
53+
alias: pbShutdownV1
54+
- pkg: github.com/arangodb/kube-arangodb/integrations/shutdown/v1
55+
alias: pbImplShutdownV1
56+
- pkg: github.com/arangodb/kube-arangodb/integrations/shared/v1/definition
57+
alias: pbSharedV1
58+
- pkg: github.com/arangodb/kube-arangodb/integrations/shared/v1
59+
alias: pbImplSharedV1
5260
gci:
5361
sections:
5462
- standard

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## [master](https://github.com/arangodb/kube-arangodb/tree/master) (N/A)
44
- (Feature) Extract GRPC Server
5+
- (Feature) Extract Integration Service
56

67
## [1.2.37](https://github.com/arangodb/kube-arangodb/tree/1.2.37) (2024-01-22)
78
- (Documentation) Improve documentation rendering for GitHub Pages
Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// DISCLAIMER
33
//
4-
// Copyright 2023 ArangoDB GmbH, Cologne, Germany
4+
// Copyright 2024 ArangoDB GmbH, Cologne, Germany
55
//
66
// Licensed under the Apache License, Version 2.0 (the "License");
77
// you may not use this file except in compliance with the License.
@@ -20,20 +20,10 @@
2020

2121
package cmd
2222

23-
import (
24-
"github.com/spf13/cobra"
25-
)
26-
27-
var (
28-
cmdML = &cobra.Command{
29-
Use: "ml",
30-
Run: func(cmd *cobra.Command, args []string) {
31-
32-
},
33-
Hidden: true,
34-
}
35-
)
23+
import "github.com/arangodb/kube-arangodb/cmd/integrations"
3624

3725
func init() {
38-
cmdMain.AddCommand(cmdML)
26+
if err := integrations.Register(&cmdMain); err != nil {
27+
panic(err.Error())
28+
}
3929
}

cmd/integrations/integration.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
"context"
25+
26+
"github.com/spf13/cobra"
27+
28+
"github.com/arangodb/kube-arangodb/pkg/util/svc"
29+
)
30+
31+
type Factory func() Integration
32+
33+
type ArgGen func(name string) string
34+
35+
type Integration interface {
36+
Name() string
37+
Description() string
38+
39+
Register(cmd *cobra.Command, arg ArgGen) error
40+
41+
Handler(ctx context.Context) (svc.Handler, error)
42+
}

cmd/integrations/logger.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2016-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+
"time"
25+
26+
"github.com/arangodb/kube-arangodb/pkg/logging"
27+
)
28+
29+
var (
30+
logger = logging.Global().RegisterAndGetLogger("integrations", logging.Info, logging.WithSamplingPeriod(time.Second*10))
31+
)

cmd/integrations/register.go

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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+
}

cmd/integrations/shutdown_v1.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
"context"
25+
26+
"github.com/spf13/cobra"
27+
28+
pbImplShutdownV1 "github.com/arangodb/kube-arangodb/integrations/shutdown/v1"
29+
"github.com/arangodb/kube-arangodb/pkg/util/shutdown"
30+
"github.com/arangodb/kube-arangodb/pkg/util/svc"
31+
)
32+
33+
func init() {
34+
register(func() Integration {
35+
return &shutdownV1{}
36+
})
37+
}
38+
39+
type shutdownV1 struct {
40+
}
41+
42+
func (s *shutdownV1) Handler(ctx context.Context) (svc.Handler, error) {
43+
return shutdown.NewGlobalShutdownServer(), nil
44+
}
45+
46+
func (s *shutdownV1) Name() string {
47+
return pbImplShutdownV1.Name
48+
}
49+
50+
func (s *shutdownV1) Description() string {
51+
return "ShutdownV1 Handler"
52+
}
53+
54+
func (s *shutdownV1) Register(cmd *cobra.Command, arg ArgGen) error {
55+
return nil
56+
}

0 commit comments

Comments
 (0)