From 3d24a2b8578c20773c0840fa0372d8800a0ccca6 Mon Sep 17 00:00:00 2001 From: cuisongliu Date: Mon, 27 Feb 2023 16:18:32 +0800 Subject: [PATCH] fix(main): delete pkg/infra code (#2674) Signed-off-by: cuisongliu --- go.work.sum | 3 + pkg/apply/run.go | 2 +- pkg/apply/scale_test.go | 38 +- pkg/ssh/ssh.go | 2 +- pkg/types/v1beta1/constants.go | 9 - pkg/types/v1beta1/defaults.go | 38 -- pkg/types/v1beta1/defaults_cluster.go | 26 -- pkg/types/v1beta1/defaults_infra.go | 166 --------- pkg/types/v1beta1/helper.go | 9 + pkg/types/v1beta1/infra.go | 186 ---------- pkg/types/v1beta1/zz_generated.deepcopy.go | 385 ++------------------- pkg/types/validation/validation_infra.go | 135 -------- test/testhelper/utils.go | 12 - 13 files changed, 62 insertions(+), 949 deletions(-) delete mode 100644 pkg/types/v1beta1/defaults.go delete mode 100644 pkg/types/v1beta1/defaults_cluster.go delete mode 100644 pkg/types/v1beta1/defaults_infra.go delete mode 100644 pkg/types/v1beta1/infra.go delete mode 100644 pkg/types/validation/validation_infra.go diff --git a/go.work.sum b/go.work.sum index 1acd1a64df4..82e41201461 100644 --- a/go.work.sum +++ b/go.work.sum @@ -1686,6 +1686,7 @@ golang.org/x/mobile v0.0.0-20201217150744-e6ae53a27f4f/go.mod h1:skQtrUTUwhdJvXM golang.org/x/mod v0.1.1-0.20191209134235-331c550502dd/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.5.0/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s= +golang.org/x/mod v0.6.0 h1:b9gGHsz9/HhJ3HF5DHQytPpuwocVTChQJK3AvoLRD5I= golang.org/x/mod v0.6.0/go.mod h1:4mET923SAdbXp2ki8ey+zGs1SLqsuM2Y0uvdZR/fUNI= golang.org/x/net v0.0.0-20180530234432-1e491301e022/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190923162816-aa69164e4478/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= @@ -1793,6 +1794,7 @@ golang.org/x/tools v0.1.6/go.mod h1:LGqMHiF4EqQNHR1JncWGqT5BVaXmza+X+BDGol+dOxo= golang.org/x/tools v0.1.7/go.mod h1:LGqMHiF4EqQNHR1JncWGqT5BVaXmza+X+BDGol+dOxo= golang.org/x/tools v0.1.11/go.mod h1:SgwaegtQh8clINPpECJMqnxLv9I09HLqnW3RMqW0CA4= golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU= +golang.org/x/tools v0.2.0 h1:G6AHpWxTMGY1KyEYoAQ5WTtIekUUvDNjan3ugu60JvE= golang.org/x/tools v0.2.0/go.mod h1:y4OqIKeOV/fWJetJ8bXPU1sEVniLMIyDAZWeHdV+NTA= golang.org/x/xerrors v0.0.0-20190410155217-1f06c39b4373/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= @@ -1898,6 +1900,7 @@ honnef.co/go/tools v0.2.1/go.mod h1:lPVVZ2BS5TfnjLyizF7o7hv7j9/L+8cZY2hLyjP9cGY= k8s.io/cloud-provider v0.24.3 h1:Z4O3AiuFxGRPn1ilzaeWS2uBnW8LsH5MbniCTf1xAgs= k8s.io/cloud-provider v0.25.6/go.mod h1:CHXlDObyf8hE7sKxSuzbHi5lA/zHjkgEdT46r+wwvLU= k8s.io/code-generator v0.24.3 h1:itd1V1ZAYKM+WT+qQDlFKhU1D/Ff5HcEFL/icfClnZA= +k8s.io/code-generator v0.25.6 h1:md8jqhQiMqos8WaTY0ZxhDk8ttlmJl/ijCneJ8ahBNQ= k8s.io/controller-manager v0.24.3 h1:MK0JXIp5HrBkaJsZzBTrTx2MYIrMEM27LYlen/iRTnQ= k8s.io/controller-manager v0.25.6/go.mod h1:NSq3oQUkOXxcj0RZCSxRBOXbEOQSZ1cYnVbaQb1LaQE= k8s.io/csi-translation-lib v0.24.3 h1:5gZQjHNyPpy10TtchUhZPwtL86RdboeVN7YWgpDSrUc= diff --git a/pkg/apply/run.go b/pkg/apply/run.go index 3da24c7d2ca..37b85d4a8ba 100644 --- a/pkg/apply/run.go +++ b/pkg/apply/run.go @@ -145,7 +145,7 @@ func (r *ClusterArgs) setHostWithIpsPort(ips []string, roles []string) { _, master0Port := iputils.GetHostIPAndPortOrDefault(ips[0], defaultPort) for port, host := range hostMap { host.IPS = removeIPListDuplicatesAndEmpty(host.IPS) - if port == master0Port && stringsutil.InList(v2.Master, roles) { + if port == master0Port && stringsutil.InList(v2.MASTER, roles) { r.hosts = append([]v2.Host{*host}, r.hosts...) continue } diff --git a/pkg/apply/scale_test.go b/pkg/apply/scale_test.go index ca1e34ddb41..697f793cb81 100644 --- a/pkg/apply/scale_test.go +++ b/pkg/apply/scale_test.go @@ -54,7 +54,7 @@ func TestDelete(t *testing.T) { }, { IPS: []string{"192.168.16.1:22", "192.168.16.2:22", "192.168.16.3:22", "192.168.16.4:22"}, - Roles: []string{v2.Node}, + Roles: []string{v2.NODE}, }, }, }, @@ -90,7 +90,7 @@ func TestDelete(t *testing.T) { }, { IPS: []string{"192.168.16.1:22", "192.168.16.2:22", "192.168.16.3:22", "192.168.16.4:22"}, - Roles: []string{v2.Node}, + Roles: []string{v2.NODE}, }, }, }, @@ -126,7 +126,7 @@ func TestDelete(t *testing.T) { }, { IPS: []string{"192.168.16.1:22", "192.168.16.2:22", "192.168.16.3:22", "192.168.16.4:22"}, - Roles: []string{v2.Node}, + Roles: []string{v2.NODE}, }, }, }, @@ -162,7 +162,7 @@ func TestDelete(t *testing.T) { }, { IPS: []string{"192.168.16.1:22", "192.168.16.2:22", "192.168.16.3:22", "192.168.16.4:22"}, - Roles: []string{v2.Node}, + Roles: []string{v2.NODE}, }, }, }, @@ -198,7 +198,7 @@ func TestDelete(t *testing.T) { }, { IPS: []string{"192.168.16.1:22", "192.168.16.2:22", "192.168.16.3:22", "192.168.16.4:22"}, - Roles: []string{v2.Node}, + Roles: []string{v2.NODE}, }, }, }, @@ -234,7 +234,7 @@ func TestDelete(t *testing.T) { }, { IPS: []string{"192.168.16.1:22", "192.168.16.2:22", "192.168.16.3:22", "192.168.16.4:22"}, - Roles: []string{v2.Node}, + Roles: []string{v2.NODE}, }, }, }, @@ -270,7 +270,7 @@ func TestDelete(t *testing.T) { }, { IPS: []string{"192.168.16.1:22", "192.168.16.2:22", "192.168.16.3:22", "192.168.16.4:22"}, - Roles: []string{v2.Node}, + Roles: []string{v2.NODE}, }, }, }, @@ -306,7 +306,7 @@ func TestDelete(t *testing.T) { }, { IPS: []string{"192.168.16.1:22", "192.168.16.2:22", "192.168.16.3:22", "192.168.16.4:22"}, - Roles: []string{v2.Node}, + Roles: []string{v2.NODE}, }, }, }, @@ -342,7 +342,7 @@ func TestDelete(t *testing.T) { }, { IPS: []string{"192.168.16.1:22", "192.168.16.2:22", "192.168.16.3:22", "192.168.16.4:22"}, - Roles: []string{v2.Node}, + Roles: []string{v2.NODE}, }, }, }, @@ -378,7 +378,7 @@ func TestDelete(t *testing.T) { }, { IPS: []string{"192.168.16.1:22", "192.168.16.2:22", "192.168.16.3:22", "192.168.16.4:22"}, - Roles: []string{v2.Node}, + Roles: []string{v2.NODE}, }, }, }, @@ -414,7 +414,7 @@ func TestDelete(t *testing.T) { }, { IPS: []string{"192.168.16.1:22", "192.168.16.2:22", "192.168.16.3:22", "192.168.16.4:22"}, - Roles: []string{v2.Node}, + Roles: []string{v2.NODE}, }, }, }, @@ -472,7 +472,7 @@ func TestJoin(t *testing.T) { }, { IPS: []string{"192.168.16.1:22", "192.168.16.2:22", "192.168.16.3:22", "192.168.16.4:22"}, - Roles: []string{v2.Node}, + Roles: []string{v2.NODE}, }, }, }, @@ -508,7 +508,7 @@ func TestJoin(t *testing.T) { }, { IPS: []string{"192.168.16.1:22", "192.168.16.2:22", "192.168.16.3:22", "192.168.16.4:22"}, - Roles: []string{v2.Node}, + Roles: []string{v2.NODE}, }, }, }, @@ -544,7 +544,7 @@ func TestJoin(t *testing.T) { }, { IPS: []string{"192.168.16.1:22", "192.168.16.2:22", "192.168.16.3:22", "192.168.16.4:22"}, - Roles: []string{v2.Node}, + Roles: []string{v2.NODE}, }, }, }, @@ -580,7 +580,7 @@ func TestJoin(t *testing.T) { }, { IPS: []string{"192.168.16.1:22", "192.168.16.2:22", "192.168.16.3:22", "192.168.16.4:22"}, - Roles: []string{v2.Node}, + Roles: []string{v2.NODE}, }, }, }, @@ -616,7 +616,7 @@ func TestJoin(t *testing.T) { }, { IPS: []string{"192.168.16.1:22", "192.168.16.2:22", "192.168.16.3:22", "192.168.16.4:22"}, - Roles: []string{v2.Node}, + Roles: []string{v2.NODE}, }, }, }, @@ -652,7 +652,7 @@ func TestJoin(t *testing.T) { }, { IPS: []string{"192.168.16.1:22", "192.168.16.2:22", "192.168.16.3:22", "192.168.16.4:22"}, - Roles: []string{v2.Node}, + Roles: []string{v2.NODE}, }, }, }, @@ -688,7 +688,7 @@ func TestJoin(t *testing.T) { }, { IPS: []string{"192.168.16.1:22", "192.168.16.2:22", "192.168.16.3:22", "192.168.16.4:22"}, - Roles: []string{v2.Node}, + Roles: []string{v2.NODE}, }, }, }, @@ -724,7 +724,7 @@ func TestJoin(t *testing.T) { }, { IPS: []string{"192.168.16.1:22", "192.168.16.2:22", "192.168.16.3:22", "192.168.16.4:22"}, - Roles: []string{v2.Node}, + Roles: []string{v2.NODE}, }, }, }, diff --git a/pkg/ssh/ssh.go b/pkg/ssh/ssh.go index c05e90bec8c..5208f8fc1f8 100644 --- a/pkg/ssh/ssh.go +++ b/pkg/ssh/ssh.go @@ -85,7 +85,7 @@ func NewSSHClient(ssh *v2.SSH, isStdout bool) Interface { func NewSSHByCluster(cluster *v2.Cluster, isStdout bool) (Interface, error) { var ipList []string sshClient := NewSSHClient(&cluster.Spec.SSH, isStdout) - ipList = append(ipList, append(cluster.GetIPSByRole(v2.Master), cluster.GetIPSByRole(v2.Node)...)...) + ipList = append(ipList, append(cluster.GetIPSByRole(v2.MASTER), cluster.GetIPSByRole(v2.NODE)...)...) return sshClient, WaitSSHReady(sshClient, defaultMaxRetry, ipList...) } diff --git a/pkg/types/v1beta1/constants.go b/pkg/types/v1beta1/constants.go index 69458e72611..532c0568001 100644 --- a/pkg/types/v1beta1/constants.go +++ b/pkg/types/v1beta1/constants.go @@ -26,18 +26,9 @@ var ( REGISTRY = "registry" ) -type Provider string - type Arch string const ( AMD64 Arch = "amd64" ARM64 Arch = "arm64" ) - -type Protocol string - -const ( - ProtocolTCP Protocol = "tcp" - ProtocolUDP Protocol = "udp" -) diff --git a/pkg/types/v1beta1/defaults.go b/pkg/types/v1beta1/defaults.go deleted file mode 100644 index 8a983eac20d..00000000000 --- a/pkg/types/v1beta1/defaults.go +++ /dev/null @@ -1,38 +0,0 @@ -/* -Copyright 2021 cuisongliu@qq.com. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package v1beta1 - -func DefaultInfra(infra *Infra, fn func(infra *Infra) error) error { - defaultCluster(infra) - defaultHosts(infra) - defaultToStatus(infra) - return fn(infra) -} - -func In(key string, slice []string) bool { - for _, s := range slice { - if key == s { - return true - } - } - return false -} - -func DefaultCluster(cluster *Cluster, fn func(cluster *Cluster) error) error { - defaultSSH(cluster) - return fn(cluster) -} diff --git a/pkg/types/v1beta1/defaults_cluster.go b/pkg/types/v1beta1/defaults_cluster.go deleted file mode 100644 index 6516fba6171..00000000000 --- a/pkg/types/v1beta1/defaults_cluster.go +++ /dev/null @@ -1,26 +0,0 @@ -/* -Copyright 2022 cuisongliu@qq.com. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package v1beta1 - -func defaultSSH(config *Cluster) { - if config.Spec.SSH.Port == 0 { - config.Spec.SSH.Port = 22 - } - if len(config.Spec.SSH.User) == 0 { - config.Spec.SSH.User = DefaultUserRoot - } -} diff --git a/pkg/types/v1beta1/defaults_infra.go b/pkg/types/v1beta1/defaults_infra.go deleted file mode 100644 index d3e6eecf959..00000000000 --- a/pkg/types/v1beta1/defaults_infra.go +++ /dev/null @@ -1,166 +0,0 @@ -/* -Copyright 2022 cuisongliu@qq.com. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package v1beta1 - -import ( - "math/rand" - "time" - - "github.com/labring/sealos/pkg/utils/strings" - - "k8s.io/apimachinery/pkg/util/sets" -) - -const ( - SourceCidrIP = "0.0.0.0/0" - SSHPort = "22/22" - APIServerPort = "6443/6443" -) - -var ( - sshExportPort = InfraExportPort{ - Protocol: ProtocolTCP, - CidrIP: SourceCidrIP, - PortRange: SSHPort, - } - apiserverExportPort = InfraExportPort{ - Protocol: ProtocolTCP, - CidrIP: SourceCidrIP, - PortRange: APIServerPort, - } -) - -func defaultCluster(infra *Infra) { - infra.Spec.Metadata.AccessChannels.SSH.Port = 22 - if infra.Spec.Metadata.AccessChannels.SSH.Passwd == "" { - infra.Spec.Metadata.AccessChannels.SSH.Passwd = createPassword() - } - if infra.Spec.Metadata.Annotations == nil { - infra.Spec.Metadata.Annotations = make(map[string]string) - } - - if infra.Spec.Metadata.RegionIDs != nil { - infra.Spec.Metadata.RegionIDs = strings.RemoveSliceEmpty(infra.Spec.Metadata.RegionIDs) - } - if infra.Spec.Metadata.ZoneIDs != nil { - infra.Spec.Metadata.ZoneIDs = strings.RemoveSliceEmpty(infra.Spec.Metadata.ZoneIDs) - } - if infra.Spec.Metadata.Instance.Network.Bandwidth == "" { - infra.Spec.Metadata.Instance.Network.Bandwidth = "100" - } - if len(infra.Spec.Metadata.Instance.Network.ExportPorts) == 0 { - infra.Spec.Metadata.Instance.Network.ExportPorts = []InfraExportPort{ - sshExportPort, - apiserverExportPort, - } - } else { - ports := sets.NewString() - for _, port := range infra.Spec.Metadata.Instance.Network.ExportPorts { - ports.Insert(port.PortRange) - } - if !ports.Has(SSHPort) { - infra.Spec.Metadata.Instance.Network.ExportPorts = append(infra.Spec.Metadata.Instance.Network.ExportPorts, sshExportPort) - } - if !ports.Has(APIServerPort) { - infra.Spec.Metadata.Instance.Network.ExportPorts = append(infra.Spec.Metadata.Instance.Network.ExportPorts, apiserverExportPort) - } - } - if infra.Spec.Metadata.Instance.Network.PrivateCidrIP == "" { - infra.Spec.Metadata.Instance.Network.PrivateCidrIP = "172.16.0.0/24" - } -} - -func defaultHosts(infra *Infra) { - for i, h := range infra.Spec.Hosts { - if string(h.Arch) == "" { - infra.Spec.Hosts[i].Arch = AMD64 - } - if h.Memory <= 0 { - infra.Spec.Hosts[i].Memory = 4 - } - if h.CPU <= 0 { - infra.Spec.Hosts[i].CPU = 2 - } - if len(h.Disks) == 0 { - disks := make([]InfraDisk, 0) - disks = append(disks, InfraDisk{ - Capacity: 50, - }) - infra.Spec.Hosts[i].Disks = disks - } - } -} - -func defaultToStatus(infra *Infra) { - if infra.Status.Cluster.Annotations == nil { - infra.Status.Cluster.Annotations = make(map[string]string) - } - status := infra.Status.Hosts - if status == nil { - status = make([]InfraHostStatus, 0) - } - for _, h := range infra.Spec.Hosts { - index := infra.Status.FindHostsByRoles(h.Roles) - if index == -1 { - status = append(status, InfraHostStatus{Roles: h.Roles, Arch: h.Arch}) - } - } - infra.Status.Hosts = status -} - -const ( - digits = "0123456789" - specials = "~=+%^*/()[]{}/!@#$?|" - letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" - passwordLength = 16 - Master = "master" - Node = "node" -) - -type HostRole string - -func createPassword() string { - rnd := rand.New(rand.NewSource(time.Now().UnixNano())) - all := digits + specials + letter - length := passwordLength - buf := make([]byte, length) - buf[0] = digits[rnd.Intn(len(digits))] - buf[1] = specials[rnd.Intn(len(specials))] - for i := 2; i < length; i++ { - buf[i] = all[rnd.Intn(len(all))] - } - rnd.Shuffle(len(buf), func(i, j int) { - buf[i], buf[j] = buf[j], buf[i] - }) - return string(buf) -} - -func IsMaster(roles []string) bool { - return In(Master, roles) -} - -func IsNode(roles []string) bool { - return In(Node, roles) -} - -func IsAmd64(roles []string) bool { - return In(string(AMD64), roles) -} - -func IsArm64(roles []string) bool { - return In(string(AMD64), roles) -} diff --git a/pkg/types/v1beta1/helper.go b/pkg/types/v1beta1/helper.go index fc40a443b2c..bffd3668df6 100644 --- a/pkg/types/v1beta1/helper.go +++ b/pkg/types/v1beta1/helper.go @@ -103,3 +103,12 @@ func DeleteCondition(conditions []ClusterCondition, conditionType string) []Clus conditions = newConditions return conditions } + +func In(key string, slice []string) bool { + for _, s := range slice { + if key == s { + return true + } + } + return false +} diff --git a/pkg/types/v1beta1/infra.go b/pkg/types/v1beta1/infra.go deleted file mode 100644 index 2613d627d54..00000000000 --- a/pkg/types/v1beta1/infra.go +++ /dev/null @@ -1,186 +0,0 @@ -// Copyright © 2021 Alibaba Group Holding Ltd. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package v1beta1 - -import ( - "encoding/json" - "strings" - - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! -// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized. - -type InfraSSH struct { - Passwd string `json:"passwd,omitempty"` - Port int32 `json:"port"` -} - -type InfraHost struct { - Roles []string `json:"roles,omitempty"` - CPU int `json:"cpu,omitempty"` - Memory int `json:"memory,omitempty"` - Count int `json:"count"` - Disks []InfraDisk `json:"disks"` - Arch Arch `json:"arch,omitempty"` - EcsType string `json:"ecsType,omitempty"` - OS InfraOS `json:"os,omitempty"` -} - -func (h InfraHost) ToRole() HostRole { - if IsMaster(h.Roles) { - return Master - } - return Node -} - -func (h InfraHost) String() string { - data, _ := json.Marshal(&h) - return string(data) -} - -type InfraOS struct { - Name string `json:"name"` - Version string `json:"version"` - ID string `json:"id,omitempty"` -} - -type InfraDisk struct { - Capacity int `json:"capacity"` - //MountPoint string `json:"mountPoint,omitempty"` - Category string `json:"category"` -} - -type InfraCredential struct { - AccessKey string `json:"accessKey"` - AccessSecret string `json:"accessSecret"` - ProjectID string `json:"ProjectID,omitempty"` -} - -type InfraAccessChannels struct { - SSH InfraSSH `json:"ssh,omitempty"` -} - -type InfraInstance struct { - IsSeize bool `json:"isSeize,omitempty"` - - Network InfraNetworkMeta `json:"network"` -} - -type InfraNetworkMeta struct { - Bandwidth string `json:"bandwidth"` - ExportPorts []InfraExportPort `json:"exportPorts,omitempty"` - PrivateCidrIP string `json:"privateCidrIP,omitempty"` -} - -type InfraExportPort struct { - Protocol Protocol `json:"protocol"` - CidrIP string `json:"cidrIP"` - PortRange string `json:"portRange"` -} - -type InfraMetadata struct { - RegionIDs []string `json:"regionIDs,omitempty"` - ZoneIDs []string `json:"zoneIDs,omitempty"` - Annotations map[string]string `json:"annotations,omitempty"` - AccessChannels InfraAccessChannels `json:"accessChannels"` - Instance InfraInstance `json:"instance,omitempty"` -} - -// InfraSpec defines the desired state of Infra -type InfraSpec struct { - // Foo is an example field of Infra. Edit types.go to remove/update - Provider Provider `json:"provider,omitempty"` - Credential InfraCredential `json:"credential,omitempty"` - - Metadata InfraMetadata `json:"metadata,omitempty"` - Hosts []InfraHost `json:"hosts,omitempty"` -} - -type InfraClusterStatus struct { - RegionID string `json:"regionID,omitempty"` - ZoneID string `json:"zoneID,omitempty"` - SpotStrategy string `json:"spotStrategy,omitempty"` - Annotations map[string]string `json:"annotations,omitempty"` - EIP string `json:"eip,omitempty"` - Master0ID string `json:"master0ID,omitempty"` - Master0InternalIP string `json:"master0InternalIP,omitempty"` -} -type InfraHostStatus struct { - Ready bool `json:"ready"` - Roles []string `json:"roles"` - IDs string `json:"IDs,omitempty"` - IPs []string `json:"IPs,omitempty"` - InstanceType string `json:"instanceType,omitempty"` - SystemCategory string `json:"systemCategory"` - DataCategory string `json:"dataCategory,omitempty"` - Arch Arch `json:"arch,omitempty"` - ImageID string `json:"imageID,omitempty"` -} - -// InfraStatus defines the observed state of Infra -type InfraStatus struct { - Cluster InfraClusterStatus `json:"cluster"` - Hosts []InfraHostStatus `json:"hosts"` -} - -func (hs InfraHostStatus) ToHost() *InfraHost { - return &InfraHost{ - Roles: hs.Roles, - Arch: hs.Arch, - } -} - -func (s InfraStatus) FindHostsByRoles(roles []string) int { - for i, h := range s.Hosts { - if strings.Join(h.Roles, ",") == strings.Join(roles, ",") { - return i - } - } - return -1 -} - -func (s InfraStatus) FindHostsByRolesString(roles string) int { - for i, h := range s.Hosts { - if strings.Join(h.Roles, ",") == roles { - return i - } - } - return -1 -} - -// +kubebuilder:object:root=true -// +kubebuilder:subresource:status -// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object - -// Infra is the Schema for the Infras API -type Infra struct { - metav1.TypeMeta `json:",inline"` - metav1.ObjectMeta `json:"metadata,omitempty"` - - Spec InfraSpec `json:"spec,omitempty"` - Status InfraStatus `json:"status,omitempty"` -} - -// +kubebuilder:object:root=true -// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object - -// InfraList contains a list of Infra -type InfraList struct { - metav1.TypeMeta `json:",inline"` - metav1.ListMeta `json:"metadata,omitempty"` - Items []Infra `json:"items"` -} diff --git a/pkg/types/v1beta1/zz_generated.deepcopy.go b/pkg/types/v1beta1/zz_generated.deepcopy.go index f9aca114ff4..40527a2b6a5 100644 --- a/pkg/types/v1beta1/zz_generated.deepcopy.go +++ b/pkg/types/v1beta1/zz_generated.deepcopy.go @@ -124,6 +124,13 @@ func (in *ClusterStatus) DeepCopyInto(out *ClusterStatus) { (*in)[i].DeepCopyInto(&(*out)[i]) } } + if in.CommandConditions != nil { + in, out := &in.CommandConditions, &out.CommandConditions + *out = make([]CommandCondition, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } return } @@ -137,6 +144,28 @@ func (in *ClusterStatus) DeepCopy() *ClusterStatus { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *CommandCondition) DeepCopyInto(out *CommandCondition) { + *out = *in + in.LastHeartbeatTime.DeepCopyInto(&out.LastHeartbeatTime) + if in.Images != nil { + in, out := &in.Images, &out.Images + *out = make([]string, len(*in)) + copy(*out, *in) + } + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CommandCondition. +func (in *CommandCondition) DeepCopy() *CommandCondition { + if in == nil { + return nil + } + out := new(CommandCondition) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *Config) DeepCopyInto(out *Config) { *out = *in @@ -264,362 +293,6 @@ func (in ImageList) DeepCopy() ImageList { return *out } -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *Infra) DeepCopyInto(out *Infra) { - *out = *in - out.TypeMeta = in.TypeMeta - in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) - in.Spec.DeepCopyInto(&out.Spec) - in.Status.DeepCopyInto(&out.Status) - return -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Infra. -func (in *Infra) DeepCopy() *Infra { - if in == nil { - return nil - } - out := new(Infra) - in.DeepCopyInto(out) - return out -} - -// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. -func (in *Infra) DeepCopyObject() runtime.Object { - if c := in.DeepCopy(); c != nil { - return c - } - return nil -} - -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *InfraAccessChannels) DeepCopyInto(out *InfraAccessChannels) { - *out = *in - out.SSH = in.SSH - return -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new InfraAccessChannels. -func (in *InfraAccessChannels) DeepCopy() *InfraAccessChannels { - if in == nil { - return nil - } - out := new(InfraAccessChannels) - in.DeepCopyInto(out) - return out -} - -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *InfraClusterStatus) DeepCopyInto(out *InfraClusterStatus) { - *out = *in - if in.Annotations != nil { - in, out := &in.Annotations, &out.Annotations - *out = make(map[string]string, len(*in)) - for key, val := range *in { - (*out)[key] = val - } - } - return -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new InfraClusterStatus. -func (in *InfraClusterStatus) DeepCopy() *InfraClusterStatus { - if in == nil { - return nil - } - out := new(InfraClusterStatus) - in.DeepCopyInto(out) - return out -} - -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *InfraCredential) DeepCopyInto(out *InfraCredential) { - *out = *in - return -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new InfraCredential. -func (in *InfraCredential) DeepCopy() *InfraCredential { - if in == nil { - return nil - } - out := new(InfraCredential) - in.DeepCopyInto(out) - return out -} - -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *InfraDisk) DeepCopyInto(out *InfraDisk) { - *out = *in - return -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new InfraDisk. -func (in *InfraDisk) DeepCopy() *InfraDisk { - if in == nil { - return nil - } - out := new(InfraDisk) - in.DeepCopyInto(out) - return out -} - -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *InfraExportPort) DeepCopyInto(out *InfraExportPort) { - *out = *in - return -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new InfraExportPort. -func (in *InfraExportPort) DeepCopy() *InfraExportPort { - if in == nil { - return nil - } - out := new(InfraExportPort) - in.DeepCopyInto(out) - return out -} - -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *InfraHost) DeepCopyInto(out *InfraHost) { - *out = *in - if in.Roles != nil { - in, out := &in.Roles, &out.Roles - *out = make([]string, len(*in)) - copy(*out, *in) - } - if in.Disks != nil { - in, out := &in.Disks, &out.Disks - *out = make([]InfraDisk, len(*in)) - copy(*out, *in) - } - out.OS = in.OS - return -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new InfraHost. -func (in *InfraHost) DeepCopy() *InfraHost { - if in == nil { - return nil - } - out := new(InfraHost) - in.DeepCopyInto(out) - return out -} - -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *InfraHostStatus) DeepCopyInto(out *InfraHostStatus) { - *out = *in - if in.Roles != nil { - in, out := &in.Roles, &out.Roles - *out = make([]string, len(*in)) - copy(*out, *in) - } - if in.IPs != nil { - in, out := &in.IPs, &out.IPs - *out = make([]string, len(*in)) - copy(*out, *in) - } - return -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new InfraHostStatus. -func (in *InfraHostStatus) DeepCopy() *InfraHostStatus { - if in == nil { - return nil - } - out := new(InfraHostStatus) - in.DeepCopyInto(out) - return out -} - -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *InfraInstance) DeepCopyInto(out *InfraInstance) { - *out = *in - in.Network.DeepCopyInto(&out.Network) - return -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new InfraInstance. -func (in *InfraInstance) DeepCopy() *InfraInstance { - if in == nil { - return nil - } - out := new(InfraInstance) - in.DeepCopyInto(out) - return out -} - -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *InfraList) DeepCopyInto(out *InfraList) { - *out = *in - out.TypeMeta = in.TypeMeta - in.ListMeta.DeepCopyInto(&out.ListMeta) - if in.Items != nil { - in, out := &in.Items, &out.Items - *out = make([]Infra, len(*in)) - for i := range *in { - (*in)[i].DeepCopyInto(&(*out)[i]) - } - } - return -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new InfraList. -func (in *InfraList) DeepCopy() *InfraList { - if in == nil { - return nil - } - out := new(InfraList) - in.DeepCopyInto(out) - return out -} - -// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. -func (in *InfraList) DeepCopyObject() runtime.Object { - if c := in.DeepCopy(); c != nil { - return c - } - return nil -} - -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *InfraMetadata) DeepCopyInto(out *InfraMetadata) { - *out = *in - if in.RegionIDs != nil { - in, out := &in.RegionIDs, &out.RegionIDs - *out = make([]string, len(*in)) - copy(*out, *in) - } - if in.ZoneIDs != nil { - in, out := &in.ZoneIDs, &out.ZoneIDs - *out = make([]string, len(*in)) - copy(*out, *in) - } - if in.Annotations != nil { - in, out := &in.Annotations, &out.Annotations - *out = make(map[string]string, len(*in)) - for key, val := range *in { - (*out)[key] = val - } - } - out.AccessChannels = in.AccessChannels - in.Instance.DeepCopyInto(&out.Instance) - return -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new InfraMetadata. -func (in *InfraMetadata) DeepCopy() *InfraMetadata { - if in == nil { - return nil - } - out := new(InfraMetadata) - in.DeepCopyInto(out) - return out -} - -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *InfraNetworkMeta) DeepCopyInto(out *InfraNetworkMeta) { - *out = *in - if in.ExportPorts != nil { - in, out := &in.ExportPorts, &out.ExportPorts - *out = make([]InfraExportPort, len(*in)) - copy(*out, *in) - } - return -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new InfraNetworkMeta. -func (in *InfraNetworkMeta) DeepCopy() *InfraNetworkMeta { - if in == nil { - return nil - } - out := new(InfraNetworkMeta) - in.DeepCopyInto(out) - return out -} - -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *InfraOS) DeepCopyInto(out *InfraOS) { - *out = *in - return -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new InfraOS. -func (in *InfraOS) DeepCopy() *InfraOS { - if in == nil { - return nil - } - out := new(InfraOS) - in.DeepCopyInto(out) - return out -} - -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *InfraSSH) DeepCopyInto(out *InfraSSH) { - *out = *in - return -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new InfraSSH. -func (in *InfraSSH) DeepCopy() *InfraSSH { - if in == nil { - return nil - } - out := new(InfraSSH) - in.DeepCopyInto(out) - return out -} - -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *InfraSpec) DeepCopyInto(out *InfraSpec) { - *out = *in - out.Credential = in.Credential - in.Metadata.DeepCopyInto(&out.Metadata) - if in.Hosts != nil { - in, out := &in.Hosts, &out.Hosts - *out = make([]InfraHost, len(*in)) - for i := range *in { - (*in)[i].DeepCopyInto(&(*out)[i]) - } - } - return -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new InfraSpec. -func (in *InfraSpec) DeepCopy() *InfraSpec { - if in == nil { - return nil - } - out := new(InfraSpec) - in.DeepCopyInto(out) - return out -} - -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *InfraStatus) DeepCopyInto(out *InfraStatus) { - *out = *in - in.Cluster.DeepCopyInto(&out.Cluster) - if in.Hosts != nil { - in, out := &in.Hosts, &out.Hosts - *out = make([]InfraHostStatus, len(*in)) - for i := range *in { - (*in)[i].DeepCopyInto(&(*out)[i]) - } - } - return -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new InfraStatus. -func (in *InfraStatus) DeepCopy() *InfraStatus { - if in == nil { - return nil - } - out := new(InfraStatus) - in.DeepCopyInto(out) - return out -} - // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *MountImage) DeepCopyInto(out *MountImage) { *out = *in diff --git a/pkg/types/validation/validation_infra.go b/pkg/types/validation/validation_infra.go deleted file mode 100644 index 8c0992112a5..00000000000 --- a/pkg/types/validation/validation_infra.go +++ /dev/null @@ -1,135 +0,0 @@ -// Copyright © 2021 sealos. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package validation - -import ( - "strings" - - apiequality "k8s.io/apimachinery/pkg/api/equality" - apimachineryvalidation "k8s.io/apimachinery/pkg/api/validation" - "k8s.io/apimachinery/pkg/util/sets" - "k8s.io/apimachinery/pkg/util/validation/field" - - "github.com/labring/sealos/pkg/types/v1beta1" - strings2 "github.com/labring/sealos/pkg/utils/strings" -) - -// ValidateInfraName validates that the given name can be used as a infra name. -var ValidateInfraName = apimachineryvalidation.NameIsDNSSubdomain - -func validateHost(host *v1beta1.InfraHost, fldPath *field.Path) field.ErrorList { - allErrors := field.ErrorList{} - if host.Count == 0 { - allErrors = append(allErrors, field.Invalid(fldPath.Key("count"), host.Count, - "host count not set")) - } - if host.CPU == 0 { - allErrors = append(allErrors, field.Invalid(fldPath.Key("cpu"), host.CPU, - "host cpu not set")) - } - if host.Memory == 0 { - allErrors = append(allErrors, field.Invalid(fldPath.Key("memory"), host.Memory, - "host memory not set")) - } - if len(host.Disks) == 0 { - allErrors = append(allErrors, field.Invalid(fldPath.Key("disks"), host.Disks, - "host disk not set")) - } else { - for _, d := range host.Disks { - if d.Capacity <= 0 { - allErrors = append(allErrors, field.Invalid(fldPath.Child("disks").Key("capacity"), d.Capacity, - "host disk capacity not set")) - } - } - } - switch host.Arch { - case v1beta1.ARM64: - case v1beta1.AMD64: - default: - allErrors = append(allErrors, field.Invalid(fldPath.Key("arch"), host.Arch, - "arch not support")) - } - return allErrors -} - -func validateCluster(cluster *v1beta1.InfraMetadata, fldPath *field.Path) field.ErrorList { - allErrors := field.ErrorList{} - - if len(cluster.RegionIDs) == 0 { - allErrors = append(allErrors, field.Invalid(fldPath.Key("regionIDs"), cluster.RegionIDs, - "regionIDs not empty")) - } - return allErrors -} - -func validateCredential(credential *v1beta1.InfraCredential, fldPath *field.Path) field.ErrorList { - allErrors := field.ErrorList{} - - if len(credential.AccessKey) == 0 { - allErrors = append(allErrors, field.Invalid(fldPath.Key("accessKey"), credential.AccessKey, - "accessKey not empty")) - } - if len(credential.AccessSecret) == 0 { - allErrors = append(allErrors, field.Invalid(fldPath.Key("accessSecret"), credential.AccessSecret, - "accessSecret not empty")) - } - return allErrors -} - -func ValidateInfra(infra *v1beta1.Infra, fun func(infra *v1beta1.Infra) field.ErrorList) field.ErrorList { - allErrors := apimachineryvalidation.ValidateObjectMeta(&infra.ObjectMeta, false, ValidateInfraName, field.NewPath("metadata")) - allErrors = append(allErrors, validateInfraSpec(&infra.Spec, field.NewPath("spec"))...) - allErrors = append(allErrors, fun(infra)...) - return allErrors -} -func validateInfraSpec(spec *v1beta1.InfraSpec, fldPath *field.Path) field.ErrorList { - allErrors := field.ErrorList{} - allErrors = append(allErrors, validateCredential(&spec.Credential, fldPath.Child("credential"))...) - allErrors = append(allErrors, validateCluster(&spec.Metadata, fldPath.Child("cluster"))...) - var roles []string - roleSet := sets.NewString() - if spec.Hosts != nil { - for i, h := range spec.Hosts { - allErrors = append(allErrors, validateHost(&h, fldPath.Child("hosts").Index(i))...) - roles = append(roles, string(h.ToRole())) - if !roleSet.Has(strings.Join(h.Roles, ",")) { - roleSet.Insert(strings.Join(h.Roles, ",")) - } else { - allErrors = append(allErrors, field.Invalid(fldPath.Child("hosts").Index(i), h, - "hosts has repeat")) - } - } - } else { - allErrors = append(allErrors, field.Invalid(fldPath.Key("hosts"), spec.Hosts, - "hosts not empty")) - } - if !strings2.In(v1beta1.Master, roles) { - allErrors = append(allErrors, field.Invalid(fldPath.Key("hosts"), spec.Hosts, - "hosts must has role is master")) - } - - return allErrors -} -func ValidateInfraUpdate(infra, oldInfra *v1beta1.Infra) field.ErrorList { - allErrs := apimachineryvalidation.ValidateObjectMetaUpdate(&infra.ObjectMeta, &oldInfra.ObjectMeta, field.NewPath("metadata")) - newInfraClone := infra.DeepCopy() - if newInfraClone.Spec.Provider != oldInfra.Spec.Provider { - allErrs = append(allErrs, field.Forbidden(field.NewPath("spec").Key("provider"), "fields can't modify are forbidden")) - } - if !apiequality.Semantic.DeepEqual(newInfraClone.Spec.Credential, oldInfra.Spec.Credential) { - allErrs = append(allErrs, field.Forbidden(field.NewPath("spec").Key("credential"), "fields can't modify are forbidden")) - } - return allErrs -} diff --git a/test/testhelper/utils.go b/test/testhelper/utils.go index dfe19907cde..7c10327ba24 100644 --- a/test/testhelper/utils.go +++ b/test/testhelper/utils.go @@ -23,7 +23,6 @@ import ( "github.com/labring/sealos/pkg/ssh" "github.com/labring/sealos/pkg/utils/exec" - "github.com/labring/sealos/pkg/types/v1beta1" "github.com/labring/sealos/test/testhelper/settings" "github.com/onsi/gomega" @@ -65,17 +64,6 @@ type SSHClient struct { SSH *ssh.SSH } -func NewSSHClientByInfra(infra *v1beta1.Infra) *SSHClient { - config := &ssh.SSH{ - User: "root", - Password: infra.Spec.Metadata.AccessChannels.SSH.Passwd, - } - return &SSHClient{ - RemoteHostIP: infra.Status.Cluster.EIP, - SSH: config, - } -} - func IsFileExist(filename string) bool { _, err := os.Stat(filename) return !os.IsNotExist(err)