Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Extract Integration Service #1591

Merged
merged 1 commit into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ linters-settings:
alias: mlSharedTests
- pkg: github.com/arangodb/kube-arangodb/pkg/apis/ml/v1alpha1
alias: mlApi
- pkg: github.com/arangodb/kube-arangodb/integrations/shutdown/v1/definition
alias: pbShutdownV1
- pkg: github.com/arangodb/kube-arangodb/integrations/shutdown/v1
alias: pbImplShutdownV1
- pkg: github.com/arangodb/kube-arangodb/integrations/shared/v1/definition
alias: pbSharedV1
- pkg: github.com/arangodb/kube-arangodb/integrations/shared/v1
alias: pbImplSharedV1
gci:
sections:
- standard
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

## [1.2.37](https://github.com/arangodb/kube-arangodb/tree/1.2.37) (2024-01-22)
- (Documentation) Improve documentation rendering for GitHub Pages
Expand Down
20 changes: 5 additions & 15 deletions cmd/ml.go → cmd/integration.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// DISCLAIMER
//
// Copyright 2023 ArangoDB GmbH, Cologne, Germany
// Copyright 2024 ArangoDB GmbH, Cologne, Germany
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -20,20 +20,10 @@

package cmd

import (
"github.com/spf13/cobra"
)

var (
cmdML = &cobra.Command{
Use: "ml",
Run: func(cmd *cobra.Command, args []string) {

},
Hidden: true,
}
)
import "github.com/arangodb/kube-arangodb/cmd/integrations"

func init() {
cmdMain.AddCommand(cmdML)
if err := integrations.Register(&cmdMain); err != nil {
panic(err.Error())
}
}
42 changes: 42 additions & 0 deletions cmd/integrations/integration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// DISCLAIMER
//
// Copyright 2024 ArangoDB GmbH, Cologne, Germany
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//

package integrations

import (
"context"

"github.com/spf13/cobra"

"github.com/arangodb/kube-arangodb/pkg/util/svc"
)

type Factory func() Integration

type ArgGen func(name string) string

type Integration interface {
Name() string
Description() string

Register(cmd *cobra.Command, arg ArgGen) error

Handler(ctx context.Context) (svc.Handler, error)
}
31 changes: 31 additions & 0 deletions cmd/integrations/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// DISCLAIMER
//
// Copyright 2016-2024 ArangoDB GmbH, Cologne, Germany
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//

package integrations

import (
"time"

"github.com/arangodb/kube-arangodb/pkg/logging"
)

var (
logger = logging.Global().RegisterAndGetLogger("integrations", logging.Info, logging.WithSamplingPeriod(time.Second*10))
)
142 changes: 142 additions & 0 deletions cmd/integrations/register.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
//
// DISCLAIMER
//
// Copyright 2024 ArangoDB GmbH, Cologne, Germany
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//

package integrations

import (
"fmt"
"sort"
"sync"

"github.com/spf13/cobra"

"github.com/arangodb/kube-arangodb/pkg/util/errors"
"github.com/arangodb/kube-arangodb/pkg/util/shutdown"
"github.com/arangodb/kube-arangodb/pkg/util/svc"
)

var (
lock sync.Mutex
registered []Factory
)

func register(i Factory) {
lock.Lock()
defer lock.Unlock()

registered = append(registered, i)
}

func Register(cmd *cobra.Command) error {
var c configuration

return c.Register(cmd)
}

type configuration struct {
registered []Integration

health struct {
shutdownEnabled bool

config svc.Configuration
}

services struct {
config svc.Configuration
}
}

func (c *configuration) Register(cmd *cobra.Command) error {
lock.Lock()
defer lock.Unlock()

c.registered = make([]Integration, len(registered))
for id := range registered {
c.registered[id] = registered[id]()
}

sort.Slice(c.registered, func(i, j int) bool {
return c.registered[i].Name() < c.registered[j].Name()
})

subCommand := &cobra.Command{
Use: "integration",
RunE: c.run,
}

f := subCommand.Flags()

f.StringVar(&c.health.config.Address, "health.address", "0.0.0.0:9091", "Address to expose health service")
f.BoolVar(&c.health.shutdownEnabled, "health.shutdown.enabled", true, "Determines if shutdown service should be enabled and exposed")
f.StringVar(&c.services.config.Address, "services.address", "127.0.0.1:9092", "Address to expose services")

for _, service := range c.registered {
prefix := fmt.Sprintf("integration.%s", service.Name())

f.Bool(prefix, false, service.Description())

if err := service.Register(subCommand, func(name string) string {
return fmt.Sprintf("%s.%s", prefix, name)
}); err != nil {
return errors.Wrapf(err, "Unable to register service %s", service.Name())
}
}

cmd.AddCommand(subCommand)
return nil
}

func (c *configuration) run(cmd *cobra.Command, args []string) error {
handlers := make([]svc.Handler, 0, len(c.registered))

for _, handler := range c.registered {
if ok, err := cmd.Flags().GetBool(fmt.Sprintf("integration.%s", handler.Name())); err != nil {
return err
} else {
logger.Str("service", handler.Name()).Bool("enabled", ok).Info("Service discovered")
if ok {
if svc, err := handler.Handler(shutdown.Context()); err != nil {
return err
} else {
handlers = append(handlers, svc)
}
}
}
}

var healthServices []svc.Handler

if c.health.shutdownEnabled {
healthServices = append(healthServices, shutdown.NewGlobalShutdownServer())
}

health := svc.NewHealthService(c.health.config, svc.Readiness, healthServices...)

healthHandler := health.Start(shutdown.Context())

logger.Str("address", healthHandler.Address()).Info("Health handler started")

s := svc.NewService(c.services.config, handlers...).StartWithHealth(shutdown.Context(), health)

logger.Str("address", s.Address()).Info("Service handler started")

return s.Wait()
}
56 changes: 56 additions & 0 deletions cmd/integrations/shutdown_v1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//
// DISCLAIMER
//
// Copyright 2024 ArangoDB GmbH, Cologne, Germany
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//

package integrations

import (
"context"

"github.com/spf13/cobra"

pbImplShutdownV1 "github.com/arangodb/kube-arangodb/integrations/shutdown/v1"
"github.com/arangodb/kube-arangodb/pkg/util/shutdown"
"github.com/arangodb/kube-arangodb/pkg/util/svc"
)

func init() {
register(func() Integration {
return &shutdownV1{}
})
}

type shutdownV1 struct {
}

func (s *shutdownV1) Handler(ctx context.Context) (svc.Handler, error) {
return shutdown.NewGlobalShutdownServer(), nil
}

func (s *shutdownV1) Name() string {
return pbImplShutdownV1.Name
}

func (s *shutdownV1) Description() string {
return "ShutdownV1 Handler"
}

func (s *shutdownV1) Register(cmd *cobra.Command, arg ArgGen) error {
return nil
}
Loading