-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
config.go
186 lines (157 loc) · 5.33 KB
/
config.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
180
181
182
183
184
185
186
/*
Copyright 2022 The KubeEdge Authors.
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 cmd
import (
"fmt"
"strings"
"github.com/spf13/cobra"
kubetypes "k8s.io/kubernetes/pkg/kubelet/types"
cmdcommon "github.com/kubeedge/kubeedge/keadm/cmd/keadm/app/cmd/common"
"github.com/kubeedge/kubeedge/keadm/cmd/keadm/app/cmd/util"
"github.com/kubeedge/kubeedge/pkg/image"
)
// Configuration represent keadm config options
type Configuration struct {
// eg. v1.9.0
KubeEdgeVersion string
// eg. kubeedge
ImageRepository string
// eg. cloud/edge
Part string
RuntimeType string
RemoteRuntimeEndpoint string
}
func newDefaultConfiguration() *Configuration {
return &Configuration{
ImageRepository: "kubeedge",
Part: "",
RuntimeType: kubetypes.RemoteContainerRuntime,
}
}
func (cfg *Configuration) GetImageRepository() string {
return cfg.ImageRepository
}
// newCmdConfig returns cobra.Command for "keadm config" command
func newCmdConfig() *cobra.Command {
cmd := &cobra.Command{
Use: "config",
Short: "Use this command to configure keadm",
Long: "Use this command to configure keadm",
}
cmd.AddCommand(newCmdConfigImages())
return cmd
}
// newCmdConfigImages returns the "keadm config images" command
func newCmdConfigImages() *cobra.Command {
cmd := &cobra.Command{
Use: "images",
Short: "Interact with container images used by keadm",
Long: "Use this command to `list/pull` keadm required container images",
}
cmd.AddCommand(newCmdConfigImagesList())
cmd.AddCommand(newCmdConfigImagesPull())
return cmd
}
// newCmdConfigImagesList returns the "keadm config images list" command
func newCmdConfigImagesList() *cobra.Command {
cfg := newDefaultConfiguration()
cmd := &cobra.Command{
Use: "list",
Short: "Print a list of images keadm will use.",
RunE: func(_ *cobra.Command, _ []string) error {
ver, err := util.GetCurrentVersion(cfg.KubeEdgeVersion)
if err != nil {
return err
}
cfg.KubeEdgeVersion = ver
images := GetKubeEdgeImages(cfg)
for _, v := range images {
fmt.Println(v)
}
return nil
},
Args: cobra.NoArgs,
}
AddImagesCommonConfigFlags(cmd, cfg)
return cmd
}
// newCmdConfigImagesPull returns the `keadm config images pull` command
func newCmdConfigImagesPull() *cobra.Command {
cfg := newDefaultConfiguration()
cmd := &cobra.Command{
Use: "pull",
Short: "Pull images used by keadm",
RunE: func(_ *cobra.Command, _ []string) error {
ver, err := util.GetCurrentVersion(cfg.KubeEdgeVersion)
if err != nil {
return err
}
cfg.KubeEdgeVersion = ver
images := GetKubeEdgeImages(cfg)
return pullImages(cfg.RuntimeType, cfg.RemoteRuntimeEndpoint, images)
},
Args: cobra.NoArgs,
}
AddImagesCommonConfigFlags(cmd, cfg)
return cmd
}
func pullImages(runtimeType string, endpoint string, images []string) error {
runtime, err := util.NewContainerRuntime(runtimeType, endpoint)
if err != nil {
return err
}
return runtime.PullImages(images)
}
// AddImagesCommonConfigFlags adds the flags that configure keadm
func AddImagesCommonConfigFlags(cmd *cobra.Command, cfg *Configuration) {
cmd.Flags().StringVar(&cfg.KubeEdgeVersion, cmdcommon.KubeEdgeVersion, cfg.KubeEdgeVersion,
`Use this key to decide which a specific KubeEdge version to be used.`,
)
cmd.Flags().StringVar(&cfg.ImageRepository, cmdcommon.ImageRepository, cfg.ImageRepository,
`Use this key to decide which image repository to pull images from.`,
)
cmd.Flags().StringVar(&cfg.Part, "part", cfg.Part,
"Use this key to set which part keadm will install: cloud part or edge part. If not set, keadm will list/pull all images used by both cloud part and edge part.")
cmd.Flags().StringVar(&cfg.RuntimeType, cmdcommon.RuntimeType, cfg.RuntimeType,
"Container runtime type, default is remote")
cmd.Flags().StringVar(&cfg.RemoteRuntimeEndpoint, cmdcommon.RemoteRuntimeEndpoint, cfg.RemoteRuntimeEndpoint,
"The endpoint of remote runtime service in edge node")
}
// GetKubeEdgeImages returns a list of container images that related part expects to use
func GetKubeEdgeImages(cfg *Configuration) []string {
var images []string
switch strings.ToLower(cfg.Part) {
case "cloud":
images = image.CloudSet(cfg.ImageRepository, cfg.KubeEdgeVersion).List()
case "edge":
images = image.EdgeSet(&cmdcommon.JoinOptions{
WithMQTT: true,
InitBaseOptions: cmdcommon.InitBaseOptions{
KubeEdgeVersion: cfg.KubeEdgeVersion,
},
ImageRepository: cfg.ImageRepository,
}).List()
default:
// if not specified, will return all images used by both cloud part and edge part
cloudSet := image.CloudSet(cfg.ImageRepository, cfg.KubeEdgeVersion)
edgeSet := image.EdgeSet(&cmdcommon.JoinOptions{
WithMQTT: true,
InitBaseOptions: cmdcommon.InitBaseOptions{
KubeEdgeVersion: cfg.KubeEdgeVersion,
},
ImageRepository: cfg.ImageRepository,
})
images = cloudSet.Merge(edgeSet).List()
}
return images
}