Skip to content

Commit

Permalink
fix: reporting ingress-based control plane endpoint (#141)
Browse files Browse the repository at this point in the history
Signed-off-by: Dario Tranchitella <[email protected]>
  • Loading branch information
prometherion authored Oct 24, 2024
1 parent 77bbe06 commit 1b88822
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
2 changes: 1 addition & 1 deletion controllers/kamajicontrolplane_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func (r *KamajiControlPlaneReconciler) Reconcile(ctx context.Context, req ctrl.R
// Patching the Infrastructure Cluster:
// this will be removed on the upcoming Kamaji Control Plane versions.
TrackConditionType(&conditions, kcpv1alpha1.InfrastructureClusterPatchedConditionType, kcp.Generation, func() error {
err = r.patchCluster(ctx, cluster, tcp.Status.ControlPlaneEndpoint)
err = r.patchCluster(ctx, cluster, &kcp, tcp.Status.ControlPlaneEndpoint)

return err
})
Expand Down
43 changes: 31 additions & 12 deletions controllers/kamajicontrolplane_controller_cluster_patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"net"
"strconv"
"strings"

"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand All @@ -20,15 +21,38 @@ import (
"github.com/clastix/cluster-api-control-plane-provider-kamaji/api/v1alpha1"
)

func (r *KamajiControlPlaneReconciler) patchControlPlaneEndpoint(ctx context.Context, controlPlane *v1alpha1.KamajiControlPlane, hostPort string) error {
endpoint, strPort, err := net.SplitHostPort(hostPort)
func (r *KamajiControlPlaneReconciler) controlPlaneEndpoint(controlPlane *v1alpha1.KamajiControlPlane, statusEndpoint string) (string, int64, error) {
endpoint, strPort, err := net.SplitHostPort(statusEndpoint)
if err != nil {
return errors.Wrap(err, "cannot split the Kamaji endpoint host port pair")
return "", 0, errors.Wrap(err, "cannot split the Kamaji endpoint host port pair")
}

port, pErr := strconv.ParseInt(strPort, 10, 16)
if pErr != nil {
return errors.Wrap(pErr, "cannot convert port to integer")
return "", 0, errors.Wrap(pErr, "cannot convert port to integer")
}

if ingress := controlPlane.Spec.Network.Ingress; ingress != nil {
if len(strings.Split(ingress.Hostname, ":")) == 1 {
ingress.Hostname += ":443"
}

if endpoint, strPort, err = net.SplitHostPort(ingress.Hostname); err != nil {
return "", 0, errors.Wrap(err, "cannot split the Kamaji Ingress hostname host port pair")
}

if port, pErr = strconv.ParseInt(strPort, 10, 64); pErr != nil {
return "", 0, errors.Wrap(pErr, "cannot convert Kamaji Ingress hostname port pair")
}
}

return endpoint, port, nil
}

func (r *KamajiControlPlaneReconciler) patchControlPlaneEndpoint(ctx context.Context, controlPlane *v1alpha1.KamajiControlPlane, hostPort string) error {
endpoint, port, err := r.controlPlaneEndpoint(controlPlane, hostPort)
if err != nil {
return errors.Wrap(err, "cannot retrieve ControlPlaneEndpoint")
}

if err = retry.RetryOnConflict(retry.DefaultRetry, func() error {
Expand All @@ -50,19 +74,14 @@ func (r *KamajiControlPlaneReconciler) patchControlPlaneEndpoint(ctx context.Con
}

//nolint:cyclop
func (r *KamajiControlPlaneReconciler) patchCluster(ctx context.Context, cluster capiv1beta1.Cluster, hostPort string) error {
func (r *KamajiControlPlaneReconciler) patchCluster(ctx context.Context, cluster capiv1beta1.Cluster, controlPlane *v1alpha1.KamajiControlPlane, hostPort string) error {
if cluster.Spec.InfrastructureRef == nil {
return errors.New("capiv1beta1.Cluster has no InfrastructureRef")
}

endpoint, strPort, err := net.SplitHostPort(hostPort)
if err != nil {
return errors.Wrap(err, "cannot split the Kamaji endpoint host port pair")
}

port, err := strconv.ParseInt(strPort, 10, 64)
endpoint, port, err := r.controlPlaneEndpoint(controlPlane, hostPort)
if err != nil {
return errors.Wrap(err, "cannot convert Kamaji endpoint port pair")
return errors.Wrap(err, "cannot retrieve ControlPlaneEndpoint")
}

switch cluster.Spec.InfrastructureRef.Kind {
Expand Down

0 comments on commit 1b88822

Please sign in to comment.