This repository was archived by the owner on Mar 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
110 lines (93 loc) · 2.44 KB
/
Copy pathmain.go
File metadata and controls
110 lines (93 loc) · 2.44 KB
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
package main
import (
"flag"
"fmt"
"io"
"os"
"strings"
"github.com/acorn-io/baaah/pkg/restconfig"
"github.com/sirupsen/logrus"
"github.com/tylerslaton/acorn-load-balancer-plugin/pkg/controller"
"github.com/tylerslaton/acorn-load-balancer-plugin/pkg/scheme"
"github.com/tylerslaton/acorn-load-balancer-plugin/pkg/version"
"gopkg.in/yaml.v2"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
)
var (
versionFlag = flag.Bool("version", false, "print version")
// TODO - Determine default
path = flag.String("path", "", "path to a yaml file of annotations")
annotations = flag.String("annotations", "", "annotations in the form of key=value,foo=bar")
)
func main() {
flag.Parse()
fmt.Printf("Version: %s\n", version.Get())
if *versionFlag {
return
}
loadBalancerAnnotations := make(map[string]string)
if *path != "" {
logrus.Infof("Reading file path %s for annotations", *path)
err := addFileAnnotations(*path, loadBalancerAnnotations)
if err != nil {
logrus.Fatal(err)
}
}
if *annotations != "" {
err := addFlagAnnotations(*annotations, loadBalancerAnnotations)
if err != nil {
logrus.Fatal(err)
}
}
config, err := restconfig.Default()
if err != nil {
logrus.Fatal(err)
}
config.APIPath = "api"
config.GroupVersion = &corev1.SchemeGroupVersion
config.NegotiatedSerializer = scheme.Codecs
k8s := kubernetes.NewForConfigOrDie(config)
ctx := signals.SetupSignalHandler()
if err := controller.Start(ctx, controller.Options{
K8s: k8s,
Annotations: loadBalancerAnnotations,
}); err != nil {
logrus.Fatal(err)
}
<-ctx.Done()
logrus.Fatal(ctx.Err())
}
func addFileAnnotations(path string, annotations map[string]string) error {
// Read the file
file, err := os.Open(path)
if err != nil {
return err
}
data, err := io.ReadAll(file)
if err != nil {
return err
}
// Create a struct to hold the YAML data
var newAnnotations map[string]string
// Unmarshal the YAML data into the struct
err = yaml.Unmarshal(data, &annotations)
if err != nil {
return err
}
for k, v := range newAnnotations {
annotations[k] = v
}
return nil
}
func addFlagAnnotations(flag string, annotations map[string]string) error {
for _, pair := range strings.Split(flag, ",") {
parsed := strings.Split(pair, "=")
if len(parsed) != 2 {
return fmt.Errorf("specified annotations are invalid")
}
annotations[parsed[0]] = parsed[1]
}
return nil
}