Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
49 changes: 28 additions & 21 deletions backend/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type Config struct {
Options *options.CompletedOptions

Provider multicluster.Provider
ExternalAddressGenerator kuberesources.ExternalAddreesGeneratorFunc
ExternalAddressGenerator kuberesources.ExternalAddressGeneratorFunc
Manager mcmanager.Manager
Scheme *runtime.Scheme

Expand Down Expand Up @@ -106,10 +106,10 @@ func NewConfig(options *options.CompletedOptions) (*Config, error) {
return nil, fmt.Errorf("error setting up kcp provider: %w", err)
}

config.ExternalAddressGenerator = parseKCPServerURL
config.ExternalAddressGenerator = newKCPExternalAddressGenerator(options.ExternalAddress)
config.Provider = provider
default:
config.ExternalAddressGenerator = kuberesources.DefaultExternalAddreesGenerator
config.ExternalAddressGenerator = kuberesources.NewFixedExternalAddressGenerator(options.ExternalAddress)
config.Provider = nil
}

Expand All @@ -133,26 +133,33 @@ func NewConfig(options *options.CompletedOptions) (*Config, error) {
return config, nil
}

func parseKCPServerURL(ctx context.Context, clusterConfig *rest.Config) (string, error) {
// In kcp case, we are talking via apiexport so clientconfig will be pointing to
// https://192.168.2.166:6443/services/apiexport/2sssrgg8ivlpw0cy/kube-bind.io/clusters/2p0rtkf7b697s6mj
// We need to extract host and /clusters/... part
u, err := url.Parse(clusterConfig.Host)
if err != nil {
return "", err
}
func newKCPExternalAddressGenerator(externalAddress string) kuberesources.ExternalAddressGeneratorFunc {
return func(_ context.Context, clusterConfig *rest.Config) (string, error) {
// In kcp case, we are talking via apiexport so clientconfig will be pointing to
// https://192.168.2.166:6443/services/apiexport/root:org:ws/<apiexport-name>/clusters/2p0rtkf7b697s6mj
// We need to extract host and /clusters/... part
u, err := url.Parse(clusterConfig.Host)
if err != nil {
return "", err
}

// Extract cluster ID from the path
// Path format: /services/apiexport/{export-id}/kube-bind.io/clusters/{cluster-id}
pathParts := strings.Split(strings.Trim(u.Path, "/"), "/")
if len(pathParts) < 5 || pathParts[4] != "clusters" {
return "", fmt.Errorf("invalid apiexport URL format")
}
// Extract cluster ID from the path
// Path format: /services/apiexport/root:org:ws/<apiexport-name>/clusters/{cluster-id}
pathParts := strings.Split(strings.Trim(u.Path, "/"), "/")
if len(pathParts) < 6 || pathParts[4] != "clusters" {
return "", fmt.Errorf("invalid apiexport URL format")
}

clusterID := pathParts[5]
clusterID := pathParts[5]

// Construct new URL with cluster path
u.Path = "/clusters/" + clusterID
// Construct new URL with cluster path
extU, err := url.Parse(externalAddress)
if err != nil {
return "", fmt.Errorf("invalid --external-address: %w", err)
}

return u.String(), nil
extU.Path = "/clusters/" + clusterID

return extU.String(), nil
}
}
4 changes: 2 additions & 2 deletions backend/kubernetes/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type Manager struct {
providerPrettyName string
scope kubebindv1alpha2.InformerScope

externalAddressGenerator kuberesources.ExternalAddreesGeneratorFunc
externalAddressGenerator kuberesources.ExternalAddressGeneratorFunc
externalCA []byte
externalTLSServerName string

Expand All @@ -51,7 +51,7 @@ type Manager struct {
func NewKubernetesManager(
ctx context.Context,
namespacePrefix, providerPrettyName string,
externalAddressGenerator kuberesources.ExternalAddreesGeneratorFunc,
externalAddressGenerator kuberesources.ExternalAddressGeneratorFunc,
scope kubebindv1alpha2.InformerScope,
externalCA []byte,
externalTLSServerName string,
Expand Down
19 changes: 9 additions & 10 deletions backend/kubernetes/resources/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,13 @@ import (
func GenerateKubeconfig(ctx context.Context,
client client.Client,
clusterConfig *rest.Config,
externalAddressGenerator ExternalAddreesGeneratorFunc,
externalAddressGenerator ExternalAddressGeneratorFunc,
externalCA []byte,
externalTLSServerName string,
saSecretName, ns, kubeconfigSecretName string,
) (*corev1.Secret, error) {
logger := klog.FromContext(ctx)

if externalAddressGenerator == nil {
externalAddressGenerator = DefaultExternalAddreesGenerator
}

externalAddress, err := externalAddressGenerator(ctx, clusterConfig)
if err != nil {
return nil, fmt.Errorf("failed to generate external address: %w", err)
Expand Down Expand Up @@ -141,11 +137,14 @@ func GenerateKubeconfig(ctx context.Context,
return &existing, nil
}

// ExternalAddreesGeneratorFunc is a function that generates the external address for a cluster based on the clusterConfig
// ExternalAddressGeneratorFunc is a function that generates the external address for a cluster based on the clusterConfig
// and is dependent on the provider.
type ExternalAddreesGeneratorFunc func(ctx context.Context, clusterConfig *rest.Config) (string, error)
type ExternalAddressGeneratorFunc func(ctx context.Context, clusterConfig *rest.Config) (string, error)

// DefaultExternalAddreesGenerator is the default implementation of the ExternalAddreesGeneratorFunc.
func DefaultExternalAddreesGenerator(ctx context.Context, clusterConfig *rest.Config) (string, error) {
return clusterConfig.Host, nil
// FixedExternalAddressGenerator returns an address generator that ignores its input and always returns
// the same address.
func NewFixedExternalAddressGenerator(address string) ExternalAddressGeneratorFunc {
return func(_ context.Context, _ *rest.Config) (string, error) {
return address, nil
}
}
Loading