Skip to content

Commit 9a0a1f5

Browse files
authored
use IPs within custom service cidr range for registry and seaweedfs services (#238)
* use IPs within custom service cidr range for registry and seaweedfs services * f * factor out ClusterServiceCIDR function
1 parent 9c35586 commit 9a0a1f5

File tree

4 files changed

+48
-9
lines changed

4 files changed

+48
-9
lines changed

controllers/helm.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
ectypes "github.com/replicatedhq/embedded-cluster-kinds/types"
1818
"github.com/replicatedhq/embedded-cluster-operator/pkg/k8sutil"
1919
"github.com/replicatedhq/embedded-cluster-operator/pkg/registry"
20+
"github.com/replicatedhq/embedded-cluster-operator/pkg/util"
2021
)
2122

2223
const DEFAULT_VENDOR_CHART_ORDER = 10
@@ -185,9 +186,16 @@ func updateInfraChartsFromInstall(ctx context.Context, in *v1beta1.Installation,
185186
continue
186187
}
187188

188-
serviceCIDR := k0sv1beta1.DefaultNetwork().ServiceCIDR
189-
if clusterConfig.Spec != nil && clusterConfig.Spec.Network != nil {
190-
serviceCIDR = clusterConfig.Spec.Network.ServiceCIDR
189+
serviceCIDR := util.ClusterServiceCIDR(clusterConfig, in)
190+
registryEndpoint, err := registry.GetRegistryServiceIP(serviceCIDR)
191+
if err != nil {
192+
log.Error(err, "failed to get registry endpoint", "chart", chart.Name)
193+
continue
194+
}
195+
196+
newVals, err := setHelmValue(chart.Values, "service.clusterIP", registryEndpoint)
197+
if err != nil {
198+
log.Error(err, "failed to set helm values service.clusterIP", "chart", chart.Name)
191199
}
192200

193201
seaweedfsS3Endpoint, err := registry.GetSeaweedfsS3Endpoint(serviceCIDR)
@@ -196,9 +204,9 @@ func updateInfraChartsFromInstall(ctx context.Context, in *v1beta1.Installation,
196204
continue
197205
}
198206

199-
newVals, err := setHelmValue(chart.Values, "s3.regionEndpoint", seaweedfsS3Endpoint)
207+
newVals, err = setHelmValue(newVals, "s3.regionEndpoint", seaweedfsS3Endpoint)
200208
if err != nil {
201-
log.Error(err, "failed to set helm values embeddedClusterID", "chart", chart.Name)
209+
log.Error(err, "failed to set helm values s3.regionEndpoint", "chart", chart.Name)
202210
continue
203211
}
204212

controllers/installation_controller.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -701,10 +701,7 @@ func (r *InstallationReconciler) ReconcileRegistry(ctx context.Context, in *v1be
701701
return fmt.Errorf("failed to get cluster config: %w", err)
702702
}
703703

704-
serviceCIDR := k0sv1beta1.DefaultNetwork().ServiceCIDR
705-
if clusterConfig.Spec != nil && clusterConfig.Spec.Network != nil {
706-
serviceCIDR = clusterConfig.Spec.Network.ServiceCIDR
707-
}
704+
serviceCIDR := util.ClusterServiceCIDR(clusterConfig, in)
708705

709706
err := registry.EnsureResources(ctx, in, r.Client, serviceCIDR)
710707
if err != nil {

pkg/registry/registry.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66

77
clusterv1beta1 "github.com/replicatedhq/embedded-cluster-kinds/apis/v1beta1"
8+
"github.com/replicatedhq/embedded-cluster-operator/pkg/util"
89
corev1 "k8s.io/api/core/v1"
910
k8serrors "k8s.io/apimachinery/pkg/api/errors"
1011
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -17,6 +18,10 @@ const (
1718
// registryNamespace is the namespace where the Registry secret is stored.
1819
// This namespace is defined in the chart in the release metadata.
1920
registryNamespace = "registry"
21+
22+
// registryLowerBandIPIndex is the index of the registry service IP in the service CIDR.
23+
// this is shared with the CLI as it is set on initial installation as well.
24+
registryLowerBandIPIndex = 10
2025
)
2126

2227
func EnsureResources(ctx context.Context, in *clusterv1beta1.Installation, cli client.Client, serviceCIDR string) error {
@@ -63,6 +68,14 @@ func RegistryNamespace() string {
6368
return registryNamespace
6469
}
6570

71+
func GetRegistryServiceIP(serviceCIDR string) (string, error) {
72+
ip, err := util.GetLowerBandIP(serviceCIDR, registryLowerBandIPIndex)
73+
if err != nil {
74+
return "", fmt.Errorf("get lower band ip at index %d: %w", registryLowerBandIPIndex, err)
75+
}
76+
return ip.String(), nil
77+
}
78+
6679
func ensureRegistryNamespace(ctx context.Context, cli client.Client) error {
6780
obj := &corev1.Namespace{
6881
ObjectMeta: metav1.ObjectMeta{Name: registryNamespace},

pkg/util/cidr.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package util
2+
3+
import (
4+
"github.com/k0sproject/k0s/pkg/apis/k0s/v1beta1"
5+
v1beta12 "github.com/replicatedhq/embedded-cluster-kinds/apis/v1beta1"
6+
)
7+
8+
// ClusterServiceCIDR determines the service CIDR for the cluster
9+
// if there is no custom service CIDR, return the default
10+
// if the service CIDR is set in the cluster config, use that - unless
11+
// if the service CIDR is set in the installation, use that because occasionally the cluster config is incorrect
12+
func ClusterServiceCIDR(clusterConfig v1beta1.ClusterConfig, in *v1beta12.Installation) string {
13+
serviceCIDR := v1beta1.DefaultNetwork().ServiceCIDR
14+
if clusterConfig.Spec != nil && clusterConfig.Spec.Network != nil {
15+
serviceCIDR = clusterConfig.Spec.Network.ServiceCIDR
16+
}
17+
if in.Spec.Network != nil && in.Spec.Network.ServiceCIDR != "" {
18+
serviceCIDR = in.Spec.Network.ServiceCIDR
19+
}
20+
return serviceCIDR
21+
}

0 commit comments

Comments
 (0)