Skip to content

Commit 1b88822

Browse files
authored
fix: reporting ingress-based control plane endpoint (#141)
Signed-off-by: Dario Tranchitella <[email protected]>
1 parent 77bbe06 commit 1b88822

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

controllers/kamajicontrolplane_controller.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ func (r *KamajiControlPlaneReconciler) Reconcile(ctx context.Context, req ctrl.R
182182
// Patching the Infrastructure Cluster:
183183
// this will be removed on the upcoming Kamaji Control Plane versions.
184184
TrackConditionType(&conditions, kcpv1alpha1.InfrastructureClusterPatchedConditionType, kcp.Generation, func() error {
185-
err = r.patchCluster(ctx, cluster, tcp.Status.ControlPlaneEndpoint)
185+
err = r.patchCluster(ctx, cluster, &kcp, tcp.Status.ControlPlaneEndpoint)
186186

187187
return err
188188
})

controllers/kamajicontrolplane_controller_cluster_patch.go

+31-12
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"fmt"
1010
"net"
1111
"strconv"
12+
"strings"
1213

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

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

2930
port, pErr := strconv.ParseInt(strPort, 10, 16)
3031
if pErr != nil {
31-
return errors.Wrap(pErr, "cannot convert port to integer")
32+
return "", 0, errors.Wrap(pErr, "cannot convert port to integer")
33+
}
34+
35+
if ingress := controlPlane.Spec.Network.Ingress; ingress != nil {
36+
if len(strings.Split(ingress.Hostname, ":")) == 1 {
37+
ingress.Hostname += ":443"
38+
}
39+
40+
if endpoint, strPort, err = net.SplitHostPort(ingress.Hostname); err != nil {
41+
return "", 0, errors.Wrap(err, "cannot split the Kamaji Ingress hostname host port pair")
42+
}
43+
44+
if port, pErr = strconv.ParseInt(strPort, 10, 64); pErr != nil {
45+
return "", 0, errors.Wrap(pErr, "cannot convert Kamaji Ingress hostname port pair")
46+
}
47+
}
48+
49+
return endpoint, port, nil
50+
}
51+
52+
func (r *KamajiControlPlaneReconciler) patchControlPlaneEndpoint(ctx context.Context, controlPlane *v1alpha1.KamajiControlPlane, hostPort string) error {
53+
endpoint, port, err := r.controlPlaneEndpoint(controlPlane, hostPort)
54+
if err != nil {
55+
return errors.Wrap(err, "cannot retrieve ControlPlaneEndpoint")
3256
}
3357

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

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

58-
endpoint, strPort, err := net.SplitHostPort(hostPort)
59-
if err != nil {
60-
return errors.Wrap(err, "cannot split the Kamaji endpoint host port pair")
61-
}
62-
63-
port, err := strconv.ParseInt(strPort, 10, 64)
82+
endpoint, port, err := r.controlPlaneEndpoint(controlPlane, hostPort)
6483
if err != nil {
65-
return errors.Wrap(err, "cannot convert Kamaji endpoint port pair")
84+
return errors.Wrap(err, "cannot retrieve ControlPlaneEndpoint")
6685
}
6786

6887
switch cluster.Spec.InfrastructureRef.Kind {

0 commit comments

Comments
 (0)