feat(init): allow to customize kubectl-shell image by cli flag [BE-11419] (#162)

pull/11530/merge
Oscar Zhou 2024-11-26 10:17:46 +13:00 committed by GitHub
parent 4265ae4dae
commit ee0dbf2d22
12 changed files with 24 additions and 11 deletions

View File

@ -59,6 +59,7 @@ func CLIFlags() *portainer.CLIFlags {
SecretKeyName: kingpin.Flag("secret-key-name", "Secret key name for encryption and will be used as /run/secrets/<secret-key-name>.").Default(defaultSecretKeyName).String(), SecretKeyName: kingpin.Flag("secret-key-name", "Secret key name for encryption and will be used as /run/secrets/<secret-key-name>.").Default(defaultSecretKeyName).String(),
LogLevel: kingpin.Flag("log-level", "Set the minimum logging level to show").Default("INFO").Enum("DEBUG", "INFO", "WARN", "ERROR"), LogLevel: kingpin.Flag("log-level", "Set the minimum logging level to show").Default("INFO").Enum("DEBUG", "INFO", "WARN", "ERROR"),
LogMode: kingpin.Flag("log-mode", "Set the logging output mode").Default("PRETTY").Enum("NOCOLOR", "PRETTY", "JSON"), LogMode: kingpin.Flag("log-mode", "Set the logging output mode").Default("PRETTY").Enum("NOCOLOR", "PRETTY", "JSON"),
KubectlShellImage: kingpin.Flag("kubectl-shell-image", "Kubectl shell image").Envar(portainer.KubectlShellImageEnvVar).Default(portainer.DefaultKubectlShellImage).String(),
} }
} }

View File

@ -93,7 +93,7 @@ func initDataStore(flags *portainer.CLIFlags, secretKey []byte, fileService port
log.Fatal().Msg("failed creating database connection: expecting a boltdb database type but a different one was received") log.Fatal().Msg("failed creating database connection: expecting a boltdb database type but a different one was received")
} }
store := datastore.NewStore(*flags.Data, fileService, connection) store := datastore.NewStore(flags, fileService, connection)
isNew, err := store.Open() isNew, err := store.Open()
if err != nil { if err != nil {
@ -120,7 +120,7 @@ func initDataStore(flags *portainer.CLIFlags, secretKey []byte, fileService port
log.Fatal().Err(err).Msg("failed generating instance id") log.Fatal().Err(err).Msg("failed generating instance id")
} }
migratorInstance := migrator.NewMigrator(&migrator.MigratorParameters{}) migratorInstance := migrator.NewMigrator(&migrator.MigratorParameters{Flags: flags})
migratorCount := migratorInstance.GetMigratorCountOfCurrentAPIVersion() migratorCount := migratorInstance.GetMigratorCountOfCurrentAPIVersion()
// from MigrateData // from MigrateData

View File

@ -16,8 +16,9 @@ import (
) )
// NewStore initializes a new Store and the associated services // NewStore initializes a new Store and the associated services
func NewStore(storePath string, fileService portainer.FileService, connection portainer.Connection) *Store { func NewStore(cliFlags *portainer.CLIFlags, fileService portainer.FileService, connection portainer.Connection) *Store {
return &Store{ return &Store{
flags: cliFlags,
fileService: fileService, fileService: fileService,
connection: connection, connection: connection,
} }

View File

@ -57,7 +57,7 @@ func (store *Store) checkOrCreateDefaultSettings() error {
HelmRepositoryURL: portainer.DefaultHelmRepositoryURL, HelmRepositoryURL: portainer.DefaultHelmRepositoryURL,
UserSessionTimeout: portainer.DefaultUserSessionTimeout, UserSessionTimeout: portainer.DefaultUserSessionTimeout,
KubeconfigExpiry: portainer.DefaultKubeconfigExpiry, KubeconfigExpiry: portainer.DefaultKubeconfigExpiry,
KubectlShellImage: portainer.DefaultKubectlShellImage, KubectlShellImage: *store.flags.KubectlShellImage,
IsDockerDesktopExtension: isDDExtention, IsDockerDesktopExtension: isDDExtention,
} }

View File

@ -32,7 +32,7 @@ func (store *Store) MigrateData() error {
return errors.Wrap(err, "while migrating legacy version") return errors.Wrap(err, "while migrating legacy version")
} }
migratorParams := store.newMigratorParameters(version) migratorParams := store.newMigratorParameters(version, store.flags)
migrator := migrator.NewMigrator(migratorParams) migrator := migrator.NewMigrator(migratorParams)
if !migrator.NeedsMigration() { if !migrator.NeedsMigration() {
@ -62,8 +62,9 @@ func (store *Store) MigrateData() error {
return nil return nil
} }
func (store *Store) newMigratorParameters(version *models.Version) *migrator.MigratorParameters { func (store *Store) newMigratorParameters(version *models.Version, flags *portainer.CLIFlags) *migrator.MigratorParameters {
return &migrator.MigratorParameters{ return &migrator.MigratorParameters{
Flags: flags,
CurrentDBVersion: version, CurrentDBVersion: version,
EndpointGroupService: store.EndpointGroupService, EndpointGroupService: store.EndpointGroupService,
EndpointService: store.EndpointService, EndpointService: store.EndpointService,

View File

@ -109,7 +109,7 @@ func TestMigrateData(t *testing.T) {
t.FailNow() t.FailNow()
} }
migratorParams := store.newMigratorParameters(v) migratorParams := store.newMigratorParameters(v, store.flags)
m := migrator.NewMigrator(migratorParams) m := migrator.NewMigrator(migratorParams)
latestMigrations := m.LatestMigrations() latestMigrations := m.LatestMigrations()

View File

@ -48,6 +48,7 @@ func TestMigrateSettings(t *testing.T) {
} }
m := migrator.NewMigrator(&migrator.MigratorParameters{ m := migrator.NewMigrator(&migrator.MigratorParameters{
Flags: store.flags,
EndpointGroupService: store.EndpointGroupService, EndpointGroupService: store.EndpointGroupService,
EndpointService: store.EndpointService, EndpointService: store.EndpointService,
EndpointRelationService: store.EndpointRelationService, EndpointRelationService: store.EndpointRelationService,

View File

@ -1,8 +1,6 @@
package migrator package migrator
import ( import (
portainer "github.com/portainer/portainer/api"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
) )
@ -20,7 +18,7 @@ func (m *Migrator) migrateSettingsToDB33() error {
} }
log.Info().Msg("setting default kubectl shell image") log.Info().Msg("setting default kubectl shell image")
settings.KubectlShellImage = portainer.DefaultKubectlShellImage settings.KubectlShellImage = *m.flags.KubectlShellImage
return m.settingsService.UpdateSettings(settings) return m.settingsService.UpdateSettings(settings)
} }

View File

@ -33,6 +33,7 @@ import (
type ( type (
// Migrator defines a service to migrate data after a Portainer version update. // Migrator defines a service to migrate data after a Portainer version update.
Migrator struct { Migrator struct {
flags *portainer.CLIFlags
currentDBVersion *models.Version currentDBVersion *models.Version
migrations []Migrations migrations []Migrations
@ -62,6 +63,7 @@ type (
// MigratorParameters represents the required parameters to create a new Migrator instance. // MigratorParameters represents the required parameters to create a new Migrator instance.
MigratorParameters struct { MigratorParameters struct {
Flags *portainer.CLIFlags
CurrentDBVersion *models.Version CurrentDBVersion *models.Version
EndpointGroupService *endpointgroup.Service EndpointGroupService *endpointgroup.Service
EndpointService *endpoint.Service EndpointService *endpoint.Service
@ -91,6 +93,7 @@ type (
// NewMigrator creates a new Migrator. // NewMigrator creates a new Migrator.
func NewMigrator(parameters *MigratorParameters) *Migrator { func NewMigrator(parameters *MigratorParameters) *Migrator {
migrator := &Migrator{ migrator := &Migrator{
flags: parameters.Flags,
currentDBVersion: parameters.CurrentDBVersion, currentDBVersion: parameters.CurrentDBVersion,
endpointGroupService: parameters.EndpointGroupService, endpointGroupService: parameters.EndpointGroupService,
endpointService: parameters.EndpointService, endpointService: parameters.EndpointService,

View File

@ -42,6 +42,7 @@ import (
// Store defines the implementation of portainer.DataStore using // Store defines the implementation of portainer.DataStore using
// BoltDB as the storage system. // BoltDB as the storage system.
type Store struct { type Store struct {
flags *portainer.CLIFlags
connection portainer.Connection connection portainer.Connection
fileService portainer.FileService fileService portainer.FileService

View File

@ -29,6 +29,10 @@ func MustNewTestStore(t testing.TB, init, secure bool) (bool, *Store) {
func NewTestStore(t testing.TB, init, secure bool) (bool, *Store, func(), error) { func NewTestStore(t testing.TB, init, secure bool) (bool, *Store, func(), error) {
// Creates unique temp directory in a concurrency friendly manner. // Creates unique temp directory in a concurrency friendly manner.
storePath := t.TempDir() storePath := t.TempDir()
defaultKubectlShellImage := portainer.DefaultKubectlShellImage
flags := &portainer.CLIFlags{
KubectlShellImage: &defaultKubectlShellImage,
}
fileService, err := filesystem.NewService(storePath, "") fileService, err := filesystem.NewService(storePath, "")
if err != nil { if err != nil {
@ -45,7 +49,7 @@ func NewTestStore(t testing.TB, init, secure bool) (bool, *Store, func(), error)
panic(err) panic(err)
} }
store := NewStore(storePath, fileService, connection) store := NewStore(flags, fileService, connection)
newStore, err := store.Open() newStore, err := store.Open()
if err != nil { if err != nil {
return newStore, nil, nil, err return newStore, nil, nil, err

View File

@ -133,6 +133,7 @@ type (
SecretKeyName *string SecretKeyName *string
LogLevel *string LogLevel *string
LogMode *string LogMode *string
KubectlShellImage *string
} }
// CustomTemplateVariableDefinition // CustomTemplateVariableDefinition
@ -1665,6 +1666,8 @@ const (
AuthCookieKey = "portainer_api_key" AuthCookieKey = "portainer_api_key"
// PortainerCacheHeader is used to enabled FE caching for Kubernetes resources // PortainerCacheHeader is used to enabled FE caching for Kubernetes resources
PortainerCacheHeader = "X-Portainer-Cache" PortainerCacheHeader = "X-Portainer-Cache"
// KubectlShellImageEnvVar is the environment variable used to override the default kubectl shell image
KubectlShellImageEnvVar = "KUBECTL_SHELL_IMAGE"
) )
// List of supported features // List of supported features