-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
121 lines (101 loc) · 2.7 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright 2024 Akamai Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"flag"
"fmt"
"os"
"os/signal"
"syscall"
"time"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/klog/v2"
metadata "github.com/linode/go-metadata"
"github.com/linode/k8s-node-decorator/pkg/decorator"
)
var version string
func init() {
_ = flag.Set("logtostderr", "true")
}
func getClientset() (*kubernetes.Clientset, error) {
config, err := rest.InClusterConfig()
if err != nil {
return nil, err
}
return kubernetes.NewForConfig(config)
}
func main() {
nodeName := os.Getenv("NODE_NAME")
if nodeName == "" {
klog.Fatal("Environment variable NODE_NAME is not set")
}
var interval time.Duration
flag.DurationVar(
&interval, "poll-interval", 5*time.Minute,
"The time interval to poll and update node information",
)
var timeout time.Duration
flag.DurationVar(
&timeout, "timeout", 30*time.Second,
"The timeout for metadata and k8s client operations",
)
var prefix string
flag.StringVar(
&prefix, "prefix", "decorator.linode.com",
"Node label prefix",
)
var tagsPrefix string
flag.StringVar(
&tagsPrefix, "tags-prefix", "tags",
"Node label tags prefix",
)
flag.Parse()
if !decorator.IsValidObjectName(prefix) {
klog.Fatal(fmt.Errorf("invalid prefix"))
}
if !decorator.IsValidObjectName(tagsPrefix) {
klog.Fatal(fmt.Errorf("invalid tags prefix"))
}
klog.Infof("Starting Linode Kubernetes Node Decorator: version %s", version)
klog.Infof("The poll interval is set to %v.", interval)
klog.Infof("The timeout is set to %v.", timeout)
ctx, stop := signal.NotifyContext(context.Background(),
os.Interrupt,
syscall.SIGINT,
syscall.SIGTERM,
)
defer stop()
clientset, err := getClientset()
if err != nil {
klog.Fatal(err)
}
client, err := metadata.NewClient(
ctx,
metadata.ClientWithManagedToken(),
)
if err != nil {
klog.Fatal(err)
}
decorator.NewDecorator(
decorator.WithClient(client),
decorator.WithClientSet(clientset),
decorator.WithInterval(interval),
decorator.WithTimeout(timeout),
decorator.WithNodeName(nodeName),
decorator.WithPrefix(prefix),
decorator.WithTagsPrefix(tagsPrefix),
).Start(ctx)
}