Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make kmesh cni and manage controller consitent during pod enrollment #623

Merged
merged 10 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
124 changes: 7 additions & 117 deletions pkg/cni/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
"context"
"encoding/json"
"fmt"
"strconv"
"strings"

"github.com/cilium/ebpf"
"github.com/containernetworking/cni/pkg/skel"
Expand All @@ -30,14 +28,10 @@
"github.com/containernetworking/cni/pkg/version"
netns "github.com/containernetworking/plugins/pkg/ns"
"github.com/vishvananda/netlink"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8stypes "k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"

"kmesh.net/kmesh/pkg/constants"
"kmesh.net/kmesh/pkg/logger"
"kmesh.net/kmesh/pkg/nets"
"kmesh.net/kmesh/pkg/utils"
)

Expand Down Expand Up @@ -86,100 +80,6 @@
return &cniConf, &k8sCommonArgs, result, nil
}

// checkKmesh checks whether we should enable kmesh for the given pod
func checkKmesh(client kubernetes.Interface, pod *v1.Pod) (bool, error) {
namespace, err := client.CoreV1().Namespaces().Get(context.TODO(), pod.Namespace, metav1.GetOptions{})
if err != nil {
return false, err
}
var enableSidecar bool
injectLabel := namespace.Labels["istio-injection"]
if injectLabel == "enabled" {
enableSidecar = true
}
// According to istio, it support per pod config.
injValue := pod.Annotations["sidecar.istio.io/inject"]
if v, ok := pod.Labels["sidecar.istio.io/inject"]; ok {
injValue = v
}
if inject, err := strconv.ParseBool(injValue); err == nil {
enableSidecar = inject
}

// If sidecar inject enabled, kmesh do not take charge of it.
if enableSidecar {
return false, nil
}

// Exclude istio managed gateway
if gateway, ok := pod.Labels["gateway.istio.io/managed"]; ok {
if strings.EqualFold(gateway, "istio.io-mesh-controller") {
return false, nil
}
}

mode := namespace.Labels[constants.DataPlaneModeLabel]
if strings.EqualFold(mode, constants.DataPlaneModeKmesh) {
return true, nil
}

return false, nil
}

func disableKmeshControl(ns string) error {
if ns == "" {
return nil
}

execFunc := func(netns.NetNS) error {
/*
* Attempt to connect to a special IP address. The
* connection triggers the cgroup/connect4/6 ebpf
* program and records the netns cookie information
* of the current connection. The cookie can be used
* to determine whether the netns is managed by Kmesh.
* ControlCommandIp4/6:930(0x3a2) is "cipher key" for cgroup/connect4/6
* ebpf program disable kmesh control
*/
return nets.TriggerControlCommand(constants.OperDisableControl)
}

if err := netns.WithNetNSPath(ns, execFunc); err != nil {
err = fmt.Errorf("enter ns path :%v, run execFunc failed: %v", ns, err)
return err
}
return nil
}

func enableKmeshControl(ns string) error {
execFunc := func(netns.NetNS) error {
/*
* Attempt to connect to a special IP address. The
* connection triggers the cgroup/connect4/6 ebpf
* program and records the netns cookie information
* of the current connection. The cookie can be used
* to determine whether the netns is managed by Kmesh.
* ControlCommandIp4/6:929(0x3a1) is "cipher key" for cgroup/connect4/6
* ebpf program.
*/
return nets.TriggerControlCommand(constants.OperEnableControl)
}

if err := netns.WithNetNSPath(ns, execFunc); err != nil {
err = fmt.Errorf("enter ns path :%v, run execFunc failed: %v", ns, err)
return err
}
return nil
}

const KmeshRedirection = "kmesh.net/redirection"

var annotationPatch = []byte(fmt.Sprintf(
`{"metadata":{"annotations":{"%s":"%s"}}}`,
KmeshRedirection,
"enabled",
))

func getPrevCniResult(conf *cniConf) (*cniv1.Result, error) {
var err error
if conf.RawPrevResult == nil {
Expand Down Expand Up @@ -228,17 +128,6 @@
return nil
}

func patchKmeshAnnotation(client kubernetes.Interface, pod *v1.Pod) error {
_, err := client.CoreV1().Pods(pod.Namespace).Patch(
context.Background(),
pod.Name,
k8stypes.MergePatchType,
annotationPatch,
metav1.PatchOptions{},
)
return err
}

// if cmdadd failed, then we cannot return failed, do nothing and print pre result
func CmdAdd(args *skel.CmdArgs) error {
var err error
Expand Down Expand Up @@ -271,22 +160,23 @@
return err
}

enableKmesh, err := checkKmesh(client, pod)
namespace, err := client.CoreV1().Namespaces().Get(context.TODO(), pod.Namespace, metav1.GetOptions{})

Check warning on line 163 in pkg/cni/plugin/plugin.go

View check run for this annotation

Codecov / codecov/patch

pkg/cni/plugin/plugin.go#L163

Added line #L163 was not covered by tests
if err != nil {
log.Errorf("failed to check enable kmesh information: %v", err)
return err
return fmt.Errorf("failed to get namespace %s: %v", pod.Namespace, err)

Check warning on line 165 in pkg/cni/plugin/plugin.go

View check run for this annotation

Codecov / codecov/patch

pkg/cni/plugin/plugin.go#L165

Added line #L165 was not covered by tests
}

enableKmesh := utils.ShouldEnroll(pod, namespace)

Check warning on line 168 in pkg/cni/plugin/plugin.go

View check run for this annotation

Codecov / codecov/patch

pkg/cni/plugin/plugin.go#L168

Added line #L168 was not covered by tests

if !enableKmesh {
return types.PrintResult(preResult, cniConf.CNIVersion)
}

if err := enableKmeshControl(args.Netns); err != nil {
if err := utils.HandleKmeshManage(args.Netns, true); err != nil {

Check warning on line 174 in pkg/cni/plugin/plugin.go

View check run for this annotation

Codecov / codecov/patch

pkg/cni/plugin/plugin.go#L174

Added line #L174 was not covered by tests
log.Errorf("failed to enable kmesh control, err is %v", err)
return err
}

if err := patchKmeshAnnotation(client, pod); err != nil {
if err := utils.PatchKmeshRedirectAnnotation(client, pod); err != nil {

Check warning on line 179 in pkg/cni/plugin/plugin.go

View check run for this annotation

Codecov / codecov/patch

pkg/cni/plugin/plugin.go#L179

Added line #L179 was not covered by tests
log.Errorf("failed to annotate kmesh redirection, err is %v", err)
}

Expand Down Expand Up @@ -314,7 +204,7 @@

func CmdDelete(args *skel.CmdArgs) error {
// clean
if err := disableKmeshControl(args.Netns); err != nil {
if err := utils.HandleKmeshManage(args.Netns, false); err != nil {

Check warning on line 207 in pkg/cni/plugin/plugin.go

View check run for this annotation

Codecov / codecov/patch

pkg/cni/plugin/plugin.go#L207

Added line #L207 was not covered by tests
log.Errorf("failed to disable Kmesh control, err: %v", err)
}

Expand Down
119 changes: 0 additions & 119 deletions pkg/cni/plugin/plugin_test.go

This file was deleted.

2 changes: 0 additions & 2 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ const (
// Oper code for control command
OperEnableControl = 929
OperDisableControl = 930
OperEnableBypass = 931
OperDisableByPass = 932

// tail call index in tail call prog map
TailCallConnect4Index = 0
Expand Down
14 changes: 3 additions & 11 deletions pkg/controller/bypass/bypass_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"time"

netns "github.com/containernetworking/plugins/pkg/ns"
"istio.io/api/annotation"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/informers"
Expand All @@ -32,6 +31,7 @@ import (
ns "kmesh.net/kmesh/pkg/controller/netns"
"kmesh.net/kmesh/pkg/logger"
"kmesh.net/kmesh/pkg/utils"
"kmesh.net/kmesh/pkg/utils/istio"
)

var (
Expand Down Expand Up @@ -64,7 +64,7 @@ func NewByPassController(client kubernetes.Interface) *Controller {
log.Errorf("expected *corev1.Pod but got %T", obj)
return
}
if !podHasSidecar(pod) {
if !istio.PodHasSidecar(pod) {
log.Infof("pod %s/%s does not have sidecar injected, skip", pod.GetNamespace(), pod.GetName())
return
}
Expand Down Expand Up @@ -94,7 +94,7 @@ func NewByPassController(client kubernetes.Interface) *Controller {
return
}

if !podHasSidecar(newPod) {
if !istio.PodHasSidecar(newPod) {
log.Debugf("pod %s/%s does not have a sidecar", newPod.GetNamespace(), newPod.GetName())
return
}
Expand Down Expand Up @@ -190,11 +190,3 @@ func deleteIptables(ns string) error {
}
return nil
}

func podHasSidecar(pod *corev1.Pod) bool {
if _, f := pod.GetAnnotations()[annotation.SidecarStatus.Name]; f {
return true
}

return false
}
Loading
Loading