-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.go
More file actions
163 lines (146 loc) · 4.44 KB
/
Copy pathmain.go
File metadata and controls
163 lines (146 loc) · 4.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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// SPDX-FileCopyrightText: 2025 SAP SE or an SAP affiliate company and IronCore contributors
// SPDX-License-Identifier: MIT
package main
import (
"flag"
"fmt"
"os"
"time"
"github.com/coredhcp/coredhcp/logger"
"github.com/sirupsen/logrus"
"github.com/coredhcp/coredhcp/config"
"github.com/coredhcp/coredhcp/plugins"
"github.com/coredhcp/coredhcp/plugins/autoconfigure"
"github.com/coredhcp/coredhcp/plugins/dns"
"github.com/coredhcp/coredhcp/plugins/example"
"github.com/coredhcp/coredhcp/plugins/file"
"github.com/coredhcp/coredhcp/plugins/leasetime"
"github.com/coredhcp/coredhcp/plugins/mtu"
"github.com/coredhcp/coredhcp/plugins/nbp"
"github.com/coredhcp/coredhcp/plugins/netmask"
"github.com/coredhcp/coredhcp/plugins/prefix"
rangeplugin "github.com/coredhcp/coredhcp/plugins/range"
"github.com/coredhcp/coredhcp/plugins/router"
"github.com/coredhcp/coredhcp/plugins/searchdomains"
"github.com/coredhcp/coredhcp/plugins/serverid"
"github.com/coredhcp/coredhcp/plugins/sleep"
"github.com/coredhcp/coredhcp/plugins/staticroute"
"github.com/coredhcp/coredhcp/server"
"github.com/ironcore-dev/fedhcp/internal/helper"
"github.com/ironcore-dev/fedhcp/internal/kubernetes"
"github.com/ironcore-dev/fedhcp/plugins/bluefield"
"github.com/ironcore-dev/fedhcp/plugins/httpboot"
"github.com/ironcore-dev/fedhcp/plugins/ipam"
"github.com/ironcore-dev/fedhcp/plugins/leases"
"github.com/ironcore-dev/fedhcp/plugins/macfilter"
"github.com/ironcore-dev/fedhcp/plugins/metal"
"github.com/ironcore-dev/fedhcp/plugins/ntp"
"github.com/ironcore-dev/fedhcp/plugins/onmetal"
"github.com/ironcore-dev/fedhcp/plugins/oob"
"github.com/ironcore-dev/fedhcp/plugins/pxeboot"
"github.com/ironcore-dev/fedhcp/plugins/stateless"
"github.com/ironcore-dev/fedhcp/plugins/ztp"
"k8s.io/apimachinery/pkg/util/sets"
)
var desiredPlugins = []*plugins.Plugin{
&autoconfigure.Plugin,
&dns.Plugin,
&example.Plugin,
&file.Plugin,
&leasetime.Plugin,
&mtu.Plugin,
&nbp.Plugin,
&netmask.Plugin,
&prefix.Plugin,
&rangeplugin.Plugin,
&router.Plugin,
&searchdomains.Plugin,
&serverid.Plugin,
&sleep.Plugin,
&staticroute.Plugin,
&bluefield.Plugin,
&httpboot.Plugin,
&ipam.Plugin,
&leases.Plugin,
&onmetal.Plugin,
&oob.Plugin,
&pxeboot.Plugin,
&macfilter.Plugin,
&metal.Plugin,
&ntp.Plugin,
&stateless.Plugin,
&ztp.Plugin,
}
var (
log = logger.GetLogger("main")
pluginsRequiringKubernetes = sets.New[string]("oob", "ipam", "metal", "leases")
)
func main() {
var configFile, logLevel string
var listPlugins bool
flag.DurationVar(&helper.Config.IpPollingInterval, "ip-polling-interval", 2*time.Second,
"Interval between polling ip state")
flag.DurationVar(&helper.Config.IpPollingTimeout, "ip-polling-timeout", 30*time.Second,
"Timeout for polling ip state")
flag.StringVar(&configFile, "config", "", "config file")
flag.BoolVar(&listPlugins, "list-plugins", false, "list plugins")
flag.StringVar(&logLevel, "loglevel", "info", "log level (debug, info, warning, error, fatal, panic)")
flag.Parse()
if listPlugins {
for _, p := range desiredPlugins {
fmt.Println(p.Name)
}
os.Exit(0)
}
level, err := logrus.ParseLevel(logLevel)
if err != nil {
fmt.Println("Invalid log level specified: ", err)
os.Exit(1)
}
log.Logger.SetLevel(level)
cfg, err := config.Load(configFile)
if err != nil {
log.Fatalf("Failed to load configuration %s: %v", configFile, err)
os.Exit(1)
}
// register plugins
for _, plugin := range desiredPlugins {
if err := plugins.RegisterPlugin(plugin); err != nil {
log.Fatalf("Failed to register plugin '%s': %v", plugin.Name, err)
os.Exit(1)
}
}
// initialize kubernetes client, if needed
if shouldSetupKubeClient(cfg) {
if err := kubernetes.InitClient(); err != nil {
log.Fatalf("Failed to initialize kubernetes client: %v", err)
os.Exit(1)
}
}
// start server
srv, err := server.Start(cfg)
if err != nil {
log.Fatalf("Failed to start server: %v", err)
os.Exit(1)
}
if err := srv.Wait(); err != nil {
log.Fatalf("Failed to wait server: %v", err)
}
}
func shouldSetupKubeClient(cfg *config.Config) bool {
configuredPlugins := sets.Set[string]{}
if cfg.Server4 != nil {
for _, plugin := range cfg.Server4.Plugins {
configuredPlugins.Insert(plugin.Name)
}
}
if cfg.Server6 != nil {
for _, plugin := range cfg.Server6.Plugins {
configuredPlugins.Insert(plugin.Name)
}
}
if configuredPlugins.HasAny(pluginsRequiringKubernetes.UnsortedList()...) {
return true
}
return false
}