Skip to content

Commit

Permalink
When k8s exporter is up, start upsert request to port to update the i… (
Browse files Browse the repository at this point in the history
  • Loading branch information
dvirsegev authored Aug 16, 2023
1 parent 175181c commit ada6c9b
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 26 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

.idea

.vscode

__debug_bin

config.yaml

deployments/k8s
19 changes: 17 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package main
import (
"flag"
"fmt"
"os"

"github.com/port-labs/port-k8s-exporter/pkg/config"
"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/cli"
"github.com/port-labs/port-k8s-exporter/pkg/port/integration"
"github.com/port-labs/port-k8s-exporter/pkg/signal"
"k8s.io/klog/v2"
"os"
)

var (
Expand All @@ -34,7 +36,14 @@ func main() {
klog.Fatalf("Error building Port K8s Exporter config: %s", err.Error())
}

k8sClient, err := k8s.NewClient()
k8sConfig := k8s.NewKubeConfig()

clientConfig, err := k8sConfig.ClientConfig()
if err != nil {
klog.Fatalf("Error getting K8s client config: %s", err.Error())
}

k8sClient, err := k8s.NewClient(clientConfig)
if err != nil {
klog.Fatalf("Error building K8s client: %s", err.Error())
}
Expand All @@ -44,10 +53,16 @@ func main() {
cli.WithClientID(portClientId), cli.WithClientSecret(portClientSecret),
cli.WithDeleteDependents(deleteDependents), cli.WithCreateMissingRelatedEntities(createMissingRelatedEntities),
)

if err != nil {
klog.Fatalf("Error building Port client: %s", err.Error())
}

err = integration.NewIntegration(portClient, k8sConfig, stateKey)
if err != nil {
klog.Fatalf("Error creating K8s integration: %s", err.Error())
}

klog.Info("Starting controllers handler")
controllersHandler := handlers.NewControllersHandler(exporterConfig, k8sClient, portClient)
controllersHandler.Handle(stopCh)
Expand Down
21 changes: 3 additions & 18 deletions pkg/k8s/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"k8s.io/client-go/dynamic"
"k8s.io/client-go/rest"
"k8s.io/client-go/restmapper"
"k8s.io/client-go/tools/clientcmd"
)

type Client struct {
Expand All @@ -15,28 +14,14 @@ type Client struct {
DiscoveryMapper *restmapper.DeferredDiscoveryRESTMapper
}

func getKubeConfig() (*rest.Config, error) {
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
configOverrides := &clientcmd.ConfigOverrides{}
config, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, configOverrides).ClientConfig()
if err != nil {
return nil, err
}
return config, nil
}

func NewClient() (*Client, error) {
kubeConfig, err := getKubeConfig()
if err != nil {
return nil, err
}
func NewClient(config *rest.Config) (*Client, error) {

discoveryClient, err := discovery.NewDiscoveryClientForConfig(kubeConfig)
discoveryClient, err := discovery.NewDiscoveryClientForConfig(config)
if err != nil {
return nil, err
}

dynamicClient, err := dynamic.NewForConfig(kubeConfig)
dynamicClient, err := dynamic.NewForConfig(config)
if err != nil {
return nil, err
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/k8s/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package k8s

import "k8s.io/client-go/tools/clientcmd"

func NewKubeConfig() clientcmd.ClientConfig {
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
configOverrides := &clientcmd.ConfigOverrides{}
return clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, configOverrides)
}
2 changes: 1 addition & 1 deletion pkg/port/cli/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package cli
import (
"context"
"encoding/json"
"github.com/port-labs/port-k8s-exporter/pkg/port"
"strings"

"github.com/go-resty/resty/v2"
"github.com/port-labs/port-k8s-exporter/pkg/port"
)

type (
Expand Down
23 changes: 23 additions & 0 deletions pkg/port/cli/integration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package cli

import (
"fmt"

"github.com/port-labs/port-k8s-exporter/pkg/port"
)

func (c *PortClient) CreateIntegration(i *port.Integration) (*port.Integration, error) {
pb := &port.ResponseBody{}
resp, err := c.Client.R().
SetBody(i).
SetResult(&pb).
SetQueryParam("upsert", "true").
Post("v1/integration")
if err != nil {
return nil, err
}
if !pb.OK {
return nil, fmt.Errorf("failed to create integration, got: %s", resp.Body())
}
return &pb.Integration, nil
}
36 changes: 36 additions & 0 deletions pkg/port/integration/integration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package integration

import (
"context"
"fmt"

"github.com/port-labs/port-k8s-exporter/pkg/port"
"github.com/port-labs/port-k8s-exporter/pkg/port/cli"
"k8s.io/client-go/tools/clientcmd"
)

func NewIntegration(portClient *cli.PortClient, k8sConfig clientcmd.ClientConfig, stateKey string) error {

k8sRawConfig, err := k8sConfig.RawConfig()
if err != nil {
return err
}

clusterName := k8sRawConfig.Contexts[k8sRawConfig.CurrentContext].Cluster

integration := &port.Integration{
Title: clusterName,
InstallationAppType: "kubernetes",
InstallationId: stateKey,
}
_, err = portClient.Authenticate(context.Background(), portClient.ClientID, portClient.ClientSecret)
if err != nil {
return fmt.Errorf("error authenticating with Port: %v", err)
}

_, err = portClient.CreateIntegration(integration)
if err != nil {
return fmt.Errorf("error creating Port integration: %v", err)
}
return nil
}
18 changes: 13 additions & 5 deletions pkg/port/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ type (
Relations map[string]interface{} `json:"relations"`
}

Integration struct {
InstallationId string `json:"installationId"`
Title string `json:"title,omitempty"`
Version string `json:"version,omitempty"`
InstallationAppType string `json:"installationAppType,omitempty"`
}

BlueprintProperty struct {
Type string `json:"type,omitempty"`
Title string `json:"title,omitempty"`
Expand Down Expand Up @@ -115,11 +122,12 @@ type SearchBody struct {
}

type ResponseBody struct {
OK bool `json:"ok"`
Entity Entity `json:"entity"`
Blueprint Blueprint `json:"blueprint"`
Action Action `json:"action"`
Entities []Entity `json:"entities"`
OK bool `json:"ok"`
Entity Entity `json:"entity"`
Blueprint Blueprint `json:"blueprint"`
Action Action `json:"action"`
Entities []Entity `json:"entities"`
Integration Integration `json:"integration"`
}

type EntityMapping struct {
Expand Down

0 comments on commit ada6c9b

Please sign in to comment.