Skip to content

Commit

Permalink
feat(plugin): set User-Agent in HTTP requests (cloudnative-pg#6153)
Browse files Browse the repository at this point in the history
Properly set the `User-Agent` header in HTTP requests to the Kubernetes
API server.

Closes cloudnative-pg#6038

Signed-off-by: Jeff Mealo <[email protected]>
Signed-off-by: Marco Nenciarini <[email protected]>
Signed-off-by: Leonardo Cecchi <[email protected]>
Co-authored-by: Marco Nenciarini <[email protected]>
Co-authored-by: Leonardo Cecchi <[email protected]>
(cherry picked from commit 9ca9edc)
  • Loading branch information
jmealo committed Dec 6, 2024
1 parent 6dcfb07 commit dcd3ac1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
4 changes: 4 additions & 0 deletions internal/cmd/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
apiv1 "github.com/cloudnative-pg/cloudnative-pg/api/v1"
"github.com/cloudnative-pg/cloudnative-pg/pkg/specs"
"github.com/cloudnative-pg/cloudnative-pg/pkg/utils"
"github.com/cloudnative-pg/cloudnative-pg/pkg/versions"
)

var (
Expand Down Expand Up @@ -108,11 +109,14 @@ func SetupKubernetesClient(configFlags *genericclioptions.ConfigFlags) error {

func createClient(cfg *rest.Config) error {
var err error

scheme := runtime.NewScheme()
_ = clientgoscheme.AddToScheme(scheme)
_ = apiv1.AddToScheme(scheme)
_ = storagesnapshotv1.AddToScheme(scheme)

cfg.UserAgent = fmt.Sprintf("kubectl-cnpg/v%s (%s)", versions.Version, versions.Info.Commit)

Client, err = client.New(cfg, client.Options{Scheme: scheme})
if err != nil {
return err
Expand Down
7 changes: 7 additions & 0 deletions internal/cmd/plugin/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,22 @@ import (

apiv1 "github.com/cloudnative-pg/cloudnative-pg/api/v1"
"github.com/cloudnative-pg/cloudnative-pg/internal/scheme"
"github.com/cloudnative-pg/cloudnative-pg/pkg/versions"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("create client", func() {
It("with given configuration", func() {
// createClient is not a pure function and as a side effect
// it will:
// - set the Client global variable
// - set the UserAgent field inside cfg
err := createClient(cfg)

Expect(err).NotTo(HaveOccurred())
Expect(cfg.UserAgent).To(Equal("kubectl-cnpg/v" + versions.Version + " (" + versions.Info.Commit + ")"))
Expect(Client).NotTo(BeNil())
})
})
Expand Down

0 comments on commit dcd3ac1

Please sign in to comment.