Skip to content

Commit

Permalink
style: rename some functions and constants related with runtime imple…
Browse files Browse the repository at this point in the history
…mentation (#3763)

* style: rename constants.Data to PathResolver

* style: move non-generic constants to respective module directories

* style: rename functions

* fix: path of remote config dir

* fix: path of kubeadm init and upate file in e2e test

* fix: avoid reusing the private ConvertedKubeadmConfig

* style: rename ConfigsPath
  • Loading branch information
fengxsong committed Aug 28, 2023
1 parent 40eb424 commit d1f0ede
Show file tree
Hide file tree
Showing 43 changed files with 463 additions and 456 deletions.
5 changes: 1 addition & 4 deletions cmd/sealctl/cmd/static_pod.go
Expand Up @@ -33,13 +33,10 @@ func newStaticPodCmd() *cobra.Command {
var staticPodCmd = &cobra.Command{
Use: "static-pod",
Short: "generator static pod",
//Run: func(cmd *cobra.Command, args []string) {
//
//},
}
// check route for host
staticPodCmd.AddCommand(newLvscareCmd())
staticPodCmd.PersistentFlags().StringVar(&staticPodPath, "path", constants.KubernetesEtcStaticPod, "default kubernetes static pod path")
staticPodCmd.PersistentFlags().StringVar(&staticPodPath, "path", "/etc/kubernetes/manifests", "default kubernetes static pod path")
return staticPodCmd
}

Expand Down
39 changes: 27 additions & 12 deletions cmd/sealos/cmd/cert.go
Expand Up @@ -15,21 +15,22 @@
package cmd

import (
"errors"
"fmt"
"path"
"strings"

"github.com/spf13/cobra"

"github.com/labring/sealos/pkg/apply/processor"
"github.com/labring/sealos/pkg/clusterfile"
"github.com/labring/sealos/pkg/constants"
"github.com/labring/sealos/pkg/runtime/kubernetes"
fileutils "github.com/labring/sealos/pkg/utils/file"
)

var altNames string

func newCertCmd() *cobra.Command {
var altNames []string

cmd := &cobra.Command{
Use: "cert",
Short: "update Kubernetes API server's cert",
Expand All @@ -52,26 +53,40 @@ func newCertCmd() *cobra.Command {
processor.SyncNewVersionConfig(cluster.Name)
clusterPath := constants.Clusterfile(cluster.Name)

pathResolver := constants.NewPathResolver(cluster.Name)

var kubeadmInitFilepath string

for _, f := range []string{
path.Join(pathResolver.ConfigsPath(), "kubeadm-init.yaml"),
path.Join(pathResolver.EtcPath(), "kubeadm-init.yaml"),
} {
if fileutils.IsExist(f) {
kubeadmInitFilepath = f
break
}
}
if kubeadmInitFilepath == "" {
return errors.New("cannot locate the default kubeadm-init.yaml file")
}

cf := clusterfile.NewClusterFile(clusterPath,
clusterfile.WithCustomKubeadmFiles([]string{path.Join(constants.NewData(cluster.Name).EtcPath(), constants.DefaultInitKubeadmFileName)}),
clusterfile.WithCustomKubeadmFiles([]string{kubeadmInitFilepath}),
)
if err = cf.Process(); err != nil {
return err
}
// TODO: using different runtime
rt, err := kubernetes.New(cluster, cf.GetKubeadmConfig())
if err != nil {
return fmt.Errorf("get default runtime failed, %v", err)
}
return rt.UpdateCert(strings.Split(altNames, ","))
},
PreRunE: func(cmd *cobra.Command, args []string) error {
if strings.TrimSpace(altNames) == "" {
return fmt.Errorf("this command alt-names param can't empty")
}
return nil
return rt.UpdateCertSANs(altNames)
},
}
cmd.Flags().StringVarP(&clusterName, "cluster", "c", "default", "name of cluster to applied exec action")
cmd.Flags().StringVar(&altNames, "alt-names", "", "add domain or ip in certs, sealos.io or 10.103.97.2")
cmd.Flags().StringSliceVar(&altNames, "alt-names", []string{}, "add extra Subject Alternative Names for certs, domain or ip, eg. sealos.io or 10.103.97.2")
_ = cmd.MarkFlagRequired("alt-names")

return cmd
}
2 changes: 1 addition & 1 deletion cmd/sealos/cmd/root.go
Expand Up @@ -119,7 +119,7 @@ func onBootOnDie() {

var rootDirs = []string{
constants.LogPath(),
constants.Workdir(),
constants.WorkDir(),
}
errExit(file.MkDirs(rootDirs...))
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/apply/gen.go
Expand Up @@ -57,7 +57,7 @@ func NewClusterFromGenArgs(cmd *cobra.Command, args *RunArgs, imageNames []strin
if err != nil {
return nil, err
}
return rt.GetConfig()
return rt.GetRawConfig()
}

func genImageInfo(imageName string) (*v1beta1.MountImage, error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/apply/processor/delete.go
Expand Up @@ -112,7 +112,7 @@ func (d *DeleteProcessor) UnMountImage(cluster *v2.Cluster) error {

func (d *DeleteProcessor) CleanFS(cluster *v2.Cluster) error {
workDir := constants.ClusterDir(cluster.Name)
dataDir := constants.NewData(cluster.Name).Homedir()
dataDir := constants.NewPathResolver(cluster.Name).Root()
return fileutil.CleanFiles(workDir, dataDir)
}

Expand Down
9 changes: 5 additions & 4 deletions pkg/apply/processor/interface.go
Expand Up @@ -40,15 +40,16 @@ type Interface interface {
Execute(cluster *v2.Cluster) error
}

// compatible with older sealos versions
func SyncNewVersionConfig(clusterName string) {
d := constants.NewData(clusterName)
d := constants.NewPathResolver(clusterName)
if !file.IsExist(d.PkiPath()) {
src, target := path.Join(d.Homedir(), constants.PkiDirName), d.PkiPath()
src, target := path.Join(d.Root(), constants.PkiDirName), d.PkiPath()
logger.Info("sync new version copy pki config: %s %s", src, target)
_ = file.RecursionCopy(src, target)
}
if !file.IsExist(d.EtcPath()) {
src, target := path.Join(d.Homedir(), constants.EtcDirName), d.EtcPath()
src, target := path.Join(d.Root(), constants.EtcDirName), d.EtcPath()
logger.Info("sync new version copy etc config: %s %s", src, target)
_ = file.RecursionCopy(src, target)
}
Expand Down Expand Up @@ -153,7 +154,7 @@ func MirrorRegistry(cluster *v2.Cluster, mounts []v2.MountImage) error {
registries := cluster.GetRegistryIPAndPortList()
logger.Debug("registry nodes is: %+v", registries)
sshClient := ssh.NewSSHByCluster(cluster, true)
syncer := registry.New(constants.NewData(cluster.GetName()), sshClient, mounts)
syncer := registry.New(constants.NewPathResolver(cluster.GetName()), sshClient, mounts)
return syncer.Sync(context.Background(), registries...)
}

Expand Down
26 changes: 13 additions & 13 deletions pkg/bootstrap/context.go
Expand Up @@ -27,17 +27,17 @@ import (
type Context interface {
GetBash() constants.Bash
GetCluster() *v2.Cluster
GetData() constants.Data
GetPathResolver() constants.PathResolver
GetExecer() ssh.Interface
GetRemoter() remote.Interface
}

type realContext struct {
bash constants.Bash
cluster *v2.Cluster
data constants.Data
execer ssh.Interface
remoter remote.Interface
bash constants.Bash
cluster *v2.Cluster
pathResolver constants.PathResolver
execer ssh.Interface
remoter remote.Interface
}

func (ctx realContext) GetBash() constants.Bash {
Expand All @@ -48,8 +48,8 @@ func (ctx realContext) GetCluster() *v2.Cluster {
return ctx.cluster
}

func (ctx realContext) GetData() constants.Data {
return ctx.data
func (ctx realContext) GetPathResolver() constants.PathResolver {
return ctx.pathResolver
}

func (ctx realContext) GetExecer() ssh.Interface {
Expand All @@ -74,10 +74,10 @@ func NewContextFrom(cluster *v2.Cluster) Context {
return stringsutil.RenderShellFromEnv(shell, envs)
}
return &realContext{
cluster: cluster,
execer: execer,
bash: constants.NewBash(cluster.GetName(), cluster.GetImageLabels(), shellWrapper),
data: constants.NewData(cluster.GetName()),
remoter: remoter,
cluster: cluster,
execer: execer,
bash: constants.NewBash(cluster.GetName(), cluster.GetImageLabels(), shellWrapper),
pathResolver: constants.NewPathResolver(cluster.GetName()),
remoter: remoter,
}
}
11 changes: 5 additions & 6 deletions pkg/bootstrap/registry.go
Expand Up @@ -17,11 +17,10 @@ package bootstrap
import (
"fmt"

"github.com/labring/sealos/pkg/registry/helpers"

"k8s.io/apimachinery/pkg/util/sets"

"github.com/labring/sealos/pkg/constants"
"github.com/labring/sealos/pkg/registry/helpers"
"github.com/labring/sealos/pkg/registry/password"
"github.com/labring/sealos/pkg/utils/iputils"
"github.com/labring/sealos/pkg/utils/logger"
Expand All @@ -41,8 +40,8 @@ func (a *registryApplier) Apply(ctx Context, host string) error {
if a.upgrade == nil {
a.upgrade = password.NewUpgrade(ctx.GetCluster().GetName(), ctx.GetExecer())
}
rc := helpers.GetRegistryInfo(ctx.GetExecer(), ctx.GetData().RootFSPath(), ctx.GetCluster().GetRegistryIPAndPort())
lnCmd := fmt.Sprintf(constants.DefaultLnFmt, ctx.GetData().RootFSRegistryPath(), rc.Data)
rc := helpers.GetRegistryInfo(ctx.GetExecer(), ctx.GetPathResolver().RootFSPath(), ctx.GetCluster().GetRegistryIPAndPort())
lnCmd := fmt.Sprintf(constants.DefaultLnFmt, ctx.GetPathResolver().RootFSRegistryPath(), rc.Data)
logger.Debug("make soft link: %s", lnCmd)
if err := ctx.GetExecer().CmdAsync(host, lnCmd); err != nil {
return fmt.Errorf("failed to make link: %v", err)
Expand All @@ -67,12 +66,12 @@ func (*registryHostApplier) Filter(_ Context, _ string) bool {
}

func (*registryHostApplier) Undo(ctx Context, host string) error {
rc := helpers.GetRegistryInfo(ctx.GetExecer(), ctx.GetData().RootFSPath(), ctx.GetCluster().GetRegistryIPAndPort())
rc := helpers.GetRegistryInfo(ctx.GetExecer(), ctx.GetPathResolver().RootFSPath(), ctx.GetCluster().GetRegistryIPAndPort())
return ctx.GetRemoter().HostsDelete(host, rc.Domain)
}

func (a *registryHostApplier) Apply(ctx Context, host string) error {
rc := helpers.GetRegistryInfo(ctx.GetExecer(), ctx.GetData().RootFSPath(), ctx.GetCluster().GetRegistryIPAndPort())
rc := helpers.GetRegistryInfo(ctx.GetExecer(), ctx.GetPathResolver().RootFSPath(), ctx.GetCluster().GetRegistryIPAndPort())

if err := ctx.GetRemoter().HostsAdd(host, iputils.GetHostIP(rc.IP), rc.Domain); err != nil {
return fmt.Errorf("failed to add hosts: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/checker/cluster_checker.go
Expand Up @@ -48,7 +48,7 @@ func (n *ClusterChecker) Check(cluster *v2.Cluster, phase string) error {
}

// checker if all the node is ready
data := constants.NewData(cluster.Name)
data := constants.NewPathResolver(cluster.Name)
c, err := kubernetes.NewKubernetesClient(data.AdminFile(), "")
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion pkg/checker/crictl_checker.go
Expand Up @@ -105,7 +105,7 @@ func (n *CRICtlChecker) Check(cluster *v2.Cluster, phase string) error {
}
}
sshCtx := ssh.NewSSHByCluster(cluster, false)
root := constants.NewData(cluster.Name).RootFSPath()
root := constants.NewPathResolver(cluster.Name).RootFSPath()
regInfo := helpers.GetRegistryInfo(sshCtx, root, cluster.GetRegistryIPAndPort())

regStatus, err := n.getRegistryStatus(crictlPath, pauseImage, fmt.Sprintf("%s:%s", regInfo.Domain, regInfo.Port))
Expand Down
2 changes: 1 addition & 1 deletion pkg/checker/node_checker.go
Expand Up @@ -49,7 +49,7 @@ func (n *NodeChecker) Check(cluster *v2.Cluster, phase string) error {
return nil
}
// checker if all the node is ready
data := constants.NewData(cluster.Name)
data := constants.NewPathResolver(cluster.Name)
c, err := kubernetes.NewKubernetesClient(data.AdminFile(), "")
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion pkg/checker/pod_checker.go
Expand Up @@ -48,7 +48,7 @@ func (n *PodChecker) Check(cluster *v2.Cluster, phase string) error {
return nil
}
// checker if all the node is ready
data := constants.NewData(cluster.Name)
data := constants.NewPathResolver(cluster.Name)
c, err := kubernetes.NewKubernetesClient(data.AdminFile(), "")
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion pkg/checker/registry_checker.go
Expand Up @@ -88,7 +88,7 @@ func (n *RegistryChecker) Check(cluster *v2.Cluster, phase string) error {
}

sshCtx := ssh.NewSSHByCluster(cluster, false)
root := constants.NewData(cluster.Name).RootFSPath()
root := constants.NewPathResolver(cluster.Name).RootFSPath()
regInfo := helpers.GetRegistryInfo(sshCtx, root, cluster.GetRegistryIPAndPort())
status.Auth = fmt.Sprintf("%s:%s", regInfo.Username, regInfo.Password)
status.RegistryDomain = fmt.Sprintf("%s:%s", regInfo.Domain, regInfo.Port)
Expand Down
2 changes: 1 addition & 1 deletion pkg/checker/svc_checker.go
Expand Up @@ -51,7 +51,7 @@ func (n *SvcChecker) Check(cluster *v2.Cluster, phase string) error {
return nil
}
// checker if all the node is ready
data := constants.NewData(cluster.Name)
data := constants.NewPathResolver(cluster.Name)
c, err := kubernetes.NewKubernetesClient(data.AdminFile(), "")
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion pkg/clusterfile/util.go
Expand Up @@ -31,7 +31,7 @@ import (
var ErrClusterNotExist = fmt.Errorf("no cluster exist")

func GetDefaultClusterName() (string, error) {
files, err := os.ReadDir(constants.Workdir())
files, err := os.ReadDir(constants.WorkDir())
if err != nil {
return "", err
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/config/config.go
Expand Up @@ -20,11 +20,10 @@ import (
"os"
"path/filepath"

"github.com/labring/sealos/pkg/clusterfile"

"github.com/imdario/mergo"
"sigs.k8s.io/yaml"

"github.com/labring/sealos/pkg/clusterfile"
"github.com/labring/sealos/pkg/constants"
"github.com/labring/sealos/pkg/types/v1beta1"
"github.com/labring/sealos/pkg/utils/file"
Expand Down Expand Up @@ -78,7 +77,7 @@ func NewConfiguration(name, rootPath string, configs []v1beta1.Config) Interface

func NewDefaultConfiguration(clusterName string) Interface {
return &Dumper{
RootPath: constants.NewData(clusterName).RootFSPath(),
RootPath: constants.NewPathResolver(clusterName).RootFSPath(),
}
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/constants/bash.go
Expand Up @@ -40,7 +40,7 @@ type Bash interface {
}

type bash struct {
data Data
pathResolver PathResolver
renderContext map[string]string
wrap func(string, string) string
}
Expand All @@ -53,7 +53,7 @@ func (b *bash) getFromRenderContextOrDefault(key string) string {
}

func (b *bash) WrapBash(host, shell string) string {
return fmt.Sprintf(DefaultBashFmt, b.data.RootFSScriptsPath(), b.wrap(host, shell))
return fmt.Sprintf(DefaultBashFmt, b.pathResolver.RootFSScriptsPath(), b.wrap(host, shell))
}

func (b *bash) CheckBash(host string) string {
Expand All @@ -77,5 +77,5 @@ func (b *bash) CleanRegistryBash(host string) string {
}

func NewBash(clusterName string, renderContext map[string]string, shellWrapper func(string, string) string) Bash {
return &bash{data: NewData(clusterName), renderContext: renderContext, wrap: shellWrapper}
return &bash{pathResolver: NewPathResolver(clusterName), renderContext: renderContext, wrap: shellWrapper}
}
8 changes: 2 additions & 6 deletions pkg/constants/contants.go → pkg/constants/consts.go
Expand Up @@ -14,8 +14,8 @@

package constants

import (
"github.com/containers/storage/pkg/homedir"
const (
DefaultClusterFileName = "Clusterfile"
)

const (
Expand All @@ -36,10 +36,6 @@ const (
Cluster = "Cluster"
)

func GetHomeDir() string {
return homedir.Get()
}

var AppName = "sealos"

var Contact = `
Expand Down
23 changes: 0 additions & 23 deletions pkg/constants/kube.go

This file was deleted.

0 comments on commit d1f0ede

Please sign in to comment.