Skip to content

Commit

Permalink
add servicediscovery subscription id limiting
Browse files Browse the repository at this point in the history
Signed-off-by: Markus Blaschke <[email protected]>
  • Loading branch information
mblaschke committed Sep 4, 2023
1 parent 133c778 commit cb9cf73
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions azuresdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
| Variable name | Default | Description |
|----------------------------------------------------|-----------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `AZURE_SERVICEDISCOVERY_CACHE_TTL` | `60m` (time.Duration) | ServiceDiscovery cache (eg. subscription, resourceGroup list,...) |
| `AZURE_SERVICEDISCOVERY_SUBSCRIPTION_ID` | | Subscription IDs separated by commas (whitespaces are trimmed) |
| `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
23 changes: 22 additions & 1 deletion azuresdk/armclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

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

Expand Down Expand Up @@ -102,7 +103,19 @@ func (azureClient *ArmClient) initCache() {

// init serviceDiscovery settings
func (azureClient *ArmClient) initServiceDiscovery() {
// parse tag selector (using kubernetes label selector)
// use fixed list of subscription ids
if val := os.Getenv(EnvVarServiceDiscoverySubscriptionId); val != "" {
azureClient.serviceDiscovery.subscriptionIds = []string{}
for _, subscriptionId := range strings.Split(val, ",") {
subscriptionId = strings.TrimSpace(subscriptionId)
azureClient.serviceDiscovery.subscriptionIds = append(
azureClient.serviceDiscovery.subscriptionIds,
subscriptionId,
)
}
}

// parse subscription tag selector (using kubernetes label selector)
if val := os.Getenv(EnvVarServiceDiscoverySubscriptionTagSelector); val != "" {
selector, err := labels.Parse(val)
if err != nil {
Expand Down Expand Up @@ -242,6 +255,14 @@ func (azureClient *ArmClient) SetSubscriptionID(subscriptionId ...string) {
azureClient.serviceDiscovery.subscriptionIds = subscriptionId
}

// AddSubscriptionID add subscription filter, other subscriptions will be ignored
func (azureClient *ArmClient) AddSubscriptionID(subscriptionId ...string) {
azureClient.serviceDiscovery.subscriptionIds = append(
azureClient.serviceDiscovery.subscriptionIds,
subscriptionId...,
)
}

func (azureClient *ArmClient) cacheData(identifier string, callback func() (interface{}, error)) (interface{}, error) {
if v, ok := azureClient.cache.Get(identifier); ok {
return v, nil
Expand Down

0 comments on commit cb9cf73

Please sign in to comment.