Skip to content

Commit

Permalink
refactor: use net.IP after dealing with IP string in validation (#1566)
Browse files Browse the repository at this point in the history
Signed-off-by: Allen Sun <[email protected]>
  • Loading branch information
allencloud authored Jul 9, 2022
1 parent 5857bb9 commit 4f515ac
Show file tree
Hide file tree
Showing 68 changed files with 638 additions and 389 deletions.
3 changes: 2 additions & 1 deletion apply/applydriver/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package applydriver

import (
"fmt"
"net"

"github.com/sealerio/sealer/apply/processor"
"github.com/sealerio/sealer/common"
Expand Down Expand Up @@ -159,7 +160,7 @@ func (c *Applier) reconcileCluster() error {
return c.scaleCluster(mj, md, nj, nd)
}

func (c *Applier) scaleCluster(mj, md, nj, nd []string) error {
func (c *Applier) scaleCluster(mj, md, nj, nd []net.IP) error {
logrus.Info("Start to scale this cluster")
logrus.Debugf("current cluster: master %s, worker %s", c.ClusterCurrent.GetMasterIPList(), c.ClusterCurrent.GetNodeIPList())

Expand Down
19 changes: 10 additions & 9 deletions apply/applydriver/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package applydriver

import (
"fmt"
"net"

"github.com/Masterminds/semver/v3"
"github.com/sirupsen/logrus"
Expand All @@ -24,7 +25,7 @@ import (
"github.com/sealerio/sealer/common"
"github.com/sealerio/sealer/pkg/client/k8s"
v2 "github.com/sealerio/sealer/types/api/v2"
"github.com/sealerio/sealer/utils/strings"
utilsnet "github.com/sealerio/sealer/utils/net"
)

const MasterRoleLabel = "node-role.kubernetes.io/master"
Expand All @@ -39,12 +40,12 @@ func GetCurrentCluster(client *k8s.Client) (*v2.Cluster, error) {
}

cluster := &v2.Cluster{}
var masterIPList []string
var nodeIPList []string
var masterIPList []net.IP
var nodeIPList []net.IP

for _, node := range nodes.Items {
addr := getNodeAddress(node)
if addr == "" {
if addr == nil {
continue
}
if _, ok := node.Labels[MasterRoleLabel]; ok {
Expand All @@ -58,15 +59,15 @@ func GetCurrentCluster(client *k8s.Client) (*v2.Cluster, error) {
return cluster, nil
}

func DeleteNodes(client *k8s.Client, nodeIPs []string) error {
func DeleteNodes(client *k8s.Client, nodeIPs []net.IP) error {
logrus.Infof("delete nodes %s", nodeIPs)
nodes, err := client.ListNodes()
if err != nil {
return err
}
for _, node := range nodes.Items {
addr := getNodeAddress(node)
if addr == "" || strings.NotIn(addr, nodeIPs) {
if addr == nil || utilsnet.NotInIPList(addr, nodeIPs) {
continue
}
if err := client.DeleteNode(node.Name); err != nil {
Expand All @@ -76,11 +77,11 @@ func DeleteNodes(client *k8s.Client, nodeIPs []string) error {
return nil
}

func getNodeAddress(node corev1.Node) string {
func getNodeAddress(node corev1.Node) net.IP {
if len(node.Status.Addresses) < 1 {
return ""
return nil
}
return node.Status.Addresses[0].Address
return net.ParseIP(node.Status.Addresses[0].Address)
}

func VersionCompatible(version, constraint string) bool {
Expand Down
4 changes: 2 additions & 2 deletions apply/processor/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"github.com/sealerio/sealer/pkg/plugin"
"github.com/sealerio/sealer/pkg/runtime"
v2 "github.com/sealerio/sealer/types/api/v2"
"github.com/sealerio/sealer/utils/strings"
utilsnet "github.com/sealerio/sealer/utils/net"
)

type DeleteProcessor struct {
Expand Down Expand Up @@ -66,7 +66,7 @@ func (d *DeleteProcessor) GetPhasePluginFunc(phase plugin.Phase) func(cluster *v
func (d *DeleteProcessor) UnMountRootfs(cluster *v2.Cluster) error {
hosts := append(cluster.GetMasterIPList(), cluster.GetNodeIPList()...)
config := runtime.GetRegistryConfig(common.DefaultTheClusterRootfsDir(cluster.Name), cluster.GetMaster0IP())
if strings.NotIn(config.IP, hosts) {
if utilsnet.NotInIPList(config.IP, hosts) {
hosts = append(hosts, config.IP)
}
fs, err := filesystem.NewFilesystem(common.DefaultTheClusterRootfsDir(cluster.Name))
Expand Down
16 changes: 8 additions & 8 deletions apply/processor/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,19 @@ package processor

import (
"fmt"
"net"
"strconv"

"github.com/sealerio/sealer/pkg/clusterfile"

"github.com/sealerio/sealer/utils/net"

"github.com/sealerio/sealer/common"
"github.com/sealerio/sealer/pkg/client/k8s"
"github.com/sealerio/sealer/pkg/clusterfile"
"github.com/sealerio/sealer/pkg/filesystem"
"github.com/sealerio/sealer/pkg/filesystem/clusterimage"
"github.com/sealerio/sealer/pkg/image"
"github.com/sealerio/sealer/pkg/runtime"
apiv1 "github.com/sealerio/sealer/types/api/v1"
v2 "github.com/sealerio/sealer/types/api/v2"
utilsnet "github.com/sealerio/sealer/utils/net"
"github.com/sealerio/sealer/utils/platform"
"github.com/sealerio/sealer/utils/ssh"

Expand Down Expand Up @@ -93,7 +92,8 @@ func (g *GenerateProcessor) GetPipeLine() ([]func(cluster *v2.Cluster) error, er
}

func GenerateCluster(arg *ParserArg) (*v2.Cluster, error) {
var nodeip, masterip []string
var nodeip, masterip []net.IP

cluster := &v2.Cluster{}

cluster.Kind = common.Kind
Expand All @@ -118,10 +118,10 @@ func GenerateCluster(arg *ParserArg) (*v2.Cluster, error) {
for _, v := range n.Status.Addresses {
if _, ok := n.Labels[masterLabel]; ok {
if v.Type == v1.NodeInternalIP {
masterip = append(masterip, v.Address)
masterip = append(masterip, net.ParseIP(v.Address))
}
} else if v.Type == v1.NodeInternalIP {
nodeip = append(nodeip, v.Address)
nodeip = append(nodeip, net.ParseIP(v.Address))
}
}
}
Expand All @@ -147,7 +147,7 @@ func (g *GenerateProcessor) MountRootfs(cluster *v2.Cluster) error {
}
hosts := append(cluster.GetMasterIPList(), cluster.GetNodeIPList()...)
regConfig := runtime.GetRegistryConfig(common.DefaultTheClusterRootfsDir(cluster.Name), cluster.GetMaster0IP())
if net.NotInIPList(regConfig.IP, hosts) {
if utilsnet.NotInIPList(regConfig.IP, hosts) {
hosts = append(hosts, regConfig.IP)
}
return fs.MountRootfs(cluster, hosts, false)
Expand Down
17 changes: 8 additions & 9 deletions apply/processor/scale.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ package processor

import (
"fmt"
"net"

"github.com/sealerio/sealer/common"
"github.com/sealerio/sealer/pkg/clusterfile"
"github.com/sealerio/sealer/pkg/config"
"github.com/sealerio/sealer/pkg/plugin"

"github.com/sealerio/sealer/pkg/filesystem/cloudfilesystem"

"github.com/sealerio/sealer/pkg/filesystem"
"github.com/sealerio/sealer/pkg/filesystem/cloudfilesystem"
"github.com/sealerio/sealer/pkg/plugin"
"github.com/sealerio/sealer/pkg/runtime"
v2 "github.com/sealerio/sealer/types/api/v2"
)
Expand All @@ -36,10 +35,10 @@ type ScaleProcessor struct {
KubeadmConfig *runtime.KubeadmConfig
Config config.Interface
Plugins plugin.Plugins
MastersToJoin []string
MastersToDelete []string
NodesToJoin []string
NodesToDelete []string
MastersToJoin []net.IP
MastersToDelete []net.IP
NodesToJoin []net.IP
NodesToDelete []net.IP
IsScaleUp bool
}

Expand Down Expand Up @@ -126,7 +125,7 @@ func (s *ScaleProcessor) Delete(cluster *v2.Cluster) error {
return s.Runtime.DeleteNodes(s.NodesToDelete)
}

func NewScaleProcessor(kubeadmConfig *runtime.KubeadmConfig, clusterFile clusterfile.Interface, masterToJoin, masterToDelete, nodeToJoin, nodeToDelete []string) (Processor, error) {
func NewScaleProcessor(kubeadmConfig *runtime.KubeadmConfig, clusterFile clusterfile.Interface, masterToJoin, masterToDelete, nodeToJoin, nodeToDelete []net.IP) (Processor, error) {
fs, err := filesystem.NewFilesystem(common.DefaultTheClusterRootfsDir(clusterFile.GetCluster().Name))
if err != nil {
return nil, err
Expand Down
17 changes: 11 additions & 6 deletions apply/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ package apply

import (
"fmt"
"net"
"strconv"
"strings"

"github.com/sealerio/sealer/apply/applydriver"
"github.com/sealerio/sealer/common"
v1 "github.com/sealerio/sealer/types/api/v1"
v2 "github.com/sealerio/sealer/types/api/v2"
"github.com/sealerio/sealer/utils/net"
utilsnet "github.com/sealerio/sealer/utils/net"
)

func ConstructClusterFromArg(imageName string, runArgs *Args) (*v2.Cluster, error) {
Expand Down Expand Up @@ -96,14 +97,14 @@ func validateArgs(runArgs *Args) error {
func getHosts(inMasters, inNodes string) ([]v2.Host, error) {
var err error
if isRange(inMasters) {
inMasters, err = net.IPRangeToList(inMasters)
inMasters, err = utilsnet.IPRangeToList(inMasters)
if err != nil {
return nil, err
}
}

if isRange(inNodes) {
if inNodes, err = net.IPRangeToList(inNodes); err != nil {
if inNodes, err = utilsnet.IPRangeToList(inNodes); err != nil {
return nil, err
}
}
Expand All @@ -114,15 +115,19 @@ func getHosts(inMasters, inNodes string) ([]v2.Host, error) {
if index == 0 {
// only master0 should add two roles: master and master0
masterHosts = append(masterHosts, v2.Host{
IPS: []string{master},
IPS: []net.IP{
net.ParseIP(master),
},
Roles: []string{common.MASTER, common.MASTER0},
})

continue
}

masterHosts = append(masterHosts, v2.Host{
IPS: []string{master},
IPS: []net.IP{
net.ParseIP(master),
},
Roles: []string{common.MASTER},
})
}
Expand All @@ -134,7 +139,7 @@ func getHosts(inMasters, inNodes string) ([]v2.Host, error) {
for _, node := range nodes {
if node != "" {
nodeHosts = append(nodeHosts, v2.Host{
IPS: []string{node},
IPS: []net.IP{net.ParseIP(node)},
Roles: []string{common.NODE},
})
}
Expand Down
Loading

0 comments on commit 4f515ac

Please sign in to comment.