Skip to content

Commit

Permalink
better config management
Browse files Browse the repository at this point in the history
  • Loading branch information
yairsimantov20 committed Dec 13, 2023
1 parent 97edf37 commit 56bad41
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 33 deletions.
32 changes: 16 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@ package main
import (
"flag"
"fmt"
"github.com/port-labs/port-k8s-exporter/pkg/goutils"
"github.com/port-labs/port-k8s-exporter/pkg/port"
"os"

"github.com/port-labs/port-k8s-exporter/pkg/config"
"github.com/port-labs/port-k8s-exporter/pkg/event_listener"
"github.com/port-labs/port-k8s-exporter/pkg/handlers"
"github.com/port-labs/port-k8s-exporter/pkg/k8s"
"github.com/port-labs/port-k8s-exporter/pkg/port"
"github.com/port-labs/port-k8s-exporter/pkg/port/cli"
"github.com/port-labs/port-k8s-exporter/pkg/port/integration"
"k8s.io/klog/v2"
Expand Down Expand Up @@ -66,7 +63,7 @@ func main() {
klog.Fatalf("Error building Port client: %s", err.Error())
}

exporterConfig, err := config.New(configFilePath, resyncInterval, stateKey, eventListenerType)
exporterConfig, err := config.GetConfigFile(configFilePath, resyncInterval, stateKey, eventListenerType)
if err != nil {
klog.Fatalf("Error building Port K8s Exporter config: %s", err.Error())
}
Expand All @@ -92,18 +89,21 @@ func main() {
if err != nil {
klog.Fatalf("Error starting event listener: %s", err.Error())
}
klog.Info("Started controllers handler")
}

func init() {
flag.StringVar(&configFilePath, "config", "", "Path to Port K8s Exporter config file. Required.")
flag.StringVar(&stateKey, "state-key", "", "Port K8s Exporter state key id. Required.")
flag.BoolVar(&deleteDependents, "delete-dependents", false, "Flag to enable deletion of dependent Port Entities. Optional.")
flag.BoolVar(&createMissingRelatedEntities, "create-missing-related-entities", false, "Flag to enable creation of missing related Port entities. Optional.")
flag.UintVar(&resyncInterval, "resync-interval", 0, "The re-sync interval in minutes. Optional.")
flag.StringVar(&portBaseURL, "port-base-url", "https://api.getport.io", "Port base URL. Optional.")
portClientId = os.Getenv("PORT_CLIENT_ID")
portClientSecret = os.Getenv("PORT_CLIENT_SECRET")

eventListenerType = goutils.GetEnvOrDefault("EVENT_LISTENER__TYPE", "POLLING")
configFilePath = config.NewString("config", "", "Path to Port K8s Exporter config file. Required.")
stateKey = config.NewString("state-key", "", "Port K8s Exporter state key id. Required.")

// change to the app config
//config.NewBoolean("delete-dependents", false, "Flag to enable deletion of dependent Port Entities. Optional.")
//config.NewBoolean("create-missing-related-entities", false, "Flag to enable creation of missing related Port entities. Optional.")

resyncInterval = config.NewUInt("resync-interval", 0, "The re-sync interval in minutes. Optional.")
portBaseURL = config.NewString("port-base-url", "https://api.getport.io", "Port base URL. Optional.")

portClientId = config.NewString("port-client-id", "", "Port client id. Required.")
portClientSecret = config.NewString("port-client-secret", "", "Port client secret. Required.")

config.NewString("event-listener-type", "POLLING", "Event listener type. Optional.")
}
43 changes: 40 additions & 3 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,50 @@
package config

import (
"flag"
"github.com/port-labs/port-k8s-exporter/pkg/goutils"
"github.com/port-labs/port-k8s-exporter/pkg/port"
"os"

"gopkg.in/yaml.v2"
"k8s.io/klog/v2"
"os"
"slices"

Check failure on line 10 in pkg/config/config.go

View workflow job for this annotation

GitHub Actions / build

package slices is not in GOROOT (/opt/hostedtoolcache/go/1.19.13/x64/src/slices)
"strings"
)

func New(filepath string, resyncInterval uint, stateKey string, eventListenerType string) (*port.Config, error) {
var keys []string

func prepareEnvKey(key string) string {
newKey := strings.ToUpper(strings.ReplaceAll(key, "-", "_"))

if slices.Contains(keys, newKey) {
klog.Fatalf("Application Error : Found duplicate config key: %s", newKey)
}

keys = append(keys, newKey)
return newKey
}

func NewString(key string, defaultValue string, description string) string {
var value string
flag.StringVar(&value, key, "", description)
if value == "" {
value = goutils.GetStringEnvOrDefault(prepareEnvKey(key), defaultValue)
}

return value
}

func NewUInt(key string, defaultValue uint, description string) uint {
var value uint64
flag.Uint64Var(&value, key, 0, description)
if value == 0 {
value = goutils.GetUintEnvOrDefault(prepareEnvKey(key), uint64(defaultValue))
}

return uint(value)
}

func GetConfigFile(filepath string, resyncInterval uint, stateKey string, eventListenerType string) (*port.Config, error) {
c := &port.Config{
ResyncInterval: resyncInterval,
StateKey: stateKey,
Expand Down
17 changes: 9 additions & 8 deletions pkg/event_listener/event_listener_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package event_listener
import (
"encoding/json"
"fmt"
"github.com/port-labs/port-k8s-exporter/pkg/config"
"github.com/port-labs/port-k8s-exporter/pkg/event_listener/consumer"
"github.com/port-labs/port-k8s-exporter/pkg/event_listener/polling"
"github.com/port-labs/port-k8s-exporter/pkg/goutils"
Expand Down Expand Up @@ -61,24 +62,24 @@ func startKafkaEventListener(l *EventListener, resync func()) error {
if err != nil {
return err
}
config := &consumer.KafkaConfiguration{
Brokers: goutils.GetEnvOrDefault("EVENT_LISTENER__BROKERS", "b-1-public.publicclusterprod.t9rw6w.c1.kafka.eu-west-1.amazonaws.com:9196,b-2-public.publicclusterprod.t9rw6w.c1.kafka.eu-west-1.amazonaws.com:9196,b-3-public.publicclusterprod.t9rw6w.c1.kafka.eu-west-1.amazonaws.com:9196"),
SecurityProtocol: goutils.GetEnvOrDefault("EVENT_LISTENER__SECURITY_PROTOCOL", "SASL_SSL"),
AuthenticationMechanism: goutils.GetEnvOrDefault("EVENT_LISTENER__AUTHENTICATION_MECHANISM", "SCRAM-SHA-512"),
KafkaSecurityEnabled: goutils.GetBooleanEnvOrDefault("EVENT_LISTENER__KAFKA_SECURITY_ENABLED", true),

c := &consumer.KafkaConfiguration{
Brokers: config.NewString("event-listener-brokers", "localhost:9092", "Kafka brokers"),
SecurityProtocol: config.NewString("event-listener-security-protocol", "plaintext", "Kafka security protocol"),
AuthenticationMechanism: config.NewString("event-listener-authentication-mechanism", "none", "Kafka authentication mechanism"),
Username: credentials.Username,
Password: credentials.Password,
GroupID: orgId + ".k8s." + l.stateKey,
}

topic := orgId + ".change.log"
instance, err := consumer.NewConsumer(config)
instance, err := consumer.NewConsumer(c)

if err != nil {
return err
}

klog.Infof("Starting consumer for topic %s and groupId %s", topic, config.GroupID)
klog.Infof("Starting consumer for topic %s and groupId %s", topic, c.GroupID)
instance.Consume(topic, func(value []byte) {
incomingMessage := &IncomingMessage{}
parsingError := json.Unmarshal(value, &incomingMessage)
Expand All @@ -95,7 +96,7 @@ func startKafkaEventListener(l *EventListener, resync func()) error {

func startPollingEventListener(l *EventListener, resync func()) {
klog.Infof("Starting polling event listener")
pollingRate := goutils.GetIntEnvOrDefault("EVENT_LISTENER__POLLING_RATE", 60)
pollingRate := goutils.GetUintEnvOrDefault("EVENT_LISTENER__POLLING_RATE", 60)
klog.Infof("Polling rate set to %d seconds", pollingRate)
pollingHandler := polling.NewPollingHandler(pollingRate, l.stateKey, l.portClient)
pollingHandler.Run(resync)
Expand Down
4 changes: 2 additions & 2 deletions pkg/event_listener/polling/polling.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ type PollingHandler struct {
ticker *time.Ticker
stateKey string
portClient *cli.PortClient
pollingRate int
pollingRate uint64
}

func NewPollingHandler(pollingRate int, stateKey string, portClient *cli.PortClient) *PollingHandler {
func NewPollingHandler(pollingRate uint64, stateKey string, portClient *cli.PortClient) *PollingHandler {
rv := &PollingHandler{
ticker: time.NewTicker(time.Second * time.Duration(pollingRate)),
stateKey: stateKey,
Expand Down
8 changes: 4 additions & 4 deletions pkg/goutils/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strconv"
)

func GetEnvOrDefault(key string, defaultValue string) string {
func GetStringEnvOrDefault(key string, defaultValue string) string {
value := os.Getenv(key)
if value == "" {
return defaultValue
Expand All @@ -22,14 +22,14 @@ func GetBooleanEnvOrDefault(key string, defaultValue bool) bool {
return value == "true"
}

func GetIntEnvOrDefault(key string, defaultValue int) int {
func GetUintEnvOrDefault(key string, defaultValue uint64) uint64 {
value := os.Getenv(key)
if value == "" {
return defaultValue
}
result, err := strconv.Atoi(value)
result, err := strconv.ParseUint(value, 10, 32)
if err != nil {
fmt.Printf("Using default value "+strconv.Itoa(defaultValue)+" for "+key+". error parsing env variable %s: %s", key, err.Error())
fmt.Printf("Using default value "+strconv.FormatUint(uint64(defaultValue), 10)+" for "+key+". error parsing env variable %s: %s", key, err.Error())
return defaultValue
}
return result
Expand Down

0 comments on commit 56bad41

Please sign in to comment.