Skip to content

Commit

Permalink
Use sets when checking if kubernetes client should be initialized
Browse files Browse the repository at this point in the history
  • Loading branch information
damyan committed Sep 25, 2024
1 parent 8686286 commit a95b992
Showing 1 changed file with 10 additions and 17 deletions.
27 changes: 10 additions & 17 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package main
import (
"flag"
"fmt"
"k8s.io/apimachinery/pkg/util/sets"
"os"

"github.com/coredhcp/coredhcp/config"
Expand Down Expand Up @@ -65,7 +66,7 @@ var desiredPlugins = []*plugins.Plugin{

var (
setupLog = ctrl.Log.WithName("setup")
pluginsRequiringKubernetes = []string{"oob", "ipam", "metal"}
pluginsRequiringKubernetes = sets.New[string]("oob", "ipam", "metal")
)

func main() {
Expand Down Expand Up @@ -104,7 +105,7 @@ func main() {
}

// initialize kubernetes client, if needed
if kubernetesEnvironmentNeeded(cfg) {
if shouldSetupKubeClient(cfg) {
if err := kubernetes.InitClient(); err != nil {
setupLog.Error(err, "Failed to initialize kubernetes client")
os.Exit(1)
Expand All @@ -122,30 +123,22 @@ func main() {
}
}

func kubernetesEnvironmentNeeded(cfg *config.Config) bool {
func shouldSetupKubeClient(cfg *config.Config) bool {
configuredPlugins := sets.Set[string]{}
if cfg.Server4 != nil {
for _, plugin := range cfg.Server4.Plugins {
if pluginNeedsKubernetes(plugin) {
return true
}
configuredPlugins.Insert(plugin.Name)
}
}
if cfg.Server6 != nil {
for _, plugin := range cfg.Server6.Plugins {
if pluginNeedsKubernetes(plugin) {
return true
}
configuredPlugins.Insert(plugin.Name)
}
}

return false
}

func pluginNeedsKubernetes(plugin config.PluginConfig) bool {
for _, name := range pluginsRequiringKubernetes {
if name == plugin.Name {
return true
}
if configuredPlugins.HasAny(pluginsRequiringKubernetes.UnsortedList()...) {
return true
}

return false
}

0 comments on commit a95b992

Please sign in to comment.