mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-14 22:33:34 +00:00
Replaces path.Operation with filepath.Operation (part 2)
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
00aae4c10c
commit
87f094c5e8
@ -22,7 +22,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"time"
|
"time"
|
||||||
@ -159,7 +158,7 @@ func StartTestServer(t Logger, instanceOptions *TestServerInstanceOptions, custo
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return result, err
|
return result, err
|
||||||
}
|
}
|
||||||
proxyCACertFile := path.Join(s.SecureServing.ServerCert.CertDirectory, "proxy-ca.crt")
|
proxyCACertFile := filepath.Join(s.SecureServing.ServerCert.CertDirectory, "proxy-ca.crt")
|
||||||
if err := os.WriteFile(proxyCACertFile, testutil.EncodeCertPEM(proxySigningCert), 0644); err != nil {
|
if err := os.WriteFile(proxyCACertFile, testutil.EncodeCertPEM(proxySigningCert), 0644); err != nil {
|
||||||
return result, err
|
return result, err
|
||||||
}
|
}
|
||||||
@ -186,8 +185,8 @@ func StartTestServer(t Logger, instanceOptions *TestServerInstanceOptions, custo
|
|||||||
if err := pkiutil.WriteCertAndKey(s.SecureServing.ServerCert.CertDirectory, "misty-crt", clientCrtOfAPIServer, signer); err != nil {
|
if err := pkiutil.WriteCertAndKey(s.SecureServing.ServerCert.CertDirectory, "misty-crt", clientCrtOfAPIServer, signer); err != nil {
|
||||||
return result, err
|
return result, err
|
||||||
}
|
}
|
||||||
s.ProxyClientKeyFile = path.Join(s.SecureServing.ServerCert.CertDirectory, "misty-crt.key")
|
s.ProxyClientKeyFile = filepath.Join(s.SecureServing.ServerCert.CertDirectory, "misty-crt.key")
|
||||||
s.ProxyClientCertFile = path.Join(s.SecureServing.ServerCert.CertDirectory, "misty-crt.crt")
|
s.ProxyClientCertFile = filepath.Join(s.SecureServing.ServerCert.CertDirectory, "misty-crt.crt")
|
||||||
|
|
||||||
clientSigningKey, err := testutil.NewPrivateKey()
|
clientSigningKey, err := testutil.NewPrivateKey()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -197,7 +196,7 @@ func StartTestServer(t Logger, instanceOptions *TestServerInstanceOptions, custo
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return result, err
|
return result, err
|
||||||
}
|
}
|
||||||
clientCACertFile := path.Join(s.SecureServing.ServerCert.CertDirectory, "client-ca.crt")
|
clientCACertFile := filepath.Join(s.SecureServing.ServerCert.CertDirectory, "client-ca.crt")
|
||||||
if err := os.WriteFile(clientCACertFile, testutil.EncodeCertPEM(clientSigningCert), 0644); err != nil {
|
if err := os.WriteFile(clientCACertFile, testutil.EncodeCertPEM(clientSigningCert), 0644); err != nil {
|
||||||
return result, err
|
return result, err
|
||||||
}
|
}
|
||||||
|
@ -32,7 +32,7 @@ func (kc *kubeletConfig) Mutate() error {
|
|||||||
// When "kubeadm join" downloads the KubeletConfiguration from the cluster on Windows
|
// When "kubeadm join" downloads the KubeletConfiguration from the cluster on Windows
|
||||||
// nodes, it would contain absolute paths that may lack drive letters, since the config
|
// nodes, it would contain absolute paths that may lack drive letters, since the config
|
||||||
// could have been generated on a Linux control-plane node. On Windows the
|
// could have been generated on a Linux control-plane node. On Windows the
|
||||||
// Golang path.IsAbs() function returns false unless the path contains a drive letter.
|
// Golang filepath.IsAbs() function returns false unless the path contains a drive letter.
|
||||||
// This trips client-go and the kubelet, creating problems on Windows nodes.
|
// This trips client-go and the kubelet, creating problems on Windows nodes.
|
||||||
// Fixing it in client-go or the kubelet is a breaking change to existing Windows
|
// Fixing it in client-go or the kubelet is a breaking change to existing Windows
|
||||||
// users that rely on relative paths:
|
// users that rely on relative paths:
|
||||||
@ -57,7 +57,7 @@ func (kc *kubeletConfig) Mutate() error {
|
|||||||
|
|
||||||
func mutatePaths(cfg *kubeletconfig.KubeletConfiguration, drive string) {
|
func mutatePaths(cfg *kubeletconfig.KubeletConfiguration, drive string) {
|
||||||
mutateStringField := func(name string, field *string) {
|
mutateStringField := func(name string, field *string) {
|
||||||
// path.IsAbs() is not reliable here in the Windows runtime, so check if the
|
// filepath.IsAbs() is not reliable here in the Windows runtime, so check if the
|
||||||
// path starts with "/" instead. This means the path originated from a Unix node and
|
// path starts with "/" instead. This means the path originated from a Unix node and
|
||||||
// is an absolute path.
|
// is an absolute path.
|
||||||
if !strings.HasPrefix(*field, "/") {
|
if !strings.HasPrefix(*field, "/") {
|
||||||
|
@ -20,7 +20,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@ -595,9 +594,9 @@ func GetKubeletKubeConfigPath() string {
|
|||||||
|
|
||||||
// CreateTempDirForKubeadm is a function that creates a temporary directory under /etc/kubernetes/tmp (not using /tmp as that would potentially be dangerous)
|
// CreateTempDirForKubeadm is a function that creates a temporary directory under /etc/kubernetes/tmp (not using /tmp as that would potentially be dangerous)
|
||||||
func CreateTempDirForKubeadm(kubernetesDir, dirName string) (string, error) {
|
func CreateTempDirForKubeadm(kubernetesDir, dirName string) (string, error) {
|
||||||
tempDir := path.Join(KubernetesDir, TempDirForKubeadm)
|
tempDir := filepath.Join(KubernetesDir, TempDirForKubeadm)
|
||||||
if len(kubernetesDir) != 0 {
|
if len(kubernetesDir) != 0 {
|
||||||
tempDir = path.Join(kubernetesDir, TempDirForKubeadm)
|
tempDir = filepath.Join(kubernetesDir, TempDirForKubeadm)
|
||||||
}
|
}
|
||||||
|
|
||||||
// creates target folder if not already exists
|
// creates target folder if not already exists
|
||||||
@ -614,9 +613,9 @@ func CreateTempDirForKubeadm(kubernetesDir, dirName string) (string, error) {
|
|||||||
|
|
||||||
// CreateTimestampDirForKubeadm is a function that creates a temporary directory under /etc/kubernetes/tmp formatted with the current date
|
// CreateTimestampDirForKubeadm is a function that creates a temporary directory under /etc/kubernetes/tmp formatted with the current date
|
||||||
func CreateTimestampDirForKubeadm(kubernetesDir, dirName string) (string, error) {
|
func CreateTimestampDirForKubeadm(kubernetesDir, dirName string) (string, error) {
|
||||||
tempDir := path.Join(KubernetesDir, TempDirForKubeadm)
|
tempDir := filepath.Join(KubernetesDir, TempDirForKubeadm)
|
||||||
if len(kubernetesDir) != 0 {
|
if len(kubernetesDir) != 0 {
|
||||||
tempDir = path.Join(kubernetesDir, TempDirForKubeadm)
|
tempDir = filepath.Join(kubernetesDir, TempDirForKubeadm)
|
||||||
}
|
}
|
||||||
|
|
||||||
// creates target folder if not already exists
|
// creates target folder if not already exists
|
||||||
@ -625,7 +624,7 @@ func CreateTimestampDirForKubeadm(kubernetesDir, dirName string) (string, error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
timestampDirName := fmt.Sprintf("%s-%s", dirName, time.Now().Format("2006-01-02-15-04-05"))
|
timestampDirName := fmt.Sprintf("%s-%s", dirName, time.Now().Format("2006-01-02-15-04-05"))
|
||||||
timestampDir := path.Join(tempDir, timestampDirName)
|
timestampDir := filepath.Join(tempDir, timestampDirName)
|
||||||
if err := os.Mkdir(timestampDir, 0700); err != nil {
|
if err := os.Mkdir(timestampDir, 0700); err != nil {
|
||||||
return "", errors.Wrap(err, "could not create timestamp directory")
|
return "", errors.Wrap(err, "could not create timestamp directory")
|
||||||
}
|
}
|
||||||
|
@ -21,7 +21,7 @@ import (
|
|||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
certutil "k8s.io/client-go/util/cert"
|
certutil "k8s.io/client-go/util/cert"
|
||||||
@ -192,8 +192,8 @@ func TestCreateCertificateChain(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
caCert, _ := parseCertAndKey(path.Join(dir, "test-ca"), t)
|
caCert, _ := parseCertAndKey(filepath.Join(dir, "test-ca"), t)
|
||||||
daughterCert, _ := parseCertAndKey(path.Join(dir, "test-daughter"), t)
|
daughterCert, _ := parseCertAndKey(filepath.Join(dir, "test-daughter"), t)
|
||||||
|
|
||||||
pool := x509.NewCertPool()
|
pool := x509.NewCertPool()
|
||||||
pool.AddCert(caCert)
|
pool.AddCert(caCert)
|
||||||
|
@ -23,7 +23,6 @@ import (
|
|||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@ -263,7 +262,7 @@ func TestWriteCSRFilesIfNotExist(t *testing.T) {
|
|||||||
{
|
{
|
||||||
name: "existing CSR is garbage",
|
name: "existing CSR is garbage",
|
||||||
setupFunc: func(csrPath string) error {
|
setupFunc: func(csrPath string) error {
|
||||||
return os.WriteFile(path.Join(csrPath, "dummy.csr"), []byte("a--bunch--of-garbage"), os.ModePerm)
|
return os.WriteFile(filepath.Join(csrPath, "dummy.csr"), []byte("a--bunch--of-garbage"), os.ModePerm)
|
||||||
},
|
},
|
||||||
expectedError: true,
|
expectedError: true,
|
||||||
},
|
},
|
||||||
|
@ -21,7 +21,7 @@ import (
|
|||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
@ -182,17 +182,17 @@ func loadAndEncryptCert(certPath string, key []byte) ([]byte, error) {
|
|||||||
func certsToTransfer(cfg *kubeadmapi.InitConfiguration) map[string]string {
|
func certsToTransfer(cfg *kubeadmapi.InitConfiguration) map[string]string {
|
||||||
certsDir := cfg.CertificatesDir
|
certsDir := cfg.CertificatesDir
|
||||||
certs := map[string]string{
|
certs := map[string]string{
|
||||||
kubeadmconstants.CACertName: path.Join(certsDir, kubeadmconstants.CACertName),
|
kubeadmconstants.CACertName: filepath.Join(certsDir, kubeadmconstants.CACertName),
|
||||||
kubeadmconstants.CAKeyName: path.Join(certsDir, kubeadmconstants.CAKeyName),
|
kubeadmconstants.CAKeyName: filepath.Join(certsDir, kubeadmconstants.CAKeyName),
|
||||||
kubeadmconstants.FrontProxyCACertName: path.Join(certsDir, kubeadmconstants.FrontProxyCACertName),
|
kubeadmconstants.FrontProxyCACertName: filepath.Join(certsDir, kubeadmconstants.FrontProxyCACertName),
|
||||||
kubeadmconstants.FrontProxyCAKeyName: path.Join(certsDir, kubeadmconstants.FrontProxyCAKeyName),
|
kubeadmconstants.FrontProxyCAKeyName: filepath.Join(certsDir, kubeadmconstants.FrontProxyCAKeyName),
|
||||||
kubeadmconstants.ServiceAccountPublicKeyName: path.Join(certsDir, kubeadmconstants.ServiceAccountPublicKeyName),
|
kubeadmconstants.ServiceAccountPublicKeyName: filepath.Join(certsDir, kubeadmconstants.ServiceAccountPublicKeyName),
|
||||||
kubeadmconstants.ServiceAccountPrivateKeyName: path.Join(certsDir, kubeadmconstants.ServiceAccountPrivateKeyName),
|
kubeadmconstants.ServiceAccountPrivateKeyName: filepath.Join(certsDir, kubeadmconstants.ServiceAccountPrivateKeyName),
|
||||||
}
|
}
|
||||||
|
|
||||||
if cfg.Etcd.External == nil {
|
if cfg.Etcd.External == nil {
|
||||||
certs[kubeadmconstants.EtcdCACertName] = path.Join(certsDir, kubeadmconstants.EtcdCACertName)
|
certs[kubeadmconstants.EtcdCACertName] = filepath.Join(certsDir, kubeadmconstants.EtcdCACertName)
|
||||||
certs[kubeadmconstants.EtcdCAKeyName] = path.Join(certsDir, kubeadmconstants.EtcdCAKeyName)
|
certs[kubeadmconstants.EtcdCAKeyName] = filepath.Join(certsDir, kubeadmconstants.EtcdCAKeyName)
|
||||||
} else {
|
} else {
|
||||||
certs[externalEtcdCA] = cfg.Etcd.External.CAFile
|
certs[externalEtcdCA] = cfg.Etcd.External.CAFile
|
||||||
certs[externalEtcdCert] = cfg.Etcd.External.CertFile
|
certs[externalEtcdCert] = cfg.Etcd.External.CertFile
|
||||||
|
@ -20,7 +20,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
goruntime "runtime"
|
goruntime "runtime"
|
||||||
"testing"
|
"testing"
|
||||||
@ -55,7 +55,7 @@ func TestGetDataFromInitConfig(t *testing.T) {
|
|||||||
t.Fatalf(dedent.Dedent("failed to decode key.\nfatal error: %v"), err)
|
t.Fatalf(dedent.Dedent("failed to decode key.\nfatal error: %v"), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := os.Mkdir(path.Join(tmpdir, "etcd"), 0755); err != nil {
|
if err := os.Mkdir(filepath.Join(tmpdir, "etcd"), 0755); err != nil {
|
||||||
t.Fatalf(dedent.Dedent("failed to create etcd cert dir.\nfatal error: %v"), err)
|
t.Fatalf(dedent.Dedent("failed to create etcd cert dir.\nfatal error: %v"), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,7 +21,7 @@ import (
|
|||||||
"crypto/rsa"
|
"crypto/rsa"
|
||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
"net"
|
"net"
|
||||||
"path"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
certutil "k8s.io/client-go/util/cert"
|
certutil "k8s.io/client-go/util/cert"
|
||||||
@ -230,7 +230,7 @@ func WritePKIFiles(t *testing.T, dir string, files PKIFiles) {
|
|||||||
for filename, body := range files {
|
for filename, body := range files {
|
||||||
switch body := body.(type) {
|
switch body := body.(type) {
|
||||||
case *x509.Certificate:
|
case *x509.Certificate:
|
||||||
if err := certutil.WriteCert(path.Join(dir, filename), pkiutil.EncodeCertPEM(body)); err != nil {
|
if err := certutil.WriteCert(filepath.Join(dir, filename), pkiutil.EncodeCertPEM(body)); err != nil {
|
||||||
t.Errorf("unable to write certificate to file %q: [%v]", dir, err)
|
t.Errorf("unable to write certificate to file %q: [%v]", dir, err)
|
||||||
}
|
}
|
||||||
case *rsa.PublicKey:
|
case *rsa.PublicKey:
|
||||||
@ -238,7 +238,7 @@ func WritePKIFiles(t *testing.T, dir string, files PKIFiles) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("unable to write public key to file %q: [%v]", filename, err)
|
t.Errorf("unable to write public key to file %q: [%v]", filename, err)
|
||||||
}
|
}
|
||||||
if err := keyutil.WriteKey(path.Join(dir, filename), publicKeyBytes); err != nil {
|
if err := keyutil.WriteKey(filepath.Join(dir, filename), publicKeyBytes); err != nil {
|
||||||
t.Errorf("unable to write public key to file %q: [%v]", filename, err)
|
t.Errorf("unable to write public key to file %q: [%v]", filename, err)
|
||||||
}
|
}
|
||||||
case *rsa.PrivateKey:
|
case *rsa.PrivateKey:
|
||||||
@ -246,7 +246,7 @@ func WritePKIFiles(t *testing.T, dir string, files PKIFiles) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("unable to write private key to file %q: [%v]", filename, err)
|
t.Errorf("unable to write private key to file %q: [%v]", filename, err)
|
||||||
}
|
}
|
||||||
if err := keyutil.WriteKey(path.Join(dir, filename), privateKey); err != nil {
|
if err := keyutil.WriteKey(filepath.Join(dir, filename), privateKey); err != nil {
|
||||||
t.Errorf("unable to write private key to file %q: [%v]", filename, err)
|
t.Errorf("unable to write private key to file %q: [%v]", filename, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,6 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@ -1010,8 +1009,8 @@ func getNodeName(cloud cloudprovider.Interface, hostname string) (types.NodeName
|
|||||||
// certificate and key file are generated. Returns a configured server.TLSOptions object.
|
// certificate and key file are generated. Returns a configured server.TLSOptions object.
|
||||||
func InitializeTLS(kf *options.KubeletFlags, kc *kubeletconfiginternal.KubeletConfiguration) (*server.TLSOptions, error) {
|
func InitializeTLS(kf *options.KubeletFlags, kc *kubeletconfiginternal.KubeletConfiguration) (*server.TLSOptions, error) {
|
||||||
if !kc.ServerTLSBootstrap && kc.TLSCertFile == "" && kc.TLSPrivateKeyFile == "" {
|
if !kc.ServerTLSBootstrap && kc.TLSCertFile == "" && kc.TLSPrivateKeyFile == "" {
|
||||||
kc.TLSCertFile = path.Join(kf.CertDirectory, "kubelet.crt")
|
kc.TLSCertFile = filepath.Join(kf.CertDirectory, "kubelet.crt")
|
||||||
kc.TLSPrivateKeyFile = path.Join(kf.CertDirectory, "kubelet.key")
|
kc.TLSPrivateKeyFile = filepath.Join(kf.CertDirectory, "kubelet.key")
|
||||||
|
|
||||||
canReadCertAndKey, err := certutil.CanReadCertAndKey(kc.TLSCertFile, kc.TLSPrivateKeyFile)
|
canReadCertAndKey, err := certutil.CanReadCertAndKey(kc.TLSCertFile, kc.TLSPrivateKeyFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user