Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Harmonize rest config setup for brokers #1176

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions broker/bucketbroker/client/config/getter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and IronCore contributors
// SPDX-License-Identifier: Apache-2.0

package config

import (
"os"

"github.com/ironcore-dev/ironcore/utils/client/config"
"k8s.io/apiserver/pkg/server/egressselector"
ctrl "sigs.k8s.io/controller-runtime"
)

var log = ctrl.Log.WithName("client").WithName("config")

func NewGetter() (*config.BrokerGetter, error) {
return config.NewBrokerGetter(config.GetterOptions{
Name: "bucketbroker",
NetworkContext: egressselector.ControlPlane.AsNetworkContext(),
})
}

func NewGetterOrDie() *config.BrokerGetter {
getter, err := NewGetter()
if err != nil {
log.Error(err, "Error creating getter")
os.Exit(1)
}
return getter
}
19 changes: 10 additions & 9 deletions broker/bucketbroker/cmd/bucketbroker/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ import (
"github.com/spf13/pflag"
"google.golang.org/grpc"

bucketbrokerconfig "github.com/ironcore-dev/ironcore/broker/bucketbroker/client/config"
"github.com/ironcore-dev/ironcore/broker/bucketbroker/server"
"github.com/ironcore-dev/ironcore/broker/common"
iri "github.com/ironcore-dev/ironcore/iri/apis/bucket/v1alpha1"
"github.com/ironcore-dev/ironcore/utils/client/config"

"github.com/ironcore-dev/controller-utils/configutils"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
)

type Options struct {
Kubeconfig string
Address string
GetConfigOptions config.GetConfigOptions
Address string

QPS float32
Burst int
Expand All @@ -36,7 +36,7 @@ type Options struct {
}

func (o *Options) AddFlags(fs *pflag.FlagSet) {
fs.StringVar(&o.Kubeconfig, "kubeconfig", o.Kubeconfig, "Path pointing to a kubeconfig file to use.")
o.GetConfigOptions.BindFlags(fs)
fs.StringVar(&o.Address, "address", "/var/run/iri-bucketbroker.sock", "Address to listen on.")

fs.Float32Var(&o.QPS, "qps", config.QPS, "Kubernetes client qps.")
Expand Down Expand Up @@ -78,14 +78,15 @@ func Run(ctx context.Context, opts Options) error {
log := ctrl.LoggerFrom(ctx)
setupLog := log.WithName("setup")

cfg, err := configutils.GetConfig(configutils.Kubeconfig(opts.Kubeconfig))
getter, err := bucketbrokerconfig.NewGetter()
if err != nil {
return err
return fmt.Errorf("error creating new getter: %w", err)
}

cfg.QPS = opts.QPS
cfg.Burst = opts.Burst
setupLog.Info("Kubernetes Client configuration", "QPS", cfg.QPS, "Burst", cfg.Burst)
cfg, err := getter.GetConfig(ctx, &opts.GetConfigOptions)
if err != nil {
return fmt.Errorf("error getting config: %w", err)
}

srv, err := server.New(cfg, server.Options{
Namespace: opts.Namespace,
Expand Down
30 changes: 30 additions & 0 deletions broker/machinebroker/client/config/getter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and IronCore contributors
// SPDX-License-Identifier: Apache-2.0

package config

import (
"os"

"github.com/ironcore-dev/ironcore/utils/client/config"
"k8s.io/apiserver/pkg/server/egressselector"
ctrl "sigs.k8s.io/controller-runtime"
)

var log = ctrl.Log.WithName("client").WithName("config")

func NewGetter() (*config.BrokerGetter, error) {
return config.NewBrokerGetter(config.GetterOptions{
Name: "machinebroker",
NetworkContext: egressselector.ControlPlane.AsNetworkContext(),
})
}

func NewGetterOrDie() *config.BrokerGetter {
getter, err := NewGetter()
if err != nil {
log.Error(err, "Error creating getter")
os.Exit(1)
}
return getter
}
19 changes: 10 additions & 9 deletions broker/machinebroker/cmd/machinebroker/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@ import (

"github.com/ironcore-dev/ironcore/broker/common"
commongrpc "github.com/ironcore-dev/ironcore/broker/common/grpc"
mchinebrokerconfig "github.com/ironcore-dev/ironcore/broker/machinebroker/client/config"
machinebrokerhttp "github.com/ironcore-dev/ironcore/broker/machinebroker/http"
"github.com/ironcore-dev/ironcore/broker/machinebroker/server"
iri "github.com/ironcore-dev/ironcore/iri/apis/machine/v1alpha1"
"github.com/ironcore-dev/ironcore/utils/client/config"

"github.com/ironcore-dev/controller-utils/configutils"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
)

type Options struct {
Kubeconfig string
GetConfigOptions config.GetConfigOptions
Address string
StreamingAddress string
BaseURL string
Expand All @@ -46,7 +46,7 @@ type Options struct {
}

func (o *Options) AddFlags(fs *pflag.FlagSet) {
fs.StringVar(&o.Kubeconfig, "kubeconfig", o.Kubeconfig, "Path pointing to a kubeconfig file to use.")
o.GetConfigOptions.BindFlags(fs)
fs.StringVar(&o.Address, "address", "/var/run/iri-machinebroker.sock", "Address to listen on.")
fs.StringVar(&o.StreamingAddress, "streaming-address", "127.0.0.1:20251", "Address to run the streaming server on")
fs.StringVar(&o.BaseURL, "base-url", "", "The base url to construct urls for streaming from. If empty it will be "+
Expand Down Expand Up @@ -93,9 +93,14 @@ func Run(ctx context.Context, opts Options) error {
log := ctrl.LoggerFrom(ctx)
setupLog := log.WithName("setup")

cfg, err := configutils.GetConfig(configutils.Kubeconfig(opts.Kubeconfig))
getter, err := mchinebrokerconfig.NewGetter()
if err != nil {
return err
return fmt.Errorf("error creating new getter: %w", err)
}

cfg, err := getter.GetConfig(ctx, &opts.GetConfigOptions)
if err != nil {
return fmt.Errorf("error getting config: %w", err)
}

if opts.Namespace == "" {
Expand All @@ -111,10 +116,6 @@ func Run(ctx context.Context, opts Options) error {
baseURL = u.String()
}

cfg.QPS = opts.QPS
cfg.Burst = opts.Burst
setupLog.Info("Kubernetes Client configuration", "QPS", cfg.QPS, "Burst", cfg.Burst)

log.V(1).Info("Creating server",
"Namespace", opts.Namespace,
"MachinePoolName", opts.MachinePoolName,
Expand Down
30 changes: 30 additions & 0 deletions broker/volumebroker/client/config/getter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and IronCore contributors
// SPDX-License-Identifier: Apache-2.0

package config

import (
"os"

"github.com/ironcore-dev/ironcore/utils/client/config"
"k8s.io/apiserver/pkg/server/egressselector"
ctrl "sigs.k8s.io/controller-runtime"
)

var log = ctrl.Log.WithName("client").WithName("config")

func NewGetter() (*config.BrokerGetter, error) {
return config.NewBrokerGetter(config.GetterOptions{
Name: "volumebroker",
NetworkContext: egressselector.ControlPlane.AsNetworkContext(),
})
}

func NewGetterOrDie() *config.BrokerGetter {
getter, err := NewGetter()
if err != nil {
log.Error(err, "Error creating getter")
os.Exit(1)
}
return getter
}
19 changes: 10 additions & 9 deletions broker/volumebroker/cmd/volumebroker/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ import (
"github.com/spf13/pflag"
"google.golang.org/grpc"

volumebrokerconfig "github.com/ironcore-dev/ironcore/broker/bucketbroker/client/config"
"github.com/ironcore-dev/ironcore/broker/common"
"github.com/ironcore-dev/ironcore/broker/volumebroker/server"
iri "github.com/ironcore-dev/ironcore/iri/apis/volume/v1alpha1"
"github.com/ironcore-dev/ironcore/utils/client/config"

"github.com/ironcore-dev/controller-utils/configutils"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
)

type Options struct {
Kubeconfig string
Address string
GetConfigOptions config.GetConfigOptions
Address string

QPS float32
Burst int
Expand All @@ -36,7 +36,7 @@ type Options struct {
}

func (o *Options) AddFlags(fs *pflag.FlagSet) {
fs.StringVar(&o.Kubeconfig, "kubeconfig", o.Kubeconfig, "Path pointing to a kubeconfig file to use.")
o.GetConfigOptions.BindFlags(fs)
fs.StringVar(&o.Address, "address", "/var/run/iri-volumebroker.sock", "Address to listen on.")

fs.Float32Var(&o.QPS, "qps", config.QPS, "Kubernetes client qps.")
Expand Down Expand Up @@ -78,14 +78,15 @@ func Run(ctx context.Context, opts Options) error {
log := ctrl.LoggerFrom(ctx)
setupLog := log.WithName("setup")

cfg, err := configutils.GetConfig(configutils.Kubeconfig(opts.Kubeconfig))
getter, err := volumebrokerconfig.NewGetter()
if err != nil {
return err
return fmt.Errorf("error creating new getter: %w", err)
}

cfg.QPS = opts.QPS
cfg.Burst = opts.Burst
setupLog.Info("Kubernetes Client configuration", "QPS", cfg.QPS, "Burst", cfg.Burst)
cfg, err := getter.GetConfig(ctx, &opts.GetConfigOptions)
if err != nil {
return fmt.Errorf("error getting config: %w", err)
}

srv, err := server.New(cfg, server.Options{
Namespace: opts.Namespace,
Expand Down
43 changes: 36 additions & 7 deletions utils/client/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,20 @@ func NewGetterOrDie(opts GetterOptions) *Getter {
return getter
}

type BrokerGetter struct {
name string
logConstructor func() logr.Logger
networkContext egressselector.NetworkContext
}

func NewBrokerGetter(opts GetterOptions) (*BrokerGetter, error) {
setGetterOptionsDefaults(&opts)
return &BrokerGetter{
name: opts.Name,
logConstructor: opts.LogConstructor,
}, nil
}

func StoreFromOptions(o *GetConfigOptions) (Store, error) {
switch {
case o.Kubeconfig != "" && o.KubeconfigSecretName != "":
Expand Down Expand Up @@ -251,14 +265,28 @@ func (g *Getter) GetConfig(ctx context.Context, opts ...GetConfigOption) (*rest.
return g.getAndBootstrapConfigIfNecessary(ctx, o)
default:
g.logConstructor().Info("Getting config")
return g.getConfig(ctx, o)
restConfig, err := getConfig(ctx, o, g.networkContext)
return restConfig, nil, err
}
}

func (bg *BrokerGetter) GetConfig(ctx context.Context, opts ...GetConfigOption) (*rest.Config, error) {
o := &GetConfigOptions{}
o.ApplyOptions(opts)

if o.Kubeconfig != "" && o.KubeconfigSecretName != "" {
return nil, fmt.Errorf("cannot specify kubeconfig and kubeconfig-secret-name")
}

bg.logConstructor().Info("Getting config")
return getConfig(ctx, o, bg.networkContext)

}

func (g *Getter) getConfig(ctx context.Context, o *GetConfigOptions) (*rest.Config, Controller, error) {
func getConfig(ctx context.Context, o *GetConfigOptions, networkContext egressselector.NetworkContext) (*rest.Config, error) {
loader, err := LoaderFromOptions(o)
if err != nil {
return nil, nil, fmt.Errorf("error getting loader: %w", err)
return nil, fmt.Errorf("error getting loader: %w", err)
}

var cfg *rest.Config
Expand All @@ -270,18 +298,19 @@ func (g *Getter) getConfig(ctx context.Context, o *GetConfigOptions) (*rest.Conf
cfg, err = LoadDefaultConfig(o.Context)
}
if err != nil {
return nil, nil, err
return nil, err
}

dialFunc, err := GetEgressSelectorDial(g.networkContext, o.EgressSelectorConfig)
dialFunc, err := GetEgressSelectorDial(networkContext, o.EgressSelectorConfig)
if err != nil {
return nil, nil, err
return nil, err
}

cfg.Dial = dialFunc
cfg.QPS = o.QPS
cfg.Burst = o.Burst
return cfg, nil, nil

return cfg, nil
}

func (g *Getter) getAndBootstrapConfigIfNecessary(ctx context.Context, o *GetConfigOptions) (*rest.Config, Controller, error) {
Expand Down
Loading