mirror of
https://github.com/rancher/k3k.git
synced 2026-08-18 20:07:06 +00:00
Services updates (LoadBalancerConfig and NodePortConfig) (#329)
* updates to services - added loadBalancerConfig - removed service-port - added logic to not expose services * Refactor cluster tests to improve readability and maintainability - Simplified service port expectations by directly accessing elements instead of using `ContainElement`. - Enhanced clarity of test assertions for `k3s-server-port` and `k3s-etcd-port` attributes. - Removed redundant code for checking service ports. * fix ports for ingress expose, update kubeconfig generate
This commit is contained in:
@@ -51,7 +51,7 @@ func GenerateBootstrapData(ctx context.Context, cluster *v1alpha1.Cluster, ip, t
|
||||
}
|
||||
|
||||
func requestBootstrap(token, serverIP string) (*ControlRuntimeBootstrap, error) {
|
||||
url := "https://" + serverIP + ":6443/v1-k3s/server-bootstrap"
|
||||
url := "https://" + serverIP + "/v1-k3s/server-bootstrap"
|
||||
|
||||
client := http.Client{
|
||||
Transport: &http.Transport{
|
||||
|
||||
@@ -39,7 +39,7 @@ func (s *Server) Config(init bool, serviceIP string) (*v1.Secret, error) {
|
||||
}
|
||||
|
||||
func serverConfigData(serviceIP string, cluster *v1alpha1.Cluster, token string) string {
|
||||
return "cluster-init: true\nserver: https://" + serviceIP + ":6443\n" + serverOptions(cluster, token)
|
||||
return "cluster-init: true\nserver: https://" + serviceIP + "\n" + serverOptions(cluster, token)
|
||||
}
|
||||
|
||||
func initConfigData(cluster *v1alpha1.Cluster, token string) string {
|
||||
|
||||
@@ -11,9 +11,9 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
servicePort = 443
|
||||
serverPort = 6443
|
||||
etcdPort = 2379
|
||||
httpsPort = 443
|
||||
k3sServerPort = 6443
|
||||
etcdPort = 2379
|
||||
)
|
||||
|
||||
func IngressName(clusterName string) string {
|
||||
@@ -64,7 +64,7 @@ func ingressRules(cluster *v1alpha1.Cluster) []networkingv1.IngressRule {
|
||||
Service: &networkingv1.IngressServiceBackend{
|
||||
Name: ServiceName(cluster.Name),
|
||||
Port: networkingv1.ServiceBackendPort{
|
||||
Number: serverPort,
|
||||
Number: httpsPort,
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
@@ -24,8 +24,6 @@ const (
|
||||
serverName = "server"
|
||||
configName = "server-config"
|
||||
initConfigName = "init-server-config"
|
||||
|
||||
ServerPort = 6443
|
||||
)
|
||||
|
||||
// Server
|
||||
|
||||
@@ -19,7 +19,6 @@ func Service(cluster *v1alpha1.Cluster) *v1.Service {
|
||||
Namespace: cluster.Namespace,
|
||||
},
|
||||
Spec: v1.ServiceSpec{
|
||||
Type: v1.ServiceTypeClusterIP,
|
||||
Selector: map[string]string{
|
||||
"cluster": cluster.Name,
|
||||
"role": "server",
|
||||
@@ -28,16 +27,10 @@ func Service(cluster *v1alpha1.Cluster) *v1.Service {
|
||||
}
|
||||
|
||||
k3sServerPort := v1.ServicePort{
|
||||
Name: "k3s-server-port",
|
||||
Protocol: v1.ProtocolTCP,
|
||||
Port: serverPort,
|
||||
}
|
||||
|
||||
k3sServicePort := v1.ServicePort{
|
||||
Name: "k3s-service-port",
|
||||
Name: "k3s-server-port",
|
||||
Protocol: v1.ProtocolTCP,
|
||||
Port: servicePort,
|
||||
TargetPort: intstr.FromInt(serverPort),
|
||||
Port: httpsPort,
|
||||
TargetPort: intstr.FromInt(k3sServerPort),
|
||||
}
|
||||
|
||||
etcdPort := v1.ServicePort{
|
||||
@@ -46,35 +39,90 @@ func Service(cluster *v1alpha1.Cluster) *v1.Service {
|
||||
Port: etcdPort,
|
||||
}
|
||||
|
||||
// If no expose is specified, default to ClusterIP
|
||||
if cluster.Spec.Expose == nil {
|
||||
service.Spec.Type = v1.ServiceTypeClusterIP
|
||||
service.Spec.Ports = append(service.Spec.Ports, k3sServerPort, etcdPort)
|
||||
}
|
||||
|
||||
// If expose is specified, set the type to the appropriate type
|
||||
if cluster.Spec.Expose != nil {
|
||||
nodePortConfig := cluster.Spec.Expose.NodePort
|
||||
if nodePortConfig != nil {
|
||||
expose := cluster.Spec.Expose
|
||||
|
||||
// ingress
|
||||
if expose.Ingress != nil {
|
||||
service.Spec.Type = v1.ServiceTypeClusterIP
|
||||
service.Spec.Ports = append(service.Spec.Ports, k3sServerPort, etcdPort)
|
||||
}
|
||||
|
||||
// loadbalancer
|
||||
if expose.LoadBalancer != nil {
|
||||
service.Spec.Type = v1.ServiceTypeLoadBalancer
|
||||
addLoadBalancerPorts(service, *expose.LoadBalancer, k3sServerPort, etcdPort)
|
||||
}
|
||||
|
||||
// nodeport
|
||||
if expose.NodePort != nil {
|
||||
service.Spec.Type = v1.ServiceTypeNodePort
|
||||
|
||||
if nodePortConfig.ServerPort != nil {
|
||||
k3sServerPort.NodePort = *nodePortConfig.ServerPort
|
||||
}
|
||||
|
||||
if nodePortConfig.ServicePort != nil {
|
||||
k3sServicePort.NodePort = *nodePortConfig.ServicePort
|
||||
}
|
||||
|
||||
if nodePortConfig.ETCDPort != nil {
|
||||
etcdPort.NodePort = *nodePortConfig.ETCDPort
|
||||
}
|
||||
addNodePortPorts(service, *expose.NodePort, k3sServerPort, etcdPort)
|
||||
}
|
||||
}
|
||||
|
||||
service.Spec.Ports = append(
|
||||
service.Spec.Ports,
|
||||
k3sServicePort,
|
||||
etcdPort,
|
||||
k3sServerPort,
|
||||
)
|
||||
|
||||
return service
|
||||
}
|
||||
|
||||
// addLoadBalancerPorts adds the load balancer ports to the service
|
||||
func addLoadBalancerPorts(service *v1.Service, loadbalancerConfig v1alpha1.LoadBalancerConfig, k3sServerPort, etcdPort v1.ServicePort) {
|
||||
// If the server port is not specified, use the default port
|
||||
if loadbalancerConfig.ServerPort == nil {
|
||||
service.Spec.Ports = append(service.Spec.Ports, k3sServerPort)
|
||||
} else if *loadbalancerConfig.ServerPort > 0 {
|
||||
// If the server port is specified, set the port, otherwise the service will not be exposed
|
||||
k3sServerPort.Port = *loadbalancerConfig.ServerPort
|
||||
service.Spec.Ports = append(service.Spec.Ports, k3sServerPort)
|
||||
}
|
||||
|
||||
// If the etcd port is not specified, use the default port
|
||||
if loadbalancerConfig.ETCDPort == nil {
|
||||
service.Spec.Ports = append(service.Spec.Ports, etcdPort)
|
||||
} else if *loadbalancerConfig.ETCDPort > 0 {
|
||||
// If the etcd port is specified, set the port, otherwise the service will not be exposed
|
||||
etcdPort.Port = *loadbalancerConfig.ETCDPort
|
||||
service.Spec.Ports = append(service.Spec.Ports, etcdPort)
|
||||
}
|
||||
}
|
||||
|
||||
// addNodePortPorts adds the node port ports to the service
|
||||
func addNodePortPorts(service *v1.Service, nodePortConfig v1alpha1.NodePortConfig, k3sServerPort, etcdPort v1.ServicePort) {
|
||||
// If the server port is not specified Kubernetes will set the node port to a random port between 30000-32767
|
||||
if nodePortConfig.ServerPort == nil {
|
||||
service.Spec.Ports = append(service.Spec.Ports, k3sServerPort)
|
||||
} else {
|
||||
serverNodePort := *nodePortConfig.ServerPort
|
||||
|
||||
// If the server port is in the range of 30000-32767, set the node port
|
||||
// otherwise the service will not be exposed
|
||||
if serverNodePort >= 30000 && serverNodePort <= 32767 {
|
||||
k3sServerPort.NodePort = serverNodePort
|
||||
service.Spec.Ports = append(service.Spec.Ports, k3sServerPort)
|
||||
}
|
||||
}
|
||||
|
||||
// If the etcd port is not specified Kubernetes will set the node port to a random port between 30000-32767
|
||||
if nodePortConfig.ETCDPort == nil {
|
||||
service.Spec.Ports = append(service.Spec.Ports, etcdPort)
|
||||
} else {
|
||||
etcdNodePort := *nodePortConfig.ETCDPort
|
||||
|
||||
// If the etcd port is in the range of 30000-32767, set the node port
|
||||
// otherwise the service will not be exposed
|
||||
if etcdNodePort >= 30000 && etcdNodePort <= 32767 {
|
||||
etcdPort.NodePort = etcdNodePort
|
||||
service.Spec.Ports = append(service.Spec.Ports, etcdPort)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) StatefulServerService() *v1.Service {
|
||||
return &v1.Service{
|
||||
TypeMeta: metav1.TypeMeta{
|
||||
@@ -94,15 +142,10 @@ func (s *Server) StatefulServerService() *v1.Service {
|
||||
},
|
||||
Ports: []v1.ServicePort{
|
||||
{
|
||||
Name: "k3s-server-port",
|
||||
Protocol: v1.ProtocolTCP,
|
||||
Port: serverPort,
|
||||
},
|
||||
{
|
||||
Name: "k3s-service-port",
|
||||
Name: "k3s-server-port",
|
||||
Protocol: v1.ProtocolTCP,
|
||||
Port: servicePort,
|
||||
TargetPort: intstr.FromInt(serverPort),
|
||||
Port: httpsPort,
|
||||
TargetPort: intstr.FromInt(k3sServerPort),
|
||||
},
|
||||
{
|
||||
Name: "k3s-etcd-port",
|
||||
|
||||
Reference in New Issue
Block a user