-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature] Extract Integration Service (#1591)
- Loading branch information
Showing
23 changed files
with
743 additions
and
259 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.