mirror of
https://github.com/kubesphere/kubekey.git
synced 2025-12-26 01:22:51 +00:00
Merge pull request #2318 from ImitationImmortal/feature
feat: set base default dir for kubernetes connector.
This commit is contained in:
commit
215e58d3f2
|
|
@ -18,7 +18,6 @@ package app
|
|||
|
||||
import (
|
||||
"context"
|
||||
"io/fs"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
|
@ -49,7 +48,7 @@ func NewControllerManagerCommand() *cobra.Command {
|
|||
// create workdir directory,if not exists
|
||||
_const.SetWorkDir(o.WorkDir)
|
||||
if _, err := os.Stat(o.WorkDir); os.IsNotExist(err) {
|
||||
if err := os.MkdirAll(o.WorkDir, fs.ModePerm); err != nil {
|
||||
if err := os.MkdirAll(o.WorkDir, os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,7 +17,6 @@ limitations under the License.
|
|||
package app
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
|
@ -52,7 +51,7 @@ func newArtifactExportCommand() *cobra.Command {
|
|||
_const.SetWorkDir(o.WorkDir)
|
||||
// create workdir directory,if not exists
|
||||
if _, err := os.Stat(o.WorkDir); os.IsNotExist(err) {
|
||||
if err := os.MkdirAll(o.WorkDir, fs.ModePerm); err != nil {
|
||||
if err := os.MkdirAll(o.WorkDir, os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
|
@ -81,7 +80,7 @@ func newArtifactImagesCommand() *cobra.Command {
|
|||
_const.SetWorkDir(o.WorkDir)
|
||||
// create workdir directory,if not exists
|
||||
if _, err := os.Stat(o.WorkDir); os.IsNotExist(err) {
|
||||
if err := os.MkdirAll(o.WorkDir, fs.ModePerm); err != nil {
|
||||
if err := os.MkdirAll(o.WorkDir, os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,7 +20,6 @@ limitations under the License.
|
|||
package app
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
|
@ -54,7 +53,7 @@ func newCertsRenewCommand() *cobra.Command {
|
|||
_const.SetWorkDir(o.WorkDir)
|
||||
// create workdir directory,if not exists
|
||||
if _, err := os.Stat(o.WorkDir); os.IsNotExist(err) {
|
||||
if err := os.MkdirAll(o.WorkDir, fs.ModePerm); err != nil {
|
||||
if err := os.MkdirAll(o.WorkDir, os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,7 +20,6 @@ limitations under the License.
|
|||
package app
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
|
@ -54,7 +53,7 @@ func newCreateClusterCommand() *cobra.Command {
|
|||
_const.SetWorkDir(o.WorkDir)
|
||||
// create workdir directory,if not exists
|
||||
if _, err := os.Stat(o.WorkDir); os.IsNotExist(err) {
|
||||
if err := os.MkdirAll(o.WorkDir, fs.ModePerm); err != nil {
|
||||
if err := os.MkdirAll(o.WorkDir, os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,7 +17,6 @@ limitations under the License.
|
|||
package app
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
|
@ -52,7 +51,7 @@ func newInitOSCommand() *cobra.Command {
|
|||
_const.SetWorkDir(o.WorkDir)
|
||||
// create workdir directory,if not exists
|
||||
if _, err := os.Stat(o.WorkDir); os.IsNotExist(err) {
|
||||
if err := os.MkdirAll(o.WorkDir, fs.ModePerm); err != nil {
|
||||
if err := os.MkdirAll(o.WorkDir, os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
|
@ -81,7 +80,7 @@ func newInitRegistryCommand() *cobra.Command {
|
|||
_const.SetWorkDir(o.WorkDir)
|
||||
// create workdir directory,if not exists
|
||||
if _, err := os.Stat(o.WorkDir); os.IsNotExist(err) {
|
||||
if err := os.MkdirAll(o.WorkDir, fs.ModePerm); err != nil {
|
||||
if err := os.MkdirAll(o.WorkDir, os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,7 +20,6 @@ limitations under the License.
|
|||
package app
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
|
@ -47,7 +46,7 @@ func newPreCheckCommand() *cobra.Command {
|
|||
_const.SetWorkDir(o.WorkDir)
|
||||
// create workdir directory,if not exists
|
||||
if _, err := os.Stat(o.WorkDir); os.IsNotExist(err) {
|
||||
if err := os.MkdirAll(o.WorkDir, fs.ModePerm); err != nil {
|
||||
if err := os.MkdirAll(o.WorkDir, os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,7 +19,6 @@ package app
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
|
@ -50,7 +49,7 @@ func newRunCommand() *cobra.Command {
|
|||
_const.SetWorkDir(o.WorkDir)
|
||||
// create workdir directory,if not exists
|
||||
if _, err := os.Stat(o.WorkDir); os.IsNotExist(err) {
|
||||
if err := os.MkdirAll(o.WorkDir, fs.ModePerm); err != nil {
|
||||
if err := os.MkdirAll(o.WorkDir, os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -29,13 +29,15 @@ import (
|
|||
_const "github.com/kubesphere/kubekey/v4/pkg/const"
|
||||
)
|
||||
|
||||
const kubeconfigRelPath = ".kube/config"
|
||||
|
||||
var _ Connector = &kubernetesConnector{}
|
||||
|
||||
type kubernetesConnector struct {
|
||||
clusterName string
|
||||
kubeconfig string
|
||||
kubeconfigPath string
|
||||
Cmd exec.Interface
|
||||
clusterName string
|
||||
kubeconfig string
|
||||
rootDir string
|
||||
Cmd exec.Interface
|
||||
}
|
||||
|
||||
func (c *kubernetesConnector) Init(ctx context.Context) error {
|
||||
|
|
@ -44,19 +46,23 @@ func (c *kubernetesConnector) Init(ctx context.Context) error {
|
|||
klog.V(4).InfoS("kubeconfig is not set, using local kubeconfig")
|
||||
return nil
|
||||
}
|
||||
c.kubeconfigPath = filepath.Join(_const.GetWorkDir(), "kubernetes", c.clusterName, "kubeconfig")
|
||||
if _, err := os.Stat(c.kubeconfigPath); err == nil || !os.IsNotExist(err) {
|
||||
klog.V(4).InfoS("kubeconfig file already exists", "cluster", c.clusterName)
|
||||
return err
|
||||
}
|
||||
// store kubeconfig to work dir
|
||||
if _, err := os.Stat(filepath.Dir(c.kubeconfigPath)); err != nil && os.IsNotExist(err) {
|
||||
if err := os.MkdirAll(filepath.Dir(c.kubeconfigPath), 0644); err != nil {
|
||||
// set rootDir
|
||||
c.rootDir = filepath.Join(_const.GetWorkDir(), "kubernetes", c.clusterName)
|
||||
if _, err := os.Stat(c.rootDir); err != nil && os.IsNotExist(err) {
|
||||
if err := os.MkdirAll(c.rootDir, os.ModePerm); err != nil {
|
||||
klog.V(4).ErrorS(err, "Failed to create local dir", "cluster", c.clusterName)
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := os.WriteFile(c.kubeconfigPath, []byte(c.kubeconfig), 0644); err != nil {
|
||||
// set kubeconfig to root dir
|
||||
kubeconfigPath := filepath.Join(c.rootDir, kubeconfigRelPath)
|
||||
if _, err := os.Stat(kubeconfigPath); err != nil && os.IsNotExist(err) {
|
||||
if err := os.MkdirAll(filepath.Dir(kubeconfigPath), os.ModePerm); err != nil {
|
||||
klog.V(4).ErrorS(err, "Failed to create local dir", "cluster", c.clusterName)
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := os.WriteFile(kubeconfigPath, []byte(c.kubeconfig), os.ModePerm); err != nil {
|
||||
klog.V(4).ErrorS(err, "Failed to create kubeconfig file", "cluster", c.clusterName)
|
||||
return err
|
||||
}
|
||||
|
|
@ -71,6 +77,7 @@ func (c *kubernetesConnector) Close(ctx context.Context) error {
|
|||
// Typically, the configuration file for each cluster may be different,
|
||||
// and it may be necessary to keep them in separate directories locally.
|
||||
func (c *kubernetesConnector) PutFile(ctx context.Context, src []byte, dst string, mode fs.FileMode) error {
|
||||
dst = filepath.Join(c.rootDir, dst)
|
||||
if _, err := os.Stat(filepath.Dir(dst)); err != nil && os.IsNotExist(err) {
|
||||
if err := os.MkdirAll(filepath.Dir(dst), mode); err != nil {
|
||||
klog.V(4).ErrorS(err, "Failed to create local dir", "dst_file", dst)
|
||||
|
|
@ -84,7 +91,8 @@ func (c *kubernetesConnector) PutFile(ctx context.Context, src []byte, dst strin
|
|||
func (c *kubernetesConnector) FetchFile(ctx context.Context, src string, dst io.Writer) error {
|
||||
// add "--kubeconfig" to src command
|
||||
command := c.Cmd.CommandContext(ctx, "/bin/sh", "-c", src)
|
||||
command.SetEnv([]string{"KUBECONFIG=" + c.kubeconfigPath})
|
||||
command.SetDir(c.rootDir)
|
||||
command.SetEnv([]string{"KUBECONFIG=" + filepath.Join(c.rootDir, kubeconfigRelPath)})
|
||||
command.SetStdout(dst)
|
||||
_, err := command.CombinedOutput()
|
||||
return err
|
||||
|
|
@ -94,6 +102,7 @@ func (c *kubernetesConnector) ExecuteCommand(ctx context.Context, cmd string) ([
|
|||
// add "--kubeconfig" to src command
|
||||
klog.V(4).InfoS("exec local command", "cmd", cmd)
|
||||
command := c.Cmd.CommandContext(ctx, "/bin/sh", "-c", cmd)
|
||||
command.SetEnv([]string{"KUBECONFIG=" + c.kubeconfigPath})
|
||||
command.SetDir(c.rootDir)
|
||||
command.SetEnv([]string{"KUBECONFIG=" + filepath.Join(c.rootDir, kubeconfigRelPath)})
|
||||
return command.CombinedOutput()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -54,7 +54,7 @@ func ModuleFetch(ctx context.Context, options ExecOptions) (string, string) {
|
|||
|
||||
// fetch file
|
||||
if _, err := os.Stat(filepath.Dir(destParam)); os.IsNotExist(err) {
|
||||
if err := os.MkdirAll(filepath.Dir(destParam), 0755); err != nil {
|
||||
if err := os.MkdirAll(filepath.Dir(destParam), os.ModePerm); err != nil {
|
||||
return "", fmt.Sprintf("failed to create dest dir: %v", err)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -52,7 +52,7 @@ func TestModuleGenCert(t *testing.T) {
|
|||
}
|
||||
|
||||
if _, err := os.Stat("./test_gen_cert"); os.IsNotExist(err) {
|
||||
if err := os.Mkdir("./test_gen_cert", 0755); err != nil {
|
||||
if err := os.Mkdir("./test_gen_cert", os.ModePerm); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -224,16 +224,16 @@ func (i imageTransport) RoundTrip(request *http.Request) (*http.Response, error)
|
|||
defer request.Body.Close()
|
||||
|
||||
filename := filepath.Join(i.baseDir, "blobs", request.URL.Query().Get("digest"))
|
||||
if err := os.MkdirAll(filepath.Dir(filename), 0755); err != nil {
|
||||
if err := os.MkdirAll(filepath.Dir(filename), os.ModePerm); err != nil {
|
||||
return ResponseServerError, nil
|
||||
}
|
||||
if err := os.WriteFile(filename, body, 0644); err != nil {
|
||||
if err := os.WriteFile(filename, body, os.ModePerm); err != nil {
|
||||
return ResponseServerError, nil
|
||||
}
|
||||
return ResponseCreated, nil
|
||||
} else if strings.HasSuffix(filepath.Dir(request.URL.Path), "/manifests") { // manifest
|
||||
filename := filepath.Join(i.baseDir, strings.TrimPrefix(request.URL.Path, apiPrefix))
|
||||
if err := os.MkdirAll(filepath.Dir(filename), 0755); err != nil {
|
||||
if err := os.MkdirAll(filepath.Dir(filename), os.ModePerm); err != nil {
|
||||
return ResponseServerError, nil
|
||||
}
|
||||
body, err := io.ReadAll(request.Body)
|
||||
|
|
@ -241,7 +241,7 @@ func (i imageTransport) RoundTrip(request *http.Request) (*http.Response, error)
|
|||
return ResponseServerError, nil
|
||||
}
|
||||
defer request.Body.Close()
|
||||
if err := os.WriteFile(filename, body, 0644); err != nil {
|
||||
if err := os.WriteFile(filename, body, os.ModePerm); err != nil {
|
||||
return ResponseServerError, nil
|
||||
}
|
||||
return ResponseCreated, nil
|
||||
|
|
|
|||
|
|
@ -83,7 +83,7 @@ func (s fileStorage) Create(ctx context.Context, key string, obj, out runtime.Ob
|
|||
// create file to local disk
|
||||
if _, err := os.Stat(filepath.Dir(key)); err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
if err := os.MkdirAll(filepath.Dir(key), 0755); err != nil {
|
||||
if err := os.MkdirAll(filepath.Dir(key), os.ModePerm); err != nil {
|
||||
klog.V(4).ErrorS(err, "failed to create dir", "path", filepath.Dir(key))
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,7 +17,6 @@ limitations under the License.
|
|||
package source
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"os"
|
||||
|
||||
"k8s.io/klog/v2"
|
||||
|
|
@ -39,7 +38,7 @@ type Watcher interface {
|
|||
// New returns a new source.
|
||||
func New(path string) (Source, error) {
|
||||
if _, err := os.Stat(path); err != nil {
|
||||
if err := os.MkdirAll(path, fs.ModePerm); err != nil {
|
||||
if err := os.MkdirAll(path, os.ModePerm); err != nil {
|
||||
klog.V(4).ErrorS(err, "create source path error", "path", path)
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
- name: Generate sonobuoy plugins
|
||||
template:
|
||||
src: plugins/
|
||||
dest: "{{ work_dir }}/kubernetes/{{ inventory_name }}/sonobuoy/plugins/"
|
||||
dest: "sonobuoy/plugins/"
|
||||
|
||||
- name: Run sonobuoy
|
||||
command: |
|
||||
|
|
@ -10,12 +10,12 @@
|
|||
sonobuoy run --wait \
|
||||
{% if (plugins.systemd_logs.enabled) %}-p systemd-logs {% endif %}\
|
||||
{% if (plugins.e2e.enabled) %}-p e2e {% endif %}\
|
||||
{% if (plugins.e2e_ks.enabled) %}-p {{ work_dir }}/kubernetes/{{ inventory_name }}/sonobuoy/plugins/e2e-ks.yaml {% endif %}\
|
||||
{% if (plugins.kube_bench.enabled) %}-p {{ work_dir }}/kubernetes/{{ inventory_name }}/sonobuoy/plugins/kube-bench.yaml -p {{ work_dir }}/kubernetes/{{ inventory_name }}/sonobuoy/plugins/kube-bench-master.yaml {% endif %}\
|
||||
{% if (plugins.e2e_ks.enabled) %}-p sonobuoy/plugins/e2e-ks.yaml {% endif %}\
|
||||
{% if (plugins.kube_bench.enabled) %}-p sonobuoy/plugins/kube-bench.yaml -p sonobuoy/plugins/kube-bench-master.yaml {% endif %}\
|
||||
|
||||
- name: Retrieve result
|
||||
command: |
|
||||
cd {{ work_dir }}/kubernetes/{{ inventory_name }}/sonobuoy/ && sonobuoy retrieve
|
||||
cd sonobuoy/ && sonobuoy retrieve
|
||||
|
||||
- name: Clean sonobuoy
|
||||
command: |
|
||||
|
|
|
|||
Loading…
Reference in New Issue