Skip to content

Commit

Permalink
Merge pull request kubevirt#7093 from prnaraya/pathjoin-concatenate-s…
Browse files Browse the repository at this point in the history
…trings

Pathjoin concatenate strings
  • Loading branch information
kubevirt-bot authored Jan 26, 2022
2 parents ca60b9e + 7f65a03 commit 0e3a171
Show file tree
Hide file tree
Showing 29 changed files with 97 additions and 83 deletions.
3 changes: 2 additions & 1 deletion cmd/example-cloudinit-hook-sidecar/cloudinit.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"fmt"
"net"
"os"
"path/filepath"

"google.golang.org/grpc"

Expand Down Expand Up @@ -100,7 +101,7 @@ func (s v1alpha2Server) PreCloudInitIso(ctx context.Context, params *hooksV1alph
func main() {
log.InitializeLogging("cloudinit-hook-sidecar")

socketPath := hooks.HookSocketsSharedDirectory + "/cloudinit.sock"
socketPath := filepath.Join(hooks.HookSocketsSharedDirectory, "cloudinit.sock")
socket, err := net.Listen("unix", socketPath)
if err != nil {
log.Log.Reason(err).Errorf("Failed to initialized socket on path: %s", socket)
Expand Down
3 changes: 2 additions & 1 deletion cmd/example-hook-sidecar/smbios.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"fmt"
"net"
"os"
"path/filepath"

"github.com/spf13/pflag"
"google.golang.org/grpc"
Expand Down Expand Up @@ -150,7 +151,7 @@ func main() {
pflag.StringVar(&version, "version", "", "hook version to use")
pflag.Parse()

socketPath := hooks.HookSocketsSharedDirectory + "/smbios.sock"
socketPath := filepath.Join(hooks.HookSocketsSharedDirectory, "smbios.sock")
socket, err := net.Listen("unix", socketPath)
if err != nil {
log.Log.Reason(err).Errorf("Failed to initialized socket on path: %s", socket)
Expand Down
4 changes: 2 additions & 2 deletions cmd/virt-launcher/virt-launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,12 @@ func initializeDirs(ephemeralDiskDir string,
panic(err)
}

err = cloudinit.SetLocalDirectory(ephemeralDiskDir + "/cloud-init-data")
err = cloudinit.SetLocalDirectory(filepath.Join(ephemeralDiskDir, "cloud-init-data"))
if err != nil {
panic(err)
}

err = ignition.SetLocalDirectory(ephemeralDiskDir + "/ignition-data")
err = ignition.SetLocalDirectory(filepath.Join(ephemeralDiskDir, "ignition-data"))
if err != nil {
panic(err)
}
Expand Down
20 changes: 10 additions & 10 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,30 +56,30 @@ const (

var (
// ConfigMapSourceDir represents a location where ConfigMap is attached to the pod
ConfigMapSourceDir = mountBaseDir + "/config-map"
ConfigMapSourceDir = filepath.Join(mountBaseDir, "config-map")
// SysprepSourceDir represents a location where a Sysprep is attached to the pod
SysprepSourceDir = mountBaseDir + "/sysprep"
SysprepSourceDir = filepath.Join(mountBaseDir, "sysprep")
// SecretSourceDir represents a location where Secrets is attached to the pod
SecretSourceDir = mountBaseDir + "/secret"
SecretSourceDir = filepath.Join(mountBaseDir, "secret")
// DownwardAPISourceDir represents a location where downwardapi is attached to the pod
DownwardAPISourceDir = mountBaseDir + "/downwardapi"
DownwardAPISourceDir = filepath.Join(mountBaseDir, "downwardapi")
// ServiceAccountSourceDir represents the location where the ServiceAccount token is attached to the pod
ServiceAccountSourceDir = "/var/run/secrets/kubernetes.io/serviceaccount/"

// ConfigMapDisksDir represents a path to ConfigMap iso images
ConfigMapDisksDir = mountBaseDir + "/config-map-disks"
ConfigMapDisksDir = filepath.Join(mountBaseDir, "config-map-disks")
// SecretDisksDir represents a path to Secrets iso images
SecretDisksDir = mountBaseDir + "/secret-disks"
SecretDisksDir = filepath.Join(mountBaseDir, "secret-disks")
// SysprepDisksDir represents a path to Syspreps iso images
SysprepDisksDir = mountBaseDir + "/sysprep-disks"
SysprepDisksDir = filepath.Join(mountBaseDir, "sysprep-disks")
// DownwardAPIDisksDir represents a path to DownwardAPI iso images
DownwardAPIDisksDir = mountBaseDir + "/downwardapi-disks"
DownwardAPIDisksDir = filepath.Join(mountBaseDir, "downwardapi-disks")
// DownwardMetricDisksDir represents a path to DownwardMetric block disk
DownwardMetricDisksDir = mountBaseDir + "/downwardmetric-disk"
DownwardMetricDisksDir = filepath.Join(mountBaseDir, "downwardmetric-disk")
// DownwardMetricDisks represents the disk location for the DownwardMetric disk
DownwardMetricDisk = filepath.Join(DownwardAPIDisksDir, "vhostmd0")
// ServiceAccountDiskDir represents a path to the ServiceAccount iso image
ServiceAccountDiskDir = mountBaseDir + "/service-account-disk"
ServiceAccountDiskDir = filepath.Join(mountBaseDir, "service-account-disk")
// ServiceAccountDiskName represents the name of the ServiceAccount iso image
ServiceAccountDiskName = "service-account.iso"

Expand Down
2 changes: 1 addition & 1 deletion pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ var _ = Describe("Creating config images", func() {
Expect(err).NotTo(HaveOccurred())
tempISODir, err = ioutil.TempDir("", "iso-dir")
Expect(err).NotTo(HaveOccurred())
expectedLayout = []string{"test-dir=" + tempConfDir + "/test-dir", "test-file2=" + tempConfDir + "/test-file2"}
expectedLayout = []string{"test-dir=" + filepath.Join(tempConfDir, "test-dir"), "test-file2=" + filepath.Join(tempConfDir, "test-file2")}

os.Mkdir(filepath.Join(tempConfDir, "test-dir"), 0755)
os.OpenFile(filepath.Join(tempConfDir, "test-dir", "test-file1"), os.O_RDONLY|os.O_CREATE, 0666)
Expand Down
2 changes: 1 addition & 1 deletion pkg/container-disk/container-disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ var _ = Describe("ContainerDisk", func() {
Expect(err).ToNot(HaveOccurred())
Expect(expectedVolumeMountDir).To(Equal(volumeMountDir))

filePath := filepath.Join(volumeMountDir + "/disk_0.img")
filePath := filepath.Join(volumeMountDir, "disk_0.img")
_, err := os.Create(filePath)
Expect(err).ToNot(HaveOccurred())
}
Expand Down
9 changes: 5 additions & 4 deletions pkg/inotify-informer/inotify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package inotifyinformer
import (
"io/ioutil"
"os"
"path/filepath"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -72,8 +73,8 @@ var _ = Describe("Inotify", func() {
Expect(err).ToNot(HaveOccurred())

// create two files
Expect(os.Create(tmpDir + "/" + "default_testvmi")).ToNot(BeNil())
Expect(os.Create(tmpDir + "/" + "default1_testvmi1")).ToNot(BeNil())
Expect(os.Create(filepath.Join(tmpDir, "default_testvmi"))).ToNot(BeNil())
Expect(os.Create(filepath.Join(tmpDir, "default1_testvmi1"))).ToNot(BeNil())

queue = workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter())
informer = cache.NewSharedIndexInformer(
Expand All @@ -99,7 +100,7 @@ var _ = Describe("Inotify", func() {
It("should detect multiple creations and deletions", func() {
num := 5
key := "default2/test.vmi2"
fileName := tmpDir + "/" + "default2_test.vmi2"
fileName := filepath.Join(tmpDir, "default2_test.vmi2")

for i := 0; i < num; i++ {
Expect(os.Create(fileName)).ToNot(BeNil())
Expand Down Expand Up @@ -130,7 +131,7 @@ var _ = Describe("Inotify", func() {

// Adding files in wrong formats should have an impact
// TODO should we just ignore them?
Expect(os.Create(tmpDir + "/" + "test")).ToNot(BeNil())
Expect(os.Create(filepath.Join(tmpDir, "test"))).ToNot(BeNil())

// No event should be received
Consistently(i.ResultChan()).ShouldNot(Receive())
Expand Down
7 changes: 2 additions & 5 deletions pkg/virt-api/rest/definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ package rest
import (
"fmt"
"net/http"
"path"
"reflect"
"strings"

"kubevirt.io/api/migrations"

Expand Down Expand Up @@ -593,10 +593,7 @@ func ClusterResourcePath(gvr schema.GroupVersionResource) string {
}

func SubResourcePath(subResource string) string {
if !strings.HasPrefix(subResource, "/") {
return "/" + subResource
}
return subResource
return path.Join("/", subResource)
}

const (
Expand Down
6 changes: 3 additions & 3 deletions pkg/virt-controller/watch/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ const (
)

var (
containerDiskDir = filepath.Join(util.VirtShareDir, "/container-disks")
hotplugDiskDir = filepath.Join(util.VirtShareDir, "/hotplug-disks")
containerDiskDir = filepath.Join(util.VirtShareDir, "container-disks")
hotplugDiskDir = filepath.Join(util.VirtShareDir, "hotplug-disks")

leaderGauge = prometheus.NewGauge(
prometheus.GaugeOpts{
Expand Down Expand Up @@ -483,7 +483,7 @@ func (vca *VirtControllerApp) initCommon() {
golog.Fatal(err)
}

containerdisk.SetLocalDirectory(vca.ephemeralDiskDir + "/container-disk-data")
containerdisk.SetLocalDirectory(filepath.Join(vca.ephemeralDiskDir, "container-disk-data"))
vca.templateService = services.NewTemplateService(vca.launcherImage,
vca.launcherQemuTimeout,
vca.virtShareDir,
Expand Down
4 changes: 2 additions & 2 deletions pkg/virt-handler/cmd-client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,13 @@ var _ = Describe("Virt remote commands", func() {
legacy := IsLegacySocket("/some/path/something_sock")
Expect(legacy).To(BeTrue())

legacy = IsLegacySocket("/some/path/" + StandardLauncherSocketFileName)
legacy = IsLegacySocket(filepath.Join("/some/path", StandardLauncherSocketFileName))
Expect(legacy).To(BeFalse())

monEnabled := SocketMonitoringEnabled("/some/path/something_sock")
Expect(monEnabled).To(BeFalse())

monEnabled = SocketMonitoringEnabled("/some/path/" + StandardLauncherSocketFileName)
monEnabled = SocketMonitoringEnabled(filepath.Join("/some/path", StandardLauncherSocketFileName))
Expect(monEnabled).To(BeTrue())
})

Expand Down
2 changes: 1 addition & 1 deletion pkg/virt-handler/isolation/detector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ var _ = Describe("Isolation Detector", func() {
cmdclient.SetLegacyBaseDir(tmpDir)
cmdclient.SetPodsBaseDir(tmpDir)

os.MkdirAll(tmpDir+"/sockets/", os.ModePerm)
os.MkdirAll(filepath.Join(tmpDir, "sockets/"), os.ModePerm)
socketFile := cmdclient.SocketFilePathOnHost(podUID)
os.MkdirAll(filepath.Dir(socketFile), os.ModePerm)
socket, err = net.Listen("unix", socketFile)
Expand Down
15 changes: 8 additions & 7 deletions pkg/virt-handler/migration-proxy/migration-proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"io/ioutil"
"net"
"os"
"path/filepath"
"strings"

. "github.com/onsi/ginkgo"
Expand Down Expand Up @@ -66,7 +67,7 @@ var _ = Describe("MigrationProxy", func() {
Describe("migration proxy", func() {
Context("verify proxy connections work", func() {
It("by verifying source proxy works", func() {
sourceSock := tmpDir + "/source-sock"
sourceSock := filepath.Join(tmpDir, "source-sock")

listener, err := tls.Listen("tcp", "127.0.0.1:12345", tlsConfig)
Expect(err).ShouldNot(HaveOccurred())
Expand Down Expand Up @@ -107,8 +108,8 @@ var _ = Describe("MigrationProxy", func() {
})

It("by creating both ends and sending a message", func() {
sourceSock := tmpDir + "/source-sock"
libvirtdSock := tmpDir + "/libvirtd-sock"
sourceSock := filepath.Join(tmpDir, "source-sock")
libvirtdSock := filepath.Join(tmpDir, "libvirtd-sock")
libvirtdListener, err := net.Listen("unix", libvirtdSock)

Expect(err).ShouldNot(HaveOccurred())
Expand Down Expand Up @@ -152,10 +153,10 @@ var _ = Describe("MigrationProxy", func() {

table.DescribeTable("by creating both ends with a manager and sending a message", func(migrationConfig *v1.MigrationConfiguration) {
directMigrationPort := "49152"
libvirtdSock := tmpDir + "/libvirtd-sock"
libvirtdSock := filepath.Join(tmpDir, "libvirtd-sock")
libvirtdListener, err := net.Listen("unix", libvirtdSock)
Expect(err).ShouldNot(HaveOccurred())
directSock := tmpDir + "/mykey-" + directMigrationPort
directSock := filepath.Join(tmpDir, "mykey-"+directMigrationPort)
directListener, err := net.Listen("unix", directSock)

Expect(err).ShouldNot(HaveOccurred())
Expand Down Expand Up @@ -219,11 +220,11 @@ var _ = Describe("MigrationProxy", func() {
key2 := "key2"

directMigrationPort := "49152"
libvirtdSock := tmpDir + "/libvirtd-sock"
libvirtdSock := filepath.Join(tmpDir, "libvirtd-sock")
libvirtdListener, err := net.Listen("unix", libvirtdSock)
defer libvirtdListener.Close()
Expect(err).ShouldNot(HaveOccurred())
directSock := tmpDir + "/" + key1 + "-" + directMigrationPort
directSock := filepath.Join(tmpDir, key1+"-"+directMigrationPort)
directListener, err := net.Listen("unix", directSock)
defer directListener.Close()

Expand Down
6 changes: 3 additions & 3 deletions pkg/virt-handler/selinux/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,14 @@ func (se *SELinuxImpl) selinux(args ...string) (out []byte, err error) {
}

func defaultCopyPolicyFunc(policyName string, dir string) (err error) {
sourceFile := "/" + policyName + ".cil"
sourceFile := filepath.Join("/", policyName+".cil")
// #nosec No risk for path injection. Using static string path
input, err := ioutil.ReadFile(sourceFile)
if err != nil {
return fmt.Errorf("failed to read a policy file %v: %v ", sourceFile, err)
}

destinationFile := dir + "/" + sourceFile
destinationFile := filepath.Join(dir, sourceFile)
err = ioutil.WriteFile(destinationFile, input, 0600)
if err != nil {
return fmt.Errorf("failed to create a policy file %v: %v ", destinationFile, err)
Expand All @@ -141,7 +141,7 @@ func defaultCopyPolicyFunc(policyName string, dir string) (err error) {

func (se *SELinuxImpl) InstallPolicy(dir string) (err error) {
for _, policyName := range POLICY_FILES {
fileDest := dir + "/" + policyName + ".cil"
fileDest := filepath.Join(dir, policyName+".cil")
err := se.copyPolicyFunc(policyName, dir)
if err != nil {
return fmt.Errorf("failed to copy policy %v - err: %v", fileDest, err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/virt-handler/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ func NewController(
watchdogTimeoutSeconds: watchdogTimeoutSeconds,
migrationProxy: migrationProxy,
podIsolationDetector: podIsolationDetector,
containerDiskMounter: container_disk.NewMounter(podIsolationDetector, virtPrivateDir+"/container-disk-mount-state", clusterConfig),
hotplugVolumeMounter: hotplug_volume.NewVolumeMounter(virtPrivateDir + "/hotplug-volume-mount-state"),
containerDiskMounter: container_disk.NewMounter(podIsolationDetector, filepath.Join(virtPrivateDir, "container-disk-mount-state"), clusterConfig),
hotplugVolumeMounter: hotplug_volume.NewVolumeMounter(filepath.Join(virtPrivateDir, "hotplug-volume-mount-state")),
clusterConfig: clusterConfig,
virtLauncherFSRunDirPattern: "/proc/%d/root/var/run",
capabilities: capabilities,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
Expand Down Expand Up @@ -249,7 +250,7 @@ var _ = Describe("AccessCredentials", func() {
}

// Write the file
err = ioutil.WriteFile(secretDirs[0]+"/"+user, []byte(password), 0644)
err = ioutil.WriteFile(filepath.Join(secretDirs[0], user), []byte(password), 0644)
Expect(err).To(BeNil())

// set the expected command
Expand Down Expand Up @@ -301,7 +302,7 @@ var _ = Describe("AccessCredentials", func() {
matched = false
manager.stopCh = make(chan struct{})
password = password + "morefake"
err = ioutil.WriteFile(secretDirs[0]+"/"+user, []byte(password), 0644)
err = ioutil.WriteFile(filepath.Join(secretDirs[0], user), []byte(password), 0644)
Expect(err).To(BeNil())
base64Str = base64.StdEncoding.EncodeToString([]byte(password))
cmdSetPassword = fmt.Sprintf(`{"execute":"guest-set-user-password", "arguments": {"username":"%s", "password": "%s", "crypted": false }}`, user, base64Str)
Expand Down
2 changes: 1 addition & 1 deletion pkg/virt-launcher/virtwrap/cmd-server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ var _ = Describe("Virt remote commands", func() {
Expect(err).To(HaveOccurred())
Expect(cmdclient.IsDisconnected(err)).To(BeTrue())

_, err = cmdclient.NewClient(shareDir + "/server.sock")
_, err = cmdclient.NewClient(filepath.Join(shareDir, "server.sock"))
Expect(err).To(HaveOccurred())
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package components

import (
"fmt"
"path"
"strings"

"k8s.io/apimachinery/pkg/api/resource"
Expand Down Expand Up @@ -318,7 +319,7 @@ func NewApiServerDeployment(namespace string, repository string, imagePrefix str
Type: intstr.Int,
IntVal: 8443,
},
Path: "/apis/subresources.kubevirt.io/" + virtv1.SubresourceGroupVersions[0].Version + "/healthz",
Path: path.Join("/apis/subresources.kubevirt.io", virtv1.SubresourceGroupVersions[0].Version, "healthz"),
},
},
InitialDelaySeconds: 15,
Expand Down
Loading

0 comments on commit 0e3a171

Please sign in to comment.