Skip to content

Commit

Permalink
resolve conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
Stevent-fei committed Dec 15, 2022
1 parent 3897332 commit 3a88984
Show file tree
Hide file tree
Showing 7 changed files with 246 additions and 0 deletions.
45 changes: 45 additions & 0 deletions pkg/cluster-runtime/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"
"net"
"strings"
"time"

"github.com/sealerio/sealer/common"
containerruntime "github.com/sealerio/sealer/pkg/container-runtime"
Expand All @@ -33,9 +34,13 @@ import (
"github.com/sealerio/sealer/pkg/runtime/kubernetes/kubeadm"
v1 "github.com/sealerio/sealer/types/api/v1"
v2 "github.com/sealerio/sealer/types/api/v2"
"github.com/sealerio/sealer/utils"
corev1 "k8s.io/api/core/v1"
)

var tryTimes = 10
var trySleepTime = time.Second

// RuntimeConfig for Installer
type RuntimeConfig struct {
ImageEngine imageengine.Interface
Expand Down Expand Up @@ -185,6 +190,10 @@ func (i *Installer) Install() error {
return err
}

if err = i.setNodeTaints(all, runtimeDriver); err != nil {
return err
}

appInstaller := NewAppInstaller(i.infraDriver, i.Distributor, extension)

if err = appInstaller.LaunchClusterImage(master0, cmds); err != nil {
Expand Down Expand Up @@ -276,3 +285,39 @@ func getAddress(addresses []corev1.NodeAddress) string {
}
return ""
}

func (i *Installer) setNodeTaints(hosts []net.IP, driver runtime.Driver) error {
var (
k8snode corev1.Node
ok bool
)
nodeList := corev1.NodeList{}
if err := driver.List(context.TODO(), &nodeList); err != nil {
return fmt.Errorf("failed to list cluster nodes: %v", err)
}
nodeTaint := make(map[string]corev1.Node)
for _, node := range nodeList.Items {
nodeTaint[getAddress(node.Status.Addresses)] = node
}

for _, ip := range hosts {
taints := i.infraDriver.GetHostTaints(ip)
if len(taints) == 0 {
continue
}
if k8snode, ok = nodeTaint[ip.String()]; ok {
newNode := k8snode.DeepCopy()
newNode.Spec.Taints = taints
newNode.SetResourceVersion("")
if err := utils.Retry(tryTimes, trySleepTime, func() error {
if err := driver.Update(context.TODO(), newNode); err != nil {
return err
}
return nil
}); err != nil {
return err
}
}
}
return nil
}
4 changes: 4 additions & 0 deletions pkg/cluster-runtime/scale.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ func (i *Installer) ScaleUp(newMasters, newWorkers []net.IP) (registry.Driver, r
return nil, nil, err
}

if err = i.setNodeTaints(all, runtimeDriver); err != nil {
return nil, nil, err
}

return registryDriver, runtimeDriver, nil
}

Expand Down
5 changes: 5 additions & 0 deletions pkg/infradriver/infradriver.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@ package infradriver
import (
"net"

k8sv1 "k8s.io/api/core/v1"

v1 "github.com/sealerio/sealer/types/api/v1"
v2 "github.com/sealerio/sealer/types/api/v2"
)

// InfraDriver treat the entire cluster as an operating system kernel,
// interface function here is the target system call.
type InfraDriver interface {
// GetHostTaints GetHostTaint return host taint
GetHostTaints(host net.IP) []k8sv1.Taint

GetHostIPList() []net.IP

GetHostIPListByRole(role string) []net.IP
Expand Down
24 changes: 24 additions & 0 deletions pkg/infradriver/ssh_infradriver.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ import (
"github.com/sealerio/sealer/utils/shellcommand"
"github.com/sealerio/sealer/utils/ssh"
"golang.org/x/sync/errgroup"
k8sv1 "k8s.io/api/core/v1"
k8snet "k8s.io/utils/net"
)

type SSHInfraDriver struct {
sshConfigs map[string]ssh.Interface
hosts []net.IP
hostTaint map[string][]k8sv1.Taint
roleHostsMap map[string][]net.IP
hostLabels map[string]map[string]string
hostEnvMap map[string]map[string]interface{}
Expand All @@ -60,6 +62,18 @@ func mergeList(hostEnv, globalEnv map[string]interface{}) map[string]interface{}
return hostEnv
}

func convertTaints(taints []string) ([]k8sv1.Taint, error) {
var k8staints []k8sv1.Taint
for _, taint := range taints {
data, err := formatData(taint)
if err != nil {
return nil, err
}
k8staints = append(k8staints, data)
}
return k8staints, nil
}

func copyEnv(origin map[string]interface{}) map[string]interface{} {
if origin == nil {
return nil
Expand Down Expand Up @@ -109,6 +123,7 @@ func NewInfraDriver(cluster *v2.Cluster) (InfraDriver, error) {
// todo need to separate env into app render data and sys render data
hostEnvMap: map[string]map[string]interface{}{},
hostLabels: map[string]map[string]string{},
hostTaint: map[string][]k8sv1.Taint{},
clusterHostAliases: cluster.Spec.HostAliases,
}

Expand Down Expand Up @@ -191,12 +206,21 @@ func NewInfraDriver(cluster *v2.Cluster) (InfraDriver, error) {
for _, ip := range host.IPS {
ret.hostEnvMap[ip.String()] = mergeList(ConvertEnv(host.Env), ret.clusterEnv)
ret.hostLabels[ip.String()] = host.Labels
taints, err := convertTaints(host.Taints)
if err != nil {
return nil, err
}
ret.hostTaint[ip.String()] = taints
}
}

return ret, err
}

func (d *SSHInfraDriver) GetHostTaints(host net.IP) []k8sv1.Taint {
return d.hostTaint[host.String()]
}

func (d *SSHInfraDriver) GetHostIPList() []net.IP {
return d.hosts
}
Expand Down
89 changes: 89 additions & 0 deletions pkg/infradriver/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright © 2022 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 infradriver

import (
"fmt"
"strings"

k8sv1 "k8s.io/api/core/v1"
)

const (
DelSymbol = "-"
EqualSymbol = "="
ColonSymbol = ":"
)

// FormatData process data in the specified format
//eg: key1=value1:NoSchedule;key1:NoSchedule;key1=value1:NoSchedule
func formatData(data string) (k8sv1.Taint, error) {
var (
key, value, effect string
taint k8sv1.Taint
TaintEffectValues = []k8sv1.TaintEffect{k8sv1.TaintEffectNoSchedule, k8sv1.TaintEffectNoExecute, k8sv1.TaintEffectPreferNoSchedule}
)

data = strings.TrimSpace(data)
switch {
case strings.Contains(data, EqualSymbol) && !strings.Contains(data, EqualSymbol+ColonSymbol):
temps := strings.Split(data, EqualSymbol)
if len(temps) != 2 {
return k8sv1.Taint{}, fmt.Errorf("faild to split taint argument: %s", data)
}
key = temps[0]
taintArgs := strings.Split(temps[1], ColonSymbol)
if len(taintArgs) != 2 {
return k8sv1.Taint{}, fmt.Errorf("error: invalid taint data: %s", data)
}
value, effect = taintArgs[0], taintArgs[1]
effect = strings.TrimSuffix(effect, DelSymbol)

case !strings.Contains(data, EqualSymbol):
temps := strings.Split(data, ColonSymbol)
if len(temps) != 2 {
return k8sv1.Taint{}, fmt.Errorf("faild to split taint argument: %s", data)
}
key, value, effect = temps[0], "", temps[1]

case strings.Contains(data, EqualSymbol+ColonSymbol):
temps := strings.Split(data, EqualSymbol+ColonSymbol)
if len(temps) != 2 {
return k8sv1.Taint{}, fmt.Errorf("faild to split taint argument: %s", data)
}
key, value, effect = temps[0], "", temps[1]
}

effect = strings.TrimSuffix(effect, DelSymbol)
if notInEffect(k8sv1.TaintEffect(effect), TaintEffectValues) {
return k8sv1.Taint{}, fmt.Errorf("taint effect %s need in %v", data, TaintEffectValues)
}

taint = k8sv1.Taint{
Key: key,
Value: value,
Effect: k8sv1.TaintEffect(effect),
}
return taint, nil
}

func notInEffect(effect k8sv1.TaintEffect, effects []k8sv1.TaintEffect) bool {
for _, e := range effects {
if e == effect {
return false
}
}
return true
}
78 changes: 78 additions & 0 deletions pkg/infradriver/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright © 2022 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 infradriver

import (
"testing"

"github.com/stretchr/testify/assert"
k8sv1 "k8s.io/api/core/v1"
)

func TestFormatData(t *testing.T) {
type args struct {
data string
wanted k8sv1.Taint
}

var tests = []struct {
name string
args args
}{
{
"test format date: key1=value1:NoSchedule",
args{
data: "key1=value1:NoSchedule",
wanted: k8sv1.Taint{
Key: "key1",
Value: "value1",
Effect: k8sv1.TaintEffect("NoSchedule"),
},
},
},
{
"test format date: key2:PreferNoSchedule",
args{
data: "key2=:PreferNoSchedule",
wanted: k8sv1.Taint{
Key: "key2",
Value: "",
Effect: k8sv1.TaintEffect("PreferNoSchedule"),
},
},
},
{
"test format date: key3=:NoExecute",
args{
data: "key3=:NoExecute",
wanted: k8sv1.Taint{
Key: "key3",
Value: "",
Effect: k8sv1.TaintEffect("NoExecute"),
},
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := formatData(tt.args.data)
if err != nil {
t.Errorf("failed to format data, error:%v", err)
}
assert.Equal(t, tt.args.wanted, result)
})
}
}
1 change: 1 addition & 0 deletions types/api/v2/cluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type Host struct {
//overwrite env
Env []string `json:"env,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
Taints []string `json:"taints,omitempty"`
}

// HostAlias holds the mapping between IP and hostnames that will be injected as an entry in the
Expand Down

0 comments on commit 3a88984

Please sign in to comment.