diff --git a/pkg/kosmosctl/install/install.go b/pkg/kosmosctl/install/install.go index 7f4bdb979..47d46a12d 100644 --- a/pkg/kosmosctl/install/install.go +++ b/pkg/kosmosctl/install/install.go @@ -554,30 +554,31 @@ func (o *CommandInstallOptions) createOperator() error { } func (o *CommandInstallOptions) createControlCluster() error { + clusterArgs := []string{"cluster"} + + joinOptions := join.CommandJoinOptions{ + Name: utils.DefaultClusterName, + Namespace: o.Namespace, + ImageRegistry: o.ImageRegistry, + KubeConfigStream: o.HostKubeConfigStream, + WaitTime: o.WaitTime, + KosmosClient: o.KosmosClient, + K8sClient: o.K8sClient, + K8sExtensionsClient: o.K8sExtensionsClient, + RootFlag: true, + } + switch o.Module { case utils.ClusterLink: + joinOptions.EnableLink = true + joinOptions.CNI = o.CNI + joinOptions.DefaultNICName = o.DefaultNICName + joinOptions.NetworkType = o.NetworkType + joinOptions.IpFamily = o.IpFamily + joinOptions.UseProxy = o.UseProxy controlCluster, err := o.KosmosClient.KosmosV1alpha1().Clusters().Get(context.TODO(), utils.DefaultClusterName, metav1.GetOptions{}) if err != nil { if apierrors.IsNotFound(err) { - clusterArgs := []string{"cluster"} - joinOptions := join.CommandJoinOptions{ - Name: utils.DefaultClusterName, - Namespace: o.Namespace, - ImageRegistry: o.ImageRegistry, - KubeConfigStream: o.HostKubeConfigStream, - WaitTime: o.WaitTime, - KosmosClient: o.KosmosClient, - K8sClient: o.K8sClient, - K8sExtensionsClient: o.K8sExtensionsClient, - RootFlag: true, - EnableLink: true, - CNI: o.CNI, - DefaultNICName: o.DefaultNICName, - NetworkType: o.NetworkType, - IpFamily: o.IpFamily, - UseProxy: o.UseProxy, - } - err = joinOptions.Run(clusterArgs) if err != nil { return fmt.Errorf("kosmosctl install run error, join control panel cluster failed: %s", err) @@ -613,23 +614,10 @@ func (o *CommandInstallOptions) createControlCluster() error { } } case utils.ClusterTree: + joinOptions.EnableTree = true controlCluster, err := o.KosmosClient.KosmosV1alpha1().Clusters().Get(context.TODO(), utils.DefaultClusterName, metav1.GetOptions{}) if err != nil { if apierrors.IsNotFound(err) { - clusterArgs := []string{"cluster"} - joinOptions := join.CommandJoinOptions{ - Name: utils.DefaultClusterName, - Namespace: o.Namespace, - ImageRegistry: o.ImageRegistry, - KubeConfigStream: o.HostKubeConfigStream, - K8sExtensionsClient: o.K8sExtensionsClient, - WaitTime: o.WaitTime, - KosmosClient: o.KosmosClient, - K8sClient: o.K8sClient, - RootFlag: true, - EnableTree: true, - } - err = joinOptions.Run(clusterArgs) if err != nil { return fmt.Errorf("kosmosctl install run error, join control panel cluster failed: %s", err) @@ -637,6 +625,12 @@ func (o *CommandInstallOptions) createControlCluster() error { } else { return fmt.Errorf("kosmosctl install run error, get control panel cluster failed: %s", err) } + } else { + // 'kosmos-control-cluster' has been created, only need to create serviceExport and serviceImport + err = joinOptions.CreateServiceExportAndImport() + if err != nil { + return fmt.Errorf("kosmosctl install run error, join control panel cluster failed: %s", err) + } } if len(controlCluster.Name) > 0 { @@ -650,29 +644,17 @@ func (o *CommandInstallOptions) createControlCluster() error { } } case utils.All: + joinOptions.EnableLink = true + joinOptions.EnableTree = true + joinOptions.CNI = o.CNI + joinOptions.DefaultNICName = o.DefaultNICName + joinOptions.NetworkType = o.NetworkType + joinOptions.IpFamily = o.IpFamily + joinOptions.UseProxy = o.UseProxy + controlCluster, err := o.KosmosClient.KosmosV1alpha1().Clusters().Get(context.TODO(), utils.DefaultClusterName, metav1.GetOptions{}) if err != nil { if apierrors.IsNotFound(err) { - clusterArgs := []string{"cluster"} - joinOptions := join.CommandJoinOptions{ - Name: utils.DefaultClusterName, - Namespace: o.Namespace, - ImageRegistry: o.ImageRegistry, - KubeConfigStream: o.HostKubeConfigStream, - K8sExtensionsClient: o.K8sExtensionsClient, - WaitTime: o.WaitTime, - KosmosClient: o.KosmosClient, - K8sClient: o.K8sClient, - RootFlag: true, - EnableLink: true, - CNI: o.CNI, - DefaultNICName: o.DefaultNICName, - NetworkType: o.NetworkType, - IpFamily: o.IpFamily, - UseProxy: o.UseProxy, - EnableTree: true, - } - err = joinOptions.Run(clusterArgs) if err != nil { return fmt.Errorf("kosmosctl install run error, join control panel cluster failed: %s", err) diff --git a/pkg/kosmosctl/join/join.go b/pkg/kosmosctl/join/join.go index 3cbc91167..330928092 100644 --- a/pkg/kosmosctl/join/join.go +++ b/pkg/kosmosctl/join/join.go @@ -101,8 +101,8 @@ func NewCmdJoin(f ctlutil.Factory) *cobra.Command { flags.BoolVar(&o.EnableLink, "enable-link", false, "Turn on clusterlink.") flags.StringVar(&o.CNI, "cni", "", "The cluster is configured using cni and currently supports calico and flannel.") flags.StringVar(&o.DefaultNICName, "default-nic", "", "Set default network interface card.") - flags.StringVar(&o.NetworkType, "network-type", utils.NetworkTypeGateway, "Set the cluster network connection mode, which supports gateway and p2p modes, gateway is used by default.") - flags.StringVar(&o.IpFamily, "ip-family", utils.DefaultIPv4, "Specify the IP protocol version used by network devices, common IP families include IPv4 and IPv6.") + flags.StringVar(&o.NetworkType, "network-type", "", "Set the cluster network connection mode, which supports gateway and p2p modes, gateway is used by default.") + flags.StringVar(&o.IpFamily, "ip-family", "", "Specify the IP protocol version used by network devices, common IP families include IPv4 and IPv6.") flags.StringVar(&o.UseProxy, "use-proxy", "false", "Set whether to enable proxy.") flags.BoolVar(&o.EnableTree, "enable-tree", false, "Turn on clustertree.") flags.StringVar(&o.LeafModel, "leaf-model", "", "Set leaf cluster model, which supports one-to-one model.") @@ -156,6 +156,40 @@ func (o *CommandJoinOptions) Complete(f ctlutil.Factory) error { return fmt.Errorf("kosmosctl join complete error, arg ClusterKubeConfig is required") } + //no enable-all,enable-tree,enable-link found, make 'EnableAll' with other config + if !o.EnableAll && !o.EnableTree && !o.EnableLink { + //due to NetworkType or IpFamily is not empty, make EnableLink true + if o.NetworkType != "" || o.IpFamily != "" { + klog.Warning("due to NetworkType or IpFamily is not empty, make EnableLink ture.") + o.EnableLink = true + } + + //due to LeafModel is not empty, make EnableTree true + if o.LeafModel != "" { + klog.Warning("due to LeafModel is not empty, make EnableTree true.") + o.EnableTree = true + } + + // without other config, make EnableAll default true + if !o.EnableAll && !o.EnableTree && !o.EnableLink { + klog.Warning("All of EnableAll,EnableLink,EnableTree equals false, make EnableAll default true.") + o.EnableAll = true + } + } + + if o.EnableAll { + o.EnableLink = true + o.EnableTree = true + } + + if o.IpFamily == "" { + o.IpFamily = utils.DefaultIPv4 + } + + if o.NetworkType == "" { + o.NetworkType = utils.NetworkTypeGateway + } + return nil } @@ -168,6 +202,11 @@ func (o *CommandJoinOptions) Validate(args []string) error { return fmt.Errorf("kosmosctl join validate error, namespace is not valid") } + //validate: at least one of [EnableAll,EnableTree,EnableLink] need true + if !o.EnableAll && !o.EnableTree && !o.EnableLink { + return fmt.Errorf("kosmosctl join validate error, need at least one of enable-all,enable-tree,enable-link") + } + switch args[0] { case "cluster": _, err := o.KosmosClient.KosmosV1alpha1().Clusters().Get(context.TODO(), o.Name, metav1.GetOptions{}) @@ -193,12 +232,37 @@ func (o *CommandJoinOptions) Run(args []string) error { return nil } +// CreateServiceExportAndImport only enable tree, create serviceExport and serviceImport +func (o *CommandJoinOptions) CreateServiceExportAndImport() error { + serviceExport, err := util.GenerateCustomResourceDefinition(manifest.ServiceExport, nil) + if err != nil { + return err + } + _, err = o.K8sExtensionsClient.ApiextensionsV1().CustomResourceDefinitions().Create(context.Background(), serviceExport, metav1.CreateOptions{}) + if err != nil { + if !apierrors.IsAlreadyExists(err) { + return fmt.Errorf("kosmosctl join run error, crd options failed: %v", err) + } + } + klog.Info("Create CRD " + serviceExport.Name + " successful.") + + serviceImport, err := util.GenerateCustomResourceDefinition(manifest.ServiceImport, nil) + if err != nil { + return err + } + _, err = o.K8sExtensionsClient.ApiextensionsV1().CustomResourceDefinitions().Create(context.Background(), serviceImport, metav1.CreateOptions{}) + if err != nil { + if !apierrors.IsAlreadyExists(err) { + return fmt.Errorf("kosmosctl join run error, crd options failed: %v", err) + } + } + klog.Info("Create CRD " + serviceImport.Name + " successful.") + + return nil +} + func (o *CommandJoinOptions) runCluster() error { klog.Info("Start registering cluster to kosmos control plane...") - if o.EnableAll { - o.EnableLink = true - o.EnableTree = true - } // create cluster in control panel cluster := v1alpha1.Cluster{ @@ -250,29 +314,11 @@ func (o *CommandJoinOptions) runCluster() error { } if o.EnableTree { - serviceExport, err := util.GenerateCustomResourceDefinition(manifest.ServiceExport, nil) + // create serviceExport and serviceImport + err := o.CreateServiceExportAndImport() if err != nil { return err } - _, err = o.K8sExtensionsClient.ApiextensionsV1().CustomResourceDefinitions().Create(context.Background(), serviceExport, metav1.CreateOptions{}) - if err != nil { - if !apierrors.IsAlreadyExists(err) { - return fmt.Errorf("kosmosctl join run error, crd options failed: %v", err) - } - } - klog.Info("Create CRD " + serviceExport.Name + " successful.") - - serviceImport, err := util.GenerateCustomResourceDefinition(manifest.ServiceImport, nil) - if err != nil { - return err - } - _, err = o.K8sExtensionsClient.ApiextensionsV1().CustomResourceDefinitions().Create(context.Background(), serviceImport, metav1.CreateOptions{}) - if err != nil { - if !apierrors.IsAlreadyExists(err) { - return fmt.Errorf("kosmosctl join run error, crd options failed: %v", err) - } - } - klog.Info("Create CRD " + serviceImport.Name + " successful.") if len(o.LeafModel) > 0 { switch o.LeafModel {