From 40fb7490dc1a583405a4377b6e117ef930ad16ec Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Fri, 31 Jan 2025 10:04:59 -0500 Subject: [PATCH 1/8] lxd/network/driver_physical: replace fmt.Sprintf() by fmt.Sprint() Signed-off-by: Simon Deziel --- lxd/network/driver_physical.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lxd/network/driver_physical.go b/lxd/network/driver_physical.go index fe2e7c2848ce..dd774f990b24 100644 --- a/lxd/network/driver_physical.go +++ b/lxd/network/driver_physical.go @@ -362,7 +362,7 @@ func (n *physical) setup(oldConfig map[string]string) error { // Record if we created this device or not (if we have not already recorded that we created it previously), // so it can be removed on stop. This way we won't overwrite the setting on LXD restart. if shared.IsFalseOrEmpty(n.config["volatile.last_state.created"]) { - n.config["volatile.last_state.created"] = fmt.Sprintf("%t", created) + n.config["volatile.last_state.created"] = fmt.Sprint(created) err = n.state.DB.Cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error { return tx.UpdateNetwork(ctx, n.project, n.name, n.description, n.config) }) From 6c7054c547a8283243cbc1f81f7a556817a3cb03 Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Fri, 31 Jan 2025 10:05:41 -0500 Subject: [PATCH 2/8] lxd/network/driver_physical: don't hardcode HTTP status code Signed-off-by: Simon Deziel --- lxd/network/driver_physical.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lxd/network/driver_physical.go b/lxd/network/driver_physical.go index dd774f990b24..0a475356ce4e 100644 --- a/lxd/network/driver_physical.go +++ b/lxd/network/driver_physical.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "net" + "net/http" "strconv" "github.com/canonical/lxd/lxd/cluster/request" @@ -536,7 +537,7 @@ func (n *physical) State() (*api.NetworkState, error) { state, err := resources.GetNetworkState(GetHostDevice(n.config["parent"], n.config["vlan"])) if err != nil { // If the parent is not found, return a response indicating the network is unavailable. - if api.StatusErrorCheck(err, 404) { + if api.StatusErrorCheck(err, http.StatusNotFound) { return &api.NetworkState{ State: "unavailable", Type: "unknown", From 716a6effcc82af9849e9e70e9845253b740165e5 Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Fri, 31 Jan 2025 10:08:07 -0500 Subject: [PATCH 3/8] lxc/config/cert: avoid fmt.Sprintf() as it is slow Signed-off-by: Simon Deziel --- lxc/config/cert.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lxc/config/cert.go b/lxc/config/cert.go index 70457a7c43bf..2afae35a75e2 100644 --- a/lxc/config/cert.go +++ b/lxc/config/cert.go @@ -1,7 +1,6 @@ package config import ( - "fmt" "io" "os" @@ -33,8 +32,8 @@ func (c *Config) GenerateClientCertificate() error { // CopyGlobalCert will copy global (system-wide) certificate to the user config path. func (c *Config) CopyGlobalCert(src string, dst string) error { - oldPath := c.GlobalConfigPath("servercerts", fmt.Sprintf("%s.crt", src)) - newPath := c.ConfigPath("servercerts", fmt.Sprintf("%s.crt", dst)) + oldPath := c.GlobalConfigPath("servercerts", src+".crt") + newPath := c.ConfigPath("servercerts", dst+".crt") sourceFile, err := os.Open(oldPath) if err != nil { return err From ea9163434c2ec60bb785a81adff3ca930422ab75 Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Fri, 31 Jan 2025 11:07:54 -0500 Subject: [PATCH 4/8] client/lxd_images: pass ref to image instead of copying it This was accidentally change by 963f555865fbf8cdc4855fe458430f48bfd32aad Signed-off-by: Simon Deziel --- client/lxd_images.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/lxd_images.go b/client/lxd_images.go index b0e07a296b86..13d89b52eca6 100644 --- a/client/lxd_images.go +++ b/client/lxd_images.go @@ -1052,7 +1052,7 @@ func (r *ProtocolLXD) ExportImage(fingerprint string, image api.ImageExportPost) } // Send the request - op, _, err := r.queryOperation("POST", "/images/"+url.PathEscape(fingerprint)+"/export", image, "", true) + op, _, err := r.queryOperation("POST", "/images/"+url.PathEscape(fingerprint)+"/export", &image, "", true) if err != nil { return nil, err } From 4752459c055ee98e9fce955b9c0bd5952ca18213 Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Fri, 31 Jan 2025 11:17:01 -0500 Subject: [PATCH 5/8] client/lxd_oidc: avoid fmt.Sprintf() as it is slow Signed-off-by: Simon Deziel --- client/lxd_oidc.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/lxd_oidc.go b/client/lxd_oidc.go index 0fdb97657077..394d13d7e578 100644 --- a/client/lxd_oidc.go +++ b/client/lxd_oidc.go @@ -126,7 +126,7 @@ func (o *oidcClient) do(req *http.Request) (*http.Response, error) { } // Set the new access token in the header. - req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", o.tokens.AccessToken)) + req.Header.Set("Authorization", "Bearer "+o.tokens.AccessToken) resp, err = o.httpClient.Do(req) if err != nil { From 5bf1faf80dbf0c3cada43bb111375a6b941f2b1e Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Fri, 31 Jan 2025 11:17:19 -0500 Subject: [PATCH 6/8] client/lxd_profiles: avoid fmt.Sprintf() as it is slow Signed-off-by: Simon Deziel --- client/lxd_profiles.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/client/lxd_profiles.go b/client/lxd_profiles.go index 2adca7cf7ceb..ae8f603c5e4e 100644 --- a/client/lxd_profiles.go +++ b/client/lxd_profiles.go @@ -1,7 +1,6 @@ package lxd import ( - "fmt" "net/url" "github.com/canonical/lxd/shared/api" @@ -58,7 +57,7 @@ func (r *ProtocolLXD) GetProfile(name string) (*api.Profile, string, error) { profile := api.Profile{} // Fetch the raw value - etag, err := r.queryStruct("GET", fmt.Sprintf("/profiles/%s", url.PathEscape(name)), nil, "", &profile) + etag, err := r.queryStruct("GET", "/profiles/"+url.PathEscape(name), nil, "", &profile) if err != nil { return nil, "", err } @@ -80,7 +79,7 @@ func (r *ProtocolLXD) CreateProfile(profile api.ProfilesPost) error { // UpdateProfile updates the profile to match the provided Profile struct. func (r *ProtocolLXD) UpdateProfile(name string, profile api.ProfilePut, ETag string) error { // Send the request - _, _, err := r.query("PUT", fmt.Sprintf("/profiles/%s", url.PathEscape(name)), profile, ETag) + _, _, err := r.query("PUT", "/profiles/"+url.PathEscape(name), profile, ETag) if err != nil { return err } @@ -91,7 +90,7 @@ func (r *ProtocolLXD) UpdateProfile(name string, profile api.ProfilePut, ETag st // RenameProfile renames an existing profile entry. func (r *ProtocolLXD) RenameProfile(name string, profile api.ProfilePost) error { // Send the request - _, _, err := r.query("POST", fmt.Sprintf("/profiles/%s", url.PathEscape(name)), profile, "") + _, _, err := r.query("POST", "/profiles/"+url.PathEscape(name), profile, "") if err != nil { return err } @@ -102,7 +101,7 @@ func (r *ProtocolLXD) RenameProfile(name string, profile api.ProfilePost) error // DeleteProfile deletes a profile. func (r *ProtocolLXD) DeleteProfile(name string) error { // Send the request - _, _, err := r.query("DELETE", fmt.Sprintf("/profiles/%s", url.PathEscape(name)), nil, "") + _, _, err := r.query("DELETE", "/profiles/"+url.PathEscape(name), nil, "") if err != nil { return err } From 009780de7d4777e5073186fb3848c28f7f244591 Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Fri, 31 Jan 2025 11:17:26 -0500 Subject: [PATCH 7/8] client/lxd_projects: avoid fmt.Sprintf() as it is slow Signed-off-by: Simon Deziel --- client/lxd_projects.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/client/lxd_projects.go b/client/lxd_projects.go index a05a1b6d7bf2..db0bae74c8f5 100644 --- a/client/lxd_projects.go +++ b/client/lxd_projects.go @@ -1,7 +1,6 @@ package lxd import ( - "fmt" "net/url" "github.com/canonical/lxd/shared/api" @@ -56,7 +55,7 @@ func (r *ProtocolLXD) GetProject(name string) (*api.Project, string, error) { project := api.Project{} // Fetch the raw value - etag, err := r.queryStruct("GET", fmt.Sprintf("/projects/%s", url.PathEscape(name)), nil, "", &project) + etag, err := r.queryStruct("GET", "/projects/"+url.PathEscape(name), nil, "", &project) if err != nil { return nil, "", err } @@ -74,7 +73,7 @@ func (r *ProtocolLXD) GetProjectState(name string) (*api.ProjectState, error) { projectState := api.ProjectState{} // Fetch the raw value - _, err = r.queryStruct("GET", fmt.Sprintf("/projects/%s/state", url.PathEscape(name)), nil, "", &projectState) + _, err = r.queryStruct("GET", "/projects/"+url.PathEscape(name)+"/state", nil, "", &projectState) if err != nil { return nil, err } @@ -106,7 +105,7 @@ func (r *ProtocolLXD) UpdateProject(name string, project api.ProjectPut, ETag st } // Send the request - _, _, err = r.query("PUT", fmt.Sprintf("/projects/%s", url.PathEscape(name)), project, ETag) + _, _, err = r.query("PUT", "/projects/"+url.PathEscape(name), project, ETag) if err != nil { return err } @@ -122,7 +121,7 @@ func (r *ProtocolLXD) RenameProject(name string, project api.ProjectPost) (Opera } // Send the request - op, _, err := r.queryOperation("POST", fmt.Sprintf("/projects/%s", url.PathEscape(name)), project, "", true) + op, _, err := r.queryOperation("POST", "/projects/"+url.PathEscape(name), project, "", true) if err != nil { return nil, err } @@ -138,7 +137,7 @@ func (r *ProtocolLXD) DeleteProject(name string) error { } // Send the request - _, _, err = r.query("DELETE", fmt.Sprintf("/projects/%s", url.PathEscape(name)), nil, "") + _, _, err = r.query("DELETE", "/projects/"+url.PathEscape(name), nil, "") if err != nil { return err } From 4707e81e2b9e230a7fe7955d7d56a8c58d4ac617 Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Fri, 31 Jan 2025 11:17:38 -0500 Subject: [PATCH 8/8] lxc/network_peer: avoid fmt.Sprintf() as it is slow Signed-off-by: Simon Deziel --- lxc/network_peer.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lxc/network_peer.go b/lxc/network_peer.go index 6c4c816bd61d..fae545259c54 100644 --- a/lxc/network_peer.go +++ b/lxc/network_peer.go @@ -129,7 +129,7 @@ func (c *cmdNetworkPeerList) run(cmd *cobra.Command, args []string) error { targetPeer := "Unknown" if peer.TargetProject != "" && peer.TargetNetwork != "" { - targetPeer = fmt.Sprintf("%s/%s", peer.TargetProject, peer.TargetNetwork) + targetPeer = peer.TargetProject + "/" + peer.TargetNetwork } details := []string{