-
Notifications
You must be signed in to change notification settings - Fork 119
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
detect which cloud provider we are using #38
Conversation
Here we see the debug
|
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.
What if we simplified, so we just make a table of those cloud detection items? They all come down to:
PATH-to-REQUEST. RESPONSE-EXPECTED. CUSTOM-HEADER->LHS,RHS
/v1. "Google Compute Engine". "Metadata-Flavor". "Google"
/instance. "Azure..." nil
|
peirates.go
Outdated
@@ -13,6 +13,7 @@ import ( | |||
"os/exec" | |||
"regexp" | |||
"strings" | |||
L "./lib" |
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.
It seems angry about this location. Will consult the style guide.
/home/franklin/go/bin/golangci-lint run
peirates.go:6:2: local import "./lib" in non-local package (typecheck)
L "./lib"
^
make: *** [Makefile:11: lint] Error 1
~/w/d/peirates (dev37-20230508±) ▶
I think the formatting of the result will be similar, agreed. But the difference is in the way we detect each different provider. I guess help me understand where in the code we would implement this? |
With the exception of Openstack, we can detect cloud provider via a single HTTP request. I'm thinking this might be implementable as a table of:
For OpenStack, it's a file read but that doesn't seem to be applicable to our use case. So we can leave that out. The only other file reads are checking to see whether you're in a containerd-spawned container and whether you're in a Kubernetes pod. Both of those really belong in separate code. Here's an example - but it would be better to use DoHTTPRequestAndGetBody() instead of having all this HTTP logic: Function definition for DoHTTPRequestAndGetBody(): // DoHTTPRequestAndGetBody performs an HTTP request, and returns the full
// body of the reponse as a string. If ignoreTLSErrors is true, all TLS
// errors, such as invalid certificates, will be ignored. If caCertPath is
// not an empty string, a TLS certificate will be read from the provided path
// and added to the pool of valid certificates.
func DoHTTPRequestAndGetBody(req *http.Request, https bool, ignoreTLSErrors bool, caCertPath string) ([]byte, error) { Example: type CloudProvider struct {
Name string
URL string
HTTPMethod string
CustomHeader string
CustomHeaderValue string
ResultString string
}
func populateAndCheckCloudProviders() {
providers := []CloudProvider{
{
Name: "Provider1",
URL: "https://example.com/provider1",
HTTPMethod: "GET",
CustomHeader: "X-Custom-Header",
CustomHeaderValue: "Value1",
ResultString: "Provider1 found",
},
{
Name: "Provider2",
URL: "https://example.com/provider2",
HTTPMethod: "POST",
CustomHeader: "",
CustomHeaderValue: "",
ResultString: "Provider2 found",
},
// Add more cloud providers as needed
}
for _, provider := range providers {
fmt.Printf("Checking %s...\n", provider.Name)
// Use DoHTTPRequestAndGetBody()
req, err := http.NewRequest(provider.HTTPMethod, provider.URL, nil)
if err != nil {
fmt.Printf("Failed to create request for %s: %v\n", provider.Name, err)
continue
}
if provider.CustomHeader != "" {
req.Header.Set(provider.CustomHeader, provider.CustomHeaderValue)
}
// use DoHTTPRequestAndGetBody
resp, err := client.Do(req)
if err != nil {
fmt.Printf("Failed to make request to %s: %v\n", provider.Name, err)
continue
}
defer resp.Body.Close()
// Use DoHTTPRequestAndGetBody()
if resp.StatusCode == http.StatusOK {
// Check if there's a body string returned that matches ResultString
} else {
fmt.Printf("%s responded with HTTP %d\n", provider.Name, resp.StatusCode)
}
}
} |
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.
Looks good, but there's a quick copy-paste issue on the Digital Ocean detection.
HTTPMethod: "GET", | ||
CustomHeader: "", | ||
CustomHeaderValue: "", | ||
ResultString: "Microsoft Azure", |
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.
Looks like a copy-paste error on this line that can be easily fixed.
The goal is to detect the cloud provider in a concise manner.