Skip to content

Commit

Permalink
Merge pull request #915 from topolvm/add-lvm-path
Browse files Browse the repository at this point in the history
Add lvm-path option
  • Loading branch information
peng225 committed May 15, 2024
2 parents dea55bb + 80497bd commit deffe3b
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 13 deletions.
14 changes: 11 additions & 3 deletions cmd/lvmd/app/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ import (
"sigs.k8s.io/controller-runtime/pkg/log/zap"
)

var cfgFilePath string
var zapOpts zap.Options
var (
cfgFilePath string
containerized bool
lvmPath string
zapOpts zap.Options
)

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Expand All @@ -52,6 +56,9 @@ func subMain(ctx context.Context) error {
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&zapOpts)))
logger := log.FromContext(ctx)

command.Containerized(containerized)
command.SetLVMPath(lvmPath)

if err := loadConfFile(ctx, cfgFilePath); err != nil {
return err
}
Expand Down Expand Up @@ -132,7 +139,8 @@ func Execute() {
//nolint:lll
func init() {
rootCmd.PersistentFlags().StringVar(&cfgFilePath, "config", filepath.Join("/etc", "topolvm", "lvmd.yaml"), "config file")
rootCmd.PersistentFlags().BoolVar(&command.Containerized, "container", false, "Run within a container")
rootCmd.PersistentFlags().BoolVar(&containerized, "container", false, "Run within a container")
rootCmd.PersistentFlags().StringVar(&lvmPath, "lvm-path", "", "lvm command path on the host OS")

goflags := flag.NewFlagSet("klog", flag.ExitOnError)
klog.InitFlags(goflags)
Expand Down
2 changes: 2 additions & 0 deletions cmd/topolvm-node/app/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var config struct {
secureMetricsServer bool
zapOpts zap.Options
embedLvmd bool
lvmPath string
lvmd lvmd.Config
}

Expand Down Expand Up @@ -58,6 +59,7 @@ func init() {
fs.BoolVar(&config.secureMetricsServer, "secure-metrics-server", false, "Secures the metrics server")
fs.String("nodename", "", "The resource name of the running node")
fs.BoolVar(&config.embedLvmd, "embed-lvmd", false, "Runs LVMD locally by embedding it instead of calling it externally via gRPC")
fs.StringVar(&config.lvmPath, "lvm-path", "", "lvm command path on the host OS")
fs.StringVar(&cfgFilePath, "config", filepath.Join("/etc", "topolvm", "lvmd.yaml"), "config file")

_ = viper.BindEnv("nodename", "NODE_NAME")
Expand Down
2 changes: 2 additions & 0 deletions cmd/topolvm-node/app/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ func subMain(ctx context.Context) error {
health = grpc_health_v1.NewHealthClient(conn)
}

lvmd.SetLVMPath(config.lvmPath)

if err := controller.SetupLogicalVolumeReconcilerWithServices(
mgr, client, nodename, vgService, lvService); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "LogicalVolume")
Expand Down
5 changes: 0 additions & 5 deletions internal/lvmd/command/lvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ import (
"github.com/topolvm/topolvm"
)

const (
nsenter = "/usr/bin/nsenter"
lvm = "/sbin/lvm"
)

// ErrNotFound is returned when a VG or LV is not found.
var ErrNotFound = errors.New("not found")

Expand Down
23 changes: 21 additions & 2 deletions internal/lvmd/command/lvm_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,26 @@ import (
"sigs.k8s.io/controller-runtime/pkg/log"
)

var Containerized = false
const (
nsenter = "/usr/bin/nsenter"
)

var (
containerized = false
lvm = "/sbin/lvm"
)

// Containerized sets whether to run lvm commands in a container.
func Containerized(sw bool) {
containerized = sw
}

// SetLVMPath sets the path to the lvm command.
func SetLVMPath(path string) {
if path != "" {
lvm = path
}
}

// callLVM calls lvm sub-commands and prints the output to the log.
func callLVM(ctx context.Context, args ...string) error {
Expand Down Expand Up @@ -57,7 +76,7 @@ func callLVMStreamed(ctx context.Context, args ...string) (io.ReadCloser, error)

// wrapExecCommand calls cmd with args but wrapped to run on the host with nsenter if Containerized is true.
func wrapExecCommand(cmd string, args ...string) *exec.Cmd {
if Containerized {
if containerized {
args = append([]string{"-m", "-u", "-i", "-n", "-p", "-t", "1", cmd}, args...)
cmd = nsenter
}
Expand Down
8 changes: 5 additions & 3 deletions pkg/lvmd/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
internalLvmdCommand "github.com/topolvm/topolvm/internal/lvmd/command"
)

func Containerized(sw bool) {
internalLvmdCommand.Containerized = sw
}
// Containerized sets whether to run lvm commands in a container.
var Containerized = internalLvmdCommand.Containerized

// SetLVMPath sets the path to the lvm command.
var SetLVMPath = internalLvmdCommand.SetLVMPath

0 comments on commit deffe3b

Please sign in to comment.