Skip to content
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

✨ Add User-Agent support for Equinix Metal Calls #372

Merged
merged 3 commits into from
Jul 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmd/ci-clean/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (
"github.com/packethost/packngo"
"github.com/spf13/cobra"
kerrors "k8s.io/apimachinery/pkg/util/errors"

"sigs.k8s.io/cluster-api-provider-packet/version"
)

const (
Expand Down Expand Up @@ -64,6 +66,7 @@ func main() {

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

Expand Down
9 changes: 7 additions & 2 deletions pkg/cloud/packet/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ import (

infrav1 "sigs.k8s.io/cluster-api-provider-packet/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-packet/pkg/cloud/packet/scope"
"sigs.k8s.io/cluster-api-provider-packet/version"
)

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

if token != "" {
return &Client{packngo.NewClientWithAuth(clientName, token, nil)}
metalClient := &Client{packngo.NewClientWithAuth(clientName, token, nil)}
metalClient.UserAgent = fmt.Sprintf(clientUAFormat, version.Get(), metalClient.UserAgent)

return metalClient
}

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

// Allow to override the facility for each PacketMachineTemplate
var facility = req.MachineScope.PacketCluster.Spec.Facility
facility := req.MachineScope.PacketCluster.Spec.Facility
if req.MachineScope.PacketMachine.Spec.Facility != "" {
facility = req.MachineScope.PacketMachine.Spec.Facility
}
Expand Down
5 changes: 4 additions & 1 deletion test/e2e/common_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build e2e
// +build e2e

/*
Expand Down Expand Up @@ -40,6 +41,7 @@ import (
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/clientcmd/api"
"sigs.k8s.io/cluster-api-provider-packet/pkg/cloud/packet"
"sigs.k8s.io/cluster-api-provider-packet/version"
clusterv1old "sigs.k8s.io/cluster-api/api/v1alpha3"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/test/framework"
Expand Down Expand Up @@ -208,7 +210,8 @@ func (w *wrappedClusterProxy) Dispose(ctx context.Context) {
metalAuthToken := os.Getenv(AuthTokenEnvVar)
metalProjectID := os.Getenv(ProjectIDEnvVar)
if metalAuthToken != "" && metalProjectID != "" {
metal := packet.NewClient(metalAuthToken)
metal := packet.NewClientWithAuth(clientName, metalAuthToken)
metal.UserAgent = fmt.Sprintf("capp-e2e/%s %s", version.Version, metalClient.UserAgent)

Eventually(func(g Gomega) {
clusterNames := w.clusterNames.UnsortedList()
Expand Down
4 changes: 4 additions & 0 deletions test/e2e/suite_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build e2e
// +build e2e

/*
Expand Down Expand Up @@ -43,6 +44,7 @@ import (
"sigs.k8s.io/cluster-api/util"

"sigs.k8s.io/cluster-api-provider-packet/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-packet/version"
)

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

metalClient := packngo.NewClientWithAuth("capp-e2e", metalAuthToken, nil)
metalClient.UserAgent = fmt.Sprintf("capp-e2e/%s %s", version.Version, metalClient.UserAgent)
res, _, err := metalClient.SSHKeys.Create(
&packngo.SSHKeyCreateRequest{
Label: fmt.Sprintf("capp-e2e-%s", util.RandomString(6)),
Expand Down
68 changes: 68 additions & 0 deletions version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
Copyright 2022 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package version

import (
"fmt"
"runtime"

"github.com/packethost/packngo"
)

var (
gitMajor string // major version, always numeric
gitMinor string // minor version, numeric possibly followed by "+"
gitVersion string // semantic version, derived by build scripts
gitCommit string // sha1 from git, output of $(git rev-parse HEAD)
gitTreeState string // state of git tree, either "clean" or "dirty"
buildDate string // build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ')
)

// Info contains all version-related information.
type Info struct {
Major string `json:"major,omitempty"`
Minor string `json:"minor,omitempty"`
GitVersion string `json:"gitVersion,omitempty"`
GitCommit string `json:"gitCommit,omitempty"`
GitTreeState string `json:"gitTreeState,omitempty"`
BuildDate string `json:"buildDate,omitempty"`
GoVersion string `json:"goVersion,omitempty"`
Compiler string `json:"compiler,omitempty"`
Platform string `json:"platform,omitempty"`
MetalSdkVersion string `json:"metalSdkVersion,omitempty"`
}

// Get returns version info initialized from defaults and the runtime environment.
func Get() Info {
return Info{
Major: gitMajor,
Minor: gitMinor,
GitVersion: gitVersion,
GitCommit: gitCommit,
GitTreeState: gitTreeState,
BuildDate: buildDate,
GoVersion: runtime.Version(),
Compiler: runtime.Compiler,
Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
MetalSdkVersion: packngo.Version,
}
}

// String returns version info in a human-friendly format.
func (info Info) String() string {
return info.GitVersion
}