Skip to content

Commit

Permalink
fix(#160): fix not found kubeconfig
Browse files Browse the repository at this point in the history
fix not found kubeconfig

Signed-off-by: ysicing <[email protected]>
  • Loading branch information
ysicing committed Apr 21, 2023
1 parent 137d47b commit 454cb1e
Show file tree
Hide file tree
Showing 15 changed files with 35 additions and 46 deletions.
5 changes: 2 additions & 3 deletions cmd/app/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ func NewCmdAppGet(f factory.Factory) *cobra.Command {
if err != nil {
return err
}
extargs := []string{"exp", "kubectl", "get", "-o", "wide", "pods,deploy,pvc,svc,ing", "-l", "release=" + appdata.K8Name, "--kubeconfig", common.GetDefaultNewKubeConfig()}
// extargs = append(extargs, args...)
return qcexec.CommandRun(os.Args[0], extargs...)
extArgs := []string{"exp", "kubectl", "get", "-o", "wide", "pods,deploy,pvc,svc,ing", "-l", "release=" + appdata.K8Name, "--kubeconfig", common.GetKubeConfig()}
return qcexec.CommandRun(os.Args[0], extArgs...)
},
}
app.Flags().BoolVar(&useip, "api-useip", false, "api use ip")
Expand Down
9 changes: 0 additions & 9 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,12 +242,3 @@ func HandlePluginCommand(pluginHandler PluginHandler, cmdArgs []string) error {

return nil
}

// func initConfig() {
// if globalFlags.ConfigPath == "" {
// globalFlags.ConfigPath = common.GetDefaultConfig()
// }
// viper.SetConfigFile(globalFlags.ConfigPath)
// viper.AutomaticEnv()
// viper.ReadInConfig()
// }
2 changes: 1 addition & 1 deletion common/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ ExecStart=/usr/local/bin/k3s \
{{if not .Master0 -}}
--server https://{{ .KubeAPI }}:6443 \
{{end -}}
--data-dir {{.DataDir}} \
--data-dir {{.DataDir}} \
--docker \
--pause-image hub.qucheng.com/library/rancher/mirrored-pause:3.6 \
--kube-proxy-arg "proxy-mode=ipvs" "masquerade-all=true" \
Expand Down
18 changes: 9 additions & 9 deletions common/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package common

import (
"fmt"
"os"
"runtime"
"strings"

Expand Down Expand Up @@ -99,25 +98,26 @@ func GetDefaultConfig() string {
return home + "/" + DefaultCfgDir + "/cluster.yaml"
}

func GetDefaultKubeConfig() string {
func DefaultKubeConfig() string {
d := fmt.Sprintf("%v/.kube", zos.GetHomeDir())
os.MkdirAll(d, FileMode0644)
// os.MkdirAll(d, FileMode0644)
return fmt.Sprintf("%v/config", d)
}

func GetDefaultNewKubeConfig() string {
func DefaultQuickONKubeConfig() string {
home := zos.GetHomeDir()
d := home + "/" + DefaultCfgDir + "/.kube"
os.MkdirAll(d, FileMode0644)
// os.MkdirAll(d, FileMode0644)
return fmt.Sprintf("%v/config", d)
}

// GetKubeConfig get kubeconfig
func GetKubeConfig() string {
kubecfg := GetDefaultNewKubeConfig()
if file.CheckFileExists(kubecfg) {
return kubecfg
kubeCfg := DefaultQuickONKubeConfig()
if file.CheckFileExists(kubeCfg) {
return kubeCfg
}
return GetDefaultKubeConfig()
return DefaultKubeConfig()
}

func GetCustomConfig(name string) string {
Expand Down
5 changes: 4 additions & 1 deletion hack/packaging/postinstall.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#!/bin/sh
# Determine OS platform
# shellcheck source=/dev/null
. /etc/os-release

create_link() {
# shellcheck disable=SC2015
[ -f "/usr/bin/q" ] && rm -rf /usr/bin/q || true
# shellcheck disable=SC2015
[ -f "/usr/local/bin/q" ] && rm -rf /usr/local/bin/q || true
# shellcheck disable=SC2015
[ -f "/usr/local/bin/qcadmin" ] && rm -rf /usr/local/bin/qcadmin || true
# shellcheck disable=SC2015
[ -f "/usr/bin/qcadmin" ] && ln -s /usr/bin/qcadmin /usr/bin/q || true
}
Expand Down
1 change: 0 additions & 1 deletion hack/packaging/postremove.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/bin/sh
# Determine OS platform
# shellcheck source=/dev/null
. /etc/os-release

echo "Removing quickon package."

Expand Down
1 change: 0 additions & 1 deletion hack/packaging/preinstall.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/bin/sh
# Determine OS platform
# shellcheck source=/dev/null
. /etc/os-release

ensure_sudo() {
if [ "$(id -u)" = "0" ]; then
Expand Down
3 changes: 2 additions & 1 deletion hack/packaging/preremove.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/bin/sh
# Determine OS platform
# shellcheck source=/dev/null
. /etc/os-release

# shellcheck disable=SC2015
[ -f "/usr/bin/q" ] && rm -rf /usr/bin/q || true
[ -f "/usr/local/bin/q" ] && rm -rf /usr/local/bin/q || true
4 changes: 2 additions & 2 deletions internal/pkg/cli/tool/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,12 @@ func domainAdd(f factory.Factory) *cobra.Command {
if err := helmClient.UpdateRepo(); err != nil {
log.Warnf("update repo failed, reason: %v", err)
}
if err := qcexec.Command(os.Args[0], "experimental", "kubectl", "apply", "-f", fmt.Sprintf("%s/hack/haogstls/haogs.yaml", common.GetDefaultDataDir()), "-n", common.GetDefaultSystemNamespace(true), "--kubeconfig", common.GetDefaultNewKubeConfig()).Run(); err != nil {
if err := qcexec.Command(os.Args[0], "experimental", "kubectl", "apply", "-f", fmt.Sprintf("%s/hack/haogstls/haogs.yaml", common.GetDefaultDataDir()), "-n", common.GetDefaultSystemNamespace(true), "--kubeconfig", common.GetKubeConfig()).Run(); err != nil {
log.Warnf("load tls cert for %s failed, reason: %v", common.GetDefaultSystemNamespace(true), err)
} else {
log.Donef("load tls cert for %s success", common.GetDefaultSystemNamespace(true))
}
if err := qcexec.Command(os.Args[0], "experimental", "kubectl", "apply", "-f", fmt.Sprintf("%s/hack/haogstls/haogs.yaml", common.GetDefaultDataDir()), "-n", "default", "--kubeconfig", common.GetDefaultNewKubeConfig()).Run(); err != nil {
if err := qcexec.Command(os.Args[0], "experimental", "kubectl", "apply", "-f", fmt.Sprintf("%s/hack/haogstls/haogs.yaml", common.GetDefaultDataDir()), "-n", "default", "--kubeconfig", common.GetKubeConfig()).Run(); err != nil {
log.Warnf("load tls cert for default failed, reason: %v", err)
} else {
log.Done("load tls cert for default success")
Expand Down
6 changes: 3 additions & 3 deletions internal/pkg/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (p *Item) UnInstall() error {
}
} else {
// #nosec
applycmd := qcexec.Command(os.Args[0], "experimental", "kubectl", "delete", "-f", fmt.Sprintf("%s/%s", common.GetDefaultDataDir(), p.Path), "-n", common.GetDefaultSystemNamespace(true), "--kubeconfig", common.GetDefaultNewKubeConfig())
applycmd := qcexec.Command(os.Args[0], "experimental", "kubectl", "delete", "-f", fmt.Sprintf("%s/%s", common.GetDefaultDataDir(), p.Path), "-n", common.GetDefaultSystemNamespace(true), "--kubeconfig", common.GetKubeConfig())
if output, err := applycmd.CombinedOutput(); err != nil {
p.log.Errorf("kubectl uninstall %s plugin %s failed: %s", p.Type, p.Name, string(output))
return err
Expand Down Expand Up @@ -160,7 +160,7 @@ func (p *Item) Install() error {
}
} else {
// #nosec
applycmd := qcexec.Command(os.Args[0], "experimental", "kubectl", "apply", "-f", fmt.Sprintf("%s/%s", common.GetDefaultDataDir(), p.Path), "-n", common.GetDefaultSystemNamespace(true), "--kubeconfig", common.GetDefaultNewKubeConfig())
applycmd := qcexec.Command(os.Args[0], "experimental", "kubectl", "apply", "-f", fmt.Sprintf("%s/%s", common.GetDefaultDataDir(), p.Path), "-n", common.GetDefaultSystemNamespace(true), "--kubeconfig", common.GetKubeConfig())
if output, err := applycmd.CombinedOutput(); err != nil {
p.log.Errorf("kubectl install %s plugin %s failed: %s", p.Type, p.Name, string(output))
return err
Expand Down Expand Up @@ -209,7 +209,7 @@ func (p *Item) Upgrade() (err error) {
}
} else {
// #nosec
applycmd := qcexec.Command(os.Args[0], "experimental", "kubectl", "apply", "-f", fmt.Sprintf("%s/%s", common.GetDefaultDataDir(), p.Path), "-n", common.GetDefaultSystemNamespace(true), "--kubeconfig", common.GetDefaultNewKubeConfig())
applycmd := qcexec.Command(os.Args[0], "experimental", "kubectl", "apply", "-f", fmt.Sprintf("%s/%s", common.GetDefaultDataDir(), p.Path), "-n", common.GetDefaultSystemNamespace(true), "--kubeconfig", common.GetKubeConfig())
if output, err := applycmd.CombinedOutput(); err != nil {
p.log.Errorf("kubectl upgrade %s plugin %s failed: %s", p.Type, p.Name, string(output))
return err
Expand Down
5 changes: 1 addition & 4 deletions internal/pkg/util/helm/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/easysoft/qcadmin/common"
"github.com/easysoft/qcadmin/internal/pkg/util/kutil"
"github.com/easysoft/qcadmin/internal/pkg/util/log"
"github.com/ergoapi/util/file"
"github.com/gofrs/flock"
"helm.sh/helm/v3/cmd/helm/search"
"helm.sh/helm/v3/pkg/action"
Expand Down Expand Up @@ -57,9 +56,7 @@ func NewClient(config *Config) (*Client, error) {
settings := cli.New()
client := &Client{}
settings.SetNamespace(config.Namespace)
if !file.CheckFileExists(common.GetDefaultKubeConfig()) {
settings.KubeConfig = common.GetDefaultNewKubeConfig()
}
settings.KubeConfig = common.GetKubeConfig()
settings.RepositoryCache = common.GetDefaultCacheDir()
client.settings = settings
actionConfig := &action.Configuration{}
Expand Down
4 changes: 2 additions & 2 deletions internal/pkg/util/httptls/httptls.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ func renewCertificate(domain string) error {
coreDomain := fmt.Sprintf("%s.%s.%s", ds[len(ds)-3], ds[len(ds)-2], ds[len(ds)-1])
tlsfile := fmt.Sprintf("https://pkg.qucheng.com/ssl/%s/%s/tls.yaml", mainDomain, coreDomain)
log.Debugf("renew default tls certificate use %s", tlsfile)
if err := qcexec.Command(os.Args[0], "experimental", "kubectl", "apply", "-f", tlsfile, "-n", common.GetDefaultSystemNamespace(true), "--kubeconfig", common.GetDefaultNewKubeConfig()).Run(); err != nil {
if err := qcexec.Command(os.Args[0], "experimental", "kubectl", "apply", "-f", tlsfile, "-n", common.GetDefaultSystemNamespace(true), "--kubeconfig", common.GetKubeConfig()).Run(); err != nil {
log.Warnf("load renew tls cert for %s failed, reason: %v", common.GetDefaultSystemNamespace(true), err)
}
log.Debugf("renew ingress tls certificate")
if err := qcexec.Command(os.Args[0], "experimental", "kubectl", "apply", "-f", tlsfile, "--kubeconfig", common.GetDefaultNewKubeConfig()).Run(); err != nil {
if err := qcexec.Command(os.Args[0], "experimental", "kubectl", "apply", "-f", tlsfile, "--kubeconfig", common.GetKubeConfig()).Run(); err != nil {
log.Warnf("load renew tls cert for default failed, reason: %v", err)
}
return nil
Expand Down
8 changes: 4 additions & 4 deletions pkg/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,13 +234,13 @@ func (c *Cluster) waitk3sReady(host string, sshClient ssh.Interface) error {
try++
c.log.Debugf("the %d/%d time tring to check k8s status", try, defaultBackoff.Steps)
// TODO 地址错误问题
err := sshClient.Copy(host, "/etc/rancher/k3s/k3s.yaml", common.GetDefaultNewKubeConfig())
err := sshClient.Copy(host, "/etc/rancher/k3s/k3s.yaml", common.DefaultQuickONKubeConfig())
if err != nil {
return false, nil
}
// 本地不存在config文件, 同步最新的kubeconfig文件
if !file.CheckFileExists(common.GetDefaultKubeConfig()) {
if err := sshClient.Copy(host, common.GetDefaultNewKubeConfig(), common.GetDefaultKubeConfig()); err != nil {
if !file.CheckFileExists(common.DefaultKubeConfig()) {
if err := sshClient.Copy(host, common.DefaultQuickONKubeConfig(), common.DefaultKubeConfig()); err != nil {
return false, nil
}
}
Expand Down Expand Up @@ -395,7 +395,7 @@ func (c *Cluster) DeleteNode() error {
cfg, _ := config.LoadConfig()
var wg sync.WaitGroup
sshClient := ssh.NewSSHClient(&cfg.Global.SSH, true)
kubeClient, err := k8s.NewSimpleClient(common.GetDefaultNewKubeConfig())
kubeClient, err := k8s.NewSimpleClient(common.GetKubeConfig())
if err != nil {
return errors.Errorf("load k8s client failed, reason: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/qucheng/upgrade/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,12 @@ func Upgrade(flagVersion string, log log.Logger) error {
if _, err := os.Stat(defaultTLS); err == nil {
log.StopWait()
log.Done("download tls cert success")
if err := qcexec.Command(os.Args[0], "experimental", "kubectl", "apply", "-f", defaultTLS, "-n", common.GetDefaultSystemNamespace(true), "--kubeconfig", common.GetDefaultNewKubeConfig()).Run(); err != nil {
if err := qcexec.Command(os.Args[0], "experimental", "kubectl", "apply", "-f", defaultTLS, "-n", common.GetDefaultSystemNamespace(true), "--kubeconfig", common.GetKubeConfig()).Run(); err != nil {
log.Warnf("load default tls cert failed, reason: %v", err)
} else {
log.Done("load default tls cert success")
}
qcexec.Command(os.Args[0], "experimental", "kubectl", "apply", "-f", defaultTLS, "-n", "default", "--kubeconfig", common.GetDefaultNewKubeConfig()).Run()
qcexec.Command(os.Args[0], "experimental", "kubectl", "apply", "-f", defaultTLS, "-n", "default", "--kubeconfig", common.GetKubeConfig()).Run()
args := []string{"ingress.tls.enabled=true", "ingress.tls.secretName=tls-haogs-cn"}
values, _ := helm.MergeValues(args)
defaultValue = helm.MergeMaps(defaultValue, values)
Expand Down
6 changes: 3 additions & 3 deletions pkg/quickon/quickon.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (m *Meta) GetFlags() []types.Flag {
}

func (m *Meta) GetKubeClient() error {
kubeClient, err := k8s.NewSimpleClient(common.GetDefaultNewKubeConfig())
kubeClient, err := k8s.NewSimpleClient(common.GetKubeConfig())
if err != nil {
return errors.Errorf("load k8s client failed, reason: %v", err)
}
Expand Down Expand Up @@ -236,12 +236,12 @@ func (m *Meta) Init() error {
if file.CheckFileExists(defaultTLS) {
m.log.StopWait()
m.log.Done("download tls cert success")
if err := qcexec.Command(os.Args[0], "experimental", "kubectl", "apply", "-f", defaultTLS, "-n", common.GetDefaultSystemNamespace(true), "--kubeconfig", common.GetDefaultNewKubeConfig()).Run(); err != nil {
if err := qcexec.Command(os.Args[0], "experimental", "kubectl", "apply", "-f", defaultTLS, "-n", common.GetDefaultSystemNamespace(true), "--kubeconfig", common.GetKubeConfig()).Run(); err != nil {
m.log.Warnf("load default tls cert failed, reason: %v", err)
} else {
m.log.Done("load default tls cert success")
}
qcexec.Command(os.Args[0], "experimental", "kubectl", "apply", "-f", defaultTLS, "-n", "default", "--kubeconfig", common.GetDefaultNewKubeConfig()).Run()
qcexec.Command(os.Args[0], "experimental", "kubectl", "apply", "-f", defaultTLS, "-n", "default", "--kubeconfig", common.GetKubeConfig()).Run()
break
}
_, mainDomain := kutil.SplitDomain(m.Domain)
Expand Down

0 comments on commit 454cb1e

Please sign in to comment.