From 1e9d820bb595cf42ca399057893398ed18663179 Mon Sep 17 00:00:00 2001 From: danielsinai Date: Thu, 27 Jun 2024 15:24:52 +0300 Subject: [PATCH] drill down application config --- main.go | 11 +++----- pkg/crd/crd_test.go | 20 ++++----------- pkg/defaults/defaults_test.go | 7 +----- pkg/event_handler/event_listener_factory.go | 6 ++--- pkg/event_handler/polling/polling_test.go | 10 ++------ pkg/handlers/controllers.go | 3 ++- pkg/k8s/controller.go | 10 ++------ pkg/k8s/controller_test.go | 28 ++++++++++----------- pkg/port/cli/client.go | 16 ++++-------- 9 files changed, 35 insertions(+), 76 deletions(-) diff --git a/main.go b/main.go index a218b28..e7716be 100644 --- a/main.go +++ b/main.go @@ -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) @@ -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()) diff --git a/pkg/crd/crd_test.go b/pkg/crd/crd_test.go index 2ae056d..7c63150 100644 --- a/pkg/crd/crd_test.go +++ b/pkg/crd/crd_test.go @@ -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) { @@ -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, @@ -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) @@ -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) @@ -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) diff --git a/pkg/defaults/defaults_test.go b/pkg/defaults/defaults_test.go index af7e44d..94cfb92 100644 --- a/pkg/defaults/defaults_test.go +++ b/pkg/defaults/defaults_test.go @@ -1,7 +1,6 @@ package defaults import ( - "fmt" "testing" guuid "github.com/google/uuid" @@ -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{ diff --git a/pkg/event_handler/event_listener_factory.go b/pkg/event_handler/event_listener_factory.go index b902c3e..10e63e7 100644 --- a/pkg/event_handler/event_listener_factory.go +++ b/pkg/event_handler/event_listener_factory.go @@ -3,6 +3,7 @@ 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" @@ -10,10 +11,7 @@ import ( ) 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 { diff --git a/pkg/event_handler/polling/polling_test.go b/pkg/event_handler/polling/polling_test.go index a0d4562..7f0fdf6 100644 --- a/pkg/event_handler/polling/polling_test.go +++ b/pkg/event_handler/polling/polling_test.go @@ -1,8 +1,6 @@ package polling import ( - "fmt" - _ "github.com/port-labs/port-k8s-exporter/test_utils" "testing" @@ -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 { diff --git a/pkg/handlers/controllers.go b/pkg/handlers/controllers.go index 1889b7a..f7ef18a 100644 --- a/pkg/handlers/controllers.go +++ b/pkg/handlers/controllers.go @@ -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" @@ -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) } diff --git a/pkg/k8s/controller.go b/pkg/k8s/controller.go index 3606b3c..1362bba 100644 --- a/pkg/k8s/controller.go +++ b/pkg/k8s/controller.go @@ -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) diff --git a/pkg/k8s/controller_test.go b/pkg/k8s/controller_test.go index eb667dc..967610b 100644 --- a/pkg/k8s/controller_test.go +++ b/pkg/k8s/controller_test.go @@ -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" @@ -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 @@ -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, + }), } } @@ -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) @@ -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) @@ -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) diff --git a/pkg/port/cli/client.go b/pkg/port/cli/client.go index 1d642d3..2e2e578 100644 --- a/pkg/port/cli/client.go +++ b/pkg/port/cli/client.go @@ -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. @@ -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) {