Skip to content

Commit

Permalink
feat: Add support for file-based env vars (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
hofmeister authored Apr 9, 2024
1 parent 174bf10 commit 3c610d2
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 42 deletions.
36 changes: 36 additions & 0 deletions config/configfile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2023 Kapeta Inc.
// SPDX-License-Identifier: MIT

package config

import (
"encoding/json"
"os"
)

// ReadConfigFile reads the environment configuration file and returns the map
func ReadConfigFile() map[string]string {
out := make(map[string]string)
kapetaConfigPath := os.Getenv("KAPETA_CONFIG_PATH")

if kapetaConfigPath == "" {
return out
}

// Open the JSON file
file, err := os.Open(kapetaConfigPath)
if err != nil {
panic(err)
}
defer file.Close()

decoder := json.NewDecoder(file)

err = decoder.Decode(&out)

if err != nil {
panic(err)
}

return out
}
12 changes: 6 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ module github.com/kapetacom/sdk-go-config

go 1.21.6

require (
github.com/kapetacom/schemas/packages/go v0.0.0-20240209083259-f5ce079d8abc
github.com/stretchr/testify v1.8.4
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/kapetacom/schemas/packages/go v0.0.0-20240209083259-f5ce079d8abc // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.8.4 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
10 changes: 1 addition & 9 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/kapetacom/schemas/packages/go v0.0.0-20240209083259-f5ce079d8abc h1:ghhXNScFqGXUP7uywvPF/dyYBcS7czmirlLcgtc0cEg=
github.com/kapetacom/schemas/packages/go v0.0.0-20240209083259-f5ce079d8abc/go.mod h1:dWvKSUqSQRHiqFFnGPnJofgci1dvRT1PPNJLtffVukk=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f h1:J9EGpcZtP0E/raorCMxlFGSTBrsSlaDGf3jU/qvAE2c=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHovont7NscjpAxXsDA8S8BMYve8Y5+7cuRE7R0=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17UxZ74=
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
23 changes: 18 additions & 5 deletions providers/abstract.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

package providers

import "github.com/kapetacom/schemas/packages/go/model"
import (
"github.com/kapetacom/schemas/packages/go/model"
"os"
)

type ConfigProvider interface {
GetBlockDefinition() interface{}
Expand Down Expand Up @@ -76,10 +79,11 @@ type ResourceInfo struct {
}

type AbstractConfigProvider struct {
BlockRef string `json:"blockRef"`
SystemID string `json:"systemId"`
InstanceID string `json:"instanceId"`
BlockDefinition map[string]interface{} `json:"blockDefinition"`
BlockRef string `json:"blockRef"`
SystemID string `json:"systemId"`
InstanceID string `json:"instanceId"`
BlockDefinition map[string]interface{} `json:"blockDefinition"`
EnvironmentConfiguration map[string]string `json:"environmentConfiguration"`
}

func (a *AbstractConfigProvider) GetBlockDefinition() interface{} {
Expand All @@ -102,3 +106,12 @@ func (a *AbstractConfigProvider) SetIdentity(systemID, instanceID string) {
a.SystemID = systemID
a.InstanceID = instanceID
}

func (a *AbstractConfigProvider) LookupEnv(name string) (string, bool) {
if value, exists := os.LookupEnv(name); exists {
return value, true
}

value := a.EnvironmentConfiguration[name]
return value, value != ""
}
30 changes: 16 additions & 14 deletions providers/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"encoding/json"
"errors"
"fmt"
"os"
cfg "github.com/kapetacom/sdk-go-config/config"
"strings"
)

Expand All @@ -33,12 +33,14 @@ type KubernetesConfigProvider struct {

// NewKubernetesConfigProvider creates a new instance of KubernetesConfigProvider
func NewKubernetesConfigProvider(blockRef, systemID, instanceID string, blockDefinition map[string]interface{}) ConfigProvider {
envConfig := cfg.ReadConfigFile()
return &KubernetesConfigProvider{
AbstractConfigProvider: AbstractConfigProvider{
BlockRef: blockRef,
SystemID: systemID,
InstanceID: instanceID,
BlockDefinition: blockDefinition,
BlockRef: blockRef,
SystemID: systemID,
InstanceID: instanceID,
BlockDefinition: blockDefinition,
EnvironmentConfiguration: envConfig,
},
configuration: nil,
}
Expand All @@ -51,7 +53,7 @@ func (k *KubernetesConfigProvider) GetServerPort(portType string) (string, error
}

envVar := fmt.Sprintf("KAPETA_PROVIDER_PORT_%s", toEnvName(portType))
if value, exists := os.LookupEnv(envVar); exists {
if value, exists := k.LookupEnv(envVar); exists {
return value, nil
}

Expand All @@ -61,7 +63,7 @@ func (k *KubernetesConfigProvider) GetServerPort(portType string) (string, error
// GetServerHost returns the host for the current process
func (k *KubernetesConfigProvider) GetServerHost() (string, error) {
envVar := "KAPETA_PROVIDER_HOST"
if value, exists := os.LookupEnv(envVar); exists {
if value, exists := k.LookupEnv(envVar); exists {
return value, nil
}

Expand All @@ -72,7 +74,7 @@ func (k *KubernetesConfigProvider) GetServerHost() (string, error) {
// GetServiceAddress returns the service address for the given resource name and port type
func (k *KubernetesConfigProvider) GetServiceAddress(resourceName, portType string) (string, error) {
envVar := fmt.Sprintf("KAPETA_CONSUMER_SERVICE_%s_%s", toEnvName(resourceName), toEnvName(portType))
if value, exists := os.LookupEnv(envVar); exists {
if value, exists := k.LookupEnv(envVar); exists {
return value, nil
}

Expand All @@ -82,7 +84,7 @@ func (k *KubernetesConfigProvider) GetServiceAddress(resourceName, portType stri
// GetResourceInfo returns the resource info for the given resource type, port type, and resource name
func (k *KubernetesConfigProvider) GetResourceInfo(resourceType, portType, resourceName string) (*ResourceInfo, error) {
envVar := fmt.Sprintf("KAPETA_CONSUMER_RESOURCE_%s_%s", toEnvName(resourceName), toEnvName(portType))
if value, exists := os.LookupEnv(envVar); exists {
if value, exists := k.LookupEnv(envVar); exists {
var resourceInfo ResourceInfo
err := json.Unmarshal([]byte(value), &resourceInfo)
if err != nil {
Expand All @@ -103,7 +105,7 @@ func (k *KubernetesConfigProvider) GetProviderId() string {
func (k *KubernetesConfigProvider) getConfiguration(path string, defaultValue interface{}) interface{} {
if k.configuration == nil {
envVar := "KAPETA_INSTANCE_CONFIG"
if value, exists := os.LookupEnv(envVar); exists {
if value, exists := k.LookupEnv(envVar); exists {
err := json.Unmarshal([]byte(value), &k.configuration)
if err != nil {
panic(fmt.Sprintf("Invalid JSON in environment variable: %s", envVar))
Expand Down Expand Up @@ -139,7 +141,7 @@ func (k *KubernetesConfigProvider) GetOrDefault(path string, defaultValue interf
// GetInstanceHost returns the hostname for the given instance ID
func (k *KubernetesConfigProvider) GetInstanceHost(instanceID string) (string, error) {
if k.instanceHosts == nil {
if blockHosts, exists := os.LookupEnv("KAPETA_BLOCK_HOSTS"); exists {
if blockHosts, exists := k.LookupEnv("KAPETA_BLOCK_HOSTS"); exists {
err := json.Unmarshal([]byte(blockHosts), &k.instanceHosts)
if err != nil {
panic("Invalid JSON in environment variable: KAPETA_BLOCK_HOSTS")
Expand All @@ -158,7 +160,7 @@ func (k *KubernetesConfigProvider) GetInstanceHost(instanceID string) (string, e

func (k *KubernetesConfigProvider) GetInstanceForConsumer(resourceName string) (*BlockInstanceDetails, error) {
envVar := fmt.Sprintf("KAPETA_INSTANCE_FOR_CONSUMER_%s", toEnvName(resourceName))
if value, exists := os.LookupEnv(envVar); exists {
if value, exists := k.LookupEnv(envVar); exists {
blockDetails := &BlockInstanceDetails{}
err := json.Unmarshal([]byte(value), blockDetails)
if err != nil {
Expand All @@ -172,7 +174,7 @@ func (k *KubernetesConfigProvider) GetInstanceForConsumer(resourceName string) (

func (k *KubernetesConfigProvider) GetInstanceOperator(instanceId string) (*InstanceOperator, error) {
envVar := fmt.Sprintf("KAPETA_INSTANCE_OPERATOR_%s", toEnvName(instanceId))
if value, exists := os.LookupEnv(envVar); exists {
if value, exists := k.LookupEnv(envVar); exists {
instanceOperator := &InstanceOperator{}
err := json.Unmarshal([]byte(value), instanceOperator)
if err != nil {
Expand All @@ -186,7 +188,7 @@ func (k *KubernetesConfigProvider) GetInstanceOperator(instanceId string) (*Inst

func (k *KubernetesConfigProvider) GetInstancesForProvider(resourceName string) ([]*BlockInstanceDetails, error) {
envVar := fmt.Sprintf("KAPETA_INSTANCES_FOR_PROVIDER_%s", toEnvName(resourceName))
if value, exists := os.LookupEnv(envVar); exists {
if value, exists := k.LookupEnv(envVar); exists {
instanceOperators := make([]*BlockInstanceDetails, 0)
err := json.Unmarshal([]byte(value), &instanceOperators)
if err != nil {
Expand Down
19 changes: 11 additions & 8 deletions providers/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,15 @@ type LocalConfigProvider struct {

// NewLocalConfigProvider creates an instance of LocalConfigProvider
func NewLocalConfigProvider(blockRef, systemID, instanceID string, blockDefinition map[string]interface{}) *LocalConfigProvider {
envConfig := cfg.ReadConfigFile()

localProvider := &LocalConfigProvider{
AbstractConfigProvider: AbstractConfigProvider{
BlockRef: blockRef,
SystemID: systemID,
InstanceID: instanceID,
BlockDefinition: blockDefinition,
BlockRef: blockRef,
SystemID: systemID,
InstanceID: instanceID,
BlockDefinition: blockDefinition,
EnvironmentConfiguration: envConfig,
},
configuration: make(map[string]interface{}),
cfg: cfg.NewClusterConfig(),
Expand Down Expand Up @@ -122,7 +125,7 @@ func (l *LocalConfigProvider) GetServerPort(portType string) (string, error) {
}

envVar := fmt.Sprintf("KAPETA_LOCAL_SERVER_PORT_%s", strings.ToUpper(portType))
if port, ok := os.LookupEnv(envVar); ok {
if port, ok := l.LookupEnv(envVar); ok {
return port, nil
}

Expand All @@ -135,16 +138,16 @@ func (l *LocalConfigProvider) GetServerPort(portType string) (string, error) {
return port, nil
}

func getEnvWithDefault(envVar, defaultValue string) string {
if value, ok := os.LookupEnv(envVar); ok {
func (l *LocalConfigProvider) getEnvWithDefault(envVar, defaultValue string) string {
if value, ok := l.LookupEnv(envVar); ok {
return value
}
return defaultValue
}

// GetServerHost gets the server host for the current instance
func (l *LocalConfigProvider) GetServerHost() (string, error) {
return getEnvWithDefault("KAPETA_LOCAL_SERVER", "127.0.0.1"), nil
return l.getEnvWithDefault("KAPETA_LOCAL_SERVER", "127.0.0.1"), nil
}

// RegisterInstanceWithLocalClusterService registers the instance with the cluster service
Expand Down

0 comments on commit 3c610d2

Please sign in to comment.