Skip to content

Commit

Permalink
Add DeleteDependents flag support
Browse files Browse the repository at this point in the history
  • Loading branch information
talsabagport committed Dec 29, 2022
1 parent 2311502 commit 03072b1
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
12 changes: 8 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import (

var (
configFilePath string
stateKey string
resyncInterval uint
stateKey string
deleteDependents bool
portBaseURL string
portClientId string
portClientSecret string
Expand All @@ -27,7 +28,7 @@ func main() {

stopCh := signal.SetupSignalHandler()

exporterConfig, err := config.New(configFilePath)
exporterConfig, err := config.New(configFilePath, resyncInterval, stateKey)
if err != nil {
klog.Fatalf("Error building Port K8s Exporter config: %s", err.Error())
}
Expand All @@ -37,8 +38,10 @@ func main() {
klog.Fatalf("Error building K8s client: %s", err.Error())
}

portClient, err := cli.New(portBaseURL, cli.WithHeader("User-Agent", fmt.Sprintf("port-k8s-exporter/0.1 (statekey/%s)", stateKey)),
cli.WithClientID(portClientId), cli.WithClientSecret(portClientSecret))
portClient, err := cli.New(portBaseURL,
cli.WithHeader("User-Agent", fmt.Sprintf("port-k8s-exporter/0.1 (statekey/%s)", stateKey)),
cli.WithClientID(portClientId), cli.WithClientSecret(portClientSecret), cli.WithDeleteDependents(deleteDependents),
)
if err != nil {
klog.Fatalf("Error building Port client: %s", err.Error())
}
Expand All @@ -52,6 +55,7 @@ func main() {
func init() {
flag.StringVar(&configFilePath, "config", "", "Path to Port K8s Exporter config file. Required.")
flag.StringVar(&stateKey, "state-key", "", "Port K8s Exporter state key id. Required.")
flag.BoolVar(&deleteDependents, "delete-dependents", false, "Flag to enable deletion of dependent Port Entities. Optional.")
flag.UintVar(&resyncInterval, "resync-interval", 0, "The re-sync interval in minutes. Optional.")
flag.StringVar(&portBaseURL, "port-base-url", "https://api.getport.io", "Port base URL. Optional.")
portClientId = os.Getenv("PORT_CLIENT_ID")
Expand Down
7 changes: 5 additions & 2 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@ type Config struct {
StateKey string
}

func New(filepath string) (*Config, error) {
c := &Config{}
func New(filepath string, resyncInterval uint, stateKey string) (*Config, error) {
c := &Config{
ResyncInterval: resyncInterval,
StateKey: stateKey,
}
config, err := os.ReadFile(filepath)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion pkg/k8s/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ func (c *Controller) entityHandler(portEntity port.Entity, action EventActionTyp
}
klog.Infof("Successfully upserted entity '%s' of blueprint '%s'", portEntity.Identifier, portEntity.Blueprint)
case DeleteAction:
err := c.portClient.DeleteEntity(context.Background(), portEntity.Identifier, portEntity.Blueprint)
err := c.portClient.DeleteEntity(context.Background(), portEntity.Identifier, portEntity.Blueprint, c.portClient.DeleteDependents)
if err != nil {
return fmt.Errorf("error deleting Port entity '%s' of blueprint '%s': %v", portEntity.Identifier, portEntity.Blueprint, err)
}
Expand Down
13 changes: 10 additions & 3 deletions pkg/port/cli/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import (
type (
Option func(*PortClient)
PortClient struct {
Client *resty.Client
ClientID string
ClientSecret string
Client *resty.Client
ClientID string
ClientSecret string
DeleteDependents bool
}
)

Expand Down Expand Up @@ -81,3 +82,9 @@ func WithClientSecret(clientSecret string) Option {
pc.ClientSecret = clientSecret
}
}

func WithDeleteDependents(deleteDependents bool) Option {
return func(pc *PortClient) {
pc.DeleteDependents = deleteDependents
}
}
6 changes: 4 additions & 2 deletions pkg/port/cli/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/port-labs/port-k8s-exporter/pkg/port"
"k8s.io/klog/v2"
"net/url"
"strconv"
)

func (c *PortClient) SearchEntities(ctx context.Context, body port.SearchBody) ([]port.Entity, error) {
Expand Down Expand Up @@ -69,12 +70,13 @@ func (c *PortClient) CreateEntity(ctx context.Context, e *port.Entity, runID str
return &pb.Entity, nil
}

func (c *PortClient) DeleteEntity(ctx context.Context, id string, blueprint string) error {
func (c *PortClient) DeleteEntity(ctx context.Context, id string, blueprint string, deleteDependents bool) error {
pb := &port.ResponseBody{}
resp, err := c.Client.R().
SetHeader("Accept", "application/json").
SetPathParam("blueprint", blueprint).
SetPathParam("identifier", id).
SetQueryParam("delete_dependents", strconv.FormatBool(deleteDependents)).
SetResult(pb).
Delete("v1/blueprints/{blueprint}/entities/{identifier}")
if err != nil {
Expand Down Expand Up @@ -109,7 +111,7 @@ func (c *PortClient) DeleteStaleEntities(ctx context.Context, stateKey string, e
for _, portEntity := range portEntities {
_, ok := existingEntitiesSet[c.GetEntityIdentifierKey(&portEntity)]
if !ok {
err := c.DeleteEntity(ctx, portEntity.Identifier, portEntity.Blueprint)
err := c.DeleteEntity(ctx, portEntity.Identifier, portEntity.Blueprint, c.DeleteDependents)
if err != nil {
klog.Errorf("error deleting Port entity '%s' of blueprint '%s': %v", portEntity.Identifier, portEntity.Blueprint, err)
continue
Expand Down

0 comments on commit 03072b1

Please sign in to comment.