|
| 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