Skip to content

Commit

Permalink
add AZURE_SERVICEDISCOVERY_SUBSCRIPTION_TAG_SELECTOR to filter subscr…
Browse files Browse the repository at this point in the history
…iptions

Signed-off-by: Markus Blaschke <[email protected]>
  • Loading branch information
mblaschke committed Sep 4, 2023
1 parent ed7d925 commit 2a5bdb0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
7 changes: 4 additions & 3 deletions azuresdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

### Env vars

| Variable name | Default | Description |
|------------------------------------|-----------------------|-------------------------------------------------------------------|
| `AZURE_SERVICEDISCOVERY_CACHE_TTL` | `60m` (time.Duration) | ServiceDiscovery cache (eg. subscription, resourceGroup list,...) |
| Variable name | Default | Description |
|----------------------------------------------------|-----------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `AZURE_SERVICEDISCOVERY_CACHE_TTL` | `60m` (time.Duration) | ServiceDiscovery cache (eg. subscription, resourceGroup list,...) |
| `AZURE_SERVICEDISCOVERY_SUBSCRIPTION_TAG_SELECTOR` | | Tag selector `tagName=tagValue,tagName2=tagValue2` to filter subscriptions for ServiceDiscovery (uses [kubernetes label selector library](https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#label-selectors)) |

### Authentication

Expand Down
3 changes: 2 additions & 1 deletion azuresdk/armclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import (
)

const (
EnvVarServiceDiscoveryTtl = "AZURE_SERVICEDISCOVERY_CACHE_TTL"
EnvVarServiceDiscoveryTtl = "AZURE_SERVICEDISCOVERY_CACHE_TTL"
EnvVarServiceDiscoverySubscriptionTagSelector = "AZURE_SERVICEDISCOVERY_SUBSCRIPTION_TAG_SELECTOR"
)

type (
Expand Down
35 changes: 33 additions & 2 deletions azuresdk/armclient/client.subscriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ package armclient
import (
"context"
"fmt"
"os"
"strings"

"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armsubscriptions"
"k8s.io/apimachinery/pkg/labels"

"github.com/webdevops/go-common/utils/to"
)

const (
Expand Down Expand Up @@ -56,8 +60,20 @@ func (azureClient *ArmClient) ListCachedSubscriptions(ctx context.Context) (map[

// ListSubscriptions return list of Azure Subscriptions as map (key is subscription id)
func (azureClient *ArmClient) ListSubscriptions(ctx context.Context) (map[string]*armsubscriptions.Subscription, error) {
var (
subscriptionTagSelector *labels.Selector
)
list := map[string]*armsubscriptions.Subscription{}

// parse tag selector (using kubernetes label selector)
if val := os.Getenv(EnvVarServiceDiscoverySubscriptionTagSelector); val != "" {
selector, err := labels.Parse(val)
if err != nil {
panic(err)
}
subscriptionTagSelector = &selector
}

client, err := armsubscriptions.NewClient(azureClient.GetCred(), azureClient.NewArmClientOptions())
if err != nil {
return nil, err
Expand All @@ -75,15 +91,30 @@ func (azureClient *ArmClient) ListSubscriptions(ctx context.Context) (map[string
}

for _, subscription := range result.Value {
useSubscription := true

if len(azureClient.subscriptionList) > 0 {
// use subscription filter
useSubscription = false
for _, subscriptionId := range azureClient.subscriptionList {
if strings.EqualFold(*subscription.SubscriptionID, subscriptionId) {
list[*subscription.SubscriptionID] = subscription
useSubscription = true
break
}
}
} else {
}

// filter by tag selector (using kubernetes label selector)
if subscriptionTagSelector != nil {
tags := labels.Set(to.StringMap(subscription.Tags))
if (*subscriptionTagSelector).Matches(tags) {
useSubscription = true
} else {
useSubscription = false
}
}

if useSubscription {
list[*subscription.SubscriptionID] = subscription
}
}
Expand Down

0 comments on commit 2a5bdb0

Please sign in to comment.