Skip to content

Commit

Permalink
option,helm: Add a flag enable-k8s-networkpolicy
Browse files Browse the repository at this point in the history
This flag is for Cilium to support K8s NetworkPolicy.
By default the value is true. User can set "enable-k8s-networkpolicy = false" to disable it.

Signed-off-by: Li Chengyuan <chengyuanli@hotmail.com>
  • Loading branch information
ChengyuanLiCY authored and chengyli committed Feb 3, 2023
1 parent af50515 commit 64982b2
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 7 deletions.
4 changes: 4 additions & 0 deletions Documentation/helm-values.rst

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Documentation/spelling_wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,7 @@ runPath
runtime
runtimes
sEventHandover
sNetworkPolicy
sNode
sNodes
sService
Expand Down
4 changes: 4 additions & 0 deletions daemon/cmd/daemon_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -1087,6 +1087,10 @@ func initializeFlags() {
flags.Duration(option.IPAMCiliumNodeUpdateRate, 15*time.Second, "Maximum rate at which the CiliumNode custom resource is updated")
option.BindEnv(Vp, option.IPAMCiliumNodeUpdateRate)

flags.Bool(option.EnableK8sNetworkPolicy, defaults.EnableK8sNetworkPolicy, "Enable support for K8s NetworkPolicy")
flags.MarkHidden(option.EnableK8sNetworkPolicy)
option.BindEnv(Vp, option.EnableK8sNetworkPolicy)

if err := Vp.BindPFlags(flags); err != nil {
log.Fatalf("BindPFlags failed: %s", err)
}
Expand Down
1 change: 1 addition & 0 deletions install/kubernetes/cilium/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ contributors across the globe, there is almost always someone available to help.
| ipv6.enabled | bool | `false` | Enable IPv6 support. |
| ipv6NativeRoutingCIDR | string | `""` | Allows to explicitly specify the IPv6 CIDR for native routing. When specified, Cilium assumes networking for this CIDR is preconfigured and hands traffic destined for that range to the Linux network stack without applying any SNAT. Generally speaking, specifying a native routing CIDR implies that Cilium can depend on the underlying networking stack to route packets to their destination. To offer a concrete example, if Cilium is configured to use direct routing and the Kubernetes CIDR is included in the native routing CIDR, the user must configure the routes to reach pods, either manually or by setting the auto-direct-node-routes flag. |
| k8s | object | `{}` | Configure Kubernetes specific configuration |
| k8sNetworkPolicy.enabled | bool | `true` | Enable support for K8s NetworkPolicy |
| k8sServiceHost | string | `""` | Kubernetes service host |
| k8sServicePort | string | `""` | Kubernetes service port |
| keepDeprecatedLabels | bool | `false` | Keep the deprecated selector labels when deploying Cilium DaemonSet. |
Expand Down
3 changes: 3 additions & 0 deletions install/kubernetes/cilium/templates/cilium-configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,9 @@ data:
{{- if and .Values.endpointRoutes .Values.endpointRoutes.enabled }}
enable-endpoint-routes: {{ .Values.endpointRoutes.enabled | quote }}
{{- end }}
{{- if and .Values.k8sNetworkPolicy .Values.k8sNetworkPolicy.enabled }}
enable-k8s-networkpolicy: {{ .Values.k8sNetworkPolicy.enabled | quote }}
{{- end }}
{{- if .Values.cni.configMap }}
read-cni-conf: {{ .Values.cni.confFileMountPath }}/{{ .Values.cni.configMapKey }}
write-cni-conf-when-ready: {{ .Values.cni.hostConfDirMountPath }}/05-cilium.conflist
Expand Down
4 changes: 4 additions & 0 deletions install/kubernetes/cilium/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,10 @@ endpointRoutes:
# the cilium_host interface.
enabled: false

k8sNetworkPolicy:
# -- Enable support for K8s NetworkPolicy
enabled: true

eni:
# -- Enable Elastic Network Interface (ENI) integration.
enabled: false
Expand Down
4 changes: 4 additions & 0 deletions install/kubernetes/cilium/values.yaml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,10 @@ endpointRoutes:
# the cilium_host interface.
enabled: false

k8sNetworkPolicy:
# -- Enable support for K8s NetworkPolicy
enabled: true

eni:
# -- Enable Elastic Network Interface (ENI) integration.
enabled: false
Expand Down
3 changes: 3 additions & 0 deletions pkg/defaults/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,9 @@ const (

// Enable BGP control plane features.
EnableBGPControlPlane = false

// EnableK8sNetworkPolicy enables support for K8s NetworkPolicy.
EnableK8sNetworkPolicy = true
)

var (
Expand Down
6 changes: 6 additions & 0 deletions pkg/k8s/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ type GatewayAPIConfiguration interface {
K8sGatewayAPIEnabled() bool
}

// PolicyConfiguration is the required configuration for K8s NetworkPolicy
type PolicyConfiguration interface {
// K8sNetworkPolicyEnabled returns true if cilium agent needs to support K8s NetworkPolicy
K8sNetworkPolicyEnabled() bool
}

// GetServiceListOptionsModifier returns the options modifier for service object list.
// This methods returns a ListOptions modifier which adds a label selector to only
// select services that are in context of Cilium.
Expand Down
13 changes: 9 additions & 4 deletions pkg/k8s/watchers/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,10 +432,6 @@ func (k *K8sWatcher) resourceGroups() (beforeNodeInitGroups, afterNodeInitGroups
// with the right service -> backend (k8s endpoints) translation.
K8sAPIGroupServiceV1Core,

// We need all network policies in place before restoring to
// make sure we are enforcing the correct policies for each
// endpoint before restarting.
k8sAPIGroupNetworkingV1Core,
// Namespaces can contain labels which are essential for
// endpoints being restored to have the right identity.
k8sAPIGroupNamespaceV1Core,
Expand All @@ -447,6 +443,14 @@ func (k *K8sWatcher) resourceGroups() (beforeNodeInitGroups, afterNodeInitGroups
k8sAPIGroupNodeV1Core,
}

if k.cfg.K8sNetworkPolicyEnabled() {
// When the flag is set,
// We need all network policies in place before restoring to
// make sure we are enforcing the correct policies for each
// endpoint before restarting.
k8sGroups = append(k8sGroups, k8sAPIGroupNetworkingV1Core)
}

if k.cfg.K8sIngressControllerEnabled() || k.cfg.K8sGatewayAPIEnabled() {
// While Ingress controller is part of operator, we need to watch
// TLS secrets in pre-defined namespace for populating Envoy xDS SDS cache.
Expand Down Expand Up @@ -520,6 +524,7 @@ type WatcherConfiguration interface {
utils.ServiceConfiguration
utils.IngressConfiguration
utils.GatewayAPIConfiguration
utils.PolicyConfiguration
}

// enableK8sWatchers starts watchers for given resources.
Expand Down
4 changes: 4 additions & 0 deletions pkg/k8s/watchers/watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ func (f *fakeWatcherConfiguration) K8sGatewayAPIEnabled() bool {
return false
}

func (f *fakeWatcherConfiguration) K8sNetworkPolicyEnabled() bool {
return true
}

type fakePolicyManager struct {
OnTriggerPolicyUpdates func(force bool, reason string)
OnPolicyAdd func(rules api.Rules, opts *policy.AddOptions) (newRev uint64, err error)
Expand Down
21 changes: 18 additions & 3 deletions pkg/option/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -1130,6 +1130,9 @@ const (
// IPAMCiliumnodeUpdateRate is the maximum rate at which the CiliumNode custom
// resource is updated.
IPAMCiliumNodeUpdateRate = "ipam-cilium-node-update-rate"

// EnableK8sNetworkPolicy enables support for K8s NetworkPolicy.
EnableK8sNetworkPolicy = "enable-k8s-networkpolicy"
)

// Default string arguments
Expand Down Expand Up @@ -2311,6 +2314,9 @@ type DaemonConfig struct {
// IPAMCiliumNodeUpdateRate is the maximum rate at which the CiliumNode custom
// resource is updated.
IPAMCiliumNodeUpdateRate time.Duration

// EnableK8sNetworkPolicy enables support for K8s NetworkPolicy.
EnableK8sNetworkPolicy bool
}

var (
Expand Down Expand Up @@ -2357,9 +2363,10 @@ var (
K8sEnableLeasesFallbackDiscovery: defaults.K8sEnableLeasesFallbackDiscovery,
APIRateLimit: make(map[string]string),

ExternalClusterIP: defaults.ExternalClusterIP,
EnableVTEP: defaults.EnableVTEP,
EnableBGPControlPlane: defaults.EnableBGPControlPlane,
ExternalClusterIP: defaults.ExternalClusterIP,
EnableVTEP: defaults.EnableVTEP,
EnableBGPControlPlane: defaults.EnableBGPControlPlane,
EnableK8sNetworkPolicy: defaults.EnableK8sNetworkPolicy,
}
)

Expand Down Expand Up @@ -2599,6 +2606,11 @@ func (c *DaemonConfig) AgentNotReadyNodeTaintValue() string {
}
}

// K8sNetworkPolicyEnabled returns true if cilium agent needs to support K8s NetworkPolicy, false otherwise.
func (c *DaemonConfig) K8sNetworkPolicyEnabled() bool {
return c.EnableK8sNetworkPolicy
}

// K8sIngressControllerEnabled returns true if ingress controller feature is enabled in Cilium
func (c *DaemonConfig) K8sIngressControllerEnabled() bool {
return c.EnableIngressController
Expand Down Expand Up @@ -3364,6 +3376,9 @@ func (c *DaemonConfig) Populate(vp *viper.Viper) {
}
}
c.EnvoySecretNamespaces = nsList

// To support K8s NetworkPolicy
c.EnableK8sNetworkPolicy = vp.GetBool(EnableK8sNetworkPolicy)
}

func (c *DaemonConfig) additionalMetrics() []string {
Expand Down

0 comments on commit 64982b2

Please sign in to comment.