Skip to content

Commit

Permalink
fix: move IsRootless out of pkg/buildah (#2207)
Browse files Browse the repository at this point in the history
Signed-off-by: fengxsong <[email protected]>

Signed-off-by: fengxsong <[email protected]>
  • Loading branch information
fengxsong authored Dec 7, 2022
1 parent 3f15264 commit 6cd4128
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 15 deletions.
6 changes: 5 additions & 1 deletion pkg/buildah/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/containers/storage/pkg/unshare"
"github.com/spf13/cobra"

wrapunshare "github.com/labring/sealos/pkg/unshare"
"github.com/labring/sealos/pkg/utils/logger"
)

Expand All @@ -37,7 +38,10 @@ var (
needToShutdownStore = false
)

var DefaultPlatform = platforms.DefaultSpec
var (
DefaultPlatform = platforms.DefaultSpec
IsRootless = wrapunshare.IsRootless
)

func flagChanged(c *cobra.Command, name string) bool {
if fs := c.Flag(name); fs != nil && fs.Changed {
Expand Down
8 changes: 0 additions & 8 deletions pkg/buildah/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/containers/common/pkg/config"
"github.com/containers/storage/pkg/homedir"
Expand Down Expand Up @@ -107,13 +106,6 @@ func writeFileIfNotExists(filename string, data []byte) error {
return err
}

func IsRootless() bool {
if v, ok := os.LookupEnv(DisableAutoRootless); ok && strings.ToLower(v) == "true" {
return false
}
return unshare.IsRootless()
}

func MaybeReexecUsingUserNamespace() error {
if !IsRootless() {
return nil
Expand Down
4 changes: 2 additions & 2 deletions pkg/ssh/scp.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"github.com/schollz/progressbar/v3"
"golang.org/x/crypto/ssh"

"github.com/labring/sealos/pkg/buildah"
"github.com/labring/sealos/pkg/unshare"
"github.com/labring/sealos/pkg/utils/file"
"github.com/labring/sealos/pkg/utils/hash"
"github.com/labring/sealos/pkg/utils/iputils"
Expand Down Expand Up @@ -71,7 +71,7 @@ func (s *SSH) sftpConnect(host string) (*ssh.Client, *sftp.Client, error) {

// Copy is copy file or dir to remotePath, add md5 validate
func (s *SSH) Copy(host, localPath, remotePath string) error {
if iputils.IsLocalIP(host, s.localAddress) && !buildah.IsRootless() {
if iputils.IsLocalIP(host, s.localAddress) && !unshare.IsRootless() {
logger.Debug("local %s copy files src %s to dst %s", host, localPath, remotePath)
return file.RecursionCopy(localPath, remotePath)
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/ssh/sshcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ import (
"strings"
"sync"

"github.com/labring/sealos/pkg/buildah"
"github.com/labring/sealos/pkg/unshare"
"github.com/labring/sealos/pkg/utils/exec"
"github.com/labring/sealos/pkg/utils/iputils"
"github.com/labring/sealos/pkg/utils/logger"
strings2 "github.com/labring/sealos/pkg/utils/strings"
)

func (s *SSH) Ping(host string) error {
if iputils.IsLocalIP(host, s.localAddress) && !buildah.IsRootless() {
if iputils.IsLocalIP(host, s.localAddress) && !unshare.IsRootless() {
logger.Debug("host %s is local, ping is always true", host)
return nil
}
Expand All @@ -46,7 +46,7 @@ func (s *SSH) Ping(host string) error {

func (s *SSH) CmdAsync(host string, cmds ...string) error {
var isLocal bool
if iputils.IsLocalIP(host, s.localAddress) && !buildah.IsRootless() {
if iputils.IsLocalIP(host, s.localAddress) && !unshare.IsRootless() {
logger.Debug("host %s is local, command via exec", host)
isLocal = true
}
Expand Down Expand Up @@ -106,7 +106,7 @@ func (s *SSH) CmdAsync(host string, cmds ...string) error {
}

func (s *SSH) Cmd(host, cmd string) ([]byte, error) {
if iputils.IsLocalIP(host, s.localAddress) && !buildah.IsRootless() {
if iputils.IsLocalIP(host, s.localAddress) && !unshare.IsRootless() {
logger.Debug("host %s is local, command via exec", host)
d, err := exec.RunBashCmd(cmd)
return []byte(d), err
Expand Down
35 changes: 35 additions & 0 deletions pkg/unshare/unshare.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
Copyright 2022 sealos.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package unshare

import (
"os"
"strings"

"github.com/containers/storage/pkg/unshare"
)

const (
DisableAutoRootless = "DISABLE_AUTO_ROOTLESS"
)

func IsRootless() bool {
if v, ok := os.LookupEnv(DisableAutoRootless); ok && strings.ToLower(v) == "true" {
return false
}
return unshare.IsRootless()
}

0 comments on commit 6cd4128

Please sign in to comment.