Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow regex replacement in ResourceBehavior.IdentityInV1API #555

Merged
merged 1 commit into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions docs/operators/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,16 @@ resource_behavior:
- { resource: compute/instances_baremetal_large, identity_in_v1_api: compute/instances_thebigbox }
```

The fields `category` and `identity_in_v1_api` can be templated with placeholders `$1`, `$2`, etc., which will be filled with the respective match groups from the `resource` regex. For example:

```yaml
resource_behavior:
# this entire service was renamed
- { resource: 'volumev2/(.*)', identity_in_v1_api: 'cinder/$1' }
# this service contains sets of resources for each share type
- { resource: 'manila/(shares|snapshots|share_capacity|snapshot_capacity)_(.+)', category: 'share_type_$2' }
```

#### Resource renaming

Limes provides amenities for renaming and restructuring services and resources in a way where the backwards-incompatible
Expand Down
2 changes: 1 addition & 1 deletion internal/core/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func (c *Cluster) BehaviorForResource(serviceType db.ServiceType, resourceName l
fullName := string(serviceType) + "/" + string(resourceName)
for _, behavior := range c.Config.ResourceBehaviors {
if behavior.FullResourceNameRx.MatchString(fullName) {
result.Merge(behavior)
result.Merge(behavior, fullName)
}
}

Expand Down
24 changes: 21 additions & 3 deletions internal/core/resource_behavior.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (b ResourceBehavior) BuildAPIResourceInfo(resName limesresources.ResourceNa
}

// Merge computes the union of both given resource behaviors.
func (b *ResourceBehavior) Merge(other ResourceBehavior) {
func (b *ResourceBehavior) Merge(other ResourceBehavior, fullResourceName string) {
if other.OvercommitFactor != 0 {
b.OvercommitFactor = other.OvercommitFactor
}
Expand All @@ -114,19 +114,37 @@ func (b *ResourceBehavior) Merge(other ResourceBehavior) {
}
}
if other.IdentityInV1API != (ResourceRef{}) {
b.IdentityInV1API = other.IdentityInV1API
b.IdentityInV1API.ServiceType = interpolateFromResourceNameMatch(other, other.IdentityInV1API.ServiceType, fullResourceName)
b.IdentityInV1API.ResourceName = interpolateFromResourceNameMatch(other, other.IdentityInV1API.ResourceName, fullResourceName)
}
if !other.TranslationRuleInV1API.IsEmpty() {
b.TranslationRuleInV1API = other.TranslationRuleInV1API
}
if other.Category != "" {
b.Category = other.Category
b.Category = interpolateFromResourceNameMatch(other, other.Category, fullResourceName)
}
if other.CommitmentConversion != (CommitmentConversion{}) {
b.CommitmentConversion = other.CommitmentConversion
}
}

func interpolateFromResourceNameMatch[S ~string](b ResourceBehavior, value S, fullResourceName string) S {
if !strings.Contains(string(value), "$") {
return value
}
rx, err := b.FullResourceNameRx.Regexp()
if err != nil {
// defense in depth: this should not happen because the regex should have been validated at UnmarshalYAML time
return value
}
match := rx.FindStringSubmatchIndex(fullResourceName)
if match == nil {
// defense in depth: this should not happen because this is only called after the resource name has already matched
return value
}
return S(rx.ExpandString(nil, string(value), fullResourceName, match))
}

// ResourceRef contains a pair of service type and resource name using API-level identifiers.
// When read from the configuration YAML, this deserializes from a string in the "service/resource" format.
type ResourceRef struct {
Expand Down
4 changes: 2 additions & 2 deletions internal/datamodel/quota_overrides_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ const (
resource_behavior:
- resource: first/capacity
identity_in_v1_api: capacities/first
- resource: first/things
identity_in_v1_api: things/first
- resource: (first)/thi(ngs)
identity_in_v1_api: thi$2/$1
`
)

Expand Down
Loading