Skip to content

Commit b9c0900

Browse files
OrangeBaobaoyinghai
authored andcommitted
feat: add etcd export
Signed-off-by: baoyinghai_yewu <[email protected]>
1 parent f64388c commit b9c0900

File tree

4 files changed

+168
-4
lines changed

4 files changed

+168
-4
lines changed

pkg/kubenest/constants/constant.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,13 @@ const (
143143
NodeLocalDNSIp = "169.254.20.10"
144144
NodeLocalDNSClusterDomain = "cluster.local"
145145
NodeLocalDNSService = "__PILLAR__DNS__SERVER__"
146+
147+
EnabledFeaturesAnnotation = "kosmos.io/enabled-features"
148+
FeatureComponents = "feature-components"
149+
)
150+
151+
const (
152+
ExposeEtcd = "expose-etcd"
146153
)
147154

148155
type Action string

pkg/kubenest/init.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ func NewInitPhase(opts *InitOptions) *workflow.Phase {
7474
// add server
7575
initPhase.AppendTask(tasks.NewComponentsFromManifestsTask())
7676
initPhase.AppendTask(tasks.NewEndPointTask())
77+
initPhase.AppendTask(tasks.NewFeaturesTask())
7778

7879
initPhase.SetDataInitializer(func() (workflow.RunData, error) {
7980
return newRunData(opts)
@@ -104,6 +105,7 @@ func UninstallPhase(opts *InitOptions) *workflow.Phase {
104105
destroyPhase.AppendTask(tasks.UninstallVirtualClusterServiceTask())
105106
destroyPhase.AppendTask(tasks.UninstallCertsAndKubeconfigTask())
106107
destroyPhase.AppendTask(tasks.DeleteEtcdPvcTask())
108+
destroyPhase.AppendTask(tasks.DeleteFeaturesTask())
107109
//destroyPhase.AppendTask(tasks.UninstallVirtualClusterProxyTask())
108110

109111
destroyPhase.SetDataInitializer(func() (workflow.RunData, error) {

pkg/kubenest/tasks/coredns.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func UninstallCoreDNSTask() workflow.Task {
8484
}
8585
}
8686

87-
func getCoreDNSHostComponentsConfig(client clientset.Interface, keyName string) ([]ComponentConfig, error) {
87+
func getManifestComponentsConfig(client clientset.Interface, keyName string) ([]ComponentConfig, error) {
8888
cm, err := client.CoreV1().ConfigMaps(constants.KosmosNs).Get(context.Background(), constants.ManifestComponentsConfigMap, metav1.GetOptions{})
8989
if err != nil {
9090
if apierrors.IsNotFound(err) {
@@ -115,7 +115,7 @@ func runCoreDNSHostTask(r workflow.RunData) error {
115115

116116
dynamicClient := data.DynamicClient()
117117

118-
components, err := getCoreDNSHostComponentsConfig(data.RemoteClient(), constants.HostCoreDnsComponents)
118+
components, err := getManifestComponentsConfig(data.RemoteClient(), constants.HostCoreDnsComponents)
119119
if err != nil {
120120
return err
121121
}
@@ -149,7 +149,7 @@ func uninstallCorednsHostTask(r workflow.RunData) error {
149149

150150
dynamicClient := data.DynamicClient()
151151

152-
components, err := getCoreDNSHostComponentsConfig(data.RemoteClient(), constants.HostCoreDnsComponents)
152+
components, err := getManifestComponentsConfig(data.RemoteClient(), constants.HostCoreDnsComponents)
153153
if err != nil {
154154
return err
155155
}
@@ -219,7 +219,7 @@ func runCoreDNSVirtualTask(r workflow.RunData) error {
219219
return err
220220
}
221221

222-
components, err := getCoreDNSHostComponentsConfig(data.RemoteClient(), constants.VirtualCoreDNSComponents)
222+
components, err := getManifestComponentsConfig(data.RemoteClient(), constants.VirtualCoreDNSComponents)
223223
if err != nil {
224224
return err
225225
}

pkg/kubenest/tasks/features.go

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
package tasks
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"github.com/pkg/errors"
8+
"k8s.io/klog/v2"
9+
10+
"github.com/kosmos.io/kosmos/pkg/kubenest/constants"
11+
"github.com/kosmos.io/kosmos/pkg/kubenest/util"
12+
"github.com/kosmos.io/kosmos/pkg/kubenest/workflow"
13+
)
14+
15+
func NewFeaturesTask() workflow.Task {
16+
return workflow.Task{
17+
Name: "install_features",
18+
Run: runFeatures,
19+
RunSubTasks: true,
20+
}
21+
}
22+
23+
func DeleteFeaturesTask() workflow.Task {
24+
return workflow.Task{
25+
Name: "uninstall_features",
26+
Run: cleanFeatures,
27+
RunSubTasks: true,
28+
}
29+
}
30+
31+
func doExportEtcd(data InitData, components []ComponentConfig) error {
32+
component, err := findComponent(components, constants.ExposeEtcd)
33+
if err != nil {
34+
return err
35+
}
36+
37+
dynamicClient := data.DynamicClient()
38+
imageRepository, _ := util.GetImageMessage()
39+
40+
klog.V(2).Infof("Deploy component %s", component.Name)
41+
templatedMapping := map[string]interface{}{
42+
"Namespace": data.GetNamespace(),
43+
"Name": data.GetName(),
44+
"ImageRepository": imageRepository,
45+
"EtcdListenClientPort": constants.EtcdListenClientPort,
46+
}
47+
for k, v := range data.PluginOptions() {
48+
templatedMapping[k] = v
49+
}
50+
51+
err = applyYMLTemplate(dynamicClient, component.Path, templatedMapping)
52+
if err != nil {
53+
return err
54+
}
55+
56+
return nil
57+
}
58+
59+
func findComponent(components []ComponentConfig, name string) (ComponentConfig, error) {
60+
for _, compcomponent := range components {
61+
if compcomponent.Name == name {
62+
return compcomponent, nil
63+
}
64+
}
65+
66+
return ComponentConfig{}, fmt.Errorf("component %s not found", name)
67+
}
68+
69+
func runFeatures(r workflow.RunData) error {
70+
data, ok := r.(InitData)
71+
if !ok {
72+
return errors.New("features task invoked with an invalid data struct")
73+
}
74+
75+
klog.V(4).InfoS("[features] Running feature task", "virtual cluster", klog.KObj(data))
76+
77+
vc := data.VirtualCluster()
78+
features := strings.Split(vc.Annotations[constants.EnabledFeaturesAnnotation], ",")
79+
80+
components, err := getManifestComponentsConfig(data.RemoteClient(), constants.FeatureComponents)
81+
if err != nil {
82+
return err
83+
}
84+
85+
for _, f := range features {
86+
switch strings.TrimSpace(f) {
87+
case constants.ExposeEtcd:
88+
if err := doExportEtcd(data, components); err != nil {
89+
return err
90+
}
91+
default:
92+
klog.V(2).InfoS("[features] Unknown feature", "feature", f)
93+
}
94+
}
95+
96+
return nil
97+
}
98+
99+
func cleanExportEtcd(data InitData, components []ComponentConfig) error {
100+
component, err := findComponent(components, constants.ExposeEtcd)
101+
if err != nil {
102+
return err
103+
}
104+
105+
dynamicClient := data.DynamicClient()
106+
imageRepository, _ := util.GetImageMessage()
107+
108+
klog.V(2).Infof("Delete component %s", component.Name)
109+
templatedMapping := map[string]interface{}{
110+
"Namespace": data.GetNamespace(),
111+
"Name": data.GetName(),
112+
"ImageRepository": imageRepository,
113+
"EtcdListenClientPort": constants.EtcdListenClientPort,
114+
}
115+
for k, v := range data.PluginOptions() {
116+
templatedMapping[k] = v
117+
}
118+
119+
err = deleteYMLTemplate(dynamicClient, component.Path, templatedMapping)
120+
if err != nil {
121+
return err
122+
}
123+
124+
return nil
125+
}
126+
127+
func cleanFeatures(r workflow.RunData) error {
128+
data, ok := r.(InitData)
129+
if !ok {
130+
return errors.New("features task invoked with an invalid data struct")
131+
}
132+
133+
klog.V(4).InfoS("[features] Running feature task", "virtual cluster", klog.KObj(data))
134+
135+
vc := data.VirtualCluster()
136+
features := strings.Split(vc.Annotations[constants.EnabledFeaturesAnnotation], ",")
137+
138+
components, err := getManifestComponentsConfig(data.RemoteClient(), constants.FeatureComponents)
139+
if err != nil {
140+
return err
141+
}
142+
143+
for _, f := range features {
144+
switch strings.TrimSpace(f) {
145+
case constants.ExposeEtcd:
146+
if err := cleanExportEtcd(data, components); err != nil {
147+
return err
148+
}
149+
default:
150+
klog.V(2).InfoS("[features] Unknown feature", "feature", f)
151+
}
152+
}
153+
154+
return nil
155+
}

0 commit comments

Comments
 (0)