Skip to content

Commit

Permalink
Move DNS + lookup logic to url_lookup.go
Browse files Browse the repository at this point in the history
  • Loading branch information
ammujumdar-bcom committed Oct 21, 2024
1 parent 02d4772 commit 374470e
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"time"

"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -32,9 +31,6 @@ import (
const (
DefaultExpiryTime = time.Second * 120
UUIDLabelKey = "vmoperator.vmware.com/webconsolerequest-uuid"

ProxyAddrServiceName = "kube-apiserver-lb-svc"
ProxyAddrServiceNamespace = "kube-system"
)

// AddToManager adds this package's controller to the provided manager.
Expand Down Expand Up @@ -188,7 +184,7 @@ func (r *Reconciler) ReconcileNormal(ctx *pkgctx.WebConsoleRequestContext) error
ctx.WebConsoleRequest.Status.Response = ticket
ctx.WebConsoleRequest.Status.ExpiryTime = metav1.NewTime(metav1.Now().Add(DefaultExpiryTime))

proxyAddr, err := r.ProxyAddress(ctx)
proxyAddr, err := webconsoleurl.ProxyAddress(ctx, r)
if err != nil {
return err
}
Expand Down Expand Up @@ -222,39 +218,3 @@ func (r *Reconciler) ReconcileOwnerReferences(ctx *pkgctx.WebConsoleRequestConte
ctx.WebConsoleRequest.SetOwnerReferences([]metav1.OwnerReference{ownerRef})
return nil
}

// ProxyAddress first attempts to get the proxy address through the API Server DNS Names.
// If that is unset, though, fall back to using the virtual IP.
func (r *Reconciler) ProxyAddress(ctx *pkgctx.WebConsoleRequestContext) (string, error) {
if !pkgcfg.FromContext(ctx).Features.SimplifiedEnablement {
return r.ProxyAddressFromVirtualIP(ctx)
}

// Attempt to use the API Server DNS Names to get the proxy address.
proxyAddress, err := webconsoleurl.ProxyServiceDNSName(ctx, r)
if err != nil {
return "", fmt.Errorf("failed to get proxy service URL: %w", err)
}

// If no API Server DNS Name exists, fall back to using the virtual IP.
if len(proxyAddress) == 0 {
return r.ProxyAddressFromVirtualIP(ctx)
}

return proxyAddress, nil
}

// ProxyAddressFromVirtualIP retrieves the virtual IP, which will be used as the Proxy Address.
func (r *Reconciler) ProxyAddressFromVirtualIP(ctx *pkgctx.WebConsoleRequestContext) (string, error) {
proxySvc := &corev1.Service{}
proxySvcObjectKey := client.ObjectKey{Name: ProxyAddrServiceName, Namespace: ProxyAddrServiceNamespace}
err := r.Get(ctx, proxySvcObjectKey, proxySvc)
if err != nil {
return "", fmt.Errorf("failed to get proxy address service %s: %w", proxySvcObjectKey, err)
}
if len(proxySvc.Status.LoadBalancer.Ingress) == 0 {
return "", fmt.Errorf("no ingress found for proxy address service %s", proxySvcObjectKey)
}

return proxySvc.Status.LoadBalancer.Ingress[0].IP, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
vmopv1 "github.com/vmware-tanzu/vm-operator/api/v1alpha3"
webconsolerequest "github.com/vmware-tanzu/vm-operator/controllers/virtualmachinewebconsolerequest/v1alpha1"
"github.com/vmware-tanzu/vm-operator/pkg/constants/testlabels"
"github.com/vmware-tanzu/vm-operator/pkg/webconsoleurl"
"github.com/vmware-tanzu/vm-operator/test/builder"
)

Expand Down Expand Up @@ -82,8 +83,8 @@ func intgTestsReconcile() {

proxySvc = &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: webconsolerequest.ProxyAddrServiceName,
Namespace: webconsolerequest.ProxyAddrServiceNamespace,
Name: webconsoleurl.ProxyAddrServiceName,
Namespace: webconsoleurl.ProxyAddrServiceNamespace,
},
Spec: corev1.ServiceSpec{
Type: corev1.ServiceTypeLoadBalancer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ func unitTestsReconcile() {

proxySvc = &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: webconsolerequest.ProxyAddrServiceName,
Namespace: webconsolerequest.ProxyAddrServiceNamespace,
Name: webconsoleurl.ProxyAddrServiceName,
Namespace: webconsoleurl.ProxyAddrServiceNamespace,
},
Status: corev1.ServiceStatus{
LoadBalancer: corev1.LoadBalancerStatus{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"time"

"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -31,9 +30,6 @@ import (
const (
DefaultExpiryTime = time.Second * 120
UUIDLabelKey = "vmoperator.vmware.com/webconsolerequest-uuid"

ProxyAddrServiceName = "kube-apiserver-lb-svc"
ProxyAddrServiceNamespace = "kube-system"
)

// AddToManager adds this package's controller to the provided manager.
Expand Down Expand Up @@ -180,7 +176,7 @@ func (r *Reconciler) ReconcileNormal(ctx *pkgctx.WebConsoleRequestContextV1) err
ctx.WebConsoleRequest.Status.Response = ticket
ctx.WebConsoleRequest.Status.ExpiryTime = metav1.NewTime(metav1.Now().Add(DefaultExpiryTime))

proxyAddr, err := r.ProxyAddress(ctx)
proxyAddr, err := webconsoleurl.ProxyAddress(ctx, r)
if err != nil {
return err
}
Expand Down Expand Up @@ -214,39 +210,3 @@ func (r *Reconciler) ReconcileOwnerReferences(ctx *pkgctx.WebConsoleRequestConte
ctx.WebConsoleRequest.SetOwnerReferences([]metav1.OwnerReference{ownerRef})
return nil
}

// ProxyAddress first attempts to get the proxy address through the API Server DNS Names.
// If that is unset, though, fall back to using the virtual IP.
func (r *Reconciler) ProxyAddress(ctx *pkgctx.WebConsoleRequestContextV1) (string, error) {
if !pkgcfg.FromContext(ctx).Features.SimplifiedEnablement {
return r.ProxyAddressFromVirtualIP(ctx)
}

// Attempt to use the API Server DNS Names to get the proxy address.
proxyAddress, err := webconsoleurl.ProxyServiceDNSName(ctx, r)
if err != nil {
return "", fmt.Errorf("failed to get proxy service URL: %w", err)
}

// If no API Server DNS Name exists, fall back to using the virtual IP.
if len(proxyAddress) == 0 {
return r.ProxyAddressFromVirtualIP(ctx)
}

return proxyAddress, nil
}

// ProxyAddressFromVirtualIP retrieves the virtual IP, which will be used as the Proxy Address.
func (r *Reconciler) ProxyAddressFromVirtualIP(ctx *pkgctx.WebConsoleRequestContextV1) (string, error) {
proxySvc := &corev1.Service{}
proxySvcObjectKey := client.ObjectKey{Name: ProxyAddrServiceName, Namespace: ProxyAddrServiceNamespace}
err := r.Get(ctx, proxySvcObjectKey, proxySvc)
if err != nil {
return "", fmt.Errorf("failed to get proxy address service %s: %w", proxySvcObjectKey, err)
}
if len(proxySvc.Status.LoadBalancer.Ingress) == 0 {
return "", fmt.Errorf("no ingress found for proxy address service %s", proxySvcObjectKey)
}

return proxySvc.Status.LoadBalancer.Ingress[0].IP, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
vmopv1 "github.com/vmware-tanzu/vm-operator/api/v1alpha3"
webconsolerequest "github.com/vmware-tanzu/vm-operator/controllers/virtualmachinewebconsolerequest/v1alpha2"
"github.com/vmware-tanzu/vm-operator/pkg/constants/testlabels"
"github.com/vmware-tanzu/vm-operator/pkg/webconsoleurl"
"github.com/vmware-tanzu/vm-operator/test/builder"
)

Expand Down Expand Up @@ -80,8 +81,8 @@ func intgTestsReconcile() {

proxySvc = &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: webconsolerequest.ProxyAddrServiceName,
Namespace: webconsolerequest.ProxyAddrServiceNamespace,
Name: webconsoleurl.ProxyAddrServiceName,
Namespace: webconsoleurl.ProxyAddrServiceNamespace,
},
Spec: corev1.ServiceSpec{
Type: corev1.ServiceTypeLoadBalancer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ func unitTestsReconcile() {

proxySvc = &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: webconsolerequest.ProxyAddrServiceName,
Namespace: webconsolerequest.ProxyAddrServiceNamespace,
Name: webconsoleurl.ProxyAddrServiceName,
Namespace: webconsoleurl.ProxyAddrServiceNamespace,
},
Status: corev1.ServiceStatus{
LoadBalancer: corev1.LoadBalancerStatus{
Expand Down
42 changes: 42 additions & 0 deletions pkg/webconsoleurl/url_lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,41 @@ import (

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

corev1 "k8s.io/api/core/v1"

appv1a1 "github.com/vmware-tanzu/vm-operator/external/appplatform/api/v1alpha1"
pkgcfg "github.com/vmware-tanzu/vm-operator/pkg/config"
)

const (
SupervisorServiceObjName = "supervisor-env-props"
SupervisorServiceObjNamespace = "vmware-system-supervisor-services"

ProxyAddrServiceName = "kube-apiserver-lb-svc"
ProxyAddrServiceNamespace = "kube-system"
)

// ProxyAddress first attempts to get the proxy address through the API Server DNS Names.
// If that is unset, though, fall back to using the virtual IP.
func ProxyAddress(ctx context.Context, r client.Client) (string, error) {
if !pkgcfg.FromContext(ctx).Features.SimplifiedEnablement {
return ProxyAddressFromVirtualIP(ctx, r)
}

// Attempt to use the API Server DNS Names to get the proxy address.
proxyAddress, err := ProxyServiceDNSName(ctx, r)
if err != nil {
return "", fmt.Errorf("failed to get proxy service URL: %w", err)
}

// If no API Server DNS Name exists, fall back to using the virtual IP.
if len(proxyAddress) == 0 {
return ProxyAddressFromVirtualIP(ctx, r)
}

return proxyAddress, nil
}

// ProxyServiceDNSName retrieves the first API server DNS name using the provided client by
// querying the appplatform CRD, if one exists.
func ProxyServiceDNSName(ctx context.Context, r client.Client) (string, error) {
Expand All @@ -33,3 +60,18 @@ func ProxyServiceDNSName(ctx context.Context, r client.Client) (string, error) {
// Return the first FQDN by default
return proxySvc.Spec.APIServerDNSNames[0], nil
}

// ProxyAddressFromVirtualIP retrieves the virtual IP, which will be used as the Proxy Address.
func ProxyAddressFromVirtualIP(ctx context.Context, r client.Client) (string, error) {
proxySvc := &corev1.Service{}
proxySvcObjectKey := client.ObjectKey{Name: ProxyAddrServiceName, Namespace: ProxyAddrServiceNamespace}
err := r.Get(ctx, proxySvcObjectKey, proxySvc)
if err != nil {
return "", fmt.Errorf("failed to get proxy address service %s: %w", proxySvcObjectKey, err)
}
if len(proxySvc.Status.LoadBalancer.Ingress) == 0 {
return "", fmt.Errorf("no ingress found for proxy address service %s", proxySvcObjectKey)
}

return proxySvc.Status.LoadBalancer.Ingress[0].IP, nil
}

0 comments on commit 374470e

Please sign in to comment.