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

Support restricted PSA for worker pods #2410

Merged
merged 7 commits into from
Sep 14, 2022
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
10 changes: 5 additions & 5 deletions cluster-sync/clean.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ OPERATOR_MANIFEST=./_out/manifests/release/cdi-operator.yaml
LABELS=("operator.cdi.kubevirt.io" "cdi.kubevirt.io" "prometheus.cdi.kubevirt.io")
NAMESPACES=(default kube-system cdi)

set +e
_kubectl patch cdi ${CR_NAME} --type=json -p '[{ "op": "remove", "path": "/metadata/finalizers" }]'
set -e

_kubectl get ot --all-namespaces -o=custom-columns=NAME:.metadata.name,NAMESPACE:.metadata.namespace,FINALIZERS:.metadata.finalizers --no-headers | grep objectTransfer | while read p; do
arr=($p)
name="${arr[0]}"
Expand Down Expand Up @@ -52,10 +48,14 @@ if [ -f "${OPERATOR_CR_MANIFEST}" ]; then
echo "Cleaning CR object ..."
if _kubectl get crd cdis.cdi.kubevirt.io ; then
_kubectl delete --ignore-not-found -f "${OPERATOR_CR_MANIFEST}"
_kubectl wait cdis.cdi.kubevirt.io/${CR_NAME} --for=delete | echo "this is fine"
_kubectl wait cdis.cdi.kubevirt.io/${CR_NAME} --for=delete --timeout=30s | echo "this is fine"
fi
fi

set +e
_kubectl patch cdi ${CR_NAME} --type=json -p '[{ "op": "remove", "path": "/metadata/finalizers" }]'
set -e

if [ "${CDI_CLEAN}" == "all" ] && [ -f "${OPERATOR_MANIFEST}" ]; then
echo "Deleting operator ..."
_kubectl delete --ignore-not-found -f "${OPERATOR_MANIFEST}"
Expand Down
1 change: 1 addition & 0 deletions cmd/cdi-cloner/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,6 @@ container_image(
":cdi-cloner",
":cloner_startup.sh",
],
user = "1001",
visibility = ["//visibility:public"],
)
32 changes: 29 additions & 3 deletions cmd/cdi-cloner/clone-source.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,38 @@ func validateMount() {
}

func newTarReader(preallocation bool) (io.ReadCloser, error) {
args := "cv"
excludeMap := map[string]struct{}{
"lost+found": struct{}{},
}

args := []string{"/usr/bin/tar", "cv"}
if !preallocation {
// -S is used to handle sparse files. It can only be used when preallocation is not requested
args = "S" + args
args = append(args, "-S")
}
cmd := exec.Command("/usr/bin/tar", args, ".")

files, err := os.ReadDir(mountPoint)
if err != nil {
return nil, err
}

var tarFiles []string
for _, f := range files {
if _, ok := excludeMap[f.Name()]; ok {
continue
}
tarFiles = append(tarFiles, f.Name())
}

if len(tarFiles) > 0 {
args = append(args, tarFiles...)
} else {
args = append(args, "--files-from", "/dev/null")
}

klog.Infof("Executing %+v", args)

cmd := exec.Command(args[0], args[1:]...)
cmd.Dir = mountPoint

stdout, err := cmd.StdoutPipe()
Expand Down
1 change: 1 addition & 0 deletions cmd/cdi-importer/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ container_image(
"//tools/cdi-image-size-detection",
"//tools/cdi-source-update-poller",
],
user = "1001",
visibility = ["//visibility:public"],
)

Expand Down
1 change: 1 addition & 0 deletions cmd/cdi-uploadserver/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ container_image(
"-alsologtostderr",
],
files = [":cdi-uploadserver"],
user = "1001",
visibility = ["//visibility:public"],
)

Expand Down
23 changes: 3 additions & 20 deletions pkg/controller/clone-controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/record"
"k8s.io/utils/pointer"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/handler"
Expand Down Expand Up @@ -573,7 +572,7 @@ func MakeCloneSourcePodSpec(sourceVolumeMode corev1.PersistentVolumeMode, image,
}
}

preallocationRequested, _ := targetPvc.Annotations[AnnPreallocationRequested]
preallocationRequested := targetPvc.Annotations[AnnPreallocationRequested]

pod := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -592,15 +591,6 @@ func MakeCloneSourcePodSpec(sourceVolumeMode corev1.PersistentVolumeMode, image,
},
},
Spec: corev1.PodSpec{
SecurityContext: &corev1.PodSecurityContext{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! this will fix an issue someone was seeing on OpenShift 4.12 with kubevirt tekton tasks https://github.com/kubevirt/tekton-tasks-operator/blob/main/data/tekton-pipelines/okd/windows10-customize.yaml#L126
I can try to find the chats if needed

RunAsUser: &[]int64{0}[0],
SELinuxOptions: &corev1.SELinuxOptions{
User: "system_u",
Role: "system_r",
Type: "spc_t",
Level: "s0",
},
},
Containers: []corev1.Container{
{
Name: common.ClonerSourcePodName,
Expand Down Expand Up @@ -653,14 +643,6 @@ func MakeCloneSourcePodSpec(sourceVolumeMode corev1.PersistentVolumeMode, image,
Protocol: corev1.ProtocolTCP,
},
},
SecurityContext: &corev1.SecurityContext{
Capabilities: &corev1.Capabilities{
Drop: []corev1.Capability{
"ALL",
},
},
AllowPrivilegeEscalation: pointer.BoolPtr(false),
},
},
},
RestartPolicy: corev1.RestartPolicyOnFailure,
Expand All @@ -670,7 +652,6 @@ func MakeCloneSourcePodSpec(sourceVolumeMode corev1.PersistentVolumeMode, image,
VolumeSource: corev1.VolumeSource{
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{
ClaimName: sourcePvcName,
ReadOnly: true,
},
},
},
Expand Down Expand Up @@ -733,6 +714,7 @@ func MakeCloneSourcePodSpec(sourceVolumeMode corev1.PersistentVolumeMode, image,
{
Name: DataVolName,
MountPath: common.ClonerMountPath,
ReadOnly: true,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, is there a real impact for this? IIUC the other ReadOnly is just about enforcing this one

},
}
addVars = []corev1.EnvVar{
Expand All @@ -749,6 +731,7 @@ func MakeCloneSourcePodSpec(sourceVolumeMode corev1.PersistentVolumeMode, image,

pod.Spec.Containers[0].Env = append(pod.Spec.Containers[0].Env, addVars...)
SetPodPvcAnnotations(pod, targetPvc)
SetRestrictedSecurityContext(&pod.Spec)
return pod
}

Expand Down
6 changes: 0 additions & 6 deletions pkg/controller/clone-controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -840,9 +840,6 @@ func createSourcePod(pvc *corev1.PersistentVolumeClaim, pvcUID string) *corev1.P
},
},
Spec: corev1.PodSpec{
SecurityContext: &corev1.PodSecurityContext{
RunAsUser: &[]int64{0}[0],
},
Containers: []corev1.Container{
{
Name: common.ClonerSourcePodName,
Expand Down Expand Up @@ -929,9 +926,6 @@ func createSourcePod(pvc *corev1.PersistentVolumeClaim, pvcUID string) *corev1.P
Value: common.WriteBlockPath,
},
}
pod.Spec.SecurityContext = &corev1.PodSecurityContext{
RunAsUser: &[]int64{0}[0],
}
} else {
pod.Spec.Containers[0].VolumeMounts = []corev1.VolumeMount{
{
Expand Down
10 changes: 1 addition & 9 deletions pkg/controller/dataimportcron-controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ import (
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/validation"
"k8s.io/client-go/tools/record"
"k8s.io/utils/pointer"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/event"
Expand Down Expand Up @@ -752,14 +751,6 @@ func (r *DataImportCronReconciler) newCronJob(cron *cdiv1.DataImportCron) (*batc
return nil, err
}
container := corev1.Container{
SecurityContext: &corev1.SecurityContext{
Capabilities: &corev1.Capabilities{
Drop: []corev1.Capability{
"ALL",
},
},
AllowPrivilegeEscalation: pointer.BoolPtr(false),
},
Name: "cdi-source-update-poller",
Image: r.image,
Command: []string{
Expand Down Expand Up @@ -873,6 +864,7 @@ func (r *DataImportCronReconciler) newCronJob(cron *cdiv1.DataImportCron) (*batc
if err := sdk.SetLastAppliedConfiguration(cronJob, AnnLastAppliedConfig); err != nil {
return nil, err
}
SetRestrictedSecurityContext(&cronJob.Spec.JobTemplate.Spec.Template.Spec)
return cronJob, nil
}

Expand Down
21 changes: 4 additions & 17 deletions pkg/controller/datavolume-controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ import (
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/record"
"k8s.io/utils/pointer"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
Expand Down Expand Up @@ -1630,14 +1629,6 @@ func (r *DatavolumeReconciler) createExpansionPod(pvc *corev1.PersistentVolumeCl
ImagePullPolicy: corev1.PullPolicy(r.pullPolicy),
Command: []string{"/bin/bash"},
Args: []string{"-c", "echo", "'hello cdi'"},
SecurityContext: &corev1.SecurityContext{
Capabilities: &corev1.Capabilities{
Drop: []corev1.Capability{
"ALL",
},
},
AllowPrivilegeEscalation: pointer.BoolPtr(false),
},
},
},
RestartPolicy: corev1.RestartPolicyOnFailure,
Expand Down Expand Up @@ -1677,6 +1668,8 @@ func (r *DatavolumeReconciler) createExpansionPod(pvc *corev1.PersistentVolumeCl
return nil, err
}

SetRestrictedSecurityContext(&pod.Spec)

if err := r.client.Create(context.TODO(), pod); err != nil {
if !k8serrors.IsAlreadyExists(err) {
return nil, err
Expand Down Expand Up @@ -3149,6 +3142,8 @@ func (r *DatavolumeReconciler) makeSizeDetectionPodSpec(
},
}

SetRestrictedSecurityContext(&pod.Spec)

return pod
}

Expand Down Expand Up @@ -3185,14 +3180,6 @@ func (r *DatavolumeReconciler) makeSizeDetectionContainerSpec(volName string) *c
Name: volName,
},
},
SecurityContext: &corev1.SecurityContext{
Capabilities: &corev1.Capabilities{
Drop: []corev1.Capability{
"ALL",
},
},
AllowPrivilegeEscalation: pointer.BoolPtr(false),
},
}

// Get and assign container's default resource requirements
Expand Down
44 changes: 6 additions & 38 deletions pkg/controller/import-controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/tools/record"
"k8s.io/utils/pointer"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/handler"
Expand Down Expand Up @@ -943,7 +942,7 @@ func createImporterPod(log logr.Logger, client client.Client, args *importerPodA
// makeNodeImporterPodSpec creates and returns the node docker cache based importer pod spec based on the passed-in importImage and pvc.
func makeNodeImporterPodSpec(args *importerPodArgs) *corev1.Pod {
// importer pod name contains the pvc name
podName, _ := args.pvc.Annotations[AnnImportPod]
podName := args.pvc.Annotations[AnnImportPod]

volumes := []corev1.Volume{
{
Expand Down Expand Up @@ -995,14 +994,6 @@ func makeNodeImporterPodSpec(args *importerPodArgs) *corev1.Pod {
Name: "shared-volume",
},
},
SecurityContext: &corev1.SecurityContext{
Capabilities: &corev1.Capabilities{
Drop: []corev1.Capability{
"ALL",
},
},
AllowPrivilegeEscalation: pointer.BoolPtr(false),
},
},
},
Containers: []corev1.Container{
Expand All @@ -1018,14 +1009,6 @@ func makeNodeImporterPodSpec(args *importerPodArgs) *corev1.Pod {
Name: "shared-volume",
},
},
SecurityContext: &corev1.SecurityContext{
Capabilities: &corev1.Capabilities{
Drop: []corev1.Capability{
"ALL",
},
},
AllowPrivilegeEscalation: pointer.BoolPtr(false),
},
},
},
RestartPolicy: corev1.RestartPolicyOnFailure,
Expand Down Expand Up @@ -1072,13 +1055,15 @@ func makeNodeImporterPodSpec(args *importerPodArgs) *corev1.Pod {
Name: "shared-volume",
})

SetRestrictedSecurityContext(&pod.Spec)

return pod
}

// makeImporterPodSpec creates and return the importer pod spec based on the passed-in endpoint, secret and pvc.
func makeImporterPodSpec(args *importerPodArgs) *corev1.Pod {
// importer pod name contains the pvc name
podName, _ := args.pvc.Annotations[AnnImportPod]
podName := args.pvc.Annotations[AnnImportPod]

blockOwnerDeletion := true
isController := true
Expand Down Expand Up @@ -1230,6 +1215,8 @@ func makeImporterPodSpec(args *importerPodArgs) *corev1.Pod {
pod.Spec.Volumes = append(pod.Spec.Volumes, vol)
}

SetRestrictedSecurityContext(&pod.Spec)

return pod
}

Expand All @@ -1247,23 +1234,12 @@ func setImporterPodCommons(pod *corev1.Pod, podEnvVar *importPodEnvVar, pvc *cor

if getVolumeMode(pvc) == corev1.PersistentVolumeBlock {
pod.Spec.Containers[0].VolumeDevices = addVolumeDevices()
pod.Spec.SecurityContext = &corev1.PodSecurityContext{
RunAsUser: &[]int64{0}[0],
}
} else {
pod.Spec.Containers[0].VolumeMounts = addImportVolumeMounts()
}

pod.Spec.Containers[0].Env = makeImportEnv(podEnvVar, ownerUID)

if podEnvVar.contentType == string(cdiv1.DataVolumeKubeVirt) {
// Set the fsGroup on the security context to the QemuSubGid
if pod.Spec.SecurityContext == nil {
pod.Spec.SecurityContext = &corev1.PodSecurityContext{}
}
fsGroup := common.QemuSubGid
pod.Spec.SecurityContext.FSGroup = &fsGroup
}
SetPodPvcAnnotations(pod, pvc)
}

Expand All @@ -1280,14 +1256,6 @@ func makeImporterContainerSpec(image, verbose, pullPolicy string) *corev1.Contai
Protocol: corev1.ProtocolTCP,
},
},
SecurityContext: &corev1.SecurityContext{
Capabilities: &corev1.Capabilities{
Drop: []corev1.Capability{
"ALL",
},
},
AllowPrivilegeEscalation: pointer.BoolPtr(false),
},
}
}

Expand Down
Loading