Skip to content

Commit

Permalink
drill down application config
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsinai committed Jun 27, 2024
1 parent 103181b commit 1e9d820
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 76 deletions.
11 changes: 3 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ import (
)

func initiateHandler(exporterConfig *port.Config, k8sClient *k8s.Client) (*handlers.ControllersHandler, error) {
portClient, err := cli.New()
if err != nil {
return nil, fmt.Errorf("error building Port client: %v", err)
}
portClient := cli.New(config.ApplicationConfig)

i, err := integration.GetIntegration(portClient, exporterConfig.StateKey)
if err != nil {
return nil, fmt.Errorf("error getting Port integration: %v", err)
Expand Down Expand Up @@ -51,10 +49,7 @@ func main() {
if err != nil {
klog.Fatalf("Error building K8s client: %s", err.Error())
}
portClient, err := cli.New()
if err != nil {
klog.Fatalf("Error building Port client: %s", err.Error())
}
portClient := cli.New(config.ApplicationConfig)

if err := defaults.InitIntegration(portClient, applicationConfig); err != nil {
klog.Fatalf("Error initializing Port integration: %s", err.Error())
Expand Down
20 changes: 5 additions & 15 deletions pkg/crd/crd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func deleteDefaultResources(portClient *cli.PortClient) {
_ = blueprint.DeleteBlueprint(portClient, "testkind")
}

func newFixture(t *testing.T, portClientId string, portClientSecret string, userAgent string, namespaced bool, crdsDiscoveryPattern string) *Fixture {
func newFixture(t *testing.T, userAgent string, namespaced bool, crdsDiscoveryPattern string) *Fixture {
apiExtensionsFakeClient := fakeapiextensionsv1.FakeApiextensionsV1{Fake: &clienttesting.Fake{}}

apiExtensionsFakeClient.AddReactor("list", "customresourcedefinitions", func(action clienttesting.Action) (handled bool, ret runtime.Object, err error) {
Expand Down Expand Up @@ -100,22 +100,12 @@ func newFixture(t *testing.T, portClientId string, portClientSecret string, user
return true, fakeCrd, nil
})

if portClientId == "" {
portClientId = config.ApplicationConfig.PortClientId
}
if portClientSecret == "" {
portClientSecret = config.ApplicationConfig.PortClientSecret
}
if userAgent == "" {
userAgent = "port-k8s-exporter/0.1"
}

portClient, err := cli.New(cli.WithHeader("User-Agent", userAgent),
cli.WithClientID(portClientId), cli.WithClientSecret(portClientSecret))
portClient := cli.New(config.ApplicationConfig)
deleteDefaultResources(portClient)
if err != nil {
t.Errorf("Error building Port client: %s", err.Error())
}

return &Fixture{
t: t,
Expand Down Expand Up @@ -271,7 +261,7 @@ func checkBlueprintAndActionsProperties(t *testing.T, f *Fixture, namespaced boo
}

func TestCRD_crd_autoDiscoverCRDsToActionsClusterScoped(t *testing.T) {
f := newFixture(t, "", "", "", false, "true")
f := newFixture(t, "", false, "true")

AutodiscoverCRDsToActions(f.portConfig, f.apiextensionClient, f.portClient)

Expand All @@ -281,7 +271,7 @@ func TestCRD_crd_autoDiscoverCRDsToActionsClusterScoped(t *testing.T) {
}

func TestCRD_crd_autoDiscoverCRDsToActionsNamespaced(t *testing.T) {
f := newFixture(t, "", "", "", true, "true")
f := newFixture(t, "", true, "true")

AutodiscoverCRDsToActions(f.portConfig, f.apiextensionClient, f.portClient)

Expand All @@ -291,7 +281,7 @@ func TestCRD_crd_autoDiscoverCRDsToActionsNamespaced(t *testing.T) {
}

func TestCRD_crd_autoDiscoverCRDsToActionsNoCRDs(t *testing.T) {
f := newFixture(t, "", "", "", false, "false")
f := newFixture(t, "", false, "false")

AutodiscoverCRDsToActions(f.portConfig, f.apiextensionClient, f.portClient)

Expand Down
7 changes: 1 addition & 6 deletions pkg/defaults/defaults_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package defaults

import (
"fmt"
"testing"

guuid "github.com/google/uuid"
Expand All @@ -23,11 +22,7 @@ type Fixture struct {

func NewFixture(t *testing.T) *Fixture {
stateKey := guuid.NewString()
portClient, err := cli.New(cli.WithHeader("User-Agent", fmt.Sprintf("port-k8s-exporter/0.1 (statekey/%s)", stateKey)),
cli.WithClientID(config.ApplicationConfig.PortClientId), cli.WithClientSecret(config.ApplicationConfig.PortClientSecret))
if err != nil {
t.Errorf("Error building Port client: %s", err.Error())
}
portClient := cli.New(config.ApplicationConfig)

deleteDefaultResources(portClient, stateKey)
return &Fixture{
Expand Down
6 changes: 2 additions & 4 deletions pkg/event_handler/event_listener_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@ package event_handler
import (
"fmt"

"github.com/port-labs/port-k8s-exporter/pkg/config"
"github.com/port-labs/port-k8s-exporter/pkg/event_handler/consumer"
"github.com/port-labs/port-k8s-exporter/pkg/event_handler/polling"
"github.com/port-labs/port-k8s-exporter/pkg/port/cli"
"k8s.io/klog/v2"
)

func CreateEventListener(stateKey string, eventListenerType string) (IListener, error) {
portClient, err := cli.New()
if err != nil {
return nil, fmt.Errorf("error building Port client: %v", err)
}
portClient := cli.New(config.ApplicationConfig)

klog.Infof("Received event listener type: %s", eventListenerType)
switch eventListenerType {
Expand Down
10 changes: 2 additions & 8 deletions pkg/event_handler/polling/polling_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package polling

import (
"fmt"

_ "github.com/port-labs/port-k8s-exporter/test_utils"

"testing"
Expand Down Expand Up @@ -33,14 +31,10 @@ func (m *MockTicker) GetC() <-chan time.Time {

func NewFixture(t *testing.T, c chan time.Time) *Fixture {
stateKey := guuid.NewString()
portClient, err := cli.New(cli.WithHeader("User-Agent", fmt.Sprintf("port-k8s-exporter/0.1 (statekey/%s)", stateKey)),
cli.WithClientID(config.ApplicationConfig.PortClientId), cli.WithClientSecret(config.ApplicationConfig.PortClientSecret))
if err != nil {
t.Errorf("Error building Port client: %s", err.Error())
}
portClient := cli.New(config.ApplicationConfig)

_ = integration.DeleteIntegration(portClient, stateKey)
err = integration.CreateIntegration(portClient, stateKey, "", &port.IntegrationAppConfig{
err := integration.CreateIntegration(portClient, stateKey, "", &port.IntegrationAppConfig{
Resources: []port.Resource{},
})
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion pkg/handlers/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"time"

"github.com/port-labs/port-k8s-exporter/pkg/config"
"github.com/port-labs/port-k8s-exporter/pkg/port/integration"

"github.com/port-labs/port-k8s-exporter/pkg/crd"
Expand Down Expand Up @@ -52,7 +53,7 @@ func NewControllersHandler(exporterConfig *port.Config, portConfig *port.Integra
}

informer := informersFactory.ForResource(gvr)
controller := k8s.NewController(port.AggregatedResource{Kind: kind, KindConfigs: kindConfigs}, informer, portConfig, nil)
controller := k8s.NewController(port.AggregatedResource{Kind: kind, KindConfigs: kindConfigs}, informer, portConfig, config.ApplicationConfig)
controllers = append(controllers, controller)
}

Expand Down
10 changes: 2 additions & 8 deletions pkg/k8s/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,8 @@ type Controller struct {
workqueue workqueue.RateLimitingInterface
}

func NewController(resource port.AggregatedResource, informer informers.GenericInformer, integrationConfig *port.IntegrationAppConfig, portClient *cli.PortClient) *Controller {
if portClient == nil {
var err error
portClient, err = cli.New()
if err != nil {
klog.Fatalf("Error building Port client: %v", err)
}
}
func NewController(resource port.AggregatedResource, informer informers.GenericInformer, integrationConfig *port.IntegrationAppConfig, applicationConfig *config.ApplicationConfiguration) *Controller {
portClient := cli.New(applicationConfig)

cli.WithDeleteDependents(integrationConfig.DeleteDependents)(portClient)
cli.WithCreateMissingRelatedEntities(integrationConfig.CreateMissingRelatedEntities)(portClient)
Expand Down
28 changes: 13 additions & 15 deletions pkg/k8s/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"time"

"github.com/port-labs/port-k8s-exporter/pkg/jq"
"github.com/port-labs/port-k8s-exporter/pkg/port/cli"
"github.com/stretchr/testify/assert"

"github.com/port-labs/port-k8s-exporter/pkg/config"
Expand Down Expand Up @@ -39,7 +38,7 @@ type fixture struct {
type fixtureConfig struct {
portClientId string
portClientSecret string
userAgent string
stateKey string
sendRawDataExamples *bool
resource port.Resource
objects []runtime.Object
Expand All @@ -66,18 +65,17 @@ func newFixture(t *testing.T, fixtureConfig *fixtureConfig) *fixture {
if fixtureConfig.portClientSecret == "" {
fixtureConfig.portClientSecret = config.ApplicationConfig.PortClientSecret
}
if fixtureConfig.userAgent == "" {
fixtureConfig.userAgent = "port-k8s-exporter/0.1"
}

portClient, err := cli.New(cli.WithClientID(fixtureConfig.portClientId), cli.WithClientSecret(fixtureConfig.portClientSecret), cli.WithHeader("User-Agent", fixtureConfig.userAgent))
if err != nil {
t.Fatalf("Error building Port client: %v", err)
if fixtureConfig.stateKey == "" {
fixtureConfig.stateKey = "port-k8s-exporter/0.1"
}

return &fixture{
t: t,
controller: newController(fixtureConfig.resource, fixtureConfig.objects, kubeclient, interationConfig, portClient),
t: t,
controller: newController(fixtureConfig.resource, fixtureConfig.objects, kubeclient, interationConfig, &config.ApplicationConfiguration{
StateKey: fixtureConfig.stateKey,
PortClientId: fixtureConfig.portClientId,
PortClientSecret: fixtureConfig.portClientSecret,
}),
}
}

Expand Down Expand Up @@ -167,13 +165,13 @@ func newUnstructured(obj interface{}) *unstructured.Unstructured {
return &unstructured.Unstructured{Object: res}
}

func newController(resource port.Resource, objects []runtime.Object, kubeclient *k8sfake.FakeDynamicClient, integrationConfig *port.IntegrationAppConfig, portClient *cli.PortClient) *Controller {
func newController(resource port.Resource, objects []runtime.Object, kubeclient *k8sfake.FakeDynamicClient, integrationConfig *port.IntegrationAppConfig, applicationConfig *config.ApplicationConfiguration) *Controller {
k8sI := dynamicinformer.NewDynamicSharedInformerFactory(kubeclient, noResyncPeriodFunc())
s := strings.SplitN(resource.Kind, "/", 3)
gvr := schema.GroupVersionResource{Group: s[0], Version: s[1], Resource: s[2]}
informer := k8sI.ForResource(gvr)
kindConfig := port.KindConfig{Selector: resource.Selector, Port: resource.Port}
c := NewController(port.AggregatedResource{Kind: resource.Kind, KindConfigs: []port.KindConfig{kindConfig}}, informer, integrationConfig, portClient)
c := NewController(port.AggregatedResource{Kind: resource.Kind, KindConfigs: []port.KindConfig{kindConfig}}, informer, integrationConfig, applicationConfig)

for _, d := range objects {
informer.Informer().GetIndexer().Add(d)
Expand Down Expand Up @@ -365,7 +363,7 @@ func TestDeleteDeploymentSameOwner(t *testing.T) {
createItem := EventItem{Key: getKey(d, t), ActionType: CreateAction}
item := EventItem{Key: getKey(d, t), ActionType: DeleteAction}

f := newFixture(t, &fixtureConfig{userAgent: fmt.Sprintf("port-k8s-exporter/0.1 (statekey/%s)", config.ApplicationConfig.StateKey), resource: resource, objects: objects})
f := newFixture(t, &fixtureConfig{stateKey: config.ApplicationConfig.StateKey, resource: resource, objects: objects})
f.runControllerSyncHandler(createItem, false)

f.runControllerSyncHandler(item, false)
Expand All @@ -387,7 +385,7 @@ func TestDeleteDeploymentDifferentOwner(t *testing.T) {
createItem := EventItem{Key: getKey(d, t), ActionType: CreateAction}
item := EventItem{Key: getKey(d, t), ActionType: DeleteAction}

f := newFixture(t, &fixtureConfig{userAgent: fmt.Sprintf("statekey/%s", "non_exist_statekey") + "port-k8s-exporter", resource: resource, objects: objects})
f := newFixture(t, &fixtureConfig{stateKey: "non_exist_statekey", resource: resource, objects: objects})
f.runControllerSyncHandler(createItem, false)

f.runControllerSyncHandler(item, false)
Expand Down
16 changes: 5 additions & 11 deletions pkg/port/cli/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,10 @@ type (
}
)

func New(opts ...Option) (*PortClient, error) {
applicationConfig, err := config.NewConfiguration()

if err != nil {
return nil, err
}

func New(applicationConfig *config.ApplicationConfiguration, opts ...Option) *PortClient {
c := &PortClient{
Client: resty.New().
SetBaseURL(config.ApplicationConfig.PortBaseURL).
SetBaseURL(applicationConfig.PortBaseURL).
SetRetryCount(5).
SetRetryWaitTime(300).
// retry when create permission fails because scopes are created async-ly and sometimes (mainly in tests) the scope doesn't exist yet.
Expand All @@ -48,15 +42,15 @@ func New(opts ...Option) (*PortClient, error) {
}),
}

WithClientID(config.ApplicationConfig.PortClientId)(c)
WithClientSecret(config.ApplicationConfig.PortClientSecret)(c)
WithClientID(applicationConfig.PortClientId)(c)
WithClientSecret(applicationConfig.PortClientSecret)(c)
WithHeader("User-Agent", fmt.Sprintf("port-k8s-exporter/^0.3.4 (statekey/%s)", applicationConfig.StateKey))(c)

for _, opt := range opts {
opt(c)
}

return c, nil
return c
}

func (c *PortClient) Authenticate(ctx context.Context, clientID, clientSecret string) (string, error) {
Expand Down

0 comments on commit 1e9d820

Please sign in to comment.