forked from labring/sealos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.go
179 lines (160 loc) · 5.28 KB
/
run.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// 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 apply
import (
"context"
"errors"
"fmt"
"net"
"strconv"
"github.com/spf13/cobra"
"golang.org/x/exp/slices"
"github.com/labring/sealos/pkg/apply/applydrivers"
"github.com/labring/sealos/pkg/apply/processor"
"github.com/labring/sealos/pkg/clusterfile"
"github.com/labring/sealos/pkg/constants"
"github.com/labring/sealos/pkg/exec"
"github.com/labring/sealos/pkg/ssh"
v2 "github.com/labring/sealos/pkg/types/v1beta1"
"github.com/labring/sealos/pkg/utils/iputils"
"github.com/labring/sealos/pkg/utils/logger"
"github.com/labring/sealos/pkg/utils/maps"
stringsutil "github.com/labring/sealos/pkg/utils/strings"
)
type ClusterArgs struct {
cluster *v2.Cluster
hosts []v2.Host
clusterName string
}
func NewApplierFromArgs(cmd *cobra.Command, args *RunArgs, imageName []string) (applydrivers.Interface, error) {
clusterPath := constants.Clusterfile(args.ClusterName)
cf := clusterfile.NewClusterFile(clusterPath,
clusterfile.WithCustomConfigFiles(args.CustomConfigFiles),
clusterfile.WithCustomEnvs(args.CustomEnv),
)
err := cf.Process()
if err != nil && err != clusterfile.ErrClusterFileNotExists {
return nil, err
}
cluster := cf.GetCluster()
if cluster == nil {
logger.Debug("creating new cluster")
if args.Masters == "" && args.Nodes == "" {
addr, _ := iputils.ListLocalHostAddrs()
args.Masters = iputils.LocalIP(addr)
}
cluster = initCluster(args.ClusterName)
} else {
cluster = cluster.DeepCopy()
}
c := &ClusterArgs{
clusterName: cluster.Name,
cluster: cluster,
}
if err = c.runArgs(cmd, args, imageName); err != nil {
return nil, err
}
ctx := withCommonContext(cmd.Context(), cmd)
return applydrivers.NewDefaultApplier(ctx, c.cluster, cf, imageName)
}
func withCommonContext(ctx context.Context, cmd *cobra.Command) context.Context {
if flagChanged(cmd, "cmd") {
v, _ := cmd.Flags().GetStringSlice("cmd")
ctx = processor.WithCommands(ctx, v)
}
if flagChanged(cmd, "env") {
v, _ := cmd.Flags().GetStringSlice("env")
ctx = processor.WithEnvs(ctx, maps.FromSlice(v))
}
return ctx
}
func (r *ClusterArgs) runArgs(cmd *cobra.Command, args *RunArgs, imageList []string) error {
if args.Cluster.ClusterName == "" {
return errors.New("cluster name can not be empty")
}
// the first run check
if r.cluster.CreationTimestamp.IsZero() {
if len(imageList) == 0 {
return errors.New("image can not be empty")
}
if len(args.Cluster.Masters) == 0 {
return errors.New("master ip(s) must specified")
}
} else {
if r.cluster.Status.Phase != v2.ClusterSuccess {
return fmt.Errorf("cluster status is not %s", v2.ClusterSuccess)
}
}
if err := PreProcessIPList(args.Cluster); err != nil {
return err
}
override := getSSHFromCommand(cmd)
if override != nil {
r.cluster.Spec.SSH = *override
}
r.cluster.SetNewImages(imageList)
defaultPort := defaultSSHPort(r.cluster.Spec.SSH.Port)
masters := stringsutil.FilterNonEmptyFromString(args.Cluster.Masters, ",")
nodes := stringsutil.FilterNonEmptyFromString(args.Cluster.Nodes, ",")
r.hosts = []v2.Host{}
sshClient := ssh.NewCacheClientFromCluster(r.cluster, true)
execer, err := exec.New(sshClient)
if err != nil {
return err
}
if len(masters) > 0 {
host, port := iputils.GetHostIPAndPortOrDefault(masters[0], defaultPort)
master0addr := net.JoinHostPort(host, port)
r.setHostWithIpsPort(masters, []string{v2.MASTER, GetHostArch(execer, master0addr)})
}
if len(nodes) > 0 {
host, port := iputils.GetHostIPAndPortOrDefault(nodes[0], defaultPort)
node0addr := net.JoinHostPort(host, port)
r.setHostWithIpsPort(nodes, []string{v2.NODE, GetHostArch(execer, node0addr)})
}
r.cluster.Spec.Hosts = append(r.cluster.Spec.Hosts, r.hosts...)
return nil
}
func (r *ClusterArgs) setHostWithIpsPort(ips []string, roles []string) {
defaultPort := defaultSSHPort(r.cluster.Spec.SSH.Port)
hostMap := map[string]*v2.Host{}
for i := range ips {
ip, port := iputils.GetHostIPAndPortOrDefault(ips[i], defaultPort)
logger.Debug("defaultPort: %s", defaultPort)
socket := fmt.Sprintf("%s:%s", ip, port)
if slices.Contains(r.cluster.GetAllIPS(), socket) {
continue
}
if _, ok := hostMap[port]; !ok {
hostMap[port] = &v2.Host{IPS: []string{socket}, Roles: roles}
continue
}
hostMap[port].IPS = append(hostMap[port].IPS, socket)
}
_, master0Port := iputils.GetHostIPAndPortOrDefault(ips[0], defaultPort)
for port, host := range hostMap {
host.IPS = removeIPListDuplicatesAndEmpty(host.IPS)
if port == master0Port && slices.Contains(roles, v2.MASTER) {
r.hosts = append([]v2.Host{*host}, r.hosts...)
continue
}
r.hosts = append(r.hosts, *host)
}
}
func defaultSSHPort(port uint16) string {
if port == 0 {
port = v2.DefaultSSHPort
}
return strconv.Itoa(int(port))
}