Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"github.com/openshift/hypershift/support/globalconfig"
"github.com/openshift/hypershift/support/k8sutil"

pki "github.com/openshift/hypershift/control-plane-operator/controllers/hostedcontrolplane/pki"

osinv1 "github.com/openshift/api/osin/v1"

corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -72,7 +74,11 @@ func adaptOAuthConfig(cpContext component.WorkloadContext, cfg *osinv1.OsinServe
controlPlaneEndpoint := cpContext.HCP.Status.ControlPlaneEndpoint
cfg.OAuthConfig.MasterURL = masterUrl
cfg.OAuthConfig.MasterPublicURL = masterUrl
cfg.OAuthConfig.LoginURL = fmt.Sprintf("https://%s:%d", controlPlaneEndpoint.Host, controlPlaneEndpoint.Port)
loginHost := controlPlaneEndpoint.Host
if hcp := cpContext.HCP; hcp.Spec.KubeAPIServerDNSName != "" {
loginHost = hcp.Spec.KubeAPIServerDNSName
}
cfg.OAuthConfig.LoginURL = fmt.Sprintf("https://%s:%d", pki.AddBracketsIfIPv6(loginHost), controlPlaneEndpoint.Port)
// loginURLOverride can be used to specify an override for the oauth config login url. The need for this arises
// when the login a provider uses doesn't conform to the standard login url in hypershift. The only supported use case
// for this is IBMCloud Red Hat Openshift
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package oauth

import (
"fmt"
"testing"

. "github.com/onsi/gomega"

hyperv1 "github.com/openshift/hypershift/api/hypershift/v1beta1"
"github.com/openshift/hypershift/control-plane-operator/controllers/hostedcontrolplane/infra"
component "github.com/openshift/hypershift/support/controlplane-component"

osinv1 "github.com/openshift/api/osin/v1"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

func TestAdaptOAuthConfig_LoginURL(t *testing.T) {
const (
lbHost = "10.71.22.118"
dnsName = "api.ocp4.example.com"
oauthHost = "oauth.example.com"
)
var lbPort int32 = 6443

tests := []struct {
name string
kubeAPIServerDNS string
loginURLAnnotation string
expectedLoginURL string
}{
{
name: "no kubeAPIServerDNSName — uses controlPlaneEndpoint host",
kubeAPIServerDNS: "",
expectedLoginURL: fmt.Sprintf("https://%s:%d", lbHost, lbPort),
},
{
name: "kubeAPIServerDNSName set — uses DNS name",
kubeAPIServerDNS: dnsName,
expectedLoginURL: fmt.Sprintf("https://%s:%d", dnsName, lbPort),
},
{
name: "kubeAPIServerDNSName set but annotation override present — annotation wins",
kubeAPIServerDNS: dnsName,
loginURLAnnotation: "https://iam.custom.ibm.com/login",
expectedLoginURL: "https://iam.custom.ibm.com/login",
},
{
name: "only annotation override, no DNS name — annotation used",
kubeAPIServerDNS: "",
loginURLAnnotation: "https://iam.custom.ibm.com/login",
expectedLoginURL: "https://iam.custom.ibm.com/login",
},
}
Comment on lines +28 to +56

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Add a case for the new IPv6 bracketing path.

The production change now routes LoginURL through pki.AddBracketsIfIPv6(...), but this table only exercises IPv4, DNS, and annotation inputs. A regression back to an unbracketed IPv6 URL would still pass here. As per coding guidelines, Unit test any code changes and additions.

Also applies to: 71-74, 99-99

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@control-plane-operator/controllers/hostedcontrolplane/v2/oauth/config_test.go`
around lines 28 - 56, The LoginURL test table in config_test.go does not cover
the new IPv6 bracketing behavior introduced through pki.AddBracketsIfIPv6, so
add a case in the existing table-driven test that uses an IPv6 host and expects
the bracketed https URL. Update the relevant assertions in the login URL test
helper/setup so the new case exercises the same path as the current
kubeAPIServerDNS and annotation scenarios, ensuring the IPv6 formatting is
verified alongside the existing LoginURL logic.

Source: Coding guidelines


for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)

hcp := &hyperv1.HostedControlPlane{
ObjectMeta: metav1.ObjectMeta{
Name: "test-hcp",
Namespace: "test-ns",
},
Spec: hyperv1.HostedControlPlaneSpec{
KubeAPIServerDNSName: tt.kubeAPIServerDNS,
},
Status: hyperv1.HostedControlPlaneStatus{
ControlPlaneEndpoint: hyperv1.APIEndpoint{
Host: lbHost,
Port: lbPort,
},
},
}
if tt.loginURLAnnotation != "" {
hcp.Annotations = map[string]string{
hyperv1.OauthLoginURLOverrideAnnotation: tt.loginURLAnnotation,
}
}

cpContext := component.WorkloadContext{
HCP: hcp,
InfraStatus: infra.InfrastructureStatus{
OAuthHost: oauthHost,
OAuthPort: 443,
APIPort: lbPort,
},
Client: fake.NewClientBuilder().Build(),
}

cfg := &osinv1.OsinServerConfig{
OAuthConfig: osinv1.OAuthConfig{},
}

adaptOAuthConfig(cpContext, cfg)

g.Expect(cfg.OAuthConfig.LoginURL).To(Equal(tt.expectedLoginURL))
g.Expect(cfg.OAuthConfig.MasterURL).To(Equal(fmt.Sprintf("https://%s:%d", oauthHost, 443)))
g.Expect(cfg.OAuthConfig.MasterPublicURL).To(Equal(fmt.Sprintf("https://%s:%d", oauthHost, 443)))
})
}
}