/
task.go
147 lines (127 loc) · 4.37 KB
/
task.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
package task
import (
"errors"
"fmt"
"github.com/jkandasa/autoeasy/pkg/utils"
routeAPI "github.com/jkandasa/autoeasy/plugin/provider/openshift/api/route"
openshiftTY "github.com/jkandasa/autoeasy/plugin/provider/openshift/types"
osroutev1 "github.com/openshift/api/route/v1"
"go.uber.org/zap"
"sigs.k8s.io/controller-runtime/pkg/client"
)
func Run(k8sClient client.Client, cfg *openshiftTY.ProviderConfig) (interface{}, error) {
switch cfg.Function {
case openshiftTY.FuncAdd:
if len(cfg.Data) == 0 {
return nil, fmt.Errorf("no data supplied. {kind:%s, function:%s}", cfg.Kind, cfg.Function)
}
return nil, add(k8sClient, cfg)
case openshiftTY.FuncKeepOnly, openshiftTY.FuncRemove:
if len(cfg.Data) == 0 {
return nil, fmt.Errorf("no data supplied. {kind:%s, function:%s}", cfg.Kind, cfg.Function)
}
fallthrough
case openshiftTY.FuncRemoveAll:
return nil, performDelete(k8sClient, cfg)
case openshiftTY.FuncGet:
return get(k8sClient, cfg)
}
return nil, fmt.Errorf("unknown function. {kind:%s, function:%s}", cfg.Kind, cfg.Function)
}
func get(k8sClient client.Client, cfg *openshiftTY.ProviderConfig) (interface{}, error) {
cfgRaw := cfg.Data[0]
routeCfg, ok := cfgRaw.(map[string]interface{})
if !ok {
return nil, errors.New("invalid data format. expects slice of map[string]interface{}")
}
metadata, err := utils.GetObjectMeta(routeCfg)
if err != nil {
zap.L().Fatal("error on getting object meta", zap.Any("metadata", metadata), zap.Error(err))
}
return routeAPI.Get(k8sClient, metadata.Name, metadata.Namespace)
}
func performDelete(k8sClient client.Client, cfg *openshiftTY.ProviderConfig) error {
opts := []client.ListOption{
client.InNamespace(""),
}
routeList, err := routeAPI.List(k8sClient, opts)
if err != nil {
zap.L().Fatal("error on getting Route list", zap.Error(err))
}
if cfg.Function == openshiftTY.FuncRemoveAll {
return delete(k8sClient, cfg, routeList.Items)
} else if cfg.Function == openshiftTY.FuncRemove || cfg.Function == openshiftTY.FuncKeepOnly {
deletionList := make([]osroutev1.Route, 0)
suppliedItems := utils.ToNamespacedNameSlice(cfg.Data)
isRemove := cfg.Function == openshiftTY.FuncRemove
for _, route := range routeList.Items {
if isRemove { // remove
if utils.ContainsNamespacedName(suppliedItems, route.ObjectMeta) {
deletionList = append(deletionList, route)
}
} else { // keep only
if utils.ContainsNamespacedName(suppliedItems, route.ObjectMeta) {
deletionList = append(deletionList, route)
}
}
}
return delete(k8sClient, cfg, deletionList)
}
return nil
}
func delete(k8sClient client.Client, cfg *openshiftTY.ProviderConfig, items []osroutev1.Route) error {
if len(items) == 0 {
return nil
}
for _, route := range items {
err := routeAPI.Delete(k8sClient, &route)
if err != nil {
return err
}
zap.L().Debug("deleted a Route", zap.String("name", route.Name), zap.String("namespace", route.Name))
}
return nil
}
func add(k8sClient client.Client, cfg *openshiftTY.ProviderConfig) error {
for _, cfgRaw := range cfg.Data {
routeCfg, ok := cfgRaw.(map[string]interface{})
if !ok {
continue
}
opts := []client.ListOption{
client.InNamespace(""),
}
routeList, err := routeAPI.List(k8sClient, opts)
if err != nil {
zap.L().Fatal("error on getting Route list", zap.Error(err))
}
metadata, err := utils.GetObjectMeta(routeCfg)
if err != nil {
zap.L().Fatal("error on getting object meta", zap.Any("metadata", metadata), zap.Error(err))
}
found := false
for _, route := range routeList.Items {
if route.Name == metadata.Name && route.Namespace == metadata.Namespace {
zap.L().Debug("Route exists", zap.String("name", metadata.Name), zap.String("namespace", metadata.Namespace))
found = true
if cfg.Config.Recreate {
zap.L().Debug("Route recreate enabled", zap.String("name", metadata.Name), zap.String("namespace", metadata.Namespace))
err = routeAPI.Delete(k8sClient, &route)
if err != nil {
return err
}
found = false
}
break
}
}
if !found {
err = routeAPI.CreateWithMap(k8sClient, routeCfg)
if err != nil {
zap.L().Fatal("error on creating Route", zap.String("name", metadata.Name), zap.String("namespace", metadata.Namespace), zap.Error(err))
}
zap.L().Info("Route created", zap.String("name", metadata.Name), zap.String("namespace", metadata.Namespace))
}
}
return nil
}