Skip to content

Commit

Permalink
Merge pull request #487 from qiuwei68/feature_hostports
Browse files Browse the repository at this point in the history
feat: add port selection for api server service
  • Loading branch information
kosmos-robot committed Apr 25, 2024
2 parents 55dd351 + e7c5b74 commit 4287648
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ func (m *HostPortManager) AllocateHostPort(clusterName string) (int32, error) {
//使用临时变量存储原来的cm
oldHostPool := m.HostPortPool

//查询该clusterName是否已经被分配了端口,如果分配了,返回之前分配的端口
for _, cluster := range m.HostPortPool.ClusterPorts {
if cluster.Cluster == clusterName {
return cluster.Port, nil
}
}

for _, port := range m.HostPortPool.PortsPool {
if !m.isPortAllocated(port) {
m.HostPortPool.ClusterPorts = append(m.HostPortPool.ClusterPorts, ClusterPort{Port: port, Cluster: clusterName})
Expand Down
14 changes: 11 additions & 3 deletions pkg/kubenest/controlplane/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,19 @@ import (
"k8s.io/klog/v2"

"github.com/kosmos.io/kosmos/pkg/kubenest/constants"
vcnodecontroller "github.com/kosmos.io/kosmos/pkg/kubenest/controller/virtualcluster.node.controller"
"github.com/kosmos.io/kosmos/pkg/kubenest/manifest/controlplane/apiserver"
"github.com/kosmos.io/kosmos/pkg/kubenest/manifest/controlplane/etcd"
"github.com/kosmos.io/kosmos/pkg/kubenest/util"
)

func EnsureVirtualClusterService(client clientset.Interface, name, namespace string) error {
if err := createServerService(client, name, namespace); err != nil {
func EnsureVirtualClusterService(client clientset.Interface, name, namespace string, manager *vcnodecontroller.HostPortManager) error {
port, err := manager.AllocateHostPort(name)
if err != nil {
return fmt.Errorf("failed to allocate host ip for virtual cluster apiserver, err: %w", err)
}

if err := createServerService(client, name, namespace, port); err != nil {
return fmt.Errorf("failed to create virtual cluster apiserver-service, err: %w", err)
}
return nil
Expand All @@ -47,13 +53,15 @@ func DeleteVirtualClusterService(client clientset.Interface, name, namespace str
return nil
}

func createServerService(client clientset.Interface, name, namespace string) error {
func createServerService(client clientset.Interface, name, namespace string, port int32) error {
apiserverServiceBytes, err := util.ParseTemplate(apiserver.ApiserverService, struct {
ServiceName, Namespace, ServiceType string
ServicePort int32
}{
ServiceName: fmt.Sprintf("%s-%s", name, "apiserver"),
Namespace: namespace,
ServiceType: constants.ApiServerServiceType,
ServicePort: port,
})
if err != nil {
return fmt.Errorf("error when parsing virtualClusterApiserver serive template: %w", err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ metadata:
spec:
ports:
- name: client
port: 443
port: {{ .ServicePort }}
protocol: TCP
targetPort: 5443
targetPort: {{ .ServicePort }}
selector:
virtualCluster-app: apiserver
type: {{ .ServiceType }}
Expand Down
1 change: 1 addition & 0 deletions pkg/kubenest/tasks/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func runVirtualClusterService(r workflow.RunData) error {
data.RemoteClient(),
data.GetName(),
data.GetNamespace(),
data.GetHostPortManager(),
)
if err != nil {
return fmt.Errorf("failed to install virtual cluster service , err: %w", err)
Expand Down

0 comments on commit 4287648

Please sign in to comment.