Skip to content

Commit

Permalink
fixes k8s-nodeName, hostname mismatch issue (kubearmor#736)
Browse files Browse the repository at this point in the history
  • Loading branch information
nyrahul authored Jun 13, 2022
1 parent 45dd42e commit 8405914
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 0 deletions.
15 changes: 15 additions & 0 deletions KubeArmor/core/kubeUpdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,18 @@ func (dm *KubeArmorDaemon) HandleNodeAnnotations(node *tp.Node) {
}
}

func matchHost(hostName string) bool {
envName := os.Getenv("KUBEARMOR_NODENAME")
if envName != "" {
return envName == hostName
}
nodeName := strings.Split(hostName, ".")[0]
return nodeName == cfg.GlobalCfg.Host
}

// WatchK8sNodes Function
func (dm *KubeArmorDaemon) WatchK8sNodes() {
kg.Printf("GlobalCfg.Host=%s, KUBEARMOR_NODENAME=%s", cfg.GlobalCfg.Host, os.Getenv("KUBEARMOR_NODENAME"))
for {
if resp := K8s.WatchK8sNodes(); resp != nil {
defer resp.Body.Close()
Expand All @@ -94,10 +104,15 @@ func (dm *KubeArmorDaemon) WatchK8sNodes() {

// Kubearmor uses hostname to get the corresponding node information, but there are exceptions.
// For example, the node name on EKS can be of the format <hostname>.<region>.compute.internal
/* Keeping this past code for near-future ref purpose. Jun-13-2022
nodeName := strings.Split(event.Object.ObjectMeta.Name, ".")[0]
if nodeName != cfg.GlobalCfg.Host {
continue
}
*/
if !matchHost(event.Object.ObjectMeta.Name) {
continue
}

node := tp.Node{}

Expand Down
5 changes: 5 additions & 0 deletions deployments/AKS/kubearmor.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ spec:
- -gRPC=32767
- -logPath=/tmp/kubearmor.log
- -enableKubeArmorHostPolicy
env:
- name: KUBEARMOR_NODENAME
valueFrom:
fieldRef:
fieldPath: spec.nodeName
image: kubearmor/kubearmor:stable
imagePullPolicy: Always
livenessProbe:
Expand Down
5 changes: 5 additions & 0 deletions deployments/EKS/kubearmor.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ spec:
- -gRPC=32767
- -logPath=/tmp/kubearmor.log
- -enableKubeArmorHostPolicy
env:
- name: KUBEARMOR_NODENAME
valueFrom:
fieldRef:
fieldPath: spec.nodeName
image: kubearmor/kubearmor:stable
imagePullPolicy: Always
livenessProbe:
Expand Down
5 changes: 5 additions & 0 deletions deployments/GKE/kubearmor.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ spec:
- -gRPC=32767
- -logPath=/tmp/kubearmor.log
- -enableKubeArmorHostPolicy
env:
- name: KUBEARMOR_NODENAME
valueFrom:
fieldRef:
fieldPath: spec.nodeName
image: kubearmor/kubearmor:stable
imagePullPolicy: Always
livenessProbe:
Expand Down
5 changes: 5 additions & 0 deletions deployments/docker/kubearmor.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ spec:
- -gRPC=32767
- -logPath=/tmp/kubearmor.log
- -enableKubeArmorHostPolicy
env:
- name: KUBEARMOR_NODENAME
valueFrom:
fieldRef:
fieldPath: spec.nodeName
image: kubearmor/kubearmor:stable
imagePullPolicy: Always
livenessProbe:
Expand Down
5 changes: 5 additions & 0 deletions deployments/generic/kubearmor.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ spec:
- -gRPC=32767
- -logPath=/tmp/kubearmor.log
- -enableKubeArmorHostPolicy
env:
- name: KUBEARMOR_NODENAME
valueFrom:
fieldRef:
fieldPath: spec.nodeName
image: kubearmor/kubearmor:stable
imagePullPolicy: Always
livenessProbe:
Expand Down
20 changes: 20 additions & 0 deletions deployments/get/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var hostPolicyManagerDeploymentName = "kubearmor-host-policy-manager"
// DaemonSetConfig Structure
type DaemonSetConfig struct {
Args []string
Envs []corev1.EnvVar
VolumeMounts []corev1.VolumeMount
Volumes []corev1.Volume
}
Expand Down Expand Up @@ -76,12 +77,24 @@ var apparmorVol = corev1.Volume{
},
}

var envVar = []corev1.EnvVar{
{
Name: "KUBEARMOR_NODENAME",
ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{
FieldPath: "spec.nodeName",
},
},
},
}

// Environment Specific Daemonset Configuration
var defaultConfigs = map[string]DaemonSetConfig{
"generic": {
Args: []string{
"-enableKubeArmorHostPolicy",
},
Envs: envVar,
VolumeMounts: []corev1.VolumeMount{
hostUsrVolMnt,
apparmorVolMnt,
Expand Down Expand Up @@ -137,6 +150,7 @@ var defaultConfigs = map[string]DaemonSetConfig{
Args: []string{
"-enableKubeArmorHostPolicy",
},
Envs: envVar,
VolumeMounts: []corev1.VolumeMount{
hostUsrVolMnt,
apparmorVolMnt,
Expand Down Expand Up @@ -176,6 +190,7 @@ var defaultConfigs = map[string]DaemonSetConfig{
},
"minikube": {
Args: []string{},
Envs: envVar,
VolumeMounts: []corev1.VolumeMount{
hostUsrVolMnt,
apparmorVolMnt,
Expand Down Expand Up @@ -217,6 +232,7 @@ var defaultConfigs = map[string]DaemonSetConfig{
Args: []string{
"-enableKubeArmorHostPolicy",
},
Envs: envVar,
VolumeMounts: []corev1.VolumeMount{
hostUsrVolMnt,
apparmorVolMnt,
Expand Down Expand Up @@ -258,6 +274,7 @@ var defaultConfigs = map[string]DaemonSetConfig{
Args: []string{
"-enableKubeArmorHostPolicy",
},
Envs: envVar,
VolumeMounts: []corev1.VolumeMount{
hostUsrVolMnt,
apparmorVolMnt,
Expand Down Expand Up @@ -299,6 +316,7 @@ var defaultConfigs = map[string]DaemonSetConfig{
Args: []string{
"-enableKubeArmorHostPolicy",
},
Envs: envVar,
VolumeMounts: []corev1.VolumeMount{
gkeHostUsrVolMnt,
apparmorVolMnt,
Expand Down Expand Up @@ -354,6 +372,7 @@ var defaultConfigs = map[string]DaemonSetConfig{
Args: []string{
"-enableKubeArmorHostPolicy",
},
Envs: envVar,
VolumeMounts: []corev1.VolumeMount{
hostUsrVolMnt,
apparmorVolMnt,
Expand Down Expand Up @@ -409,6 +428,7 @@ var defaultConfigs = map[string]DaemonSetConfig{
Args: []string{
"-enableKubeArmorHostPolicy",
},
Envs: envVar,
VolumeMounts: []corev1.VolumeMount{
hostUsrVolMnt,
apparmorVolMnt,
Expand Down
2 changes: 2 additions & 0 deletions deployments/get/objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ func GenerateDaemonSet(env, namespace string) *appsv1.DaemonSet {
}

args = append(args, defaultConfigs[env].Args...)
envs := defaultConfigs[env].Envs

volumeMounts = append(volumeMounts, defaultConfigs[env].VolumeMounts...)
volumes = append(volumes, defaultConfigs[env].Volumes...)
Expand Down Expand Up @@ -487,6 +488,7 @@ func GenerateDaemonSet(env, namespace string) *appsv1.DaemonSet {
Privileged: &privileged,
},
Args: args,
Env: envs,
Ports: []corev1.ContainerPort{
{
ContainerPort: port,
Expand Down
5 changes: 5 additions & 0 deletions deployments/k3s/kubearmor.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ spec:
- -gRPC=32767
- -logPath=/tmp/kubearmor.log
- -enableKubeArmorHostPolicy
env:
- name: KUBEARMOR_NODENAME
valueFrom:
fieldRef:
fieldPath: spec.nodeName
image: kubearmor/kubearmor:stable
imagePullPolicy: Always
livenessProbe:
Expand Down
5 changes: 5 additions & 0 deletions deployments/microk8s/kubearmor.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ spec:
- -gRPC=32767
- -logPath=/tmp/kubearmor.log
- -enableKubeArmorHostPolicy
env:
- name: KUBEARMOR_NODENAME
valueFrom:
fieldRef:
fieldPath: spec.nodeName
image: kubearmor/kubearmor:stable
imagePullPolicy: Always
livenessProbe:
Expand Down
5 changes: 5 additions & 0 deletions deployments/minikube/kubearmor.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ spec:
- args:
- -gRPC=32767
- -logPath=/tmp/kubearmor.log
env:
- name: KUBEARMOR_NODENAME
valueFrom:
fieldRef:
fieldPath: spec.nodeName
image: kubearmor/kubearmor:stable
imagePullPolicy: Always
livenessProbe:
Expand Down

0 comments on commit 8405914

Please sign in to comment.