Skip to content

Commit

Permalink
Added env var of the feature, and implement a mechanism to reduce req…
Browse files Browse the repository at this point in the history
…uests using hash
  • Loading branch information
razsamuel committed May 16, 2024
1 parent 8da384d commit 05151d7
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
1 change: 1 addition & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func Init() {
NewString(&ApplicationConfig.PortClientSecret, "port-client-secret", "", "Port client secret. Required.")
NewBool(&ApplicationConfig.CreateDefaultResources, "create-default-resources", true, "Create default resources on installation. Optional.")
NewBool(&ApplicationConfig.OverwriteConfigurationOnRestart, "overwrite-configuration-on-restart", false, "Overwrite the configuration in port on restarting the exporter. Optional.")
NewBool(&ApplicationConfig.UpdateEntityOnlyOnDiff, "update-entity-only-on-diff", false, "Optimization to reduce requests to port. Optional.")

// Deprecated
NewBool(&ApplicationConfig.DeleteDependents, "delete-dependents", false, "Delete dependents. Optional.")
Expand Down
1 change: 1 addition & 0 deletions pkg/config/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ type ApplicationConfiguration struct {
Resources []port.Resource
DeleteDependents bool `json:"deleteDependents,omitempty"`
CreateMissingRelatedEntities bool `json:"createMissingRelatedEntities,omitempty"`
UpdateEntityOnlyOnDiff bool `json:"updateEntityOnlyOnDiff,omitempty"`
}
55 changes: 54 additions & 1 deletion pkg/k8s/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ import (
"github.com/port-labs/port-k8s-exporter/pkg/port"
"github.com/port-labs/port-k8s-exporter/pkg/port/cli"
"github.com/port-labs/port-k8s-exporter/pkg/port/mapping"
"hash/fnv"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/informers"
"strconv"
"time"

"encoding/json"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/tools/cache"
Expand Down Expand Up @@ -68,7 +71,42 @@ func NewController(resource port.AggregatedResource, portClient *cli.PortClient,
item.ActionType = UpdateAction
item.Key, err = cache.MetaNamespaceKeyFunc(new)
if err == nil {
controller.workqueue.Add(item)

if config.ApplicationConfig.UpdateEntityOnlyOnDiff == false {
controller.workqueue.Add(item)
} else {
for _, kindConfig := range controller.resource.KindConfigs {
oldEntities, err := controller.getObjectEntities(old, kindConfig.Selector, kindConfig.Port.Entity.Mappings)
if err != nil {
klog.Errorf("Error getting old entities: %v", err)
controller.workqueue.Add(item)
break
}
newEntities, err := controller.getObjectEntities(new, kindConfig.Selector, kindConfig.Port.Entity.Mappings)
if err != nil {
klog.Errorf("Error getting new entities: %v", err)
controller.workqueue.Add(item)
break
}
oldEntitiesHash, err := hashAllEntities(oldEntities)
if err != nil {
klog.Errorf("Error hashing old entities: %v", err)
controller.workqueue.Add(item)
break
}
newEntitiesHash, err := hashAllEntities(newEntities)
if err != nil {
klog.Errorf("Error hashing new entities: %v", err)
controller.workqueue.Add(item)
break
}

if oldEntitiesHash != newEntitiesHash {
controller.workqueue.Add(item)
break
}
}
}
}
},
DeleteFunc: func(obj interface{}) {
Expand Down Expand Up @@ -335,3 +373,18 @@ func (c *Controller) GetEntitiesSet() (map[string]interface{}, error) {

return k8sEntitiesSet, nil
}

func hashAllEntities(entities []port.Entity) (string, error) {
h := fnv.New64a()
for _, entity := range entities {
entityBytes, err := json.Marshal(entity)
if err != nil {
return "", err
}
_, err = h.Write(entityBytes)
if err != nil {
return "", err
}
}
return strconv.FormatUint(h.Sum64(), 10), nil
}

0 comments on commit 05151d7

Please sign in to comment.