-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
144 lines (128 loc) · 3.71 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and IronCore contributors
// SPDX-License-Identifier: MIT
package main
import (
"flag"
"fmt"
"os"
"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/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/metal"
"github.com/ironcore-dev/fedhcp/plugins/onmetal"
"github.com/ironcore-dev/fedhcp/plugins/oob"
"github.com/ironcore-dev/fedhcp/plugins/pxeboot"
"k8s.io/apimachinery/pkg/util/sets"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
)
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,
&ipam.Plugin,
&onmetal.Plugin,
&oob.Plugin,
&pxeboot.Plugin,
&httpboot.Plugin,
&metal.Plugin,
}
var (
setupLog = ctrl.Log.WithName("setup")
pluginsRequiringKubernetes = sets.New[string]("oob", "ipam", "metal")
)
func main() {
var configFile string
var listPlugins bool
flag.StringVar(&configFile, "config", "", "config file")
flag.BoolVar(&listPlugins, "list-plugins", false, "list plugins")
opts := zap.Options{
Development: true,
}
opts.BindFlags(flag.CommandLine)
flag.Parse()
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
if listPlugins {
for _, p := range desiredPlugins {
fmt.Println(p.Name)
}
os.Exit(0)
}
cfg, err := config.Load(configFile)
if err != nil {
setupLog.Error(err, "Failed to load configuration", "ConfigFile", configFile)
os.Exit(1)
}
// register plugins
for _, plugin := range desiredPlugins {
if err := plugins.RegisterPlugin(plugin); err != nil {
setupLog.Error(err, "Failed to register plugin", "Plugin", plugin.Name)
os.Exit(1)
}
}
// initialize kubernetes client, if needed
if shouldSetupKubeClient(cfg) {
if err := kubernetes.InitClient(); err != nil {
setupLog.Error(err, "Failed to initialize kubernetes client")
os.Exit(1)
}
}
// start server
srv, err := server.Start(cfg)
if err != nil {
setupLog.Error(err, "Failed to start server")
os.Exit(1)
}
if err := srv.Wait(); err != nil {
setupLog.Error(err, "Failed to wait server")
}
}
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
}