From 1eef6ca6e538effaf144a64825525858eb171beb Mon Sep 17 00:00:00 2001 From: Yuriy Losev Date: Mon, 30 Oct 2023 13:06:40 +0400 Subject: [PATCH] Fix app config --- cmd/shell-operator/main.go | 14 +++++------ {pkg => internal}/app/app.go | 0 {pkg => internal}/app/debug.go | 0 {pkg => internal}/app/jq.go | 0 {pkg => internal}/app/kube-client.go | 0 {pkg => internal}/app/log.go | 0 {pkg => internal}/app/webhook.go | 0 pkg/debug/client.go | 2 +- pkg/debug/debug-cmd.go | 25 +++++++++---------- pkg/executor/executor.go | 2 +- pkg/executor/executor_test.go | 3 +-- pkg/hook/hook.go | 2 +- pkg/hook/hook_manager_test.go | 2 +- pkg/kube/object_patch/helpers.go | 2 +- pkg/kube_events_manager/filter.go | 2 +- pkg/shell-operator/bootstrap.go | 22 ++++++++-------- pkg/shell-operator/http_server.go | 3 +-- pkg/shell-operator/kube_client.go | 2 +- pkg/shell-operator/metrics_hooks.go | 2 +- pkg/shell-operator/metrics_operator.go | 2 +- pkg/utils/file/dir.go | 3 +-- .../kube_event_manager_test.go | 2 +- 22 files changed, 43 insertions(+), 47 deletions(-) rename {pkg => internal}/app/app.go (100%) rename {pkg => internal}/app/debug.go (100%) rename {pkg => internal}/app/jq.go (100%) rename {pkg => internal}/app/kube-client.go (100%) rename {pkg => internal}/app/log.go (100%) rename {pkg => internal}/app/webhook.go (100%) diff --git a/cmd/shell-operator/main.go b/cmd/shell-operator/main.go index c153f125..77a1a796 100644 --- a/cmd/shell-operator/main.go +++ b/cmd/shell-operator/main.go @@ -2,6 +2,7 @@ package main import ( "fmt" + app2 "github.com/flant/shell-operator/internal/app" "math/rand" "os" "time" @@ -9,7 +10,6 @@ import ( "gopkg.in/alecthomas/kingpin.v2" "github.com/flant/kube-client/klogtologrus" - "github.com/flant/shell-operator/pkg/app" "github.com/flant/shell-operator/pkg/debug" "github.com/flant/shell-operator/pkg/jq" shell_operator "github.com/flant/shell-operator/pkg/shell-operator" @@ -17,20 +17,20 @@ import ( ) func main() { - kpApp := kingpin.New(app.AppName, fmt.Sprintf("%s %s: %s", app.AppName, app.Version, app.AppDescription)) + kpApp := kingpin.New(app2.AppName, fmt.Sprintf("%s %s: %s", app2.AppName, app2.Version, app2.AppDescription)) // override usage template to reveal additional commands with information about start command - kpApp.UsageTemplate(app.OperatorUsageTemplate(app.AppName)) + kpApp.UsageTemplate(app2.OperatorUsageTemplate(app2.AppName)) // Initialize klog wrapper when all values are parsed kpApp.Action(func(c *kingpin.ParseContext) error { - klogtologrus.InitAdapter(app.DebugKubernetesAPI) + klogtologrus.InitAdapter(app2.DebugKubernetesAPI) return nil }) // print version kpApp.Command("version", "Show version.").Action(func(c *kingpin.ParseContext) error { - fmt.Printf("%s %s\n", app.AppName, app.Version) + fmt.Printf("%s %s\n", app2.AppName, app2.Version) fmt.Println(jq.FilterInfo()) return nil }) @@ -39,7 +39,7 @@ func main() { startCmd := kpApp.Command("start", "Start shell-operator."). Default(). Action(func(c *kingpin.ParseContext) error { - app.AppStartMessage = fmt.Sprintf("%s %s", app.AppName, app.Version) + app2.AppStartMessage = fmt.Sprintf("%s %s", app2.AppName, app2.Version) // Init rand generator. rand.Seed(time.Now().UnixNano()) @@ -59,7 +59,7 @@ func main() { return nil }) - app.DefineStartCommandFlags(kpApp, startCmd) + app2.DefineStartCommandFlags(kpApp, startCmd) debug.DefineDebugCommands(kpApp) debug.DefineDebugCommandsSelf(kpApp) diff --git a/pkg/app/app.go b/internal/app/app.go similarity index 100% rename from pkg/app/app.go rename to internal/app/app.go diff --git a/pkg/app/debug.go b/internal/app/debug.go similarity index 100% rename from pkg/app/debug.go rename to internal/app/debug.go diff --git a/pkg/app/jq.go b/internal/app/jq.go similarity index 100% rename from pkg/app/jq.go rename to internal/app/jq.go diff --git a/pkg/app/kube-client.go b/internal/app/kube-client.go similarity index 100% rename from pkg/app/kube-client.go rename to internal/app/kube-client.go diff --git a/pkg/app/log.go b/internal/app/log.go similarity index 100% rename from pkg/app/log.go rename to internal/app/log.go diff --git a/pkg/app/webhook.go b/internal/app/webhook.go similarity index 100% rename from pkg/app/webhook.go rename to internal/app/webhook.go diff --git a/pkg/debug/client.go b/pkg/debug/client.go index b9285367..b813abf8 100644 --- a/pkg/debug/client.go +++ b/pkg/debug/client.go @@ -4,11 +4,11 @@ import ( "bytes" "context" "fmt" + "github.com/flant/shell-operator/internal/app" "io" "net" "net/http" - "github.com/flant/shell-operator/pkg/app" utils "github.com/flant/shell-operator/pkg/utils/file" ) diff --git a/pkg/debug/debug-cmd.go b/pkg/debug/debug-cmd.go index 07f8ddd7..86cceacf 100644 --- a/pkg/debug/debug-cmd.go +++ b/pkg/debug/debug-cmd.go @@ -2,11 +2,10 @@ package debug import ( "fmt" + app2 "github.com/flant/shell-operator/internal/app" "time" "gopkg.in/alecthomas/kingpin.v2" - - "github.com/flant/shell-operator/pkg/app" ) var ( @@ -16,7 +15,7 @@ var ( func DefineDebugCommands(kpApp *kingpin.Application) { // Queue dump commands. - queueCmd := app.CommandWithDefaultUsageTemplate(kpApp, "queue", "Dump queues.") + queueCmd := app2.CommandWithDefaultUsageTemplate(kpApp, "queue", "Dump queues.") queueListCmd := queueCmd.Command("list", "Dump tasks in all queues."). Action(func(c *kingpin.ParseContext) error { @@ -31,7 +30,7 @@ func DefineDebugCommands(kpApp *kingpin.Application) { Default("false"). BoolVar(&showEmpty) AddOutputJsonYamlTextFlag(queueListCmd) - app.DefineDebugUnixSocketFlag(queueListCmd) + app2.DefineDebugUnixSocketFlag(queueListCmd) queueMainCmd := queueCmd.Command("main", "Dump tasks in the main queue."). Action(func(c *kingpin.ParseContext) error { @@ -43,10 +42,10 @@ func DefineDebugCommands(kpApp *kingpin.Application) { return nil }) AddOutputJsonYamlTextFlag(queueMainCmd) - app.DefineDebugUnixSocketFlag(queueMainCmd) + app2.DefineDebugUnixSocketFlag(queueMainCmd) // Runtime config command. - configCmd := app.CommandWithDefaultUsageTemplate(kpApp, "config", "Manage runtime parameters.") + configCmd := app2.CommandWithDefaultUsageTemplate(kpApp, "config", "Manage runtime parameters.") configListCmd := configCmd.Command("list", "List available runtime parameters."). Action(func(c *kingpin.ParseContext) error { @@ -58,7 +57,7 @@ func DefineDebugCommands(kpApp *kingpin.Application) { return nil }) AddOutputJsonYamlTextFlag(configListCmd) - app.DefineDebugUnixSocketFlag(configListCmd) + app2.DefineDebugUnixSocketFlag(configListCmd) var paramName string var paramValue string @@ -75,11 +74,11 @@ func DefineDebugCommands(kpApp *kingpin.Application) { configSetCmd.Arg("name", "A name of runtime parameter").Required().StringVar(¶mName) configSetCmd.Arg("value", "A new value for the runtime parameter").Required().StringVar(¶mValue) configSetCmd.Arg("duration", "Set value for a period of time, then return a previous value. Use Go notation: 10s, 15m30s, etc.").DurationVar(¶mDuration) - app.DefineDebugUnixSocketFlag(configSetCmd) + app2.DefineDebugUnixSocketFlag(configSetCmd) // Raw request command var rawUrl string - rawCommand := app.CommandWithDefaultUsageTemplate(kpApp, "raw", "Make a raw request to debug endpoint."). + rawCommand := app2.CommandWithDefaultUsageTemplate(kpApp, "raw", "Make a raw request to debug endpoint."). Action(func(c *kingpin.ParseContext) error { url := fmt.Sprintf("http://unix%s", rawUrl) resp, err := DefaultClient().Get(url) @@ -90,12 +89,12 @@ func DefineDebugCommands(kpApp *kingpin.Application) { return nil }) rawCommand.Arg("urlpath", "An url to send to debug endpoint. Example: /queue/list.json").StringVar(&rawUrl) - app.DefineDebugUnixSocketFlag(rawCommand) + app2.DefineDebugUnixSocketFlag(rawCommand) } func DefineDebugCommandsSelf(kpApp *kingpin.Application) { // Get hook names - hookCmd := app.CommandWithDefaultUsageTemplate(kpApp, "hook", "Actions for hooks") + hookCmd := app2.CommandWithDefaultUsageTemplate(kpApp, "hook", "Actions for hooks") hookListCmd := hookCmd.Command("list", "List all hooks."). Action(func(c *kingpin.ParseContext) error { outBytes, err := Hook(DefaultClient()).List(outputFormat) @@ -106,7 +105,7 @@ func DefineDebugCommandsSelf(kpApp *kingpin.Application) { return nil }) AddOutputJsonYamlTextFlag(hookListCmd) - app.DefineDebugUnixSocketFlag(hookListCmd) + app2.DefineDebugUnixSocketFlag(hookListCmd) // Get hook snapshots var hookName string @@ -121,7 +120,7 @@ func DefineDebugCommandsSelf(kpApp *kingpin.Application) { }) hookSnapshotCmd.Arg("hook_name", "").Required().StringVar(&hookName) AddOutputJsonYamlTextFlag(hookSnapshotCmd) - app.DefineDebugUnixSocketFlag(hookSnapshotCmd) + app2.DefineDebugUnixSocketFlag(hookSnapshotCmd) } func AddOutputJsonYamlTextFlag(cmd *kingpin.CmdClause) { diff --git a/pkg/executor/executor.go b/pkg/executor/executor.go index c57c0d9a..11146856 100644 --- a/pkg/executor/executor.go +++ b/pkg/executor/executor.go @@ -2,6 +2,7 @@ package executor import ( "encoding/json" + "github.com/flant/shell-operator/internal/app" "os/exec" "strings" "syscall" @@ -9,7 +10,6 @@ import ( log "github.com/sirupsen/logrus" - "github.com/flant/shell-operator/pkg/app" utils "github.com/flant/shell-operator/pkg/utils/labels" ) diff --git a/pkg/executor/executor_test.go b/pkg/executor/executor_test.go index d3815555..a459bb90 100644 --- a/pkg/executor/executor_test.go +++ b/pkg/executor/executor_test.go @@ -2,6 +2,7 @@ package executor import ( "bytes" + "github.com/flant/shell-operator/internal/app" "io" "math/rand" "os" @@ -12,8 +13,6 @@ import ( "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - - "github.com/flant/shell-operator/pkg/app" ) func TestRunAndLogLines(t *testing.T) { diff --git a/pkg/hook/hook.go b/pkg/hook/hook.go index e0001f0a..4317ae17 100644 --- a/pkg/hook/hook.go +++ b/pkg/hook/hook.go @@ -3,6 +3,7 @@ package hook import ( "context" "fmt" + "github.com/flant/shell-operator/internal/app" "os" "path" "path/filepath" @@ -12,7 +13,6 @@ import ( "github.com/kennygrant/sanitize" "golang.org/x/time/rate" - "github.com/flant/shell-operator/pkg/app" "github.com/flant/shell-operator/pkg/executor" . "github.com/flant/shell-operator/pkg/hook/binding_context" "github.com/flant/shell-operator/pkg/hook/config" diff --git a/pkg/hook/hook_manager_test.go b/pkg/hook/hook_manager_test.go index 0067eed9..a1de0e37 100644 --- a/pkg/hook/hook_manager_test.go +++ b/pkg/hook/hook_manager_test.go @@ -1,13 +1,13 @@ package hook import ( + "github.com/flant/shell-operator/internal/app" "path/filepath" "strings" "testing" . "github.com/onsi/gomega" - "github.com/flant/shell-operator/pkg/app" "github.com/flant/shell-operator/pkg/hook/controller" "github.com/flant/shell-operator/pkg/hook/types" "github.com/flant/shell-operator/pkg/webhook/admission" diff --git a/pkg/kube/object_patch/helpers.go b/pkg/kube/object_patch/helpers.go index 507c8374..b78c40b5 100644 --- a/pkg/kube/object_patch/helpers.go +++ b/pkg/kube/object_patch/helpers.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "fmt" + "github.com/flant/shell-operator/internal/app" "io" "gopkg.in/yaml.v3" @@ -12,7 +13,6 @@ import ( k8yaml "sigs.k8s.io/yaml" "github.com/flant/kube-client/manifest" - "github.com/flant/shell-operator/pkg/app" "github.com/flant/shell-operator/pkg/jq" ) diff --git a/pkg/kube_events_manager/filter.go b/pkg/kube_events_manager/filter.go index 2165f2c9..47129936 100644 --- a/pkg/kube_events_manager/filter.go +++ b/pkg/kube_events_manager/filter.go @@ -4,13 +4,13 @@ import ( "context" "encoding/json" "fmt" + "github.com/flant/shell-operator/internal/app" "reflect" "runtime" "runtime/trace" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" - "github.com/flant/shell-operator/pkg/app" "github.com/flant/shell-operator/pkg/jq" . "github.com/flant/shell-operator/pkg/kube_events_manager/types" utils_checksum "github.com/flant/shell-operator/pkg/utils/checksum" diff --git a/pkg/shell-operator/bootstrap.go b/pkg/shell-operator/bootstrap.go index 38222f61..b6651ac4 100644 --- a/pkg/shell-operator/bootstrap.go +++ b/pkg/shell-operator/bootstrap.go @@ -3,10 +3,10 @@ package shell_operator import ( "context" "fmt" + app2 "github.com/flant/shell-operator/internal/app" log "github.com/sirupsen/logrus" - "github.com/flant/shell-operator/pkg/app" "github.com/flant/shell-operator/pkg/config" "github.com/flant/shell-operator/pkg/debug" "github.com/flant/shell-operator/pkg/hook" @@ -24,18 +24,18 @@ import ( func Init() (*ShellOperator, error) { runtimeConfig := config.NewConfig() // Init logging subsystem. - app.SetupLogging(runtimeConfig) + app2.SetupLogging(runtimeConfig) // Log version and jq filtering implementation. - log.Infof(app.AppStartMessage) + log.Infof(app2.AppStartMessage) log.Debug(jq.FilterInfo()) - hooksDir, err := utils.RequireExistingDirectory(app.HooksDir) + hooksDir, err := utils.RequireExistingDirectory(app2.HooksDir) if err != nil { log.Errorf("Fatal: hooks directory is required: %s", err) return nil, err } - tempDir, err := utils.EnsureTempDirectory(app.TempDir) + tempDir, err := utils.EnsureTempDirectory(app2.TempDir) if err != nil { log.Errorf("Fatal: temp directory: %s", err) return nil, err @@ -44,13 +44,13 @@ func Init() (*ShellOperator, error) { op := NewShellOperator(context.Background()) // Debug server. - debugServer, err := RunDefaultDebugServer(app.DebugUnixSocket, app.DebugHttpServerAddr) + debugServer, err := RunDefaultDebugServer(app2.DebugUnixSocket, app2.DebugHttpServerAddr) if err != nil { log.Errorf("Fatal: start Debug server: %s", err) return nil, err } - err = op.AssembleCommonOperator(app.ListenAddress, app.ListenPort) + err = op.AssembleCommonOperator(app2.ListenAddress, app2.ListenPort) if err != nil { log.Errorf("Fatal: %s", err) return nil, err @@ -172,14 +172,14 @@ func (op *ShellOperator) SetupEventManagers() { func (op *ShellOperator) setupHookManagers(hooksDir string, tempDir string) { // Initialize admission webhooks manager. op.AdmissionWebhookManager = admission.NewWebhookManager(op.KubeClient) - op.AdmissionWebhookManager.Settings = app.ValidatingWebhookSettings - op.AdmissionWebhookManager.Namespace = app.Namespace + op.AdmissionWebhookManager.Settings = app2.ValidatingWebhookSettings + op.AdmissionWebhookManager.Namespace = app2.Namespace // Initialize conversion webhooks manager. op.ConversionWebhookManager = conversion.NewWebhookManager() op.ConversionWebhookManager.KubeClient = op.KubeClient - op.ConversionWebhookManager.Settings = app.ConversionWebhookSettings - op.ConversionWebhookManager.Namespace = app.Namespace + op.ConversionWebhookManager.Settings = app2.ConversionWebhookSettings + op.ConversionWebhookManager.Namespace = app2.Namespace // Initialize Hook manager. cfg := &hook.HookManagerConfig{ diff --git a/pkg/shell-operator/http_server.go b/pkg/shell-operator/http_server.go index 6ec8aac6..69fccb9c 100644 --- a/pkg/shell-operator/http_server.go +++ b/pkg/shell-operator/http_server.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "fmt" + "github.com/flant/shell-operator/internal/app" "net/http" "strings" "time" @@ -11,8 +12,6 @@ import ( "github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5/middleware" log "github.com/sirupsen/logrus" - - "github.com/flant/shell-operator/pkg/app" ) type baseHTTPServer struct { diff --git a/pkg/shell-operator/kube_client.go b/pkg/shell-operator/kube_client.go index 3095cb96..a32e61ab 100644 --- a/pkg/shell-operator/kube_client.go +++ b/pkg/shell-operator/kube_client.go @@ -2,9 +2,9 @@ package shell_operator import ( "fmt" + "github.com/flant/shell-operator/internal/app" klient "github.com/flant/kube-client/client" - "github.com/flant/shell-operator/pkg/app" "github.com/flant/shell-operator/pkg/kube/object_patch" "github.com/flant/shell-operator/pkg/metric_storage" utils "github.com/flant/shell-operator/pkg/utils/labels" diff --git a/pkg/shell-operator/metrics_hooks.go b/pkg/shell-operator/metrics_hooks.go index 08bb7521..a65dbdca 100644 --- a/pkg/shell-operator/metrics_hooks.go +++ b/pkg/shell-operator/metrics_hooks.go @@ -1,9 +1,9 @@ package shell_operator import ( + "github.com/flant/shell-operator/internal/app" "net/http" - "github.com/flant/shell-operator/pkg/app" "github.com/flant/shell-operator/pkg/metric_storage" ) diff --git a/pkg/shell-operator/metrics_operator.go b/pkg/shell-operator/metrics_operator.go index 7c32c0cf..6bf8ff78 100644 --- a/pkg/shell-operator/metrics_operator.go +++ b/pkg/shell-operator/metrics_operator.go @@ -2,8 +2,8 @@ package shell_operator import ( "context" + "github.com/flant/shell-operator/internal/app" - "github.com/flant/shell-operator/pkg/app" "github.com/flant/shell-operator/pkg/metric_storage" ) diff --git a/pkg/utils/file/dir.go b/pkg/utils/file/dir.go index 19ee95fd..1939b54f 100644 --- a/pkg/utils/file/dir.go +++ b/pkg/utils/file/dir.go @@ -2,10 +2,9 @@ package utils import ( "fmt" + "github.com/flant/shell-operator/internal/app" "os" "path/filepath" - - "github.com/flant/shell-operator/pkg/app" ) func RequireExistingDirectory(inDir string) (dir string, err error) { diff --git a/test/integration/kube_event_manager/kube_event_manager_test.go b/test/integration/kube_event_manager/kube_event_manager_test.go index 532ddd54..4cca9e96 100644 --- a/test/integration/kube_event_manager/kube_event_manager_test.go +++ b/test/integration/kube_event_manager/kube_event_manager_test.go @@ -6,12 +6,12 @@ package kube_event_manager_test import ( "context" "fmt" + "github.com/flant/shell-operator/internal/app" "testing" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" - "github.com/flant/shell-operator/pkg/app" "github.com/flant/shell-operator/pkg/kube_events_manager" . "github.com/flant/shell-operator/pkg/kube_events_manager/types" . "github.com/flant/shell-operator/test/integration/suite"