-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
process-user-data: improve startup time
the binary is currently importing a lot of libraries which have costly init() procedures (around 8-10s at startup) this is due to sharing some consts with the CAA modules. We can avoid that by inlining code in process-user-data and moving shared consts into dedicated modules. We can use a cpuid field to narrow down a hypervisor, so we don't have to probe an IMDS endpoint while circling through the cloud provider, delaying startup. Signed-off-by: Magnus Kulke <magnuskulke@microsoft.com>
- Loading branch information
Showing
16 changed files
with
169 additions
and
401 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package initdata | ||
|
||
type InitData struct { | ||
Algorithm string `toml:"algorithm"` | ||
Version string `toml:"version"` | ||
Data map[string]string `toml:"data,omitempty"` | ||
} |
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,11 @@ | ||
package paths | ||
|
||
const ( | ||
AACfgPath = "/run/peerpod/aa.toml" | ||
AuthFilePath = "/run/peerpod/auth.json" | ||
CDHCfgPath = "/run/peerpod/cdh.toml" | ||
InitDataPath = "/run/peerpod/initdata" | ||
AgentCfgPath = "/run/peerpod/agent-config.toml" | ||
ForwarderCfgPath = "/run/peerpod/daemon.json" | ||
DockerUserDataPath = "/peerpod/userdata.json" | ||
) |
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,33 @@ | ||
package userdata | ||
|
||
import ( | ||
"context" | ||
"os" | ||
|
||
"github.com/klauspost/cpuid/v2" | ||
) | ||
|
||
func isAzureVM() bool { | ||
return cpuid.CPU.HypervisorVendorID == cpuid.MSVM | ||
} | ||
|
||
func isAWSVM(ctx context.Context) bool { | ||
if cpuid.CPU.HypervisorVendorID != cpuid.KVM { | ||
return false | ||
} | ||
_, err := imdsGet(ctx, AWSImdsUrl, false, nil) | ||
return err == nil | ||
} | ||
|
||
func isGCPVM(ctx context.Context) bool { | ||
if cpuid.CPU.HypervisorVendorID != cpuid.KVM { | ||
return false | ||
} | ||
_, err := imdsGet(ctx, GcpImdsUrl, false, []kvPair{{"Metadata-Flavor", "Google"}}) | ||
return err == nil | ||
} | ||
|
||
func isDockerContainer() bool { | ||
_, err := os.ReadFile("/.dockerenv") | ||
return err == nil | ||
} |
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,63 @@ | ||
package userdata | ||
|
||
import ( | ||
"context" | ||
"encoding/base64" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
) | ||
|
||
type kvPair struct { | ||
k string | ||
v string | ||
} | ||
|
||
func imdsGet(ctx context.Context, url string, b64 bool, headers []kvPair) ([]byte, error) { | ||
// If url is empty then return empty string | ||
if url == "" { | ||
return nil, fmt.Errorf("url is empty") | ||
} | ||
|
||
// Create a new HTTP client | ||
client := &http.Client{} | ||
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create request: %s", err) | ||
|
||
} | ||
|
||
for _, header := range headers { | ||
req.Header.Add(header.k, header.v) | ||
} | ||
|
||
// Send the request and retrieve the response | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to send request: %s", err) | ||
|
||
} | ||
defer resp.Body.Close() | ||
|
||
// Check if the response was successful | ||
if resp.StatusCode != http.StatusOK { | ||
return nil, fmt.Errorf("endpoint %s returned != 200 status code: %s", url, resp.Status) | ||
} | ||
|
||
// Read the response body and return it as a string | ||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to read response body: %s", err) | ||
} | ||
|
||
if !b64 { | ||
return body, nil | ||
} | ||
|
||
decoded, err := base64.StdEncoding.DecodeString(string(body)) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to decode b64 encoded userData: %s", err) | ||
} | ||
return decoded, nil | ||
} |
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
Oops, something went wrong.