Skip to content

Instantly share code, notes, and snippets.

@mariomac
Last active April 6, 2022 11:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mariomac/5d7ae51717f5d8a935a69787aa40e896 to your computer and use it in GitHub Desktop.
Save mariomac/5d7ae51717f5d8a935a69787aa40e896 to your computer and use it in GitHub Desktop.
Privileged Netobs Agent WIP

Problem: the privileged pod can be correctly deployed in OpenShift but the netobserv-agent fails in Kubernetes when it tries to remove the memory lock, internally executing the prlimit syscall. The received error is "operation not permitted".

time="2022-04-06T11:04:27Z" level=fatal msg="can't start netobserv-agent"
error="removing mem lock: failed to set memlock rlimit: operation not permitted"

We have tested it in Kind and Minikube. Maybe we should try it in another Vanilla K8s distribution.

# This approach doesn't work. As PodSecurityPolicy are deprecated, the documentation
# recommends just tagging the namespace. But also fails with the same permission error as in the previous case
apiVersion: v1
kind: Namespace
metadata:
name: network-observability
labels:
pod-security.kubernetes.io/enforce: privileged
pod-security.kubernetes.io/audit: privileged
# This doesn't work in K8s to deploy the netobserv-agent (tried only in Kind and Minikube)
# The pod is deployed but crashes due to lack of privileges, as if the "privileged: true" option of the container
# didn't take effect. Also tried adding manual capabilities
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: netobserv-agent
namespace: network-observability
---
kind: PodSecurityPolicy
apiVersion: policy/v1beta1
metadata:
name: netobserv-agent
namespace: network-observability
spec:
privileged: true
hostNetwork: true
allowPrivilegeEscalation: true
defaultAllowPrivilegeEscalation: true
fsGroup:
rule: RunAsAny
runAsUser:
rule: RunAsAny
seLinux:
rule: RunAsAny
supplementalGroups:
rule: RunAsAny
defaultAddCapabilities:
- IPC_LOCK
- SYS_ADMIN
- NET_BIND_SYSTEM
- SYS_RESOURCE
allowedCapabilities:
- IPC_LOCK
- SYS_ADMIN
- NET_BIND_SYSTEM
- SYS_RESOURCE
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: netobserv-agent
namespace: network-observability
rules:
- apiGroups: ['policy']
resources: ['podsecuritypolicies']
verbs: ['use']
resourceNames:
- netobserv-agent
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: netobserv-agent
namespace: network-observability
roleRef:
kind: Role
name: netobserv-agent
apiGroup: rbac.authorization.k8s.io
subjects:
# Authorize all service accounts in a namespace (recommended):
- kind: Group
apiGroup: rbac.authorization.k8s.io
name: system:serviceaccounts:network-obseravbility
- kind: ServiceAccount
name: netobserv-agent
namespace: network-observability
# This works perfectly in Openshift to deploy the netobserv-agent
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: netobserv-agent
---
apiVersion: security.openshift.io/v1
kind: SecurityContextConstraints
metadata:
name: example
allowPrivilegedContainer: true
allowHostDirVolumePlugin: true
allowHostNetwork: true
allowHostPorts: true
runAsUser:
type: RunAsAny
seLinuxContext:
type: RunAsAny
users:
- system:serviceaccount:network-observability:netobserv-agent
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment