-
Notifications
You must be signed in to change notification settings - Fork 8
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
demo: capability matching on vm sizes #12
Open
alexeldeib
wants to merge
1
commit into
Azure:main
Choose a base branch
from
alexeldeib:ace/match
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,3 +49,8 @@ const ( | |
// Generation 2. | ||
HyperVGeneration2 = "V2" | ||
) | ||
|
||
const ( | ||
base10 = 10 | ||
base64 = 64 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,24 @@ | ||
module github.com/Azure/skewer | ||
|
||
go 1.13 | ||
go 1.18 | ||
|
||
require ( | ||
github.com/Azure/azure-sdk-for-go v46.0.0+incompatible | ||
github.com/Azure/go-autorest/autorest v0.11.4 // indirect | ||
github.com/Azure/go-autorest/autorest/adal v0.9.2 // indirect | ||
github.com/Azure/go-autorest/autorest/to v0.4.0 | ||
github.com/Azure/go-autorest/autorest/validation v0.3.0 // indirect | ||
github.com/google/go-cmp v0.5.1 | ||
github.com/pkg/errors v0.9.1 | ||
github.com/sanity-io/litter v1.5.5 | ||
) | ||
|
||
require ( | ||
github.com/Azure/go-autorest v14.2.0+incompatible // indirect | ||
github.com/Azure/go-autorest/autorest v0.11.4 // indirect | ||
github.com/Azure/go-autorest/autorest/adal v0.9.2 // indirect | ||
github.com/Azure/go-autorest/autorest/date v0.3.0 // indirect | ||
github.com/Azure/go-autorest/autorest/validation v0.3.0 // indirect | ||
github.com/Azure/go-autorest/logger v0.2.0 // indirect | ||
github.com/Azure/go-autorest/tracing v0.6.0 // indirect | ||
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect | ||
golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a // indirect | ||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package skewer | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
) | ||
|
||
func Match(ctx context.Context, cache *Cache, sku *SKU, location string) *SKU { | ||
sizes := cache.List(ctx, ResourceTypeFilter(sku.GetResourceType()), LocationFilter(normalizeLocation(location))) | ||
|
||
capabilities := map[string]string{} | ||
for _, capability := range *sku.Capabilities { | ||
if capability.Name != nil { | ||
if capability.Value != nil { | ||
capabilities[*capability.Name] = *capability.Value | ||
} else { | ||
capabilities[*capability.Name] = "" | ||
} | ||
} | ||
} | ||
|
||
for i := range sizes { | ||
candidate := &sizes[i] | ||
if candidate.GetName() == sku.GetName() { | ||
continue | ||
} | ||
if allCapabilitiesMatch(candidate, capabilities) { | ||
return candidate | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func allCapabilitiesMatch(sku *SKU, capabilities map[string]string) bool { | ||
matched := 0 | ||
desired := len(capabilities) | ||
for _, capability := range *sku.Capabilities { | ||
if capability.Name != nil { | ||
// TODO(ace): this is not actually accurate, really for each | ||
// capability, we should decide whether you need subset, exact match, | ||
// or numerically greater/less than. | ||
if capabilitiesToIgnore[*capability.Name] { | ||
continue | ||
} | ||
if capability.Value != nil { | ||
// TODO(ace): this is far too strict and results in basically zero matches. | ||
if capabilities[*capability.Name] != *capability.Value { | ||
fmt.Printf("failed on capability %s=%s\n", *capability.Name, *capability.Value) | ||
return false | ||
} | ||
matched++ | ||
} else { | ||
val, ok := capabilities[*capability.Name] | ||
if !ok || val != "" { | ||
fmt.Printf("failed on capability %s with no value\n", *capability.Name) | ||
return false | ||
} | ||
matched++ | ||
} | ||
} | ||
} | ||
|
||
if matched != desired { | ||
fmt.Printf("failed to find all desired capabilities want %d got %d\n", desired, matched) | ||
} | ||
return matched == desired | ||
} | ||
|
||
var capabilitiesToIgnore = map[string]bool{ | ||
MaxResourceVolumeMB: true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tried ignoring some, just fails matching different capabilities :D |
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this currently returns zero matches because == is too strict on capabilities, but you get the idea..