mirror of
				https://github.com/optim-enterprises-bv/kubernetes.git
				synced 2025-10-31 18:28:13 +00:00 
			
		
		
		
	apis: add validation for HostUsers
This commit just adds a validation according to KEP-127. We check that only the supported volumes for phase 1 of the KEP are accepted. Signed-off-by: Rodrigo Campos <rodrigoca@microsoft.com>
This commit is contained in:
		| @@ -18277,6 +18277,7 @@ func TestValidateOSFields(t *testing.T) { | ||||
| 		"SecurityContext.HostIPC", | ||||
| 		"SecurityContext.HostNetwork", | ||||
| 		"SecurityContext.HostPID", | ||||
| 		"SecurityContext.HostUsers", | ||||
| 		"SecurityContext.RunAsGroup", | ||||
| 		"SecurityContext.RunAsUser", | ||||
| 		"SecurityContext.SELinuxOptions", | ||||
| @@ -20572,6 +20573,172 @@ func TestValidateNonSpecialIP(t *testing.T) { | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestValidateHostUsers(t *testing.T) { | ||||
| 	falseVar := false | ||||
| 	trueVar := true | ||||
|  | ||||
| 	cases := []struct { | ||||
| 		name    string | ||||
| 		success bool | ||||
| 		spec    *core.PodSpec | ||||
| 	}{ | ||||
| 		{ | ||||
| 			name:    "empty", | ||||
| 			success: true, | ||||
| 			spec:    &core.PodSpec{}, | ||||
| 		}, | ||||
| 		{ | ||||
| 			name:    "hostUsers unset", | ||||
| 			success: true, | ||||
| 			spec: &core.PodSpec{ | ||||
| 				SecurityContext: &core.PodSecurityContext{}, | ||||
| 			}, | ||||
| 		}, | ||||
| 		{ | ||||
| 			name:    "hostUsers=false", | ||||
| 			success: true, | ||||
| 			spec: &core.PodSpec{ | ||||
| 				SecurityContext: &core.PodSecurityContext{ | ||||
| 					HostUsers: &falseVar, | ||||
| 				}, | ||||
| 			}, | ||||
| 		}, | ||||
| 		{ | ||||
| 			name:    "hostUsers=true", | ||||
| 			success: true, | ||||
| 			spec: &core.PodSpec{ | ||||
| 				SecurityContext: &core.PodSecurityContext{ | ||||
| 					HostUsers: &trueVar, | ||||
| 				}, | ||||
| 			}, | ||||
| 		}, | ||||
| 		{ | ||||
| 			name:    "hostUsers=false & volumes", | ||||
| 			success: true, | ||||
| 			spec: &core.PodSpec{ | ||||
| 				SecurityContext: &core.PodSecurityContext{ | ||||
| 					HostUsers: &falseVar, | ||||
| 				}, | ||||
| 				Volumes: []core.Volume{ | ||||
| 					{ | ||||
| 						Name: "configmap", | ||||
| 						VolumeSource: core.VolumeSource{ | ||||
| 							ConfigMap: &core.ConfigMapVolumeSource{ | ||||
| 								LocalObjectReference: core.LocalObjectReference{Name: "configmap"}, | ||||
| 							}, | ||||
| 						}, | ||||
| 					}, | ||||
| 					{ | ||||
| 						Name: "secret", | ||||
| 						VolumeSource: core.VolumeSource{ | ||||
| 							Secret: &core.SecretVolumeSource{ | ||||
| 								SecretName: "secret", | ||||
| 							}, | ||||
| 						}, | ||||
| 					}, | ||||
| 					{ | ||||
| 						Name: "downward-api", | ||||
| 						VolumeSource: core.VolumeSource{ | ||||
| 							DownwardAPI: &core.DownwardAPIVolumeSource{}, | ||||
| 						}, | ||||
| 					}, | ||||
| 					{ | ||||
| 						Name: "proj", | ||||
| 						VolumeSource: core.VolumeSource{ | ||||
| 							Projected: &core.ProjectedVolumeSource{}, | ||||
| 						}, | ||||
| 					}, | ||||
| 					{ | ||||
| 						Name: "empty-dir", | ||||
| 						VolumeSource: core.VolumeSource{ | ||||
| 							EmptyDir: &core.EmptyDirVolumeSource{}, | ||||
| 						}, | ||||
| 					}, | ||||
| 				}, | ||||
| 			}, | ||||
| 		}, | ||||
| 		{ | ||||
| 			name:    "hostUsers=false - unsupported volume", | ||||
| 			success: false, | ||||
| 			spec: &core.PodSpec{ | ||||
| 				SecurityContext: &core.PodSecurityContext{ | ||||
| 					HostUsers: &falseVar, | ||||
| 				}, | ||||
| 				Volumes: []core.Volume{ | ||||
| 					{ | ||||
| 						Name: "host-path", | ||||
| 						VolumeSource: core.VolumeSource{ | ||||
| 							HostPath: &core.HostPathVolumeSource{}, | ||||
| 						}, | ||||
| 					}, | ||||
| 				}, | ||||
| 			}, | ||||
| 		}, | ||||
| 		{ | ||||
| 			// It should ignore unsupported volumes with hostUsers=true. | ||||
| 			name:    "hostUsers=true - unsupported volume", | ||||
| 			success: true, | ||||
| 			spec: &core.PodSpec{ | ||||
| 				SecurityContext: &core.PodSecurityContext{ | ||||
| 					HostUsers: &trueVar, | ||||
| 				}, | ||||
| 				Volumes: []core.Volume{ | ||||
| 					{ | ||||
| 						Name: "host-path", | ||||
| 						VolumeSource: core.VolumeSource{ | ||||
| 							HostPath: &core.HostPathVolumeSource{}, | ||||
| 						}, | ||||
| 					}, | ||||
| 				}, | ||||
| 			}, | ||||
| 		}, | ||||
| 		{ | ||||
| 			name:    "hostUsers=false & HostNetwork", | ||||
| 			success: false, | ||||
| 			spec: &core.PodSpec{ | ||||
| 				SecurityContext: &core.PodSecurityContext{ | ||||
| 					HostUsers:   &falseVar, | ||||
| 					HostNetwork: true, | ||||
| 				}, | ||||
| 			}, | ||||
| 		}, | ||||
| 		{ | ||||
| 			name:    "hostUsers=false & HostPID", | ||||
| 			success: false, | ||||
| 			spec: &core.PodSpec{ | ||||
| 				SecurityContext: &core.PodSecurityContext{ | ||||
| 					HostUsers: &falseVar, | ||||
| 					HostPID:   true, | ||||
| 				}, | ||||
| 			}, | ||||
| 		}, | ||||
| 		{ | ||||
| 			name:    "hostUsers=false & HostIPC", | ||||
| 			success: false, | ||||
| 			spec: &core.PodSpec{ | ||||
| 				SecurityContext: &core.PodSecurityContext{ | ||||
| 					HostUsers: &falseVar, | ||||
| 					HostIPC:   true, | ||||
| 				}, | ||||
| 			}, | ||||
| 		}, | ||||
| 	} | ||||
|  | ||||
| 	for _, tc := range cases { | ||||
| 		t.Run(tc.name, func(t *testing.T) { | ||||
| 			fPath := field.NewPath("spec") | ||||
|  | ||||
| 			allErrs := validateHostUsers(tc.spec, fPath) | ||||
| 			if !tc.success && len(allErrs) == 0 { | ||||
| 				t.Errorf("Unexpected success") | ||||
| 			} | ||||
| 			if tc.success && len(allErrs) != 0 { | ||||
| 				t.Errorf("Unexpected error(s): %v", allErrs) | ||||
| 			} | ||||
| 		}) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestValidateWindowsHostProcessPod(t *testing.T) { | ||||
| 	const containerName = "container" | ||||
| 	falseVar := false | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Rodrigo Campos
					Rodrigo Campos