Skip to content
This repository was archived by the owner on Aug 12, 2025. It is now read-only.

Commit 13a5c30

Browse files
authored
Merge pull request #372 from kubernetes-sigs/user-agent-2
✨ Add User-Agent support for Equinix Metal Calls
2 parents 3ccd2f3 + 4d336e8 commit 13a5c30

File tree

5 files changed

+86
-3
lines changed

5 files changed

+86
-3
lines changed

cmd/ci-clean/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import (
2727
"github.com/packethost/packngo"
2828
"github.com/spf13/cobra"
2929
kerrors "k8s.io/apimachinery/pkg/util/errors"
30+
31+
"sigs.k8s.io/cluster-api-provider-packet/version"
3032
)
3133

3234
const (
@@ -64,6 +66,7 @@ func main() {
6466

6567
func cleanup(metalAuthToken, metalProjectID string) error {
6668
metalClient := packngo.NewClientWithAuth("capp-e2e", metalAuthToken, nil)
69+
metalClient.UserAgent = fmt.Sprintf("capp-e2e/%s %s", version.Get(), metalClient.UserAgent)
6770
listOpts := &packngo.ListOptions{}
6871
var errs []error
6972

pkg/cloud/packet/client.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,13 @@ import (
3333

3434
infrav1 "sigs.k8s.io/cluster-api-provider-packet/api/v1beta1"
3535
"sigs.k8s.io/cluster-api-provider-packet/pkg/cloud/packet/scope"
36+
"sigs.k8s.io/cluster-api-provider-packet/version"
3637
)
3738

3839
const (
3940
apiTokenVarName = "PACKET_API_KEY" //nolint:gosec
4041
clientName = "CAPP-v1beta1"
42+
clientUAFormat = "cluster-api-provider-packet/%s %s"
4143
ipxeOS = "custom_ipxe"
4244
envVarLocalASN = "METAL_LOCAL_ASN"
4345
envVarBGPPass = "METAL_BGP_PASS" //nolint:gosec
@@ -61,7 +63,10 @@ func NewClient(packetAPIKey string) *Client {
6163
token := strings.TrimSpace(packetAPIKey)
6264

6365
if token != "" {
64-
return &Client{packngo.NewClientWithAuth(clientName, token, nil)}
66+
metalClient := &Client{packngo.NewClientWithAuth(clientName, token, nil)}
67+
metalClient.UserAgent = fmt.Sprintf(clientUAFormat, version.Get(), metalClient.UserAgent)
68+
69+
return metalClient
6570
}
6671

6772
return nil
@@ -134,7 +139,7 @@ func (p *Client) NewDevice(ctx context.Context, req CreateDeviceRequest) (*packn
134139
userData = stringWriter.String()
135140

136141
// Allow to override the facility for each PacketMachineTemplate
137-
var facility = req.MachineScope.PacketCluster.Spec.Facility
142+
facility := req.MachineScope.PacketCluster.Spec.Facility
138143
if req.MachineScope.PacketMachine.Spec.Facility != "" {
139144
facility = req.MachineScope.PacketMachine.Spec.Facility
140145
}

test/e2e/common_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build e2e
12
// +build e2e
23

34
/*
@@ -40,6 +41,7 @@ import (
4041
"k8s.io/client-go/tools/clientcmd"
4142
"k8s.io/client-go/tools/clientcmd/api"
4243
"sigs.k8s.io/cluster-api-provider-packet/pkg/cloud/packet"
44+
"sigs.k8s.io/cluster-api-provider-packet/version"
4345
clusterv1old "sigs.k8s.io/cluster-api/api/v1alpha3"
4446
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
4547
"sigs.k8s.io/cluster-api/test/framework"
@@ -208,7 +210,8 @@ func (w *wrappedClusterProxy) Dispose(ctx context.Context) {
208210
metalAuthToken := os.Getenv(AuthTokenEnvVar)
209211
metalProjectID := os.Getenv(ProjectIDEnvVar)
210212
if metalAuthToken != "" && metalProjectID != "" {
211-
metal := packet.NewClient(metalAuthToken)
213+
metal := packet.NewClientWithAuth(clientName, metalAuthToken)
214+
metal.UserAgent = fmt.Sprintf("capp-e2e/%s %s", version.Version, metalClient.UserAgent)
212215

213216
Eventually(func(g Gomega) {
214217
clusterNames := w.clusterNames.UnsortedList()

test/e2e/suite_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build e2e
12
// +build e2e
23

34
/*
@@ -43,6 +44,7 @@ import (
4344
"sigs.k8s.io/cluster-api/util"
4445

4546
"sigs.k8s.io/cluster-api-provider-packet/api/v1beta1"
47+
"sigs.k8s.io/cluster-api-provider-packet/version"
4648
)
4749

4850
const (
@@ -178,6 +180,7 @@ var _ = SynchronizedAfterSuite(func() {
178180
if metalAuthToken != "" && sshKeyID != "" {
179181
By("Cleaning up the generated SSH Key")
180182
metalClient := packngo.NewClientWithAuth("capp-e2e", metalAuthToken, nil)
183+
metalClient.UserAgent = fmt.Sprintf("capp-e2e/%s %s", version.Version, metalClient.UserAgent)
181184
_, err := metalClient.SSHKeys.Delete(sshKeyID)
182185
Expect(err).NotTo(HaveOccurred())
183186
}
@@ -286,6 +289,7 @@ func generateSSHKey() (string, string) {
286289
Expect(err).NotTo(HaveOccurred())
287290

288291
metalClient := packngo.NewClientWithAuth("capp-e2e", metalAuthToken, nil)
292+
metalClient.UserAgent = fmt.Sprintf("capp-e2e/%s %s", version.Version, metalClient.UserAgent)
289293
res, _, err := metalClient.SSHKeys.Create(
290294
&packngo.SSHKeyCreateRequest{
291295
Label: fmt.Sprintf("capp-e2e-%s", util.RandomString(6)),

version/version.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
Copyright 2022 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package version
18+
19+
import (
20+
"fmt"
21+
"runtime"
22+
23+
"github.com/packethost/packngo"
24+
)
25+
26+
var (
27+
gitMajor string // major version, always numeric
28+
gitMinor string // minor version, numeric possibly followed by "+"
29+
gitVersion string // semantic version, derived by build scripts
30+
gitCommit string // sha1 from git, output of $(git rev-parse HEAD)
31+
gitTreeState string // state of git tree, either "clean" or "dirty"
32+
buildDate string // build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ')
33+
)
34+
35+
// Info contains all version-related information.
36+
type Info struct {
37+
Major string `json:"major,omitempty"`
38+
Minor string `json:"minor,omitempty"`
39+
GitVersion string `json:"gitVersion,omitempty"`
40+
GitCommit string `json:"gitCommit,omitempty"`
41+
GitTreeState string `json:"gitTreeState,omitempty"`
42+
BuildDate string `json:"buildDate,omitempty"`
43+
GoVersion string `json:"goVersion,omitempty"`
44+
Compiler string `json:"compiler,omitempty"`
45+
Platform string `json:"platform,omitempty"`
46+
MetalSdkVersion string `json:"metalSdkVersion,omitempty"`
47+
}
48+
49+
// Get returns version info initialized from defaults and the runtime environment.
50+
func Get() Info {
51+
return Info{
52+
Major: gitMajor,
53+
Minor: gitMinor,
54+
GitVersion: gitVersion,
55+
GitCommit: gitCommit,
56+
GitTreeState: gitTreeState,
57+
BuildDate: buildDate,
58+
GoVersion: runtime.Version(),
59+
Compiler: runtime.Compiler,
60+
Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
61+
MetalSdkVersion: packngo.Version,
62+
}
63+
}
64+
65+
// String returns version info in a human-friendly format.
66+
func (info Info) String() string {
67+
return info.GitVersion
68+
}

0 commit comments

Comments
 (0)