Skip to content

Commit

Permalink
Reduce complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
CoreyCook8 authored and ccojocar committed Jan 23, 2024
1 parent 3935166 commit a0df1d5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
46 changes: 28 additions & 18 deletions internal/pkg/webhooks/binding/binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -116,22 +117,36 @@ func (p *podBinder) Handle(ctx context.Context, req admission.Request) admission
return admission.Errored(http.StatusInternalServerError, err)
}
profilebindings := profileBindings.Items

pod, admissionResponse := p.updatePod(ctx, profilebindings, req)
if admissionResponse.Result.Status == metav1.StatusFailure {
return admissionResponse
}

marshaledPod, err := json.Marshal(pod)
if err != nil {
p.log.Error(err, "failed to encode pod")
return admission.Errored(http.StatusInternalServerError, err)
}

return admission.PatchResponseFromRaw(req.Object.Raw, marshaledPod)
}

func (p *podBinder) updatePod(ctx context.Context, profilebindings []profilebindingv1alpha1.ProfileBinding, req admission.Request) (*corev1.Pod, admission.Response) {
podChanged := false
podID := req.Namespace + "/" + req.Name
pod := &corev1.Pod{}
var podBindProfile *interface{}
var podProfileBinding *profilebindingv1alpha1.ProfileBinding

podID := req.Namespace + "/" + req.Name
pod := &corev1.Pod{}
var containers sync.Map
if req.Operation != "DELETE" {
pod, err = p.impl.DecodePod(req)
pod, err := p.impl.DecodePod(req)
if err != nil {
p.log.Error(err, "failed to decode pod")
return admission.Errored(http.StatusBadRequest, err)
return pod, admission.Errored(http.StatusBadRequest, err)
}
initContainerMap(&containers, &pod.Spec)
}

for i := range profilebindings {
profileKind := profilebindings[i].Spec.ProfileRef.Kind
if profileKind != profilebindingv1alpha1.ProfileBindingKindSeccompProfile {
Expand All @@ -144,7 +159,7 @@ func (p *podBinder) Handle(ctx context.Context, req admission.Request) admission
profileName := profilebindings[i].Spec.ProfileRef.Name
if req.Operation == "DELETE" {
if err := p.removePodFromBinding(ctx, podID, &profilebindings[i]); err != nil {
return admission.Errored(http.StatusInternalServerError, err)
return pod, admission.Errored(http.StatusInternalServerError, err)
}
continue
}
Expand All @@ -162,7 +177,7 @@ func (p *podBinder) Handle(ctx context.Context, req admission.Request) admission

if err != nil {
p.log.Error(err, fmt.Sprintf("failed to get %v %#v", profileKind, namespacedName))
return admission.Errored(http.StatusInternalServerError, err)
return pod, admission.Errored(http.StatusInternalServerError, err)
}

if profilebindings[i].Spec.Image == profilebindingv1alpha1.SelectAllContainersImage {
Expand All @@ -184,28 +199,23 @@ func (p *podBinder) Handle(ctx context.Context, req admission.Request) admission
}
if podChanged {
if err := p.addPodToBinding(ctx, podID, &profilebindings[i]); err != nil {
return admission.Errored(http.StatusInternalServerError, err)
return pod, admission.Errored(http.StatusInternalServerError, err)
}
}
}

if !podChanged {
if podBindProfile == nil || podProfileBinding == nil {
return admission.Allowed("pod unchanged")
return pod, admission.Allowed("pod unchanged")
}
podChanged = p.addPodSecurityContext(pod, *podBindProfile)
if podChanged {
if err := p.addPodToBinding(ctx, podID, podProfileBinding); err != nil {
return admission.Errored(http.StatusInternalServerError, err)
return pod, admission.Errored(http.StatusInternalServerError, err)
}
}
}
marshaledPod, err := json.Marshal(pod)
if err != nil {
p.log.Error(err, "failed to encode pod")
return admission.Errored(http.StatusInternalServerError, err)
}

return admission.PatchResponseFromRaw(req.Object.Raw, marshaledPod)
return pod, admission.Allowed("pod changed")
}

func (p *podBinder) getSeccompProfile(
Expand Down
4 changes: 2 additions & 2 deletions test/tc_selinux_profilebindings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const (
func (e *e2e) testCaseSelinuxProfileBinding() {
e.selinuxOnlyTestCase()

cleanup := e.profileBindingTestPrep(nsBindingEnabled, true, , "busybox:latest")
cleanup := e.profileBindingTestPrep(nsBindingEnabled, true, "busybox:latest")
defer cleanup()

e.logf("the workload should not have errored")
Expand Down Expand Up @@ -69,7 +69,7 @@ func (e *e2e) testCaseSelinuxProfileBinding() {
func (e *e2e) testCaseSelinuxDefaultProfileBinding() {
e.selinuxOnlyTestCase()

cleanup := e.profileBindingTestPrep(nsBindingEnabled, true, , "busybox:latest")
cleanup := e.profileBindingTestPrep(nsBindingEnabled, true, "*")
defer cleanup()

e.logf("the workload should not have errored")
Expand Down

0 comments on commit a0df1d5

Please sign in to comment.