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
…ngeration
  • Loading branch information
dvirsegev committed Aug 16, 2023
1 parent 175181c commit e543f61
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 25 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
18 changes: 16 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ 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/signal"
"k8s.io/klog/v2"
"os"
)

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

k8sClient, err := k8s.NewClient()
k8sConfig := k8s.NewKubeConfig()
if err != nil {
klog.Fatalf("Error building K8s config: %s", err.Error())
}

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 +55,13 @@ 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())
}

k8s.NewIntegration(portClient, k8sConfig, stateKey)

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)
}
35 changes: 35 additions & 0 deletions pkg/k8s/integration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package k8s

import (
"context"

"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"
"k8s.io/klog/v2"
)

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

k8sRawConfig, err := k8sConfig.RawConfig()
if err != nil {
klog.Fatalf("Error getting K8s raw config: %s", err.Error())
}

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 {
klog.Errorf("error authenticating with Port: %v", err)
}

_, err = portClient.CreateIntegration(integration)
if err != nil {
klog.Fatalf("Error creating Port integration: %s", err.Error())
}
}
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
}
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 e543f61

Please sign in to comment.