Skip to content

Commit

Permalink
Apply nice/ionice settings from env (#746)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dipta Das authored and tamalsaha committed Apr 17, 2019
1 parent a2534c9 commit 62960e5
Show file tree
Hide file tree
Showing 14 changed files with 185 additions and 1 deletion.
5 changes: 5 additions & 0 deletions apis/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,10 @@ const (
EnableCache = "ENABLE_CACHE"
MaxConnections = "MAX_CONNECTIONS"

// from runtime settings
NiceAdjustment = "NICE_ADJUSTMENT"
IONiceClass = "IONICE_CLASS"
IONiceClassData = "IONICE_CLASS_DATA"

StatusSubresourceEnabled = "ENABLE_STATUS_SUBRESOURCE"
)
6 changes: 6 additions & 0 deletions pkg/backup/backupsession.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,12 @@ func (c *BackupSessionController) backup(backupSession *api_v1beta1.BackupSessio
return fmt.Errorf("setup option for repository fail")
}

// apply nice/ionice settings
if backupConfiguration.Spec.RuntimeSettings.Container != nil {
c.SetupOpt.Nice = backupConfiguration.Spec.RuntimeSettings.Container.Nice
c.SetupOpt.IONice = backupConfiguration.Spec.RuntimeSettings.Container.IONice
}

// init restic wrapper
resticWrapper, err := restic.NewResticWrapper(c.SetupOpt)
if err != nil {
Expand Down
12 changes: 12 additions & 0 deletions pkg/cmds/backup_mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/appscode/go/flags"
"github.com/appscode/stash/pkg/restic"
"github.com/appscode/stash/pkg/util"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/errors"
Expand Down Expand Up @@ -50,6 +51,17 @@ func NewCmdBackupMongo() *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
flags.EnsureRequiredFlags(cmd, "app-binding", "provider", "secret-dir")

// apply nice, ionice settings from env
var err error
setupOpt.Nice, err = util.NiceSettingsFromEnv()
if err != nil {
return handleResticError(outputDir, restic.DefaultOutputFileName, err)
}
setupOpt.IONice, err = util.IONiceSettingsFromEnv()
if err != nil {
return handleResticError(outputDir, restic.DefaultOutputFileName, err)
}

// prepare client
config, err := clientcmd.BuildConfigFromFlags(masterURL, kubeconfigPath)
if err != nil {
Expand Down
12 changes: 12 additions & 0 deletions pkg/cmds/backup_mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/appscode/go/flags"
"github.com/appscode/stash/pkg/restic"
"github.com/appscode/stash/pkg/util"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/errors"
Expand Down Expand Up @@ -50,6 +51,17 @@ func NewCmdBackupMySql() *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
flags.EnsureRequiredFlags(cmd, "app-binding", "provider", "secret-dir")

// apply nice, ionice settings from env
var err error
setupOpt.Nice, err = util.NiceSettingsFromEnv()
if err != nil {
return handleResticError(outputDir, restic.DefaultOutputFileName, err)
}
setupOpt.IONice, err = util.IONiceSettingsFromEnv()
if err != nil {
return handleResticError(outputDir, restic.DefaultOutputFileName, err)
}

// prepare client
config, err := clientcmd.BuildConfigFromFlags(masterURL, kubeconfigPath)
if err != nil {
Expand Down
12 changes: 12 additions & 0 deletions pkg/cmds/backup_pg.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/appscode/go/flags"
"github.com/appscode/go/log"
"github.com/appscode/stash/pkg/restic"
"github.com/appscode/stash/pkg/util"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/errors"
Expand Down Expand Up @@ -54,6 +55,17 @@ func NewCmdBackupPG() *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
flags.EnsureRequiredFlags(cmd, "app-binding", "provider", "secret-dir")

// apply nice, ionice settings from env
var err error
setupOpt.Nice, err = util.NiceSettingsFromEnv()
if err != nil {
return handleResticError(outputDir, restic.DefaultOutputFileName, err)
}
setupOpt.IONice, err = util.IONiceSettingsFromEnv()
if err != nil {
return handleResticError(outputDir, restic.DefaultOutputFileName, err)
}

// prepare client
config, err := clientcmd.BuildConfigFromFlags(masterURL, kubeconfigPath)
if err != nil {
Expand Down
12 changes: 12 additions & 0 deletions pkg/cmds/backup_pvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/appscode/go/log"
api_v1beta1 "github.com/appscode/stash/apis/stash/v1beta1"
"github.com/appscode/stash/pkg/restic"
"github.com/appscode/stash/pkg/util"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/util/errors"
)
Expand Down Expand Up @@ -35,6 +36,17 @@ func NewCmdBackupPVC() *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
flags.EnsureRequiredFlags(cmd, "backup-dirs", "provider", "secret-dir")

// apply nice, ionice settings from env
var err error
setupOpt.Nice, err = util.NiceSettingsFromEnv()
if err != nil {
return handleResticError(outputDir, restic.DefaultOutputFileName, err)
}
setupOpt.IONice, err = util.IONiceSettingsFromEnv()
if err != nil {
return handleResticError(outputDir, restic.DefaultOutputFileName, err)
}

// init restic wrapper
resticWrapper, err := restic.NewResticWrapper(setupOpt)
if err != nil {
Expand Down
12 changes: 12 additions & 0 deletions pkg/cmds/restore_mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/appscode/go/flags"
"github.com/appscode/stash/pkg/restic"
"github.com/appscode/stash/pkg/util"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/errors"
Expand Down Expand Up @@ -41,6 +42,17 @@ func NewCmdRestoreMongo() *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
flags.EnsureRequiredFlags(cmd, "app-binding", "provider", "secret-dir")

// apply nice, ionice settings from env
var err error
setupOpt.Nice, err = util.NiceSettingsFromEnv()
if err != nil {
return handleResticError(outputDir, restic.DefaultOutputFileName, err)
}
setupOpt.IONice, err = util.IONiceSettingsFromEnv()
if err != nil {
return handleResticError(outputDir, restic.DefaultOutputFileName, err)
}

// prepare client
config, err := clientcmd.BuildConfigFromFlags(masterURL, kubeconfigPath)
if err != nil {
Expand Down
12 changes: 12 additions & 0 deletions pkg/cmds/restore_mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/appscode/go/flags"
"github.com/appscode/stash/pkg/restic"
"github.com/appscode/stash/pkg/util"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/errors"
Expand Down Expand Up @@ -40,6 +41,17 @@ func NewCmdRestoreMySql() *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
flags.EnsureRequiredFlags(cmd, "app-binding", "provider", "secret-dir")

// apply nice, ionice settings from env
var err error
setupOpt.Nice, err = util.NiceSettingsFromEnv()
if err != nil {
return handleResticError(outputDir, restic.DefaultOutputFileName, err)
}
setupOpt.IONice, err = util.IONiceSettingsFromEnv()
if err != nil {
return handleResticError(outputDir, restic.DefaultOutputFileName, err)
}

// prepare client
config, err := clientcmd.BuildConfigFromFlags(masterURL, kubeconfigPath)
if err != nil {
Expand Down
12 changes: 12 additions & 0 deletions pkg/cmds/restore_pg.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/appscode/go/flags"
"github.com/appscode/stash/pkg/restic"
"github.com/appscode/stash/pkg/util"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/errors"
Expand Down Expand Up @@ -40,6 +41,17 @@ func NewCmdRestorePG() *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
flags.EnsureRequiredFlags(cmd, "app-binding", "provider", "secret-dir")

// apply nice, ionice settings from env
var err error
setupOpt.Nice, err = util.NiceSettingsFromEnv()
if err != nil {
return handleResticError(outputDir, restic.DefaultOutputFileName, err)
}
setupOpt.IONice, err = util.IONiceSettingsFromEnv()
if err != nil {
return handleResticError(outputDir, restic.DefaultOutputFileName, err)
}

// prepare client
config, err := clientcmd.BuildConfigFromFlags(masterURL, kubeconfigPath)
if err != nil {
Expand Down
13 changes: 13 additions & 0 deletions pkg/cmds/restore_pvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/appscode/go/flags"
"github.com/appscode/stash/pkg/restic"
"github.com/appscode/stash/pkg/util"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/util/errors"
)
Expand Down Expand Up @@ -32,6 +33,18 @@ func NewCmdRestorePVC() *cobra.Command {
DisableAutoGenTag: true,
RunE: func(cmd *cobra.Command, args []string) error {
flags.EnsureRequiredFlags(cmd, "restore-dirs", "provider", "secret-dir")

// apply nice, ionice settings from env
var err error
setupOpt.Nice, err = util.NiceSettingsFromEnv()
if err != nil {
return handleResticError(outputDir, restic.DefaultOutputFileName, err)
}
setupOpt.IONice, err = util.IONiceSettingsFromEnv()
if err != nil {
return handleResticError(outputDir, restic.DefaultOutputFileName, err)
}

// init restic wrapper
resticWrapper, err := restic.NewResticWrapper(setupOpt)
if err != nil {
Expand Down
22 changes: 22 additions & 0 deletions pkg/resolve/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"

"github.com/appscode/stash/apis"
v1beta1_api "github.com/appscode/stash/apis/stash/v1beta1"
cs "github.com/appscode/stash/client/clientset/versioned"
"github.com/appscode/stash/pkg/util"
Expand Down Expand Up @@ -129,6 +130,27 @@ func applyContainerRuntimeSettings(container core.Container, settings ofst.Conta
if settings.SecurityContext != nil {
container.SecurityContext = settings.SecurityContext
}
// set nice, ionice settings as env
if settings.Nice != nil && settings.Nice.Adjustment != nil {
container.Env = core_util.UpsertEnvVars(container.Env, core.EnvVar{
Name: apis.NiceAdjustment,
Value: fmt.Sprint(*settings.Nice.Adjustment),
})
}
if settings.IONice != nil {
if settings.IONice.Class != nil {
container.Env = core_util.UpsertEnvVars(container.Env, core.EnvVar{
Name: apis.IONiceClass,
Value: fmt.Sprint(*settings.IONice.Class),
})
}
if settings.IONice.ClassData != nil {
container.Env = core_util.UpsertEnvVars(container.Env, core.EnvVar{
Name: apis.IONiceClassData,
Value: fmt.Sprint(*settings.IONice.ClassData),
})
}
}
return container
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/restic/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ func (w *ResticWrapper) applyIONiceSettings(oldCommand Command) Command {
newCommand.Args = append(newCommand.Args, "-c", fmt.Sprint(*w.config.IONice.Class))
}
if w.config.IONice.ClassData != nil {
newCommand.Args = append(newCommand.Args, "-n", fmt.Sprint(*w.config.IONice.Class))
newCommand.Args = append(newCommand.Args, "-n", fmt.Sprint(*w.config.IONice.ClassData))
}
// TODO: should we use "-t" option with ionice ?
// newCommand.Args = append(newCommand.Args, "-t")
Expand Down
5 changes: 5 additions & 0 deletions pkg/restore/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ func Restore(opt *Options) error {
if err != nil {
return err
}
// apply nice/ionice settings
if restoreSession.Spec.RuntimeSettings.Container != nil {
setupOptions.Nice = restoreSession.Spec.RuntimeSettings.Container.Nice
setupOptions.IONice = restoreSession.Spec.RuntimeSettings.Container.IONice
}
opt.SetupOpt = setupOptions

// only one pod can acquire restic repository lock. so we need leader election to determine who will acquire the lock
Expand Down
49 changes: 49 additions & 0 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"

"github.com/appscode/go/types"
"github.com/appscode/stash/apis"
api_v1beta1 "github.com/appscode/stash/apis/stash/v1beta1"
"github.com/appscode/stash/pkg/restic"
Expand All @@ -14,6 +16,7 @@ import (
core_util "kmodules.xyz/client-go/core/v1"
"kmodules.xyz/client-go/meta"
store "kmodules.xyz/objectstore-api/api/v1"
v1 "kmodules.xyz/offshoot-api/api/v1"
)

var (
Expand Down Expand Up @@ -203,3 +206,49 @@ func AttachLocalBackend(podSpec core.PodSpec, localSpec store.LocalSpec) core.Po
}
return podSpec
}

func NiceSettingsFromEnv() (*v1.NiceSettings, error) {
var settings *v1.NiceSettings
if v, ok := os.LookupEnv(apis.NiceAdjustment); ok {
vi, err := ParseInt32P(v)
if err != nil {
return nil, err
}
settings = &v1.NiceSettings{
Adjustment: vi,
}
}
return settings, nil
}

func IONiceSettingsFromEnv() (*v1.IONiceSettings, error) {
var settings *v1.IONiceSettings
if v, ok := os.LookupEnv(apis.IONiceClass); ok {
vi, err := ParseInt32P(v)
if err != nil {
return nil, err
}
settings = &v1.IONiceSettings{
Class: vi,
}
}
if v, ok := os.LookupEnv(apis.IONiceClassData); ok {
vi, err := ParseInt32P(v)
if err != nil {
return nil, err
}
if settings == nil {
settings = &v1.IONiceSettings{}
}
settings.ClassData = vi
}
return settings, nil
}

func ParseInt32P(v string) (*int32, error) {
vi, err := strconv.Atoi(v)
if err != nil {
return nil, err
}
return types.Int32P(int32(vi)), nil
}

0 comments on commit 62960e5

Please sign in to comment.