diff --git a/internal/cli/cmd/infrastructure/constant/const.go b/internal/cli/cmd/infrastructure/constant/const.go index 75fca8854849..18b3e44bbb5c 100644 --- a/internal/cli/cmd/infrastructure/constant/const.go +++ b/internal/cli/cmd/infrastructure/constant/const.go @@ -41,11 +41,10 @@ const ( ) const ( - DefaultK8sClusterName = "cluster.local" - DefaultK8sDNSDomain = "cluster.local" - DefaultAPIDNSDomain = "lb.kubeblocks.local" - DefaultK8sProxyMode = "ipvs" - DefaultAPIServerPort = 6443 + DefaultK8sDNSDomain = "cluster.local" + DefaultAPIDNSDomain = "lb.kubeblocks.local" + DefaultK8sProxyMode = "ipvs" + DefaultAPIServerPort = 6443 DefaultNetworkPlugin = "cilium" ) diff --git a/internal/cli/cmd/infrastructure/create_test.go b/internal/cli/cmd/infrastructure/create_test.go index 335f89480835..85593f929bba 100644 --- a/internal/cli/cmd/infrastructure/create_test.go +++ b/internal/cli/cmd/infrastructure/create_test.go @@ -20,21 +20,81 @@ along with this program. If not, see . package infrastructure import ( + "os" + "path/filepath" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + + "k8s.io/cli-runtime/pkg/genericclioptions" + cmdtesting "k8s.io/kubectl/pkg/cmd/testing" + + "github.com/apecloud/kubeblocks/internal/cli/testing" + "github.com/apecloud/kubeblocks/test/testdata" ) -var _ = Describe("reconfigure test", func() { +var _ = Describe("infra create test", func() { + + var ( + tf *cmdtesting.TestFactory + streams genericclioptions.IOStreams + ) BeforeEach(func() { + streams, _, _, _ = genericclioptions.NewTestIOStreams() + tf = cmdtesting.NewTestFactory().WithNamespace(testing.Namespace) }) AfterEach(func() { + tf.Cleanup() }) - It("check params for creating infra", func() { + mockPrivateKeyFile := func(tmpDir string) string { + privateKeyFile := filepath.Join(tmpDir, "id_rsa.pem") + Expect(os.WriteFile(privateKeyFile, []byte("private key"), os.ModePerm)).Should(Succeed()) + return privateKeyFile + } + + It("test create k8s cluster with config file", func() { + tmpDir, _ := os.MkdirTemp(os.TempDir(), "test-") + defer os.RemoveAll(tmpDir) + By("Create cluster with config file") + o := &createOptions{ + clusterOptions: clusterOptions{ + IOStreams: streams, + }} + o.checkAndSetDefaultVersion() + o.clusterConfig = testdata.SubTestDataPath("infrastructure/infra-cluster.yaml") + Expect(o.Complete()).To(Succeed()) + o.Cluster.User.PrivateKeyPath = mockPrivateKeyFile(tmpDir) + Expect(o.Validate()).To(Succeed()) + }) + + It("test create k8s cluster with params", func() { + tmpDir, _ := os.MkdirTemp(os.TempDir(), "test-") + defer os.RemoveAll(tmpDir) + + By("Create cluster with config file") + o := &createOptions{ + clusterOptions: clusterOptions{ + IOStreams: streams, + }} + o.checkAndSetDefaultVersion() + + o.nodes = []string{ + "node0:1.1.1.1:10.128.0.1", + "node1:1.1.1.2:10.128.0.2", + "node2:1.1.1.3:10.128.0.3", + } + o.Cluster.User.PrivateKeyPath = mockPrivateKeyFile(tmpDir) + Expect(o.Complete()).Should(Succeed()) + Expect(o.Validate()).ShouldNot(Succeed()) - By("Create cluster with args") + o.Cluster.RoleGroup.Master = []string{"node0"} + o.Cluster.RoleGroup.ETCD = []string{"node0"} + o.Cluster.RoleGroup.Worker = []string{"node1", "node2"} + Expect(o.Validate()).Should(Succeed()) }) }) diff --git a/internal/cli/cmd/infrastructure/types/type.go b/internal/cli/cmd/infrastructure/types/type.go index 9a8c54d4e79f..e73ebd1aaeb0 100644 --- a/internal/cli/cmd/infrastructure/types/type.go +++ b/internal/cli/cmd/infrastructure/types/type.go @@ -94,7 +94,7 @@ type NodeOptions struct { type Kubernetes struct { // ClusterName string `json:"clusterName"` - DNSDomain string `json:"dnsDomain"` + // DNSDomain string `json:"dnsDomain"` ProxyMode string `json:"proxyMode"` Networking Networking `json:"networking"` @@ -107,7 +107,9 @@ type Kubernetes struct { type Networking struct { // using network plugin, default is calico - Plugin string `json:"plugin"` + Plugin string `json:"plugin"` + + // apis/kubeadm/types.Networking ServiceSubnet string `json:"serviceSubnet"` PodSubnet string `json:"podSubnet"` DNSDomain string `json:"dnsDomain"` @@ -158,9 +160,9 @@ func (k *Kubernetes) AutoDefaultFill() { // if k.ClusterName == "" { // k.ClusterName = constant.DefaultK8sClusterName // } - if k.DNSDomain == "" { - k.DNSDomain = constant.DefaultK8sDNSDomain - } + // if k.DNSDomain == "" { + // k.DNSDomain = constant.DefaultK8sDNSDomain + // } if k.ProxyMode == "" { k.ProxyMode = constant.DefaultK8sProxyMode } diff --git a/test/testdata/infrastructure/infra-cluster.yaml b/test/testdata/infrastructure/infra-cluster.yaml new file mode 100644 index 000000000000..67fde9ad270b --- /dev/null +++ b/test/testdata/infrastructure/infra-cluster.yaml @@ -0,0 +1,56 @@ +metadata: + name: kb-k8s-test-cluster +user: + name: user1 + privateKeyPath: ~/.ssh/test.pem +nodes: + - name: kb-infra-node-0 + address: 1.1.1.1 + internalAddress: 10.128.0.19 + - name: kb-infra-node-1 + address: 1.1.1.2 + internalAddress: 10.128.0.20 + - name: kb-infra-node-2 + address: 1.1.1.3 + internalAddress: 10.128.0.21 + options: + hugePageFeature: + hugePageSize: 10GB +roleGroup: + etcd: + - kb-infra-node-0 + - kb-infra-node-1 + - kb-infra-node-2 + master: + - kb-infra-node-0 + worker: + - kb-infra-node-1 + - kb-infra-node-2 + +kubernetes: + containerManager: containerd + # apis/kubeadm/types.Networking + networking: + plugin: cilium + dnsDomain: cluster.local + podSubnet: 10.233.64.0/18 + serviceSubnet: 10.233.0.0/18 + controlPlaneEndpoint: + domain: lb.kubeblocks.local + port: 6443 + cri: + containerRuntimeType: "containerd" + containerRuntimeEndpoint: "unix:///run/containerd/containerd.sock" + sandBoxImage: "k8s.gcr.io/pause:3.8" +addons: + - name: openebs + namespace: kube-blocks + sources: + chart: + name: openebs + version: 3.7.0 + repo: https://openebs.github.io/charts + options: + values: + - "localprovisioner.basePath=/mnt/disks" + - "localprovisioner.hostpathClass.isDefaultClass=true"