From c09367d1085f9e17439e24e2a2661a33d7546b86 Mon Sep 17 00:00:00 2001 From: danielsinai Date: Mon, 27 May 2024 11:19:16 +0300 Subject: [PATCH 1/3] feat: added a logic to handle live changes to the crd --- pkg/crd/controller.go | 62 +++++++++++++++++++++++++++++++++++++ pkg/crd/crd.go | 27 +++++++++------- pkg/handlers/controllers.go | 6 +++- 3 files changed, 82 insertions(+), 13 deletions(-) create mode 100644 pkg/crd/controller.go diff --git a/pkg/crd/controller.go b/pkg/crd/controller.go new file mode 100644 index 0000000..718ba18 --- /dev/null +++ b/pkg/crd/controller.go @@ -0,0 +1,62 @@ +package crd + +import ( + "github.com/port-labs/port-k8s-exporter/pkg/port" + "github.com/port-labs/port-k8s-exporter/pkg/port/cli" + apiextensions "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/typed/apiextensions/v1" + "k8s.io/client-go/informers" + + "k8s.io/client-go/tools/cache" + "k8s.io/client-go/util/workqueue" +) + +type EventActionType string + +const ( + CreateAction EventActionType = "create" + UpdateAction EventActionType = "update" + DeleteAction EventActionType = "delete" + MaxNumRequeues int = 4 +) + +type EventItem struct { + Key string + ActionType EventActionType +} + +type Controller struct { + portClient *cli.PortClient + informer cache.SharedIndexInformer + workqueue workqueue.RateLimitingInterface +} + +func NewCrdController(portClient *cli.PortClient, informer informers.GenericInformer, portConfig *port.IntegrationAppConfig, apiExtensionsClient apiextensions.ApiextensionsV1Interface) *Controller { + controller := &Controller{ + portClient: portClient, + informer: informer.Informer(), + workqueue: workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter()), + } + + controller.informer.AddEventHandler(cache.ResourceEventHandlerFuncs{ + AddFunc: func(obj interface{}) { + var err error + var item EventItem + item.ActionType = CreateAction + item.Key, err = cache.MetaNamespaceKeyFunc(obj) + if err == nil { + AutoDiscoverSingleCRDToAction(portConfig, apiExtensionsClient, portClient, item.Key) + } + }, + UpdateFunc: func(old interface{}, new interface{}) { + var err error + var item EventItem + item.ActionType = UpdateAction + item.Key, err = cache.MetaNamespaceKeyFunc(new) + if err == nil { + AutoDiscoverSingleCRDToAction(portConfig, apiExtensionsClient, portClient, item.Key) + } + }, + }) + + return controller +} diff --git a/pkg/crd/crd.go b/pkg/crd/crd.go index 44a5d68..d591284 100644 --- a/pkg/crd/crd.go +++ b/pkg/crd/crd.go @@ -348,13 +348,9 @@ func handleCRD(crds []v1.CustomResourceDefinition, portConfig *port.IntegrationA _, err = cli.CreateAction(portClient, act) if err != nil { if strings.Contains(err.Error(), "taken") { - if portConfig.OverwriteCRDsActions { - _, err = cli.UpdateAction(portClient, act) - if err != nil { - klog.Errorf("Error updating blueprint action: %s", err.Error()) - } - } else { - klog.Infof("Action already exists, if you wish to overwrite it, delete it first or provide the configuration overwriteCrdsActions: true, in the exporter configuration and resync") + _, err = cli.UpdateAction(portClient, act) + if err != nil { + klog.Errorf("Error updating blueprint action: %s", err.Error()) } } else { klog.Errorf("Error creating blueprint action: %s", err.Error()) @@ -365,11 +361,6 @@ func handleCRD(crds []v1.CustomResourceDefinition, portConfig *port.IntegrationA } func AutodiscoverCRDsToActions(portConfig *port.IntegrationAppConfig, apiExtensionsClient apiextensions.ApiextensionsV1Interface, portClient *cli.PortClient) { - if portConfig.CRDSToDiscover == "" { - klog.Info("Discovering CRDs is disabled") - return - } - klog.Infof("Discovering CRDs/XRDs with pattern: %s", portConfig.CRDSToDiscover) crds, err := apiExtensionsClient.CustomResourceDefinitions().List(context.Background(), metav1.ListOptions{}) @@ -380,3 +371,15 @@ func AutodiscoverCRDsToActions(portConfig *port.IntegrationAppConfig, apiExtensi handleCRD(crds.Items, portConfig, portClient) } + +func AutoDiscoverSingleCRDToAction(portConfig *port.IntegrationAppConfig, apiExtensionsClient apiextensions.ApiextensionsV1Interface, portClient *cli.PortClient, crdName string) { + klog.Infof("Trying to export CRD/XRD: %s", crdName) + crd, err := apiExtensionsClient.CustomResourceDefinitions().Get(context.Background(), crdName, metav1.GetOptions{}) + + if err != nil { + klog.Errorf("Error getting CRD: %s", err.Error()) + return + } + + handleCRD([]v1.CustomResourceDefinition{*crd}, portConfig, portClient) +} diff --git a/pkg/handlers/controllers.go b/pkg/handlers/controllers.go index ec28fa8..6c5cd7e 100644 --- a/pkg/handlers/controllers.go +++ b/pkg/handlers/controllers.go @@ -27,7 +27,11 @@ func NewControllersHandler(exporterConfig *port.Config, portConfig *port.Integra resync := time.Minute * time.Duration(exporterConfig.ResyncInterval) informersFactory := dynamicinformer.NewDynamicSharedInformerFactory(k8sClient.DynamicClient, resync) - crd.AutodiscoverCRDsToActions(portConfig, k8sClient.ApiExtensionClient, portClient) + if portConfig.CRDSToDiscover != "" { + crd.AutodiscoverCRDsToActions(portConfig, k8sClient.ApiExtensionClient, portClient) + crdInformer := informersFactory.ForResource(schema.GroupVersionResource{Group: "apiextensions.k8s.io", Version: "v1", Resource: "customresourcedefinitions"}) + crd.NewCrdController(portClient, crdInformer, portConfig, k8sClient.ApiExtensionClient) + } aggResources := make(map[string][]port.KindConfig) for _, resource := range portConfig.Resources { From 978761eab20ec9bb4697e2a1d48a8f4c4ec34c9d Mon Sep 17 00:00:00 2001 From: danielsinai Date: Tue, 28 May 2024 00:54:15 +0300 Subject: [PATCH 2/3] chhore: generate crd --- config/crd/getport.io_crdsyncers.yaml | 99 +++++++++++++++++++++++++++ hack/gen-types.sh | 18 +++++ pkg/api/v1alpha1/crd_syncer_types.go | 56 +++++++++++++++ pkg/crd/controller.go | 62 ----------------- pkg/crd_syncer/controllers.go | 85 +++++++++++++++++++++++ pkg/{crd => crd_syncer}/crd.go | 14 +--- pkg/{crd => crd_syncer}/crd_test.go | 8 +-- pkg/{crd => crd_syncer}/utils.go | 2 +- pkg/{crd => crd_syncer}/utils_test.go | 2 +- pkg/handlers/controllers.go | 6 +- 10 files changed, 267 insertions(+), 85 deletions(-) create mode 100644 config/crd/getport.io_crdsyncers.yaml create mode 100755 hack/gen-types.sh create mode 100644 pkg/api/v1alpha1/crd_syncer_types.go delete mode 100644 pkg/crd/controller.go create mode 100644 pkg/crd_syncer/controllers.go rename pkg/{crd => crd_syncer}/crd.go (96%) rename pkg/{crd => crd_syncer}/crd_test.go (97%) rename pkg/{crd => crd_syncer}/utils.go (99%) rename pkg/{crd => crd_syncer}/utils_test.go (99%) diff --git a/config/crd/getport.io_crdsyncers.yaml b/config/crd/getport.io_crdsyncers.yaml new file mode 100644 index 0000000..9023c55 --- /dev/null +++ b/config/crd/getport.io_crdsyncers.yaml @@ -0,0 +1,99 @@ +--- +apiVersion: apiextensions.k8s.io/v1 +kind: CustomResourceDefinition +metadata: + annotations: + controller-gen.kubebuilder.io/version: v0.15.0 + name: crdsyncers.getport.io +spec: + group: getport.io + names: + kind: CrdSyncer + listKind: CrdSyncerList + plural: crdsyncers + singular: crdsyncer + scope: Namespaced + versions: + - name: v1 + schema: + openAPIV3Schema: + properties: + apiVersion: + description: |- + APIVersion defines the versioned schema of this representation of an object. + Servers should convert recognized schemas to the latest internal value, and + may reject unrecognized values. + More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources + type: string + kind: + description: |- + Kind is a string value representing the REST resource this object represents. + Servers may infer this from the endpoint the client submits requests to. + Cannot be updated. + In CamelCase. + More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds + type: string + metadata: + type: object + spec: + properties: + discover: + items: + properties: + automation: + properties: + invocationMethod: + properties: + org: + type: string + repo: + type: string + reportWorkflowStatus: + type: boolean + type: + type: string + workflow: + type: string + required: + - org + - repo + - reportWorkflowStatus + - type + - workflow + type: object + required: + - invocationMethod + type: object + filter: + type: string + propertiesAllowJQPattern: + type: string + propertiesAllowList: + items: + type: string + type: array + propertiesDenyJQPattern: + type: string + propertiesDenyList: + items: + type: string + type: array + required: + - filter + type: object + type: array + required: + - discover + type: object + status: + properties: + conditions: + items: + type: string + type: array + required: + - conditions + type: object + type: object + served: true + storage: true diff --git a/hack/gen-types.sh b/hack/gen-types.sh new file mode 100755 index 0000000..56d07de --- /dev/null +++ b/hack/gen-types.sh @@ -0,0 +1,18 @@ +#!/bin/bash + +# This script generates the CRDs from the types of the k8s exporter + +echo 'Installing required tools' + +go install sigs.k8s.io/controller-tools/cmd/controller-gen@latest + +echo 'Generating types...' + +controller-gen paths="./pkg/api/..." crd + +if [ $? -ne 0 ]; then + echo 'Error generating types' + exit 1 +else + echo 'Types generated successfully' +fi \ No newline at end of file diff --git a/pkg/api/v1alpha1/crd_syncer_types.go b/pkg/api/v1alpha1/crd_syncer_types.go new file mode 100644 index 0000000..62b516f --- /dev/null +++ b/pkg/api/v1alpha1/crd_syncer_types.go @@ -0,0 +1,56 @@ +// +groupName=getport.io +package v1 + +import ( + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + + "k8s.io/apimachinery/pkg/runtime/schema" +) + +var ( + GroupVersion = schema.GroupVersion{Group: "getport.io", Version: "v1alpha1"} +) + +type InvocationMethod struct { + Type string `json:"type"` + Org string `json:"org"` + Repo string `json:"repo"` + Workflow string `json:"workflow"` + ReportWorkflowStatus bool `json:"reportWorkflowStatus"` +} + +type Automation struct { + InvocationMethod InvocationMethod `json:"invocationMethod"` +} + +type Discover struct { + Filter string `json:"filter"` + PropertiesDenyList []string `json:"propertiesDenyList,omitempty"` + PropertiesAllowList []string `json:"propertiesAllowList,omitempty"` + PropertiesAllowJQPattern string `json:"propertiesAllowJQPattern,omitempty"` + PropertiesDenyJQPattern string `json:"propertiesDenyJQPattern,omitempty"` + Automation Automation `json:"automation,omitempty"` +} + +type CrdSyncerSpec struct { + Discover []Discover `json:"discover"` +} + +type Status struct { + Conditions []string `json:"conditions"` +} + +type CrdSyncer struct { + metav1.TypeMeta `json:",inline"` + metav1.ObjectMeta `json:"metadata,omitempty"` + + Spec CrdSyncerSpec `json:"spec,omitempty"` + Status Status `json:"status,omitempty"` +} + +type CrdSyncerList struct { + metav1.TypeMeta `json:",inline"` + metav1.ListMeta `json:"metadata,omitempty"` + + Items []CrdSyncer `json:"items"` +} diff --git a/pkg/crd/controller.go b/pkg/crd/controller.go deleted file mode 100644 index 718ba18..0000000 --- a/pkg/crd/controller.go +++ /dev/null @@ -1,62 +0,0 @@ -package crd - -import ( - "github.com/port-labs/port-k8s-exporter/pkg/port" - "github.com/port-labs/port-k8s-exporter/pkg/port/cli" - apiextensions "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/typed/apiextensions/v1" - "k8s.io/client-go/informers" - - "k8s.io/client-go/tools/cache" - "k8s.io/client-go/util/workqueue" -) - -type EventActionType string - -const ( - CreateAction EventActionType = "create" - UpdateAction EventActionType = "update" - DeleteAction EventActionType = "delete" - MaxNumRequeues int = 4 -) - -type EventItem struct { - Key string - ActionType EventActionType -} - -type Controller struct { - portClient *cli.PortClient - informer cache.SharedIndexInformer - workqueue workqueue.RateLimitingInterface -} - -func NewCrdController(portClient *cli.PortClient, informer informers.GenericInformer, portConfig *port.IntegrationAppConfig, apiExtensionsClient apiextensions.ApiextensionsV1Interface) *Controller { - controller := &Controller{ - portClient: portClient, - informer: informer.Informer(), - workqueue: workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter()), - } - - controller.informer.AddEventHandler(cache.ResourceEventHandlerFuncs{ - AddFunc: func(obj interface{}) { - var err error - var item EventItem - item.ActionType = CreateAction - item.Key, err = cache.MetaNamespaceKeyFunc(obj) - if err == nil { - AutoDiscoverSingleCRDToAction(portConfig, apiExtensionsClient, portClient, item.Key) - } - }, - UpdateFunc: func(old interface{}, new interface{}) { - var err error - var item EventItem - item.ActionType = UpdateAction - item.Key, err = cache.MetaNamespaceKeyFunc(new) - if err == nil { - AutoDiscoverSingleCRDToAction(portConfig, apiExtensionsClient, portClient, item.Key) - } - }, - }) - - return controller -} diff --git a/pkg/crd_syncer/controllers.go b/pkg/crd_syncer/controllers.go new file mode 100644 index 0000000..7a73eb7 --- /dev/null +++ b/pkg/crd_syncer/controllers.go @@ -0,0 +1,85 @@ +package crdsyncer + +import ( + "github.com/port-labs/port-k8s-exporter/pkg/port" + "github.com/port-labs/port-k8s-exporter/pkg/port/cli" + apiextensions "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/typed/apiextensions/v1" + "k8s.io/apimachinery/pkg/runtime/schema" + "k8s.io/client-go/dynamic/dynamicinformer" + + "k8s.io/client-go/tools/cache" + "k8s.io/client-go/util/workqueue" +) + +type EventActionType string + +const ( + CreateAction EventActionType = "create" + UpdateAction EventActionType = "update" + DeleteAction EventActionType = "delete" + MaxNumRequeues int = 4 +) + +type EventItem struct { + Key string + ActionType EventActionType +} + +type Controller struct { + portClient *cli.PortClient + informer cache.SharedIndexInformer + workqueue workqueue.RateLimitingInterface +} + +func InitalizeCRDSyncerControllers(portClient *cli.PortClient, portConfig *port.IntegrationAppConfig, apiExtensionsClient apiextensions.ApiextensionsV1Interface, informersFactory dynamicinformer.DynamicSharedInformerFactory) { + crdInformer := informersFactory.ForResource(schema.GroupVersionResource{Group: "apiextensions.k8s.io", Version: "v1", Resource: "customresourcedefinitions"}) + syncerConfigurationInformer := informersFactory.ForResource(schema.GroupVersionResource{Group: "getport.io", Version: "v1", Resource: "crdsyncers"}) + + crdController := &Controller{ + portClient: portClient, + informer: crdInformer.Informer(), + workqueue: workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter()), + } + + crdController.informer.AddEventHandler(cache.ResourceEventHandlerFuncs{ + AddFunc: func(obj interface{}) { + var err error + var item EventItem + item.ActionType = CreateAction + item.Key, err = cache.MetaNamespaceKeyFunc(obj) + if err == nil { + AutoDiscoverSingleCRDToAction(portConfig, apiExtensionsClient, portClient, item.Key) + } + }, + UpdateFunc: func(old interface{}, new interface{}) { + var err error + var item EventItem + item.ActionType = UpdateAction + item.Key, err = cache.MetaNamespaceKeyFunc(new) + if err == nil { + AutoDiscoverSingleCRDToAction(portConfig, apiExtensionsClient, portClient, item.Key) + } + }, + }) + + syncerConfigurationInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{ + AddFunc: func(obj interface{}) { + var err error + var item EventItem + item.ActionType = CreateAction + item.Key, err = cache.MetaNamespaceKeyFunc(obj) + if err == nil { + AutoDiscoverSingleCRDToAction(portConfig, apiExtensionsClient, portClient, item.Key) + } + }, + UpdateFunc: func(old interface{}, new interface{}) { + var err error + var item EventItem + item.ActionType = UpdateAction + item.Key, err = cache.MetaNamespaceKeyFunc(new) + if err == nil { + AutoDiscoverSingleCRDToAction(portConfig, apiExtensionsClient, portClient, item.Key) + } + }, + }) +} diff --git a/pkg/crd/crd.go b/pkg/crd_syncer/crd.go similarity index 96% rename from pkg/crd/crd.go rename to pkg/crd_syncer/crd.go index d591284..a14d4ad 100644 --- a/pkg/crd/crd.go +++ b/pkg/crd_syncer/crd.go @@ -1,4 +1,4 @@ -package crd +package crdsyncer import ( "context" @@ -360,18 +360,6 @@ func handleCRD(crds []v1.CustomResourceDefinition, portConfig *port.IntegrationA } } -func AutodiscoverCRDsToActions(portConfig *port.IntegrationAppConfig, apiExtensionsClient apiextensions.ApiextensionsV1Interface, portClient *cli.PortClient) { - klog.Infof("Discovering CRDs/XRDs with pattern: %s", portConfig.CRDSToDiscover) - crds, err := apiExtensionsClient.CustomResourceDefinitions().List(context.Background(), metav1.ListOptions{}) - - if err != nil { - klog.Errorf("Error listing CRDs: %s", err.Error()) - return - } - - handleCRD(crds.Items, portConfig, portClient) -} - func AutoDiscoverSingleCRDToAction(portConfig *port.IntegrationAppConfig, apiExtensionsClient apiextensions.ApiextensionsV1Interface, portClient *cli.PortClient, crdName string) { klog.Infof("Trying to export CRD/XRD: %s", crdName) crd, err := apiExtensionsClient.CustomResourceDefinitions().Get(context.Background(), crdName, metav1.GetOptions{}) diff --git a/pkg/crd/crd_test.go b/pkg/crd_syncer/crd_test.go similarity index 97% rename from pkg/crd/crd_test.go rename to pkg/crd_syncer/crd_test.go index 93c1c42..6b3f0d6 100644 --- a/pkg/crd/crd_test.go +++ b/pkg/crd_syncer/crd_test.go @@ -1,4 +1,4 @@ -package crd +package crdsyncer import ( "slices" @@ -273,7 +273,7 @@ func checkBlueprintAndActionsProperties(t *testing.T, f *Fixture, namespaced boo func TestCRD_crd_autoDiscoverCRDsToActionsClusterScoped(t *testing.T) { f := newFixture(t, "", "", "", false, "true") - AutodiscoverCRDsToActions(f.portConfig, f.apiextensionClient, f.portClient) + AutoDiscoverSingleCRDToAction(f.portConfig, f.apiextensionClient, f.portClient, "testkind") checkBlueprintAndActionsProperties(t, f, false) @@ -283,7 +283,7 @@ func TestCRD_crd_autoDiscoverCRDsToActionsClusterScoped(t *testing.T) { func TestCRD_crd_autoDiscoverCRDsToActionsNamespaced(t *testing.T) { f := newFixture(t, "", "", "", true, "true") - AutodiscoverCRDsToActions(f.portConfig, f.apiextensionClient, f.portClient) + AutoDiscoverSingleCRDToAction(f.portConfig, f.apiextensionClient, f.portClient, "testkind") checkBlueprintAndActionsProperties(t, f, true) @@ -293,7 +293,7 @@ func TestCRD_crd_autoDiscoverCRDsToActionsNamespaced(t *testing.T) { func TestCRD_crd_autoDiscoverCRDsToActionsNoCRDs(t *testing.T) { f := newFixture(t, "", "", "", false, "false") - AutodiscoverCRDsToActions(f.portConfig, f.apiextensionClient, f.portClient) + AutoDiscoverSingleCRDToAction(f.portConfig, f.apiextensionClient, f.portClient, "testkind") testUtils.CheckResourcesExistence(false, f.portClient, t, []string{"testkind"}, []string{}, []string{"create_testkind", "update_testkind", "delete_testkind"}) } diff --git a/pkg/crd/utils.go b/pkg/crd_syncer/utils.go similarity index 99% rename from pkg/crd/utils.go rename to pkg/crd_syncer/utils.go index dee1a48..92fa53b 100644 --- a/pkg/crd/utils.go +++ b/pkg/crd_syncer/utils.go @@ -1,4 +1,4 @@ -package crd +package crdsyncer import ( v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" diff --git a/pkg/crd/utils_test.go b/pkg/crd_syncer/utils_test.go similarity index 99% rename from pkg/crd/utils_test.go rename to pkg/crd_syncer/utils_test.go index 32323f0..6f2136d 100644 --- a/pkg/crd/utils_test.go +++ b/pkg/crd_syncer/utils_test.go @@ -1,4 +1,4 @@ -package crd +package crdsyncer import ( "reflect" diff --git a/pkg/handlers/controllers.go b/pkg/handlers/controllers.go index 6c5cd7e..fec3511 100644 --- a/pkg/handlers/controllers.go +++ b/pkg/handlers/controllers.go @@ -4,7 +4,7 @@ import ( "context" "time" - "github.com/port-labs/port-k8s-exporter/pkg/crd" + crdsyncer "github.com/port-labs/port-k8s-exporter/pkg/crd_syncer" "github.com/port-labs/port-k8s-exporter/pkg/goutils" "github.com/port-labs/port-k8s-exporter/pkg/k8s" "github.com/port-labs/port-k8s-exporter/pkg/port" @@ -28,9 +28,7 @@ func NewControllersHandler(exporterConfig *port.Config, portConfig *port.Integra informersFactory := dynamicinformer.NewDynamicSharedInformerFactory(k8sClient.DynamicClient, resync) if portConfig.CRDSToDiscover != "" { - crd.AutodiscoverCRDsToActions(portConfig, k8sClient.ApiExtensionClient, portClient) - crdInformer := informersFactory.ForResource(schema.GroupVersionResource{Group: "apiextensions.k8s.io", Version: "v1", Resource: "customresourcedefinitions"}) - crd.NewCrdController(portClient, crdInformer, portConfig, k8sClient.ApiExtensionClient) + crdsyncer.InitalizeCRDSyncerControllers(portClient, portConfig, k8sClient.ApiExtensionClient, informersFactory) } aggResources := make(map[string][]port.KindConfig) From 65dc8202603f607a5740b35a73eaef82352a865b Mon Sep 17 00:00:00 2001 From: danielsinai Date: Wed, 19 Jun 2024 15:05:23 +0300 Subject: [PATCH 3/3] chore checkpoint --- pkg/crd_syncer/controllers.go | 75 +++++++++++++++++------------- pkg/crd_syncer/{crd.go => sync.go} | 4 +- 2 files changed, 46 insertions(+), 33 deletions(-) rename pkg/crd_syncer/{crd.go => sync.go} (97%) diff --git a/pkg/crd_syncer/controllers.go b/pkg/crd_syncer/controllers.go index 7a73eb7..dad8988 100644 --- a/pkg/crd_syncer/controllers.go +++ b/pkg/crd_syncer/controllers.go @@ -7,6 +7,7 @@ import ( "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/client-go/dynamic/dynamicinformer" + crdSyncerV1alpha1 "github.com/port-labs/port-k8s-exporter/pkg/api/v1alpha1" "k8s.io/client-go/tools/cache" "k8s.io/client-go/util/workqueue" ) @@ -41,44 +42,54 @@ func InitalizeCRDSyncerControllers(portClient *cli.PortClient, portConfig *port. workqueue: workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter()), } - crdController.informer.AddEventHandler(cache.ResourceEventHandlerFuncs{ - AddFunc: func(obj interface{}) { - var err error - var item EventItem - item.ActionType = CreateAction - item.Key, err = cache.MetaNamespaceKeyFunc(obj) - if err == nil { - AutoDiscoverSingleCRDToAction(portConfig, apiExtensionsClient, portClient, item.Key) - } - }, - UpdateFunc: func(old interface{}, new interface{}) { - var err error - var item EventItem - item.ActionType = UpdateAction - item.Key, err = cache.MetaNamespaceKeyFunc(new) - if err == nil { - AutoDiscoverSingleCRDToAction(portConfig, apiExtensionsClient, portClient, item.Key) - } - }, - }) + // crdController.informer.AddEventHandler(cache.ResourceEventHandlerFuncs{ + // If a CRD is added we want to see wheter he matches any of the CRD Syncers and if so to add it + // AddFunc: func(obj interface{}) { + // var err error + // var item EventItem + // item.ActionType = CreateAction + // item.Key, err = cache.MetaNamespaceKeyFunc(obj) + // if err == nil { + // AutoDiscoverSingleCRDToAction(portConfig, apiExtensionsClient, portClient, item.Key) + // } + // }, + // If a CRD changed we want to update the relevant blueprint in Port (if there's a mapping that matches the CRD) + // UpdateFunc: func(old interface{}, new interface{}) { + // var err error + // var item EventItem + // item.ActionType = UpdateAction + // item.Key, err = cache.MetaNamespaceKeyFunc(new) + // if err == nil { + // AutoDiscoverSingleCRDToAction(portConfig, apiExtensionsClient, portClient, item.Key) + // } + // }, + // }) syncerConfigurationInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{ + // If there's a new file of CRDSyncer configuration we want to see if there's a CRD that matches it and if so to add it AddFunc: func(obj interface{}) { - var err error - var item EventItem - item.ActionType = CreateAction - item.Key, err = cache.MetaNamespaceKeyFunc(obj) - if err == nil { - AutoDiscoverSingleCRDToAction(portConfig, apiExtensionsClient, portClient, item.Key) + if crdSyncerConfiguration, ok := obj.(*crdSyncerV1alpha1.CrdSyncer); ok { + var err error + var item EventItem + item.ActionType = CreateAction + item.Key, err = cache.MetaNamespaceKeyFunc(obj) + if err == nil { + Sync([]crdSyncerV1alpha1.CrdSyncer{*crdSyncerConfiguration}, apiExtensionsClient, portClient, item.Key) + } } + }, + // If there's an update to the CRDSyncer configuration we want to update the relevant blueprints/automations that affected from it in Port UpdateFunc: func(old interface{}, new interface{}) { - var err error - var item EventItem - item.ActionType = UpdateAction - item.Key, err = cache.MetaNamespaceKeyFunc(new) - if err == nil { - AutoDiscoverSingleCRDToAction(portConfig, apiExtensionsClient, portClient, item.Key) + if crdSyncerConfiguration, ok := new.(*crdSyncerV1alpha1.CrdSyncer); ok { + var err error + var item EventItem + + item.ActionType = UpdateAction + item.Key, err = cache.MetaNamespaceKeyFunc(new) + if err == nil { + Sync([]crdSyncerV1alpha1.CrdSyncer{*crdSyncerConfiguration}, apiExtensionsClient, portClient, item.Key) + } } }, }) diff --git a/pkg/crd_syncer/crd.go b/pkg/crd_syncer/sync.go similarity index 97% rename from pkg/crd_syncer/crd.go rename to pkg/crd_syncer/sync.go index a14d4ad..e28eac7 100644 --- a/pkg/crd_syncer/crd.go +++ b/pkg/crd_syncer/sync.go @@ -6,6 +6,7 @@ import ( "fmt" "strings" + crdSyncerV1alpha1 "github.com/port-labs/port-k8s-exporter/pkg/api/v1alpha1" "github.com/port-labs/port-k8s-exporter/pkg/goutils" "github.com/port-labs/port-k8s-exporter/pkg/jq" "github.com/port-labs/port-k8s-exporter/pkg/port" @@ -360,7 +361,8 @@ func handleCRD(crds []v1.CustomResourceDefinition, portConfig *port.IntegrationA } } -func AutoDiscoverSingleCRDToAction(portConfig *port.IntegrationAppConfig, apiExtensionsClient apiextensions.ApiextensionsV1Interface, portClient *cli.PortClient, crdName string) { +func Sync(configurations []crdSyncerV1alpha1.CrdSyncer, apiExtensionsClient apiextensions.ApiextensionsV1Interface, portClient *cli.PortClient, crdName string) { + // Get all the CRDS klog.Infof("Trying to export CRD/XRD: %s", crdName) crd, err := apiExtensionsClient.CustomResourceDefinitions().Get(context.Background(), crdName, metav1.GetOptions{})