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

🌱 ROSA: Add capa agent and version to ocm client config #5320

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
9 changes: 7 additions & 2 deletions pkg/rosa/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ import (

"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud/scope"
"sigs.k8s.io/cluster-api-provider-aws/v2/util/system"
"sigs.k8s.io/cluster-api-provider-aws/v2/version"
)

const (
ocmTokenKey = "ocmToken"
ocmAPIURLKey = "ocmApiUrl"
ocmClientIDKey = "ocmClientID"
ocmClientSecretKey = "ocmClientSecret"
capaAgentName = "CAPA"
)

// NewOCMClient creates a new OCM client.
Expand All @@ -34,7 +36,9 @@ func NewOCMClient(ctx context.Context, rosaScope *scope.ROSAControlPlaneScope) (
}

ocmConfig := ocmcfg.Config{
URL: url,
URL: url,
UserAgent: capaAgentName,
Version: version.Get().GitVersion,
}

if clientID != "" && clientSecret != "" {
Expand Down Expand Up @@ -62,7 +66,8 @@ func newOCMRawConnection(ctx context.Context, rosaScope *scope.ROSAControlPlaneS

connBuilder := sdk.NewConnectionBuilder().
Logger(ocmSdkLogger).
URL(url)
URL(url).
Agent(capaAgentName + "/" + version.Get().GitVersion + " " + sdk.DefaultAgent)

if clientID != "" && clientSecret != "" {
connBuilder.Client(clientID, clientSecret)
Expand Down
18 changes: 15 additions & 3 deletions pkg/rosa/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

. "github.com/onsi/gomega"
sdk "github.com/openshift-online/ocm-sdk-go"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog/v2"
Expand All @@ -15,6 +16,7 @@ import (
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud/scope"
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/logger"
"sigs.k8s.io/cluster-api-provider-aws/v2/util/system"
"sigs.k8s.io/cluster-api-provider-aws/v2/version"
)

func createROSAControlPlaneScopeWithSecrets(cp *rosacontrolplanev1.ROSAControlPlane, secrets ...*corev1.Secret) *scope.ROSAControlPlaneScope {
Expand Down Expand Up @@ -51,26 +53,36 @@ func createSecret(name, namespace, token, url, clientID, clientSecret string) *c
}
}

func createCP(namespace string) *rosacontrolplanev1.ROSAControlPlane {
func createCP(name string, namespace string, credSecretName string) *rosacontrolplanev1.ROSAControlPlane {
return &rosacontrolplanev1.ROSAControlPlane{
Spec: rosacontrolplanev1.RosaControlPlaneSpec{
CredentialsSecretRef: &corev1.LocalObjectReference{
Name: "rosa-creds-secret",
Name: credSecretName,
},
},
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: name,
},
}
}

func TestNewOCMRawConnection(t *testing.T) {
g := NewWithT(t)
wlSecret := createSecret("rosa-hcp-creds-secret", "default", "fake-token", "https://api.stage.openshift.com", "", "")
cp := createCP("rosa-hcp-cp", "default", "rosa-hcp-creds-secret")
rcpScope := createROSAControlPlaneScopeWithSecrets(cp, wlSecret)

conn, _ := newOCMRawConnection(context.Background(), rcpScope)
g.Expect(conn.Agent()).To(Equal(capaAgentName + "/" + version.Get().GitVersion + " " + sdk.DefaultAgent))
}
func TestOcmCredentials(t *testing.T) {
g := NewWithT(t)

wlSecret := createSecret("rosa-creds-secret", "default", "", "url", "client-id", "client-secret")
mgrSecret := createSecret("rosa-creds-secret", system.GetManagerNamespace(), "", "url", "global-client-id", "global-client-secret")

cp := createCP("default")
cp := createCP("rosa-cp", "default", "rosa-creds-secret")

// Test that ocmCredentials() prefers workload secret to global and environment secrets
os.Setenv("OCM_API_URL", "env-url")
Expand Down
Loading