2022-03-02 23:47:27 +00:00
|
|
|
//go:build !windows
|
2020-02-22 18:39:33 +00:00
|
|
|
// +build !windows
|
|
|
|
|
2019-03-08 22:47:44 +00:00
|
|
|
package rootlessports
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
2022-03-02 23:47:27 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/rootless"
|
2021-07-03 08:37:19 +00:00
|
|
|
coreClients "github.com/rancher/wrangler/pkg/generated/controllers/core/v1"
|
2019-03-08 22:47:44 +00:00
|
|
|
"github.com/rootless-containers/rootlesskit/pkg/api/client"
|
|
|
|
"github.com/rootless-containers/rootlesskit/pkg/port"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
v1 "k8s.io/api/core/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/labels"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
all = "_all_"
|
|
|
|
)
|
|
|
|
|
2020-01-22 17:53:24 +00:00
|
|
|
func Register(ctx context.Context, serviceController coreClients.ServiceController, enabled bool, httpsPort int) error {
|
2019-03-08 22:47:44 +00:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
rootlessClient client.Client
|
|
|
|
)
|
|
|
|
|
|
|
|
if rootless.Sock == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < 30; i++ {
|
|
|
|
rootlessClient, err = client.New(rootless.Sock)
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
} else {
|
2020-09-21 16:56:03 +00:00
|
|
|
logrus.Infof("Waiting for rootless API socket %s: %v", rootless.Sock, err)
|
2019-03-08 22:47:44 +00:00
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
h := &handler{
|
2020-01-22 17:53:24 +00:00
|
|
|
enabled: enabled,
|
2019-03-08 22:47:44 +00:00
|
|
|
rootlessClient: rootlessClient,
|
2019-05-09 22:05:51 +00:00
|
|
|
serviceClient: serviceController,
|
|
|
|
serviceCache: serviceController.Cache(),
|
2019-03-08 22:47:44 +00:00
|
|
|
httpsPort: httpsPort,
|
|
|
|
ctx: ctx,
|
|
|
|
}
|
2019-05-09 22:05:51 +00:00
|
|
|
serviceController.OnChange(ctx, "rootlessports", h.serviceChanged)
|
|
|
|
serviceController.Enqueue("", all)
|
2019-03-08 22:47:44 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type handler struct {
|
2020-01-22 17:53:24 +00:00
|
|
|
enabled bool
|
2019-03-08 22:47:44 +00:00
|
|
|
rootlessClient client.Client
|
2019-05-09 22:05:51 +00:00
|
|
|
serviceClient coreClients.ServiceController
|
|
|
|
serviceCache coreClients.ServiceCache
|
2019-03-08 22:47:44 +00:00
|
|
|
httpsPort int
|
|
|
|
ctx context.Context
|
|
|
|
}
|
|
|
|
|
2019-05-09 22:05:51 +00:00
|
|
|
func (h *handler) serviceChanged(key string, svc *v1.Service) (*v1.Service, error) {
|
2019-03-08 22:47:44 +00:00
|
|
|
if key != all {
|
|
|
|
h.serviceClient.Enqueue("", all)
|
|
|
|
return svc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ports, err := h.rootlessClient.PortManager().ListPorts(h.ctx)
|
|
|
|
if err != nil {
|
|
|
|
return svc, err
|
|
|
|
}
|
|
|
|
|
|
|
|
boundPorts := map[int]int{}
|
|
|
|
for _, port := range ports {
|
|
|
|
boundPorts[port.Spec.ParentPort] = port.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
toBindPort, err := h.toBindPorts()
|
|
|
|
if err != nil {
|
|
|
|
return svc, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for bindPort, childBindPort := range toBindPort {
|
|
|
|
if _, ok := boundPorts[bindPort]; ok {
|
|
|
|
logrus.Debugf("Parent port %d to child already bound", bindPort)
|
|
|
|
delete(boundPorts, bindPort)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
status, err := h.rootlessClient.PortManager().AddPort(h.ctx, port.Spec{
|
|
|
|
Proto: "tcp",
|
|
|
|
ParentPort: bindPort,
|
|
|
|
ChildPort: childBindPort,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return svc, err
|
|
|
|
}
|
|
|
|
|
|
|
|
logrus.Infof("Bound parent port %s:%d to child namespace port %d", status.Spec.ParentIP,
|
|
|
|
status.Spec.ParentPort, status.Spec.ChildPort)
|
|
|
|
}
|
|
|
|
|
|
|
|
for bindPort, id := range boundPorts {
|
|
|
|
if err := h.rootlessClient.PortManager().RemovePort(h.ctx, id); err != nil {
|
|
|
|
return svc, err
|
|
|
|
}
|
|
|
|
|
|
|
|
logrus.Infof("Removed parent port %d to child namespace", bindPort)
|
|
|
|
}
|
|
|
|
|
|
|
|
return svc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *handler) toBindPorts() (map[int]int, error) {
|
|
|
|
svcs, err := h.serviceCache.List("", labels.Everything())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
toBindPorts := map[int]int{
|
|
|
|
h.httpsPort: h.httpsPort,
|
|
|
|
}
|
2020-01-22 17:53:24 +00:00
|
|
|
|
|
|
|
if !h.enabled {
|
|
|
|
return toBindPorts, nil
|
|
|
|
}
|
|
|
|
|
2019-03-08 22:47:44 +00:00
|
|
|
for _, svc := range svcs {
|
|
|
|
for _, ingress := range svc.Status.LoadBalancer.Ingress {
|
|
|
|
if ingress.IP == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, port := range svc.Spec.Ports {
|
|
|
|
if port.Protocol != v1.ProtocolTCP {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if port.Port != 0 {
|
|
|
|
if port.Port <= 1024 {
|
|
|
|
toBindPorts[10000+int(port.Port)] = int(port.Port)
|
|
|
|
} else {
|
|
|
|
toBindPorts[int(port.Port)] = int(port.Port)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return toBindPorts, nil
|
|
|
|
}
|