mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-17 15:50:10 +00:00
Replaces path.Operation with filepath.Operation (staging)
The path module has a few different functions: Clean, Split, Join, Ext, Dir, Base, IsAbs. These functions do not take into account the OS-specific path separator, meaning that they won't behave as intended on Windows. For example, Dir is supposed to return all but the last element of the path. For the path "C:\some\dir\somewhere", it is supposed to return "C:\some\dir\", however, it returns ".". Instead of these functions, the ones in filepath should be used instead.
This commit is contained in:
parent
20d0ab7ae8
commit
856bb5c8f2
@ -21,7 +21,6 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
@ -115,7 +114,7 @@ func ShortenConfig(config *Config) {
|
|||||||
// FlattenConfig changes the config object into a self-contained config (useful for making secrets)
|
// FlattenConfig changes the config object into a self-contained config (useful for making secrets)
|
||||||
func FlattenConfig(config *Config) error {
|
func FlattenConfig(config *Config) error {
|
||||||
for key, authInfo := range config.AuthInfos {
|
for key, authInfo := range config.AuthInfos {
|
||||||
baseDir, err := MakeAbs(path.Dir(authInfo.LocationOfOrigin), "")
|
baseDir, err := MakeAbs(filepath.Dir(authInfo.LocationOfOrigin), "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -130,7 +129,7 @@ func FlattenConfig(config *Config) error {
|
|||||||
config.AuthInfos[key] = authInfo
|
config.AuthInfos[key] = authInfo
|
||||||
}
|
}
|
||||||
for key, cluster := range config.Clusters {
|
for key, cluster := range config.Clusters {
|
||||||
baseDir, err := MakeAbs(path.Dir(cluster.LocationOfOrigin), "")
|
baseDir, err := MakeAbs(filepath.Dir(cluster.LocationOfOrigin), "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,6 @@ package clientcmd
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
"sort"
|
"sort"
|
||||||
@ -148,7 +147,7 @@ func NewDefaultPathOptions() *PathOptions {
|
|||||||
EnvVar: RecommendedConfigPathEnvVar,
|
EnvVar: RecommendedConfigPathEnvVar,
|
||||||
ExplicitFileFlag: RecommendedConfigPathFlag,
|
ExplicitFileFlag: RecommendedConfigPathFlag,
|
||||||
|
|
||||||
GlobalFileSubpath: path.Join(RecommendedHomeDir, RecommendedFileName),
|
GlobalFileSubpath: filepath.Join(RecommendedHomeDir, RecommendedFileName),
|
||||||
|
|
||||||
LoadingRules: NewDefaultClientConfigLoadingRules(),
|
LoadingRules: NewDefaultClientConfigLoadingRules(),
|
||||||
}
|
}
|
||||||
|
@ -21,7 +21,6 @@ import (
|
|||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
@ -539,13 +538,13 @@ func TestResolveRelativePaths(t *testing.T) {
|
|||||||
|
|
||||||
configDir1, _ := os.MkdirTemp("", "")
|
configDir1, _ := os.MkdirTemp("", "")
|
||||||
defer os.RemoveAll(configDir1)
|
defer os.RemoveAll(configDir1)
|
||||||
configFile1 := path.Join(configDir1, ".kubeconfig")
|
configFile1 := filepath.Join(configDir1, ".kubeconfig")
|
||||||
configDir1, _ = filepath.Abs(configDir1)
|
configDir1, _ = filepath.Abs(configDir1)
|
||||||
|
|
||||||
configDir2, _ := os.MkdirTemp("", "")
|
configDir2, _ := os.MkdirTemp("", "")
|
||||||
defer os.RemoveAll(configDir2)
|
defer os.RemoveAll(configDir2)
|
||||||
configDir2, _ = os.MkdirTemp(configDir2, "")
|
configDir2, _ = os.MkdirTemp(configDir2, "")
|
||||||
configFile2 := path.Join(configDir2, ".kubeconfig")
|
configFile2 := filepath.Join(configDir2, ".kubeconfig")
|
||||||
configDir2, _ = filepath.Abs(configDir2)
|
configDir2, _ = filepath.Abs(configDir2)
|
||||||
|
|
||||||
WriteToFile(pathResolutionConfig1, configFile1)
|
WriteToFile(pathResolutionConfig1, configFile1)
|
||||||
@ -564,11 +563,11 @@ func TestResolveRelativePaths(t *testing.T) {
|
|||||||
for key, cluster := range mergedConfig.Clusters {
|
for key, cluster := range mergedConfig.Clusters {
|
||||||
if key == "relative-server-1" {
|
if key == "relative-server-1" {
|
||||||
foundClusterCount++
|
foundClusterCount++
|
||||||
matchStringArg(path.Join(configDir1, pathResolutionConfig1.Clusters["relative-server-1"].CertificateAuthority), cluster.CertificateAuthority, t)
|
matchStringArg(filepath.Join(configDir1, pathResolutionConfig1.Clusters["relative-server-1"].CertificateAuthority), cluster.CertificateAuthority, t)
|
||||||
}
|
}
|
||||||
if key == "relative-server-2" {
|
if key == "relative-server-2" {
|
||||||
foundClusterCount++
|
foundClusterCount++
|
||||||
matchStringArg(path.Join(configDir2, pathResolutionConfig2.Clusters["relative-server-2"].CertificateAuthority), cluster.CertificateAuthority, t)
|
matchStringArg(filepath.Join(configDir2, pathResolutionConfig2.Clusters["relative-server-2"].CertificateAuthority), cluster.CertificateAuthority, t)
|
||||||
}
|
}
|
||||||
if key == "absolute-server-1" {
|
if key == "absolute-server-1" {
|
||||||
foundClusterCount++
|
foundClusterCount++
|
||||||
@ -587,13 +586,13 @@ func TestResolveRelativePaths(t *testing.T) {
|
|||||||
for key, authInfo := range mergedConfig.AuthInfos {
|
for key, authInfo := range mergedConfig.AuthInfos {
|
||||||
if key == "relative-user-1" {
|
if key == "relative-user-1" {
|
||||||
foundAuthInfoCount++
|
foundAuthInfoCount++
|
||||||
matchStringArg(path.Join(configDir1, pathResolutionConfig1.AuthInfos["relative-user-1"].ClientCertificate), authInfo.ClientCertificate, t)
|
matchStringArg(filepath.Join(configDir1, pathResolutionConfig1.AuthInfos["relative-user-1"].ClientCertificate), authInfo.ClientCertificate, t)
|
||||||
matchStringArg(path.Join(configDir1, pathResolutionConfig1.AuthInfos["relative-user-1"].ClientKey), authInfo.ClientKey, t)
|
matchStringArg(filepath.Join(configDir1, pathResolutionConfig1.AuthInfos["relative-user-1"].ClientKey), authInfo.ClientKey, t)
|
||||||
}
|
}
|
||||||
if key == "relative-user-2" {
|
if key == "relative-user-2" {
|
||||||
foundAuthInfoCount++
|
foundAuthInfoCount++
|
||||||
matchStringArg(path.Join(configDir2, pathResolutionConfig2.AuthInfos["relative-user-2"].ClientCertificate), authInfo.ClientCertificate, t)
|
matchStringArg(filepath.Join(configDir2, pathResolutionConfig2.AuthInfos["relative-user-2"].ClientCertificate), authInfo.ClientCertificate, t)
|
||||||
matchStringArg(path.Join(configDir2, pathResolutionConfig2.AuthInfos["relative-user-2"].ClientKey), authInfo.ClientKey, t)
|
matchStringArg(filepath.Join(configDir2, pathResolutionConfig2.AuthInfos["relative-user-2"].ClientKey), authInfo.ClientKey, t)
|
||||||
}
|
}
|
||||||
if key == "absolute-user-1" {
|
if key == "absolute-user-1" {
|
||||||
foundAuthInfoCount++
|
foundAuthInfoCount++
|
||||||
@ -607,7 +606,7 @@ func TestResolveRelativePaths(t *testing.T) {
|
|||||||
}
|
}
|
||||||
if key == "relative-cmd-1" {
|
if key == "relative-cmd-1" {
|
||||||
foundAuthInfoCount++
|
foundAuthInfoCount++
|
||||||
matchStringArg(path.Join(configDir1, pathResolutionConfig1.AuthInfos[key].Exec.Command), authInfo.Exec.Command, t)
|
matchStringArg(filepath.Join(configDir1, pathResolutionConfig1.AuthInfos[key].Exec.Command), authInfo.Exec.Command, t)
|
||||||
}
|
}
|
||||||
if key == "absolute-cmd-1" {
|
if key == "absolute-cmd-1" {
|
||||||
foundAuthInfoCount++
|
foundAuthInfoCount++
|
||||||
|
@ -22,6 +22,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
@ -118,8 +119,8 @@ func setupOutputWriter(dir string, defaultWriter io.Writer, filename string, fil
|
|||||||
if len(dir) == 0 || dir == "-" {
|
if len(dir) == 0 || dir == "-" {
|
||||||
return defaultWriter
|
return defaultWriter
|
||||||
}
|
}
|
||||||
fullFile := path.Join(dir, filename) + fileExtension
|
fullFile := filepath.Join(dir, filename) + fileExtension
|
||||||
parent := path.Dir(fullFile)
|
parent := filepath.Dir(fullFile)
|
||||||
cmdutil.CheckErr(os.MkdirAll(parent, 0755))
|
cmdutil.CheckErr(os.MkdirAll(parent, 0755))
|
||||||
|
|
||||||
file, err := os.Create(fullFile)
|
file, err := os.Create(fullFile)
|
||||||
|
@ -18,7 +18,7 @@ package clusterinfo
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"k8s.io/cli-runtime/pkg/genericiooptions"
|
"k8s.io/cli-runtime/pkg/genericiooptions"
|
||||||
@ -60,7 +60,7 @@ func TestSetupOutputWriterFile(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("unexpected error: %v", err)
|
t.Errorf("unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
fullPath := path.Join(dir, file) + extension
|
fullPath := filepath.Join(dir, file) + extension
|
||||||
defer os.RemoveAll(dir)
|
defer os.RemoveAll(dir)
|
||||||
|
|
||||||
_, _, buf, _ := genericiooptions.NewTestIOStreams()
|
_, _, buf, _ := genericiooptions.NewTestIOStreams()
|
||||||
|
@ -19,7 +19,7 @@ package config
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
@ -415,7 +415,7 @@ func TestEmptyTokenAndCertAllowed(t *testing.T) {
|
|||||||
defer utiltesting.CloseAndRemove(t, fakeCertFile)
|
defer utiltesting.CloseAndRemove(t, fakeCertFile)
|
||||||
expectedConfig := newRedFederalCowHammerConfig()
|
expectedConfig := newRedFederalCowHammerConfig()
|
||||||
authInfo := clientcmdapi.NewAuthInfo()
|
authInfo := clientcmdapi.NewAuthInfo()
|
||||||
authInfo.ClientCertificate = path.Base(fakeCertFile.Name())
|
authInfo.ClientCertificate = filepath.Base(fakeCertFile.Name())
|
||||||
expectedConfig.AuthInfos["another-user"] = authInfo
|
expectedConfig.AuthInfos["another-user"] = authInfo
|
||||||
|
|
||||||
test := configCommandTest{
|
test := configCommandTest{
|
||||||
@ -660,7 +660,7 @@ func TestCAClearsInsecure(t *testing.T) {
|
|||||||
clusterInfoWithInsecure.InsecureSkipTLSVerify = true
|
clusterInfoWithInsecure.InsecureSkipTLSVerify = true
|
||||||
|
|
||||||
clusterInfoWithCA := clientcmdapi.NewCluster()
|
clusterInfoWithCA := clientcmdapi.NewCluster()
|
||||||
clusterInfoWithCA.CertificateAuthority = path.Base(fakeCAFile.Name())
|
clusterInfoWithCA.CertificateAuthority = filepath.Base(fakeCAFile.Name())
|
||||||
|
|
||||||
startingConfig := newRedFederalCowHammerConfig()
|
startingConfig := newRedFederalCowHammerConfig()
|
||||||
startingConfig.Clusters["another-cluster"] = clusterInfoWithInsecure
|
startingConfig.Clusters["another-cluster"] = clusterInfoWithInsecure
|
||||||
|
@ -20,7 +20,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
|
|
||||||
@ -320,7 +320,7 @@ func handleConfigMapFromFileSources(configMap *corev1.ConfigMap, fileSources []s
|
|||||||
return fmt.Errorf("error listing files in %s: %v", filePath, err)
|
return fmt.Errorf("error listing files in %s: %v", filePath, err)
|
||||||
}
|
}
|
||||||
for _, item := range fileList {
|
for _, item := range fileList {
|
||||||
itemPath := path.Join(filePath, item.Name())
|
itemPath := filepath.Join(filePath, item.Name())
|
||||||
if item.Type().IsRegular() {
|
if item.Type().IsRegular() {
|
||||||
keyName = item.Name()
|
keyName = item.Name()
|
||||||
err = addKeyFromFileToConfigMap(configMap, keyName, itemPath)
|
err = addKeyFromFileToConfigMap(configMap, keyName, itemPath)
|
||||||
|
@ -20,7 +20,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
@ -349,7 +349,7 @@ func handleSecretFromFileSources(secret *corev1.Secret, fileSources []string) er
|
|||||||
return fmt.Errorf("error listing files in %s: %v", filePath, err)
|
return fmt.Errorf("error listing files in %s: %v", filePath, err)
|
||||||
}
|
}
|
||||||
for _, item := range fileList {
|
for _, item := range fileList {
|
||||||
itemPath := path.Join(filePath, item.Name())
|
itemPath := filepath.Join(filePath, item.Name())
|
||||||
if item.Type().IsRegular() {
|
if item.Type().IsRegular() {
|
||||||
keyName = item.Name()
|
keyName = item.Name()
|
||||||
if err := addKeyFromFileToSecret(secret, keyName, itemPath); err != nil {
|
if err := addKeyFromFileToSecret(secret, keyName, itemPath); err != nil {
|
||||||
|
@ -18,7 +18,7 @@ package create
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
corev1 "k8s.io/api/core/v1"
|
corev1 "k8s.io/api/core/v1"
|
||||||
@ -197,8 +197,8 @@ func write(path, contents string, t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func writeKeyPair(tmpDirPath, key, cert string, t *testing.T) (keyPath, certPath string) {
|
func writeKeyPair(tmpDirPath, key, cert string, t *testing.T) (keyPath, certPath string) {
|
||||||
keyPath = path.Join(tmpDirPath, "tls.key")
|
keyPath = filepath.Join(tmpDirPath, "tls.key")
|
||||||
certPath = path.Join(tmpDirPath, "tls.cert")
|
certPath = filepath.Join(tmpDirPath, "tls.cert")
|
||||||
write(keyPath, key, t)
|
write(keyPath, key, t)
|
||||||
write(certPath, cert, t)
|
write(certPath, cert, t)
|
||||||
return
|
return
|
||||||
|
@ -20,7 +20,6 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
@ -130,7 +129,7 @@ func TestDiffVersion(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
fcontent, err := os.ReadFile(path.Join(diff.Dir.Name, obj.Name()))
|
fcontent, err := os.ReadFile(filepath.Join(diff.Dir.Name, obj.Name()))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -201,7 +200,7 @@ func TestDiffer(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
fcontent, err := os.ReadFile(path.Join(diff.From.Dir.Name, obj.Name()))
|
fcontent, err := os.ReadFile(filepath.Join(diff.From.Dir.Name, obj.Name()))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -210,7 +209,7 @@ func TestDiffer(t *testing.T) {
|
|||||||
t.Fatalf("File has %q, expected %q", string(fcontent), econtent)
|
t.Fatalf("File has %q, expected %q", string(fcontent), econtent)
|
||||||
}
|
}
|
||||||
|
|
||||||
fcontent, err = os.ReadFile(path.Join(diff.To.Dir.Name, obj.Name()))
|
fcontent, err = os.ReadFile(filepath.Join(diff.To.Dir.Name, obj.Name()))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -286,12 +285,12 @@ metadata:
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
actualFromContent, _ := os.ReadFile(path.Join(diff.From.Dir.Name, obj.Name()))
|
actualFromContent, _ := os.ReadFile(filepath.Join(diff.From.Dir.Name, obj.Name()))
|
||||||
if string(actualFromContent) != tc.expectedFromContent {
|
if string(actualFromContent) != tc.expectedFromContent {
|
||||||
t.Fatalf("File has %q, expected %q", string(actualFromContent), tc.expectedFromContent)
|
t.Fatalf("File has %q, expected %q", string(actualFromContent), tc.expectedFromContent)
|
||||||
}
|
}
|
||||||
|
|
||||||
actualToContent, _ := os.ReadFile(path.Join(diff.To.Dir.Name, obj.Name()))
|
actualToContent, _ := os.ReadFile(filepath.Join(diff.To.Dir.Name, obj.Name()))
|
||||||
if string(actualToContent) != tc.expectedToContent {
|
if string(actualToContent) != tc.expectedToContent {
|
||||||
t.Fatalf("File has %q, expected %q", string(actualToContent), tc.expectedToContent)
|
t.Fatalf("File has %q, expected %q", string(actualToContent), tc.expectedToContent)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user