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

fix: config defaults and safety #60

Merged
merged 1 commit into from
Jan 14, 2025
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
31 changes: 25 additions & 6 deletions pkg/chassis/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ type (
GetSizeInBytes(key string) uint
Unmarshal(rawVal interface{}, opts ...viper.DecoderConfigOption) error
UnmarshalKey(key string, rawVal interface{}, opts ...viper.DecoderConfigOption) error
SetDefault(key string, value any)
}
Writer interface {
Set(key string, value any)
Expand All @@ -61,6 +60,12 @@ var configSingleton *config

// TODO -> Read config from the key/value store and not from a local static file.
func LoadConfig() Config {

// don't overwrite a previously loaded config
if configSingleton != nil {
return configSingleton
}

v := viper.New()
setDefaults(v)
v.SetEnvPrefix("DRAFT")
Expand All @@ -83,11 +88,11 @@ func LoadConfig() Config {
}

func setDefaults(v *viper.Viper) {
viper.SetDefault("service.network.port", 8090)
viper.SetDefault("service.network.bind_address", "0.0.0.0")
viper.SetDefault("service.network.advertise_address", "127.0.0.1")
viper.SetDefault("service.env", "local")
viper.SetDefault("service.logging.level", "info")
v.SetDefault("service.network.port", 8090)
v.SetDefault("service.network.bind_address", "0.0.0.0")
v.SetDefault("service.network.advertise_address", "127.0.0.1")
v.SetDefault("service.env", "local")
v.SetDefault("service.logging.level", "info")
}

func (c *config) Name() string {
Expand Down Expand Up @@ -121,7 +126,21 @@ func GetConfig() Config {
return configSingleton
}

func (c *config) Set(key string, value any) {
c.mu.Lock()
defer c.mu.Unlock()
c.Viper.Set(key, value)
}

func (c *config) SetDefault(key string, value any) {
c.mu.Lock()
defer c.mu.Unlock()
c.Viper.SetDefault(key, value)
}

func (c *config) SetAndWrite(key string, value any) error {
c.mu.Lock()
defer c.mu.Unlock()
c.Set(key, value)
return c.WriteConfig()
}
Expand Down
17 changes: 9 additions & 8 deletions pkg/chassis/networking.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,50 +25,51 @@ func (c *Runtime) withRoute(route *ntv1.Route) error {
)

route = c.setAndValidateRoute(route)
logger := c.logger.WithField("route_name", route.Name)

val, err := anypb.New(&kvv1.Value{})
if err != nil {
c.logger.WithError(err).Error("failed to create anypb value struct")
logger.WithError(err).Error("failed to create anypb value struct")
return err
}

// get fuse address from blueprint
response, err := kvv1Connect.NewKeyValueServiceClient(http.DefaultClient, c.config.Entrypoint()).
Get(ctx, connect.NewRequest(&kvv1.GetRequest{
Key: FuseAddressBlueprintKey,
Key: FuseAddressBlueprintKey,
Value: val,
}))
if err != nil {
c.logger.WithError(err).Error("failed to get fuse address")
logger.WithError(err).Error("failed to get fuse address")
return err
}

// unmarshal value
value := &kvv1.Value{}
if err := anypb.UnmarshalTo(response.Msg.GetValue(), value, proto.UnmarshalOptions{}); err != nil {
c.logger.WithError(err).Error("failed to unmarshal fuse address")
logger.WithError(err).Error("failed to unmarshal fuse address")
return err
}

// add route to fuse
res, err := ntv1Connect.NewNetworkingServiceClient(http.DefaultClient, value.Data).
_, err = ntv1Connect.NewNetworkingServiceClient(http.DefaultClient, value.Data).
AddRoute(ctx, connect.NewRequest(&ntv1.AddRouteRequest{
Route: route,
}))
if err != nil {
c.logger.WithError(err).Error("failed to add route")
logger.WithError(err).Error("failed to add route")
return err
}

c.logger.WithField("response", res).Info("successfully added route")
logger.Info("successfully added route")

return nil
}

// setAndValidateRoute will set defaults on the Route for anything not specified by the user and
// validates the route is valid
// TODO: we should do validation through protobuf annotations instead
func (c *Runtime) setAndValidateRoute(route *ntv1.Route) (*ntv1.Route) {
func (c *Runtime) setAndValidateRoute(route *ntv1.Route) *ntv1.Route {
if route.Name == "" {
route.Name = fmt.Sprintf("%s-%s", c.config.Domain(), c.config.Name())
}
Expand Down
Loading