2019-02-02 05:09:11 +00:00
|
|
|
package servicelb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-09-27 14:43:22 +00:00
|
|
|
"errors"
|
2019-02-02 05:09:11 +00:00
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
2021-09-27 14:43:22 +00:00
|
|
|
"strings"
|
2019-02-02 05:09:11 +00:00
|
|
|
|
2022-03-02 23:42:57 +00:00
|
|
|
util "github.com/k3s-io/k3s/pkg/util"
|
2022-03-02 23:47:27 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/version"
|
2019-05-09 22:05:51 +00:00
|
|
|
"github.com/rancher/wrangler/pkg/apply"
|
|
|
|
"github.com/rancher/wrangler/pkg/condition"
|
2021-07-03 11:24:58 +00:00
|
|
|
appclient "github.com/rancher/wrangler/pkg/generated/controllers/apps/v1"
|
|
|
|
coreclient "github.com/rancher/wrangler/pkg/generated/controllers/core/v1"
|
2019-05-09 22:05:51 +00:00
|
|
|
"github.com/rancher/wrangler/pkg/objectset"
|
2019-05-26 06:42:09 +00:00
|
|
|
"github.com/rancher/wrangler/pkg/relatedresource"
|
|
|
|
"github.com/rancher/wrangler/pkg/slice"
|
2019-02-02 05:09:11 +00:00
|
|
|
apps "k8s.io/api/apps/v1"
|
|
|
|
core "k8s.io/api/core/v1"
|
2021-09-27 14:43:22 +00:00
|
|
|
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
2019-02-02 05:09:11 +00:00
|
|
|
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/labels"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
|
|
"k8s.io/apimachinery/pkg/util/intstr"
|
|
|
|
"k8s.io/client-go/kubernetes"
|
2019-04-26 02:59:03 +00:00
|
|
|
v1getter "k8s.io/client-go/kubernetes/typed/apps/v1"
|
2019-02-02 05:09:11 +00:00
|
|
|
coregetter "k8s.io/client-go/kubernetes/typed/core/v1"
|
2022-03-02 23:42:57 +00:00
|
|
|
"k8s.io/client-go/tools/record"
|
2021-09-27 14:43:22 +00:00
|
|
|
utilsnet "k8s.io/utils/net"
|
2021-09-17 18:07:44 +00:00
|
|
|
utilpointer "k8s.io/utils/pointer"
|
2019-02-02 05:09:11 +00:00
|
|
|
)
|
|
|
|
|
2020-05-05 22:09:04 +00:00
|
|
|
var (
|
2022-03-01 03:37:53 +00:00
|
|
|
svcNameLabel = "svccontroller." + version.Program + ".cattle.io/svcname"
|
2022-06-09 04:39:03 +00:00
|
|
|
svcNamespaceLabel = "svccontroller." + version.Program + ".cattle.io/svcnamespace"
|
2022-03-01 03:37:53 +00:00
|
|
|
daemonsetNodeLabel = "svccontroller." + version.Program + ".cattle.io/enablelb"
|
|
|
|
daemonsetNodePoolLabel = "svccontroller." + version.Program + ".cattle.io/lbpool"
|
|
|
|
nodeSelectorLabel = "svccontroller." + version.Program + ".cattle.io/nodeselector"
|
2022-04-15 16:41:40 +00:00
|
|
|
DefaultLBImage = "rancher/klipper-lb:v0.3.5"
|
2020-05-05 22:09:04 +00:00
|
|
|
)
|
|
|
|
|
2019-02-02 05:09:11 +00:00
|
|
|
const (
|
2022-06-09 04:39:03 +00:00
|
|
|
Ready = condition.Cond("Ready")
|
|
|
|
ControllerName = "svccontroller"
|
|
|
|
KlipperNamespace = "klipper-lb-system"
|
2019-02-02 05:09:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
trueVal = true
|
|
|
|
)
|
|
|
|
|
2019-05-09 22:05:51 +00:00
|
|
|
func Register(ctx context.Context,
|
|
|
|
kubernetes kubernetes.Interface,
|
|
|
|
apply apply.Apply,
|
|
|
|
daemonSetController appclient.DaemonSetController,
|
|
|
|
deployments appclient.DeploymentController,
|
|
|
|
nodes coreclient.NodeController,
|
|
|
|
pods coreclient.PodController,
|
|
|
|
services coreclient.ServiceController,
|
|
|
|
endpoints coreclient.EndpointsController,
|
|
|
|
enabled, rootless bool) error {
|
2019-02-02 05:09:11 +00:00
|
|
|
h := &handler{
|
2019-05-03 12:51:02 +00:00
|
|
|
rootless: rootless,
|
|
|
|
enabled: enabled,
|
2019-05-09 22:05:51 +00:00
|
|
|
nodeCache: nodes.Cache(),
|
|
|
|
podCache: pods.Cache(),
|
|
|
|
deploymentCache: deployments.Cache(),
|
2022-03-02 23:42:57 +00:00
|
|
|
processor: apply.WithSetID(ControllerName).WithCacheTypes(daemonSetController),
|
|
|
|
serviceCache: services.Cache(),
|
|
|
|
services: kubernetes.CoreV1(),
|
|
|
|
daemonsets: kubernetes.AppsV1(),
|
|
|
|
deployments: kubernetes.AppsV1(),
|
2022-03-23 17:26:21 +00:00
|
|
|
recorder: util.BuildControllerEventRecorder(kubernetes, ControllerName, meta.NamespaceAll),
|
2019-02-02 05:09:11 +00:00
|
|
|
}
|
|
|
|
|
2022-03-02 23:42:57 +00:00
|
|
|
services.OnChange(ctx, ControllerName, h.onChangeService)
|
|
|
|
nodes.OnChange(ctx, ControllerName, h.onChangeNode)
|
|
|
|
relatedresource.Watch(ctx, ControllerName+"-watcher",
|
2019-02-08 04:11:30 +00:00
|
|
|
h.onResourceChange,
|
2019-05-09 22:05:51 +00:00
|
|
|
services,
|
|
|
|
pods,
|
|
|
|
endpoints)
|
2019-02-02 05:09:11 +00:00
|
|
|
|
2022-06-09 04:39:03 +00:00
|
|
|
return createOrDeleteKlipperNamespace(ctx, enabled, kubernetes)
|
2019-02-02 05:09:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type handler struct {
|
2019-05-03 12:51:02 +00:00
|
|
|
rootless bool
|
|
|
|
enabled bool
|
2019-05-09 22:05:51 +00:00
|
|
|
nodeCache coreclient.NodeCache
|
|
|
|
podCache coreclient.PodCache
|
|
|
|
deploymentCache appclient.DeploymentCache
|
|
|
|
processor apply.Apply
|
|
|
|
serviceCache coreclient.ServiceCache
|
2019-05-03 12:51:02 +00:00
|
|
|
services coregetter.ServicesGetter
|
|
|
|
daemonsets v1getter.DaemonSetsGetter
|
|
|
|
deployments v1getter.DeploymentsGetter
|
2022-03-02 23:42:57 +00:00
|
|
|
recorder record.EventRecorder
|
2019-02-02 05:09:11 +00:00
|
|
|
}
|
|
|
|
|
2022-06-09 04:39:03 +00:00
|
|
|
func createOrDeleteKlipperNamespace(ctx context.Context, enabled bool, k8s kubernetes.Interface) error {
|
|
|
|
_, err := k8s.CoreV1().Namespaces().Get(ctx, KlipperNamespace, meta.GetOptions{})
|
|
|
|
if !apierrors.IsNotFound(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if enabled {
|
|
|
|
_, err := k8s.CoreV1().Namespaces().Create(ctx, &core.Namespace{
|
|
|
|
ObjectMeta: meta.ObjectMeta{
|
|
|
|
Name: KlipperNamespace,
|
|
|
|
},
|
|
|
|
}, meta.CreateOptions{})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return k8s.CoreV1().Namespaces().Delete(ctx, KlipperNamespace, meta.DeleteOptions{})
|
|
|
|
}
|
|
|
|
|
2019-05-09 22:05:51 +00:00
|
|
|
func (h *handler) onResourceChange(name, namespace string, obj runtime.Object) ([]relatedresource.Key, error) {
|
2019-02-08 04:11:30 +00:00
|
|
|
if ep, ok := obj.(*core.Endpoints); ok {
|
2019-05-09 22:05:51 +00:00
|
|
|
return []relatedresource.Key{
|
2019-02-08 04:11:30 +00:00
|
|
|
{
|
|
|
|
Name: ep.Name,
|
|
|
|
Namespace: ep.Namespace,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-02-02 05:09:11 +00:00
|
|
|
pod, ok := obj.(*core.Pod)
|
|
|
|
if !ok {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
serviceName := pod.Labels[svcNameLabel]
|
|
|
|
if serviceName == "" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2022-06-09 04:39:03 +00:00
|
|
|
serviceNamespace := pod.Labels[svcNamespaceLabel]
|
|
|
|
if serviceNamespace == "" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2019-02-02 05:09:11 +00:00
|
|
|
if pod.Status.PodIP == "" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2019-05-09 22:05:51 +00:00
|
|
|
return []relatedresource.Key{
|
2019-02-02 05:09:11 +00:00
|
|
|
{
|
|
|
|
Name: serviceName,
|
2022-06-09 04:39:03 +00:00
|
|
|
Namespace: serviceNamespace,
|
2019-02-02 05:09:11 +00:00
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-02-10 00:26:57 +00:00
|
|
|
// onChangeService handles changes to Services.
|
2019-05-09 22:05:51 +00:00
|
|
|
func (h *handler) onChangeService(key string, svc *core.Service) (*core.Service, error) {
|
|
|
|
if svc == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2019-02-02 05:09:11 +00:00
|
|
|
if err := h.deployPod(svc); err != nil {
|
|
|
|
return svc, err
|
|
|
|
}
|
|
|
|
|
2019-02-28 19:04:02 +00:00
|
|
|
// Don't return service because we don't want another update
|
|
|
|
_, err := h.updateService(svc)
|
|
|
|
return nil, err
|
2019-02-02 05:09:11 +00:00
|
|
|
}
|
|
|
|
|
2021-02-10 00:26:57 +00:00
|
|
|
// onChangeNode handles changes to Nodes. We need to handle this as we may need to kick the DaemonSet
|
|
|
|
// to add or remove pods from nodes if labels have changed.
|
2019-05-09 22:05:51 +00:00
|
|
|
func (h *handler) onChangeNode(key string, node *core.Node) (*core.Node, error) {
|
|
|
|
if node == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2019-04-26 02:59:03 +00:00
|
|
|
if _, ok := node.Labels[daemonsetNodeLabel]; !ok {
|
|
|
|
return node, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := h.updateDaemonSets(); err != nil {
|
|
|
|
return node, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return node, nil
|
|
|
|
}
|
|
|
|
|
2021-02-10 00:26:57 +00:00
|
|
|
// updateService ensures that the Service ingress IP address list is in sync
|
|
|
|
// with the Nodes actually running pods for this service.
|
2019-02-02 05:09:11 +00:00
|
|
|
func (h *handler) updateService(svc *core.Service) (runtime.Object, error) {
|
2019-06-17 23:34:27 +00:00
|
|
|
if !h.enabled {
|
|
|
|
return svc, nil
|
|
|
|
}
|
|
|
|
|
2022-06-09 04:39:03 +00:00
|
|
|
pods, err := h.podCache.List(KlipperNamespace, labels.SelectorFromSet(map[string]string{
|
|
|
|
svcNameLabel: svc.Name,
|
|
|
|
svcNamespaceLabel: svc.Namespace,
|
2019-02-02 05:09:11 +00:00
|
|
|
}))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return svc, err
|
|
|
|
}
|
|
|
|
|
|
|
|
existingIPs := serviceIPs(svc)
|
2021-09-27 14:43:22 +00:00
|
|
|
expectedIPs, err := h.podIPs(pods, svc)
|
2019-02-02 05:09:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return svc, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(expectedIPs)
|
|
|
|
sort.Strings(existingIPs)
|
|
|
|
|
|
|
|
if slice.StringsEqual(expectedIPs, existingIPs) {
|
|
|
|
return svc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
svc = svc.DeepCopy()
|
|
|
|
svc.Status.LoadBalancer.Ingress = nil
|
|
|
|
for _, ip := range expectedIPs {
|
|
|
|
svc.Status.LoadBalancer.Ingress = append(svc.Status.LoadBalancer.Ingress, core.LoadBalancerIngress{
|
|
|
|
IP: ip,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-03-02 23:42:57 +00:00
|
|
|
h.recorder.Eventf(svc, core.EventTypeNormal, "UpdatedIngressIP", "LoadBalancer Ingress IP addresses updated: %s", strings.Join(expectedIPs, ", "))
|
2020-03-26 21:08:47 +00:00
|
|
|
return h.services.Services(svc.Namespace).UpdateStatus(context.TODO(), svc, meta.UpdateOptions{})
|
2019-02-02 05:09:11 +00:00
|
|
|
}
|
|
|
|
|
2021-02-10 00:26:57 +00:00
|
|
|
// serviceIPs returns the list of ingress IP addresses from the Service
|
2019-02-02 05:09:11 +00:00
|
|
|
func serviceIPs(svc *core.Service) []string {
|
|
|
|
var ips []string
|
|
|
|
|
|
|
|
for _, ingress := range svc.Status.LoadBalancer.Ingress {
|
|
|
|
if ingress.IP != "" {
|
|
|
|
ips = append(ips, ingress.IP)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ips
|
|
|
|
}
|
|
|
|
|
2021-02-10 00:26:57 +00:00
|
|
|
// podIPs returns a list of IPs for Nodes hosting ServiceLB Pods.
|
|
|
|
// If at least one node has External IPs available, only external IPs are returned.
|
|
|
|
// If no nodes have External IPs set, the Internal IPs of all nodes running pods are returned.
|
2021-09-27 14:43:22 +00:00
|
|
|
func (h *handler) podIPs(pods []*core.Pod, svc *core.Service) ([]string, error) {
|
2021-02-10 00:26:57 +00:00
|
|
|
// Go doesn't have sets so we stuff things into a map of bools and then get lists of keys
|
|
|
|
// to determine the unique set of IPs in use by pods.
|
|
|
|
extIPs := map[string]bool{}
|
|
|
|
intIPs := map[string]bool{}
|
2019-02-02 05:09:11 +00:00
|
|
|
|
|
|
|
for _, pod := range pods {
|
|
|
|
if pod.Spec.NodeName == "" || pod.Status.PodIP == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !Ready.IsTrue(pod) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-05-09 22:05:51 +00:00
|
|
|
node, err := h.nodeCache.Get(pod.Spec.NodeName)
|
2021-09-27 14:43:22 +00:00
|
|
|
if apierrors.IsNotFound(err) {
|
2019-02-02 05:09:11 +00:00
|
|
|
continue
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, addr := range node.Status.Addresses {
|
2019-10-15 21:17:26 +00:00
|
|
|
if addr.Type == core.NodeExternalIP {
|
2021-02-10 00:26:57 +00:00
|
|
|
extIPs[addr.Address] = true
|
|
|
|
} else if addr.Type == core.NodeInternalIP {
|
|
|
|
intIPs[addr.Address] = true
|
2019-02-02 05:09:11 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-10 00:26:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
keys := func(addrs map[string]bool) (ips []string) {
|
|
|
|
for k := range addrs {
|
|
|
|
ips = append(ips, k)
|
2019-10-15 21:17:26 +00:00
|
|
|
}
|
2021-02-10 00:26:57 +00:00
|
|
|
return ips
|
2019-02-02 05:09:11 +00:00
|
|
|
}
|
|
|
|
|
2021-02-10 00:26:57 +00:00
|
|
|
var ips []string
|
|
|
|
if len(extIPs) > 0 {
|
|
|
|
ips = keys(extIPs)
|
|
|
|
} else {
|
|
|
|
ips = keys(intIPs)
|
2019-02-02 05:09:11 +00:00
|
|
|
}
|
2019-03-08 22:47:44 +00:00
|
|
|
|
2021-09-27 14:43:22 +00:00
|
|
|
ips, err := filterByIPFamily(ips, svc)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-02-10 00:26:57 +00:00
|
|
|
if len(ips) > 0 && h.rootless {
|
2019-03-08 22:47:44 +00:00
|
|
|
return []string{"127.0.0.1"}, nil
|
|
|
|
}
|
|
|
|
|
2021-02-10 00:26:57 +00:00
|
|
|
return ips, nil
|
2019-02-02 05:09:11 +00:00
|
|
|
}
|
|
|
|
|
2021-09-27 14:43:22 +00:00
|
|
|
// filterByIPFamily filters ips based on dual-stack parameters of the service
|
|
|
|
func filterByIPFamily(ips []string, svc *core.Service) ([]string, error) {
|
|
|
|
|
|
|
|
var ipv4Addresses []string
|
|
|
|
var ipv6Addresses []string
|
|
|
|
|
|
|
|
for _, ip := range ips {
|
|
|
|
if utilsnet.IsIPv4String(ip) {
|
|
|
|
ipv4Addresses = append(ipv4Addresses, ip)
|
|
|
|
}
|
|
|
|
if utilsnet.IsIPv6String(ip) {
|
|
|
|
ipv6Addresses = append(ipv6Addresses, ip)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch *svc.Spec.IPFamilyPolicy {
|
|
|
|
case core.IPFamilyPolicySingleStack:
|
|
|
|
if svc.Spec.IPFamilies[0] == core.IPv4Protocol {
|
|
|
|
return ipv4Addresses, nil
|
|
|
|
}
|
|
|
|
if svc.Spec.IPFamilies[0] == core.IPv6Protocol {
|
|
|
|
return ipv6Addresses, nil
|
|
|
|
}
|
|
|
|
case core.IPFamilyPolicyPreferDualStack:
|
|
|
|
if svc.Spec.IPFamilies[0] == core.IPv4Protocol {
|
|
|
|
ipAddresses := append(ipv4Addresses, ipv6Addresses...)
|
|
|
|
return ipAddresses, nil
|
|
|
|
}
|
|
|
|
if svc.Spec.IPFamilies[0] == core.IPv6Protocol {
|
|
|
|
ipAddresses := append(ipv6Addresses, ipv4Addresses...)
|
|
|
|
return ipAddresses, nil
|
|
|
|
}
|
|
|
|
case core.IPFamilyPolicyRequireDualStack:
|
|
|
|
if (len(ipv4Addresses) == 0) || (len(ipv6Addresses) == 0) {
|
|
|
|
return nil, errors.New("one or more IP families did not have addresses available for service with ipFamilyPolicy=RequireDualStack")
|
|
|
|
}
|
|
|
|
if svc.Spec.IPFamilies[0] == core.IPv4Protocol {
|
|
|
|
ipAddresses := append(ipv4Addresses, ipv6Addresses...)
|
|
|
|
return ipAddresses, nil
|
|
|
|
}
|
|
|
|
if svc.Spec.IPFamilies[0] == core.IPv6Protocol {
|
|
|
|
ipAddresses := append(ipv6Addresses, ipv4Addresses...)
|
|
|
|
return ipAddresses, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, errors.New("unhandled ipFamilyPolicy")
|
|
|
|
}
|
|
|
|
|
2022-03-02 23:42:57 +00:00
|
|
|
// deployPod ensures that there is a DaemonSet for the service.
|
2021-02-10 00:26:57 +00:00
|
|
|
// It also ensures that any legacy Deployments from older versions of ServiceLB are deleted.
|
2019-02-02 05:09:11 +00:00
|
|
|
func (h *handler) deployPod(svc *core.Service) error {
|
2019-05-03 12:51:02 +00:00
|
|
|
if err := h.deleteOldDeployments(svc); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-02 05:09:11 +00:00
|
|
|
objs := objectset.NewObjectSet()
|
2022-06-09 04:39:03 +00:00
|
|
|
if !h.enabled || svc.Spec.Type != core.ServiceTypeLoadBalancer || svc.Spec.ClusterIP == "" || svc.Spec.ClusterIP == "None" {
|
2019-05-09 22:05:51 +00:00
|
|
|
return h.processor.WithOwner(svc).Apply(objs)
|
2019-02-02 05:09:11 +00:00
|
|
|
}
|
|
|
|
|
2019-04-26 02:59:03 +00:00
|
|
|
ds, err := h.newDaemonSet(svc)
|
2019-02-02 05:09:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-04-26 02:59:03 +00:00
|
|
|
if ds != nil {
|
|
|
|
objs.Add(ds)
|
2022-03-02 23:42:57 +00:00
|
|
|
h.recorder.Eventf(svc, core.EventTypeNormal, "AppliedDaemonSet", "Applied LoadBalancer DaemonSet %s/%s", ds.Namespace, ds.Name)
|
2019-02-02 05:09:11 +00:00
|
|
|
}
|
2019-05-09 22:05:51 +00:00
|
|
|
return h.processor.WithOwner(svc).Apply(objs)
|
2019-02-02 05:09:11 +00:00
|
|
|
}
|
|
|
|
|
2021-02-10 00:26:57 +00:00
|
|
|
// newDaemonSet creates a DaemonSet to ensure that ServiceLB pods are run on
|
|
|
|
// each eligible node.
|
2019-04-26 02:59:03 +00:00
|
|
|
func (h *handler) newDaemonSet(svc *core.Service) (*apps.DaemonSet, error) {
|
2022-06-09 04:39:03 +00:00
|
|
|
name := fmt.Sprintf("svclb-%s-%s", svc.Name, svc.UID[:8])
|
2019-02-05 04:41:47 +00:00
|
|
|
oneInt := intstr.FromInt(1)
|
|
|
|
|
2021-11-09 15:44:34 +00:00
|
|
|
// If ipv6 is present, we must enable ipv6 forwarding in the manifest
|
|
|
|
var ipv6Switch bool
|
|
|
|
for _, ipFamily := range svc.Spec.IPFamilies {
|
|
|
|
if ipFamily == core.IPv6Protocol {
|
|
|
|
ipv6Switch = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-26 02:59:03 +00:00
|
|
|
ds := &apps.DaemonSet{
|
2019-02-02 05:09:11 +00:00
|
|
|
ObjectMeta: meta.ObjectMeta{
|
|
|
|
Name: name,
|
2022-06-09 04:39:03 +00:00
|
|
|
Namespace: KlipperNamespace,
|
2019-04-26 02:59:03 +00:00
|
|
|
Labels: map[string]string{
|
|
|
|
nodeSelectorLabel: "false",
|
|
|
|
},
|
2019-02-02 05:09:11 +00:00
|
|
|
},
|
|
|
|
TypeMeta: meta.TypeMeta{
|
2019-04-26 02:59:03 +00:00
|
|
|
Kind: "DaemonSet",
|
2019-02-02 05:09:11 +00:00
|
|
|
APIVersion: "apps/v1",
|
|
|
|
},
|
2019-04-26 02:59:03 +00:00
|
|
|
Spec: apps.DaemonSetSpec{
|
2019-02-02 05:09:11 +00:00
|
|
|
Selector: &meta.LabelSelector{
|
|
|
|
MatchLabels: map[string]string{
|
|
|
|
"app": name,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Template: core.PodTemplateSpec{
|
|
|
|
ObjectMeta: meta.ObjectMeta{
|
|
|
|
Labels: map[string]string{
|
2022-06-09 04:39:03 +00:00
|
|
|
"app": name,
|
|
|
|
svcNameLabel: svc.Name,
|
|
|
|
svcNamespaceLabel: svc.Namespace,
|
2019-02-02 05:09:11 +00:00
|
|
|
},
|
|
|
|
},
|
2021-09-17 18:07:44 +00:00
|
|
|
Spec: core.PodSpec{
|
|
|
|
AutomountServiceAccountToken: utilpointer.Bool(false),
|
|
|
|
},
|
2019-02-02 05:09:11 +00:00
|
|
|
},
|
2019-04-26 02:59:03 +00:00
|
|
|
UpdateStrategy: apps.DaemonSetUpdateStrategy{
|
|
|
|
Type: apps.RollingUpdateDaemonSetStrategyType,
|
|
|
|
RollingUpdate: &apps.RollingUpdateDaemonSet{
|
2019-02-05 04:41:47 +00:00
|
|
|
MaxUnavailable: &oneInt,
|
2019-02-02 05:09:11 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-11-09 15:44:34 +00:00
|
|
|
if ipv6Switch {
|
|
|
|
// Add security context to enable ipv6 forwarding
|
|
|
|
securityContext := &core.PodSecurityContext{
|
|
|
|
Sysctls: []core.Sysctl{
|
|
|
|
{
|
|
|
|
Name: "net.ipv6.conf.all.forwarding",
|
|
|
|
Value: "1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
ds.Spec.Template.Spec.SecurityContext = securityContext
|
|
|
|
}
|
|
|
|
|
2019-03-14 18:53:00 +00:00
|
|
|
for _, port := range svc.Spec.Ports {
|
2022-03-02 23:42:57 +00:00
|
|
|
portName := fmt.Sprintf("lb-%s-%d", strings.ToLower(string(port.Protocol)), port.Port)
|
2019-02-02 05:09:11 +00:00
|
|
|
container := core.Container{
|
2019-02-08 04:13:26 +00:00
|
|
|
Name: portName,
|
2021-05-10 22:58:41 +00:00
|
|
|
Image: DefaultLBImage,
|
2019-02-02 05:09:11 +00:00
|
|
|
ImagePullPolicy: core.PullIfNotPresent,
|
|
|
|
Ports: []core.ContainerPort{
|
|
|
|
{
|
2019-02-08 04:13:26 +00:00
|
|
|
Name: portName,
|
2019-02-02 05:09:11 +00:00
|
|
|
ContainerPort: port.Port,
|
|
|
|
HostPort: port.Port,
|
2019-12-11 17:34:11 +00:00
|
|
|
Protocol: port.Protocol,
|
2019-02-02 05:09:11 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Env: []core.EnvVar{
|
|
|
|
{
|
|
|
|
Name: "SRC_PORT",
|
|
|
|
Value: strconv.Itoa(int(port.Port)),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "DEST_PROTO",
|
|
|
|
Value: string(port.Protocol),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "DEST_PORT",
|
2019-02-08 04:13:26 +00:00
|
|
|
Value: strconv.Itoa(int(port.Port)),
|
2019-02-02 05:09:11 +00:00
|
|
|
},
|
|
|
|
{
|
2021-09-27 14:43:22 +00:00
|
|
|
Name: "DEST_IPS",
|
|
|
|
Value: strings.Join(svc.Spec.ClusterIPs, " "),
|
2019-02-02 05:09:11 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
SecurityContext: &core.SecurityContext{
|
|
|
|
Capabilities: &core.Capabilities{
|
|
|
|
Add: []core.Capability{
|
|
|
|
"NET_ADMIN",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-04-26 02:59:03 +00:00
|
|
|
ds.Spec.Template.Spec.Containers = append(ds.Spec.Template.Spec.Containers, container)
|
|
|
|
}
|
2020-01-08 21:09:20 +00:00
|
|
|
|
|
|
|
// Add toleration to noderole.kubernetes.io/master=*:NoSchedule
|
2020-12-02 19:54:13 +00:00
|
|
|
masterToleration := core.Toleration{
|
2020-06-11 21:39:12 +00:00
|
|
|
Key: "node-role.kubernetes.io/master",
|
2020-01-08 21:09:20 +00:00
|
|
|
Operator: "Exists",
|
|
|
|
Effect: "NoSchedule",
|
|
|
|
}
|
2020-12-02 19:54:13 +00:00
|
|
|
ds.Spec.Template.Spec.Tolerations = append(ds.Spec.Template.Spec.Tolerations, masterToleration)
|
|
|
|
|
|
|
|
// Add toleration to noderole.kubernetes.io/control-plane=*:NoSchedule
|
|
|
|
controlPlaneToleration := core.Toleration{
|
|
|
|
Key: "node-role.kubernetes.io/control-plane",
|
|
|
|
Operator: "Exists",
|
|
|
|
Effect: "NoSchedule",
|
|
|
|
}
|
|
|
|
ds.Spec.Template.Spec.Tolerations = append(ds.Spec.Template.Spec.Tolerations, controlPlaneToleration)
|
2020-01-08 21:09:20 +00:00
|
|
|
|
|
|
|
// Add toleration to CriticalAddonsOnly
|
|
|
|
criticalAddonsOnlyToleration := core.Toleration{
|
|
|
|
Key: "CriticalAddonsOnly",
|
|
|
|
Operator: "Exists",
|
|
|
|
}
|
|
|
|
ds.Spec.Template.Spec.Tolerations = append(ds.Spec.Template.Spec.Tolerations, criticalAddonsOnlyToleration)
|
|
|
|
|
2019-04-26 02:59:03 +00:00
|
|
|
// Add node selector only if label "svccontroller.k3s.cattle.io/enablelb" exists on the nodes
|
|
|
|
selector, err := labels.Parse(daemonsetNodeLabel)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-05-09 22:05:51 +00:00
|
|
|
nodesWithLabel, err := h.nodeCache.List(selector)
|
2019-04-26 02:59:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(nodesWithLabel) > 0 {
|
|
|
|
ds.Spec.Template.Spec.NodeSelector = map[string]string{
|
|
|
|
daemonsetNodeLabel: "true",
|
|
|
|
}
|
2022-03-01 03:37:53 +00:00
|
|
|
// Add node selector for "svccontroller.k3s.cattle.io/lbpool=<pool>" if service has lbpool label
|
|
|
|
if svc.Labels[daemonsetNodePoolLabel] != "" {
|
|
|
|
ds.Spec.Template.Spec.NodeSelector[daemonsetNodePoolLabel] = svc.Labels[daemonsetNodePoolLabel]
|
|
|
|
}
|
2019-04-26 02:59:03 +00:00
|
|
|
ds.Labels[nodeSelectorLabel] = "true"
|
|
|
|
}
|
|
|
|
return ds, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *handler) updateDaemonSets() error {
|
2020-03-26 21:08:47 +00:00
|
|
|
daemonsets, err := h.daemonsets.DaemonSets("").List(context.TODO(), meta.ListOptions{
|
2019-04-26 02:59:03 +00:00
|
|
|
LabelSelector: nodeSelectorLabel + "=false",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ds := range daemonsets.Items {
|
|
|
|
ds.Spec.Template.Spec.NodeSelector = map[string]string{
|
|
|
|
daemonsetNodeLabel: "true",
|
|
|
|
}
|
|
|
|
ds.Labels[nodeSelectorLabel] = "true"
|
2020-03-26 21:08:47 +00:00
|
|
|
if _, err := h.daemonsets.DaemonSets(ds.Namespace).Update(context.TODO(), &ds, meta.UpdateOptions{}); err != nil {
|
2019-04-26 02:59:03 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-02-02 05:09:11 +00:00
|
|
|
}
|
|
|
|
|
2019-04-26 02:59:03 +00:00
|
|
|
return nil
|
2019-02-02 05:09:11 +00:00
|
|
|
}
|
2019-05-03 12:51:02 +00:00
|
|
|
|
2021-02-10 00:26:57 +00:00
|
|
|
// deleteOldDeployments ensures that there are no legacy Deployments for ServiceLB pods.
|
|
|
|
// ServiceLB used to use Deployments before switching to DaemonSets in 875ba28
|
2019-05-03 12:51:02 +00:00
|
|
|
func (h *handler) deleteOldDeployments(svc *core.Service) error {
|
|
|
|
name := fmt.Sprintf("svclb-%s", svc.Name)
|
|
|
|
if _, err := h.deploymentCache.Get(svc.Namespace, name); err != nil {
|
2021-09-27 14:43:22 +00:00
|
|
|
if apierrors.IsNotFound(err) {
|
2019-05-03 12:51:02 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2020-03-26 21:08:47 +00:00
|
|
|
return h.deployments.Deployments(svc.Namespace).Delete(context.TODO(), name, meta.DeleteOptions{})
|
2019-05-03 12:51:02 +00:00
|
|
|
}
|