Skip to content

Commit

Permalink
Refactor backend opts
Browse files Browse the repository at this point in the history
Reduce duplicate config data structures and open up all backend option
to the Kubernetes backend by using the already available config data
structure.

Signed-off-by: Chris Doherty <[email protected]>
  • Loading branch information
chrisdoherty4 committed Oct 10, 2023
1 parent 67cbc9d commit c6caf59
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 32 deletions.
23 changes: 4 additions & 19 deletions internal/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func New(ctx context.Context, opts Options) (Client, error) {
return flatfile.FromYAMLFile(opts.Flatfile.Path)

case opts.Kubernetes != nil:
kubeclient, err := kubernetes.NewBackend(ctx, kubernetes.BackendConfig{
kubeclient, err := kubernetes.NewBackend(ctx, kubernetes.Config{
Kubeconfig: opts.Kubernetes.Kubeconfig,
APIServerAddress: opts.Kubernetes.APIServerAddress,
Namespace: opts.Kubernetes.Namespace,
Expand All @@ -61,8 +61,8 @@ func New(ctx context.Context, opts Options) (Client, error) {
// Options contains all options for all backend implementations. Only one backend option can be
// specified at a time.
type Options struct {
Flatfile *FlatfileOptions
Kubernetes *KubernetesOptions
Flatfile *Flatfile
Kubernetes *kubernetes.Config
}

func (o Options) validate() error {
Expand All @@ -84,22 +84,7 @@ func (o Options) validate() error {
}

// FlatFileOptions is the configuration for a flatfile backend.
type FlatfileOptions struct {
type Flatfile struct {
// Path is a path to a YAML file containing a list of flatfile instances.
Path string
}

// KubernetesOptions is the configuration for a Kubernetes backend.
type KubernetesOptions struct {
// APIServerAddress is the URL of the Kube API the Kubernetes client talks to.
// Optional
APIServerAddress string

// Kuberconfig is a path to a Kubeconfig file used by the Kubernetes client.
// Optional
Kubeconfig string

// KubeNamespace is a namespace override to have Hegel use for reading resources.
// Optional
Namespace string
}
5 changes: 3 additions & 2 deletions internal/backend/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

. "github.com/tinkerbell/hegel/internal/backend"
"github.com/tinkerbell/hegel/internal/backend/kubernetes"
)

func TestNew(t *testing.T) {
Expand All @@ -17,8 +18,8 @@ func TestNew(t *testing.T) {
{
Name: "OnlyOneBackend",
Options: Options{
Flatfile: &FlatfileOptions{},
Kubernetes: &KubernetesOptions{},
Flatfile: &Flatfile{},
Kubernetes: &kubernetes.Config{},
},
Error: ErrMultipleBackends,
},
Expand Down
8 changes: 4 additions & 4 deletions internal/backend/kubernetes/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type Backend struct {
// NewBackend creates a new Backend instance. It launches a goroutine to perform synchronization
// between the cluster and internal caches. Consumers can wait for the initial sync using WaitForCachesync().
// See k8s.io/Backend-go/tools/Backendcmd for constructing *rest.Config objects.
func NewBackend(ctx context.Context, cfg BackendConfig) (*Backend, error) {
func NewBackend(ctx context.Context, cfg Config) (*Backend, error) {
// If no client was specified, build one and configure the backend with it including waiting
// for the caches to sync.
if cfg.ClientConfig == nil {
Expand Down Expand Up @@ -83,7 +83,7 @@ func NewBackend(ctx context.Context, cfg BackendConfig) (*Backend, error) {
}, nil
}

func loadConfig(cfg BackendConfig) (BackendConfig, error) {
func loadConfig(cfg Config) (Config, error) {
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
loadingRules.ExplicitPath = cfg.Kubeconfig

Expand All @@ -99,15 +99,15 @@ func loadConfig(cfg BackendConfig) (BackendConfig, error) {
loader := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, overrides)
config, err := loader.ClientConfig()
if err != nil {
return BackendConfig{}, err
return Config{}, err
}
cfg.ClientConfig = config

// In the event no namespace was provided for override, we need to fill it in with whatever
// namespace was loaded from the kubeconfig.
namespace, _, err := loader.Namespace()
if err != nil {
return BackendConfig{}, err
return Config{}, err
}
cfg.Namespace = namespace

Expand Down
2 changes: 1 addition & 1 deletion internal/backend/kubernetes/backend_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func TestBackend(t *testing.T) {
defer cancel()

// Construct the backend and attempt to retrieve our test Hardware resource.
backend, err := NewBackend(ctx, BackendConfig{ClientConfig: cfg})
backend, err := NewBackend(ctx, Config{ClientConfig: cfg})
if err != nil {
t.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/backend/kubernetes/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"k8s.io/client-go/rest"
)

// BackendConfig used by the NewBackend function family.
type BackendConfig struct {
// Config used by the NewBackend function family.
type Config struct {
// Kubeconfig is a path to a valid kubeconfig file. When in-cluster defaults to the in-cluster
// config. Optional.
Kubeconfig string
Expand Down
9 changes: 5 additions & 4 deletions internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/tinkerbell/hegel/internal/backend"
"github.com/tinkerbell/hegel/internal/backend/kubernetes"
"github.com/tinkerbell/hegel/internal/frontend/ec2"
"github.com/tinkerbell/hegel/internal/frontend/hack"
"github.com/tinkerbell/hegel/internal/healthcheck"
Expand All @@ -29,8 +30,8 @@ import (
const longHelp = `
Run a Hegel server.
Each CLI argument has a corresponding environment variable in the form of the CLI argument prefixed
with HEGEL. If both the flag and environment variable form are specified, the flag form takes
Each CLI argument has a corresponding environment variable in the form of the CLI argument prefixed
with HEGEL. If both the flag and environment variable form are specified, the flag form takes
precedence.
Examples
Expand Down Expand Up @@ -196,13 +197,13 @@ func toBackendOptions(opts RootCommandOptions) backend.Options {
switch opts.Backend {
case "flatfile":
backndOpts = backend.Options{
Flatfile: &backend.FlatfileOptions{
Flatfile: &backend.Flatfile{
Path: opts.FlatfilePath,
},
}
case "kubernetes":
backndOpts = backend.Options{
Kubernetes: &backend.KubernetesOptions{
Kubernetes: &kubernetes.Config{
APIServerAddress: opts.KubernetesAPIServer,
Kubeconfig: opts.KubernetesKubeconfig,
Namespace: opts.KubernetesNamespace,
Expand Down

0 comments on commit c6caf59

Please sign in to comment.