@@ -17,45 +17,53 @@ package kubernetes
1717import (
1818 "context"
1919 "fmt"
20- "strings"
21- "sync"
22-
23- "github.com/sealerio/sealer/pkg/registry"
24- "github.com/sealerio/sealer/pkg/runtime/kubernetes/kubeadm"
25- "github.com/sealerio/sealer/utils"
26- versionUtils "github.com/sealerio/sealer/utils/version"
27-
2820 "net"
2921 "path/filepath"
22+ "strings"
23+ "sync"
3024 "time"
3125
3226 "github.com/sealerio/sealer/common"
27+ "github.com/sealerio/sealer/pkg/client/k8s"
28+ "github.com/sealerio/sealer/pkg/registry"
3329 "github.com/sealerio/sealer/pkg/runtime"
30+ "github.com/sealerio/sealer/pkg/runtime/kubernetes/kubeadm"
3431 v2 "github.com/sealerio/sealer/types/api/v2"
32+ "github.com/sealerio/sealer/utils"
3533 "github.com/sealerio/sealer/utils/platform"
3634 "github.com/sealerio/sealer/utils/ssh"
3735 strUtils "github.com/sealerio/sealer/utils/strings"
38- "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta2 "
36+ versionUtils "github.com/sealerio/sealer/utils/version "
3937
4038 "github.com/sirupsen/logrus"
4139 "golang.org/x/sync/errgroup"
40+ "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta2"
4241)
4342
4443type Config struct {
45- Vlog int
46- VIP string
47- RegConfig * registry.Config
4844 // Clusterfile: the absolute path, we need to read kubeadm config from Clusterfile
4945 ClusterFileKubeConfig * kubeadm.KubeadmConfig
50- APIServerDomain string
5146}
5247
5348// Runtime struct is the runtime interface for kubernetes
5449type Runtime struct {
5550 * sync.Mutex
51+ // TODO: remove field cluster from runtime pkg
52+ // just make runtime to use essential data from cluster, rather than the whole cluster scope.
5653 cluster * v2.Cluster
54+
55+ // The KubeadmConfig used to setup the final cluster.
56+ // Its data is from KubeadmConfig input from Clusterfile and KubeadmConfig in ClusterImage.
5757 * kubeadm.KubeadmConfig
58- * Config
58+ // *Config
59+ KubeadmConfigFromClusterfile * kubeadm.KubeadmConfig
60+ APIServerDomain string
61+ Vlog int
62+ VIP string
63+
64+ // RegConfig contains the embedded registry configuration of cluster
65+ RegConfig * registry.Config
66+ * k8s.Client
5967}
6068
6169// NewDefaultRuntime arg "clusterfileKubeConfig" is the Clusterfile path/name, runtime need read kubeadm config from it
@@ -67,13 +75,24 @@ func NewDefaultRuntime(cluster *v2.Cluster, clusterfileKubeConfig *kubeadm.Kubea
6775func newKubernetesRuntime (cluster * v2.Cluster , clusterFileKubeConfig * kubeadm.KubeadmConfig ) (runtime.Interface , error ) {
6876 k := & Runtime {
6977 cluster : cluster ,
70- Config : & Config {
78+ /* Config: &Config{
7179 ClusterFileKubeConfig: clusterFileKubeConfig,
72- APIServerDomain : DefaultAPIserverDomain ,
73- },
74- KubeadmConfig : & kubeadm.KubeadmConfig {},
80+ },*/
81+ KubeadmConfigFromClusterfile : clusterFileKubeConfig ,
82+ KubeadmConfig : & kubeadm.KubeadmConfig {},
83+ APIServerDomain : DefaultAPIserverDomain ,
7584 }
76- k .Config .RegConfig = registry .GetConfig (k .getImageMountDir (), k .cluster .GetMaster0IP ())
85+
86+ var err error
87+ if k .Client , err = k8s .Newk8sClient (); err != nil {
88+ // In current design, as runtime controls all cluster operations including run, join, delete
89+ // and so on, then when executing run operation, it will definitely fail when creating k8s client
90+ // since no k8s cluster is setup. While when join and delete operation, the cluster already exists,
91+ // we can make it to create k8s client. Therefore just throw a warn log to move on.
92+ logrus .Warnf ("failed to create k8s client: %v" , err )
93+ }
94+
95+ k .RegConfig = registry .GetConfig (k .getImageMountDir (), k .cluster .GetMaster0IP ())
7796 k .setCertSANS (append (
7897 []string {"127.0.0.1" , k .getAPIServerDomain (), k .getVIP ().String ()},
7998 k .cluster .GetMasterIPStrList ()... ),
@@ -239,7 +258,7 @@ func (k *Runtime) getDNSDomain() string {
239258}
240259
241260func (k * Runtime ) getAPIServerDomain () string {
242- return k .Config . APIServerDomain
261+ return k .APIServerDomain
243262}
244263
245264func (k * Runtime ) getKubeVersion () string {
@@ -369,8 +388,8 @@ func (k *Runtime) MergeKubeadmConfig() error {
369388 if k .getKubeVersion () != "" {
370389 return nil
371390 }
372- if k .Config . ClusterFileKubeConfig != nil {
373- if err := k .LoadFromClusterfile (k .Config . ClusterFileKubeConfig ); err != nil {
391+ if k .KubeadmConfigFromClusterfile != nil {
392+ if err := k .LoadFromClusterfile (k .KubeadmConfigFromClusterfile ); err != nil {
374393 return fmt .Errorf ("failed to load kubeadm config from clusterfile: %v" , err )
375394 }
376395 }
0 commit comments