diff --git a/main.go b/main.go index a99962c..65e2abd 100644 --- a/main.go +++ b/main.go @@ -73,12 +73,14 @@ func main() { _, err = integration.GetIntegrationConfig(portClient, stateKey) if err != nil { - err = integration.NewIntegration(portClient, stateKey, exporterConfig) + if exporterConfig == nil { + klog.Fatalf("The integration does not exist and no config file was provided") + } + err = integration.NewIntegration(portClient, exporterConfig, exporterConfig.Resources) if err != nil { klog.Fatalf("Error creating K8s integration: %s", err.Error()) } } - cli.WithHeader("User-Agent", fmt.Sprintf("port-k8s-exporter/0.1 (statekey/%s)", exporterConfig.StateKey))(portClient) klog.Info("Starting controllers handler") handler, _ := InitiateHandler(exporterConfig, k8sClient, portClient) diff --git a/pkg/event_listener/polling/polling.go b/pkg/event_listener/polling/polling.go index cb32ad8..348e43f 100644 --- a/pkg/event_listener/polling/polling.go +++ b/pkg/event_listener/polling/polling.go @@ -12,16 +12,18 @@ import ( ) type PollingHandler struct { - ticker *time.Ticker - stateKey string - portClient *cli.PortClient + ticker *time.Ticker + stateKey string + portClient *cli.PortClient + pollingRate int } func NewPollingHandler(pollingRate int, stateKey string, portClient *cli.PortClient) *PollingHandler { rv := &PollingHandler{ - ticker: time.NewTicker(time.Second * time.Duration(pollingRate)), - stateKey: stateKey, - portClient: portClient, + ticker: time.NewTicker(time.Second * time.Duration(pollingRate)), + stateKey: stateKey, + portClient: portClient, + pollingRate: pollingRate, } return rv } @@ -44,7 +46,7 @@ func (h *PollingHandler) Run(resync func()) { klog.Infof("Received signal %v: terminating\n", sig) run = false case <-h.ticker.C: - klog.Infof("Polling event listener iteration after %d seconds. Checking for changes...", h.ticker.C) + klog.Infof("Polling event listener iteration after %d seconds. Checking for changes...", h.pollingRate) configuration, err := integration.GetIntegrationConfig(h.portClient, h.stateKey) if err != nil { klog.Errorf("error resyncing: %s", err.Error()) diff --git a/pkg/jq/parser.go b/pkg/jq/parser.go index a6ca0ee..8a02b2c 100644 --- a/pkg/jq/parser.go +++ b/pkg/jq/parser.go @@ -3,6 +3,8 @@ package jq import ( "fmt" "github.com/itchyny/gojq" + "k8s.io/klog/v2" + "os" "strings" "sync" ) @@ -12,11 +14,22 @@ var mutex = &sync.Mutex{} func runJQQuery(jqQuery string, obj interface{}) (interface{}, error) { query, err := gojq.Parse(jqQuery) if err != nil { + klog.Warningf("failed to parse jq query: %s", jqQuery) + return nil, err + } + code, err := gojq.Compile( + query, + gojq.WithEnvironLoader(func() []string { + return os.Environ() + }), + ) + if err != nil { + klog.Warningf("failed to compile jq query: %s", jqQuery) return nil, err } mutex.Lock() - queryRes, ok := query.Run(obj).Next() + queryRes, ok := code.Run(obj).Next() mutex.Unlock() if !ok { diff --git a/pkg/port/integration/integration.go b/pkg/port/integration/integration.go index 3ccc8e7..0c39090 100644 --- a/pkg/port/integration/integration.go +++ b/pkg/port/integration/integration.go @@ -7,17 +7,16 @@ import ( "github.com/port-labs/port-k8s-exporter/pkg/port/cli" ) -func NewIntegration(portClient *cli.PortClient, stateKey string, exporterConfig *port.Config) error { - +func NewIntegration(portClient *cli.PortClient, exporterConfig *port.Config, resources []port.Resource) error { integration := &port.Integration{ - Title: stateKey, - InstallationAppType: "kubernetes", - InstallationId: stateKey, + Title: exporterConfig.StateKey, + InstallationAppType: "K8S EXPORTER", + InstallationId: exporterConfig.StateKey, EventListener: port.EventListenerSettings{ Type: exporterConfig.EventListenerType, }, Config: &port.AppConfig{ - Resources: exporterConfig.Resources, + Resources: resources, }, } _, err := portClient.Authenticate(context.Background(), portClient.ClientID, portClient.ClientSecret) diff --git a/pkg/port/models.go b/pkg/port/models.go index e88adab..baec613 100644 --- a/pkg/port/models.go +++ b/pkg/port/models.go @@ -40,7 +40,7 @@ type ( Type string `json:"type,omitempty"` Title string `json:"title,omitempty"` Identifier string `json:"identifier,omitempty"` - Default string `json:"default,omitempty"` + Default any `json:"default,omitempty"` Icon string `json:"icon,omitempty"` Format string `json:"format,omitempty"` Description string `json:"description,omitempty"` @@ -87,9 +87,9 @@ type ( Description string `json:"description"` Schema BlueprintSchema `json:"schema"` FormulaProperties map[string]BlueprintFormulaProperty `json:"formulaProperties"` - MirrorProperties map[string]BlueprintMirrorProperty `json:"mirrorProperties"` + MirrorProperties map[string]BlueprintMirrorProperty `json:"mirrorProperties,omitempty"` ChangelogDestination *ChangelogDestination `json:"changelogDestination,omitempty"` - Relations map[string]Relation `json:"relations"` + Relations map[string]Relation `json:"relations,omitempty"` } Action struct { @@ -147,7 +147,7 @@ type EntityMapping struct { Identifier string `json:"identifier"` Title string `json:"title"` Blueprint string `json:"blueprint"` - Team string `json:"team"` + Team string `json:"team,omitempty"` Properties map[string]string `json:"properties,omitempty"` Relations map[string]string `json:"relations,omitempty"` }