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

[addon-operator] add sdk and batch hooks #529

Merged
merged 15 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 0 additions & 5 deletions .github/dependabot.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,3 @@ updates:
- dependency-name: "k8s.io/api"
- dependency-name: "k8s.io/apimachinery"
- dependency-name: "k8s.io/client-go"

- package-ecosystem: "docker"
ldmonster marked this conversation as resolved.
Show resolved Hide resolved
directory: "/"
schedule:
interval: "weekly"
10 changes: 5 additions & 5 deletions cmd/addon-operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"github.com/flant/addon-operator/pkg/kube_config_manager/backend/configmap"
"github.com/flant/addon-operator/pkg/utils/stdliblogtolog"
"github.com/flant/kube-client/klogtolog"
sh_app "github.com/flant/shell-operator/pkg/app"
shapp "github.com/flant/shell-operator/pkg/app"
"github.com/flant/shell-operator/pkg/debug"
utils_signal "github.com/flant/shell-operator/pkg/utils/signal"
)
Expand All @@ -39,10 +39,10 @@ func main() {
log.SetDefault(logger)

// override usage template to reveal additional commands with information about start command
kpApp.UsageTemplate(sh_app.OperatorUsageTemplate(app.AppName))
kpApp.UsageTemplate(shapp.OperatorUsageTemplate(app.AppName))

kpApp.Action(func(_ *kingpin.ParseContext) error {
klogtolog.InitAdapter(sh_app.DebugKubernetesAPI, logger.Named("klog"))
klogtolog.InitAdapter(shapp.DebugKubernetesAPI, logger.Named("klog"))
stdliblogtolog.InitAdapter(logger)
return nil
})
Expand All @@ -68,7 +68,7 @@ func main() {

func start(logger *log.Logger) func(_ *kingpin.ParseContext) error {
return func(_ *kingpin.ParseContext) error {
sh_app.AppStartMessage = fmt.Sprintf("%s %s, shell-operator %s", app.AppName, app.Version, sh_app.Version)
shapp.AppStartMessage = fmt.Sprintf("%s %s, shell-operator %s", app.AppName, app.Version, shapp.Version)

ctx := context.Background()

Expand All @@ -92,7 +92,7 @@ func start(logger *log.Logger) func(_ *kingpin.ParseContext) error {
}

func run(ctx context.Context, operator *addon_operator.AddonOperator) error {
bk := configmap.New(operator.KubeClient(), app.Namespace, app.ConfigMapName, operator.Logger.Named("kube-config-manager"))
bk := configmap.New(operator.KubeClient(), operator.DefaultNamespace, app.ConfigMapName, operator.Logger.Named("kube-config-manager"))
operator.SetupKubeConfigManager(bk)

if err := operator.Setup(); err != nil {
Expand Down
8 changes: 4 additions & 4 deletions examples/700-go-hook/global-hooks/global-go-hook.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package global_hooks

import (
"github.com/flant/addon-operator/pkg/module_manager/go_hook"
gohook "github.com/flant/addon-operator/pkg/module_manager/go_hook"
"github.com/flant/addon-operator/sdk"
)

var _ = sdk.RegisterFunc(&go_hook.HookConfig{
OnStartup: &go_hook.OrderedConfig{Order: 10},
var _ = sdk.RegisterFunc(&gohook.HookConfig{
OnStartup: &gohook.OrderedConfig{Order: 10},
}, handler)

func handler(input *go_hook.HookInput) error {
func handler(input *gohook.HookInput) error {
input.Logger.Infof("Start Global Go hook")
return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,30 @@ import (
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

"github.com/flant/addon-operator/pkg/module_manager/go_hook"
gohook "github.com/flant/addon-operator/pkg/module_manager/go_hook"
"github.com/flant/addon-operator/sdk"
)

var _ = sdk.RegisterFunc(&go_hook.HookConfig{
OnStartup: &go_hook.OrderedConfig{
var _ = sdk.RegisterFunc(&gohook.HookConfig{
OnStartup: &gohook.OrderedConfig{
Order: 10,
},

OnBeforeHelm: &go_hook.OrderedConfig{
OnBeforeHelm: &gohook.OrderedConfig{
Order: 10,
},

Kubernetes: []go_hook.KubernetesConfig{
Kubernetes: []gohook.KubernetesConfig{
{
Name: "pods",
ApiVersion: "v1",
Kind: "Pods",
FilterFunc: ObjFilter,
ExecuteHookOnSynchronization: go_hook.Bool(true),
ExecuteHookOnSynchronization: gohook.Bool(true),
},
},

Schedule: []go_hook.ScheduleConfig{
Schedule: []gohook.ScheduleConfig{
{
Name: "metrics",
Crontab: "*/5 * * * * *",
Expand All @@ -39,7 +39,7 @@ var _ = sdk.RegisterFunc(&go_hook.HookConfig{

type podSpecFilteredObj v1.PodSpec

func ObjFilter(obj *unstructured.Unstructured) (go_hook.FilterResult, error) {
func ObjFilter(obj *unstructured.Unstructured) (gohook.FilterResult, error) {
pod := &v1.Pod{}
err := sdk.FromUnstructured(obj, pod)
if err != nil {
Expand All @@ -51,7 +51,7 @@ func ObjFilter(obj *unstructured.Unstructured) (go_hook.FilterResult, error) {
return &podSpec, nil
}

func run(input *go_hook.HookInput) error {
func run(input *gohook.HookInput) error {
for _, o := range input.Snapshots["pods"] {
podSpec := o.(*podSpecFilteredObj)
input.Logger.Infof("Got podSpec: %+v", podSpec)
Expand Down
2 changes: 1 addition & 1 deletion examples/700-go-hook/register_go_hooks.go.tpl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

import (
_ "github.com/flant/addon-operator/sdk/registry"
_ "github.com/flant/addon-operator/sdk"

_ "github.com/flant/addon-operator/examples/700-go-hook/global-hooks"
_ "github.com/flant/addon-operator/examples/700-go-hook/modules/001-module-go-hooks/hooks"
Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ go 1.22.8
require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc
github.com/deckhouse/deckhouse/pkg/log v0.0.0-20241106140903-258b93b3334e
github.com/deckhouse/module-sdk v0.0.0-20241204090122-4c2df6ebf909
github.com/dominikbraun/graph v0.23.0
github.com/ettle/strcase v0.2.0
github.com/flant/kube-client v1.2.2
github.com/flant/shell-operator v1.5.1
github.com/flant/shell-operator v0.0.0-20241129085258-450318914091
github.com/go-chi/chi/v5 v5.1.0
github.com/go-openapi/loads v0.19.5
github.com/go-openapi/spec v0.19.8
Expand Down Expand Up @@ -60,7 +61,7 @@ require (
github.com/containerd/platforms v0.2.1 // indirect
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
github.com/distribution/reference v0.6.0 // indirect
github.com/docker/cli v27.1.0+incompatible // indirect
github.com/docker/cli v27.1.1+incompatible // indirect
github.com/docker/distribution v2.8.3+incompatible // indirect
github.com/docker/docker v27.1.1+incompatible // indirect
github.com/docker/docker-credential-helpers v0.7.0 // indirect
Expand Down
10 changes: 6 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,16 @@ github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/deckhouse/deckhouse/pkg/log v0.0.0-20241106140903-258b93b3334e h1:QUQy+5Bv7/UzhfrytiG3c5gfLGhPppepVbRpbMisVIw=
github.com/deckhouse/deckhouse/pkg/log v0.0.0-20241106140903-258b93b3334e/go.mod h1:Mk5HRzkc5pIcDIZ2JJ6DPuuqnwhXVkb3you8M8Mg+4w=
github.com/deckhouse/module-sdk v0.0.0-20241204090122-4c2df6ebf909 h1:ve76LXrYsDPJiLPHySiF0jqPF+H9wGO8Kp+NzeOec/Y=
github.com/deckhouse/module-sdk v0.0.0-20241204090122-4c2df6ebf909/go.mod h1:0gxlSr0WRrGnB82J+45FBIXNP/fKl8+yEvRTzrygiAE=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/distribution/distribution/v3 v3.0.0-beta.1 h1:X+ELTxPuZ1Xe5MsD3kp2wfGUhc8I+MPfRis8dZ818Ic=
github.com/distribution/distribution/v3 v3.0.0-beta.1/go.mod h1:O9O8uamhHzWWQVTjuQpyYUVm/ShPHPUDgvQMpHGVBDs=
github.com/distribution/reference v0.6.0 h1:0IXCQ5g4/QMHHkarYzh5l+u8T3t73zM5QvfrDyIgxBk=
github.com/distribution/reference v0.6.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E=
github.com/docker/cli v27.1.0+incompatible h1:P0KSYmPtNbmx59wHZvG6+rjivhKDRA1BvvWM0f5DgHc=
github.com/docker/cli v27.1.0+incompatible/go.mod h1:JLrzqnKDaYBop7H2jaqPtU4hHvMKP+vjCwu2uszcLI8=
github.com/docker/cli v27.1.1+incompatible h1:goaZxOqs4QKxznZjjBWKONQci/MywhtRv2oNn0GkeZE=
github.com/docker/cli v27.1.1+incompatible/go.mod h1:JLrzqnKDaYBop7H2jaqPtU4hHvMKP+vjCwu2uszcLI8=
github.com/docker/distribution v2.8.3+incompatible h1:AtKxIZ36LoNK51+Z6RpzLpddBirtxJnzDrHLEKxTAYk=
github.com/docker/distribution v2.8.3+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w=
github.com/docker/docker v27.1.1+incompatible h1:hO/M4MtV36kzKldqnA37IWhebRA+LnqqcqDja6kVaKY=
Expand Down Expand Up @@ -136,8 +138,8 @@ github.com/flant/kube-client v1.2.2 h1:27LBs+PKJEFnkQXjPU9eIps7a7iyI13AKcSYj897D
github.com/flant/kube-client v1.2.2/go.mod h1:eMa3aJ6V1PRWSQ/RCROkObDpY4S74uM84SJS4G/LINg=
github.com/flant/libjq-go v1.6.3-0.20201126171326-c46a40ff22ee h1:evii83J+/6QGNvyf6tjQ/p27DPY9iftxIBb37ALJRTg=
github.com/flant/libjq-go v1.6.3-0.20201126171326-c46a40ff22ee/go.mod h1:f+REaGl/+pZR97rbTcwHEka/MAipoQQ2Mc0iQUj4ak0=
github.com/flant/shell-operator v1.5.1 h1:FnNKEOUipM7eC87zP8QeFDkvRNcU6htUEhdwMKa3U68=
github.com/flant/shell-operator v1.5.1/go.mod h1:Kbq8JNtKfoT8aqbHxygvgd6WoMImne8/upAYx9QyHUA=
github.com/flant/shell-operator v0.0.0-20241129085258-450318914091 h1:h6xm463wxgDWiqwOsJZckTewFlCf4LzibCkI58hoORo=
github.com/flant/shell-operator v0.0.0-20241129085258-450318914091/go.mod h1:Kbq8JNtKfoT8aqbHxygvgd6WoMImne8/upAYx9QyHUA=
github.com/fogleman/gg v1.3.0 h1:/7zJX8F6AaYQc57WQCyN9cAIz+4bCJGO9B+dyW29am8=
github.com/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
github.com/foxcpp/go-mockdns v1.0.0 h1:7jBqxd3WDWwi/6WhDvacvH1XsN3rOLXyHM1uhvIx6FI=
Expand Down
16 changes: 9 additions & 7 deletions pkg/addon-operator/bootstrap.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,40 @@
package addon_operator

import (
"fmt"

"github.com/deckhouse/deckhouse/pkg/log"

"github.com/flant/addon-operator/pkg/app"
"github.com/flant/addon-operator/pkg/kube_config_manager"
"github.com/flant/addon-operator/pkg/kube_config_manager/backend"
"github.com/flant/addon-operator/pkg/module_manager"
sh_app "github.com/flant/shell-operator/pkg/app"
shapp "github.com/flant/shell-operator/pkg/app"
"github.com/flant/shell-operator/pkg/debug"
shell_operator "github.com/flant/shell-operator/pkg/shell-operator"
)

// Bootstrap inits all dependencies for a full-fledged AddonOperator instance.
func (op *AddonOperator) bootstrap() error {
log.Info(sh_app.AppStartMessage)
log.Info(shapp.AppStartMessage)

log.Infof("Search modules in: %s", app.ModulesDir)

log.Infof("Addon-operator namespace: %s", app.Namespace)
log.Infof("Addon-operator namespace: %s", op.DefaultNamespace)

// Debug server.
// TODO: rewrite sh_app global variables to the addon-operator ones
// TODO: rewrite shapp global variables to the addon-operator ones
var err error
op.DebugServer, err = shell_operator.RunDefaultDebugServer(sh_app.DebugUnixSocket, sh_app.DebugHttpServerAddr, op.Logger.Named("debug-server"))
op.DebugServer, err = shell_operator.RunDefaultDebugServer(shapp.DebugUnixSocket, shapp.DebugHttpServerAddr, op.Logger.Named("debug-server"))
if err != nil {
log.Errorf("Fatal: start Debug server: %s", err)
return err
return fmt.Errorf("start Debug server: %w", err)
}

err = op.Assemble(op.DebugServer)
if err != nil {
log.Errorf("Fatal: %s", err)
return err
return fmt.Errorf("assemble Debug server: %w", err)
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/addon-operator/debug_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (op *AddonOperator) RegisterDebugModuleRoutes(dbgSrv *debug.Server) {
deps := &modules.HelmModuleDependencies{
HelmClientFactory: op.Helm,
}
hm, err := modules.NewHelmModule(m, op.ModuleManager.TempDir, deps, nil, op.Logger.Named("helm-module"))
hm, err := modules.NewHelmModule(m, op.DefaultNamespace, op.ModuleManager.TempDir, deps, nil, op.Logger.Named("helm-module"))
if err != nil {
return nil, fmt.Errorf("failed to create helm module: %w", err)
}
Expand Down
14 changes: 7 additions & 7 deletions pkg/addon-operator/kube_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/flant/addon-operator/pkg/app"
"github.com/flant/addon-operator/pkg/helm_resources_manager"
klient "github.com/flant/kube-client/client"
sh_app "github.com/flant/shell-operator/pkg/app"
"github.com/flant/shell-operator/pkg/metric_storage"
shapp "github.com/flant/shell-operator/pkg/app"
metricstorage "github.com/flant/shell-operator/pkg/metric_storage"
utils "github.com/flant/shell-operator/pkg/utils/labels"
)

Expand All @@ -19,17 +19,17 @@ import (
var DefaultHelmMonitorKubeClientMetricLabels = map[string]string{"component": "helm_monitor"}

// defaultHelmMonitorKubeClient initializes a Kubernetes client for helm monitor.
func defaultHelmMonitorKubeClient(metricStorage *metric_storage.MetricStorage, metricLabels map[string]string, logger *log.Logger) *klient.Client {
func defaultHelmMonitorKubeClient(metricStorage *metricstorage.MetricStorage, metricLabels map[string]string, logger *log.Logger) *klient.Client {
client := klient.New(klient.WithLogger(logger))
client.WithContextName(sh_app.KubeContext)
client.WithConfigPath(sh_app.KubeConfig)
client.WithContextName(shapp.KubeContext)
client.WithConfigPath(shapp.KubeConfig)
client.WithRateLimiterSettings(app.HelmMonitorKubeClientQps, app.HelmMonitorKubeClientBurst)
client.WithMetricStorage(metricStorage)
client.WithMetricLabels(utils.DefaultIfEmpty(metricLabels, DefaultHelmMonitorKubeClientMetricLabels))
return client
}

func InitDefaultHelmResourcesManager(ctx context.Context, metricStorage *metric_storage.MetricStorage, logger *log.Logger) (helm_resources_manager.HelmResourcesManager, error) {
func InitDefaultHelmResourcesManager(ctx context.Context, namespace string, metricStorage *metricstorage.MetricStorage, logger *log.Logger) (helm_resources_manager.HelmResourcesManager, error) {
kubeClient := defaultHelmMonitorKubeClient(metricStorage, DefaultHelmMonitorKubeClientMetricLabels, logger.Named("helm-monitor-kube-client"))
if err := kubeClient.Init(); err != nil {
return nil, fmt.Errorf("initialize Kubernetes client for Helm resources manager: %s\n", err)
Expand All @@ -38,6 +38,6 @@ func InitDefaultHelmResourcesManager(ctx context.Context, metricStorage *metric_
if err != nil {
return nil, fmt.Errorf("initialize Helm resources manager: %s\n", err)
}
mgr.WithDefaultNamespace(app.Namespace)
mgr.WithDefaultNamespace(namespace)
return mgr, nil
}
8 changes: 4 additions & 4 deletions pkg/addon-operator/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package addon_operator
import (
"time"

"github.com/flant/shell-operator/pkg/metric_storage"
metricstorage "github.com/flant/shell-operator/pkg/metric_storage"
"github.com/flant/shell-operator/pkg/task/queue"
)

Expand All @@ -17,7 +17,7 @@ var buckets_1msTo10s = []float64{
}

// registerHookMetrics register metrics specified for addon-operator
func registerHookMetrics(metricStorage *metric_storage.MetricStorage) {
func registerHookMetrics(metricStorage *metricstorage.MetricStorage) {
// configuration metrics
metricStorage.RegisterGauge(
"{PREFIX}binding_count",
Expand Down Expand Up @@ -124,7 +124,7 @@ func registerHookMetrics(metricStorage *metric_storage.MetricStorage) {
})
}

func StartLiveTicksUpdater(metricStorage *metric_storage.MetricStorage) {
func StartLiveTicksUpdater(metricStorage *metricstorage.MetricStorage) {
// Addon-operator live ticks.
go func() {
for {
Expand All @@ -134,7 +134,7 @@ func StartLiveTicksUpdater(metricStorage *metric_storage.MetricStorage) {
}()
}

func StartTasksQueueLengthUpdater(metricStorage *metric_storage.MetricStorage, tqs *queue.TaskQueueSet) {
func StartTasksQueueLengthUpdater(metricStorage *metricstorage.MetricStorage, tqs *queue.TaskQueueSet) {
go func() {
for {
// Gather task queues lengths.
Expand Down
Loading
Loading