forked from kyma-project/cp-mod-migrator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
106 lines (89 loc) · 2.16 KB
/
main.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
package main
import (
"context"
"flag"
"fmt"
"log/slog"
"os"
migration "github.tools.sap/framefrog/cp-mod-migrator/pkg"
v211 "github.tools.sap/framefrog/cp-mod-migrator/pkg/cproxy/api/v211"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"sigs.k8s.io/controller-runtime/pkg/client"
)
type config struct {
kubeconfigPath string
isDryRun bool
}
// dryRun - returns dry run val accepted by client patch method
func (c *config) dryRun() []string {
if c.isDryRun {
return []string{"All"}
}
return []string{}
}
func addToScheme(s *runtime.Scheme) error {
for _, add := range []func(s *runtime.Scheme) error{
v211.AddToScheme,
corev1.AddToScheme,
appsv1.AddToScheme,
} {
if err := add(s); err != nil {
return fmt.Errorf("unable to add scheme: %s", err)
}
}
return nil
}
func restConfig(kubeconfigPath string) (*rest.Config, error) {
if kubeconfigPath == "" {
return rest.InClusterConfig()
}
return clientcmd.BuildConfigFromFlags("", kubeconfigPath)
}
func (cfg *config) client() (client.Client, error) {
restCfg, err := restConfig(cfg.kubeconfigPath)
if err != nil {
return nil, fmt.Errorf("unable to fetch rest config: %w", err)
}
scheme := runtime.NewScheme()
if err := addToScheme(scheme); err != nil {
return nil, err
}
return client.New(restCfg, client.Options{
Scheme: scheme,
Cache: &client.CacheOptions{
DisableFor: []client.Object{
&corev1.Secret{},
},
},
})
}
// newConfig - creates new application configuration base on passed flags
func newConfig() config {
result := config{}
flag.StringVar(&result.kubeconfigPath, "kubeconfig", "", "absolute path to the kubeconfig file")
flag.BoolVar(
&result.isDryRun,
"dry-run",
false,
"(optional) indicates that modifications should not be persisted",
)
flag.Parse()
return result
}
func exit1(err error) {
slog.Error(err.Error())
os.Exit(1)
}
func main() {
cfg := newConfig()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
dryRun := cfg.dryRun()
if err := migration.Run(ctx, cfg.client, dryRun); err != nil {
exit1(err)
}
}