Skip to content

Commit

Permalink
Extract SELinux context error handling into a common func
Browse files Browse the repository at this point in the history
Add handlerSELinuxMetricError() which bumps the right metric + either
consumes a SELinux error or lets it propagate up the stack.
  • Loading branch information
jsafrane committed Aug 4, 2022
1 parent 4df3f58 commit 8d6b721
Showing 1 changed file with 21 additions and 24 deletions.
45 changes: 21 additions & 24 deletions pkg/kubelet/volumemanager/cache/desired_state_of_world.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apiserver/pkg/util/feature"
"k8s.io/component-base/metrics"
"k8s.io/klog/v2"
apiv1resource "k8s.io/kubernetes/pkg/api/v1/resource"
"k8s.io/kubernetes/pkg/features"
Expand Down Expand Up @@ -332,13 +333,8 @@ func (dsw *desiredStateOfWorld) AddPodToVolume(
// TODO: update the error message after tests, e.g. add at least the conflicting pod names.
fullErr := fmt.Errorf("conflicting SELinux labels of volume %s: %q and %q", volumeSpec.Name(), vol.seLinuxFileLabel, seLinuxFileLabel)
isRWOP := util.IsRWOP(volumeSpec)
if isRWOP {
seLinuxVolumeContextMismatchErrors.Add(1.0)
return "", fullErr
} else {
// This is not an error yet, but it will be when support for RWO and RWX volumes is added
seLinuxVolumeContextMismatchWarnings.Add(1.0)
klog.V(4).ErrorS(err, "Please report this error in https://github.com/kubernetes/enhancements/issues/1710, together with full Pod yaml file")
if err := handlerSELinuxMetricError(fullErr, isRWOP, seLinuxVolumeContextMismatchWarnings, seLinuxVolumeContextMismatchErrors); err != nil {
return "", err
}
} else {
if seLinuxFileLabel != "" {
Expand Down Expand Up @@ -385,15 +381,8 @@ func (dsw *desiredStateOfWorld) getSELinuxLabel(volumeSpec *volume.Spec, seLinux
newLabel, err := util.SELinuxOptionsToFileLabel(containerContext)
if err != nil {
fullErr := fmt.Errorf("failed to construct SELinux label from context %q: %s", containerContext, err)
if isRWOP {
// Cannot mount with -o context if the context can't be composed.
seLinuxContainerContextErrors.Add(1.0)
return "", false, fullErr
} else {
// This is not an error yet, but it will be when support for RWO and RWX volumes is added
seLinuxContainerContextWarnings.Add(1.0)
klog.V(4).ErrorS(err, "Please report this error in https://github.com/kubernetes/enhancements/issues/1710, together with full Pod yaml file")
break
if err := handlerSELinuxMetricError(fullErr, isRWOP, seLinuxContainerContextWarnings, seLinuxContainerContextErrors); err != nil {
return "", false, err
}
}
if seLinuxFileLabel == "" {
Expand All @@ -402,14 +391,8 @@ func (dsw *desiredStateOfWorld) getSELinuxLabel(volumeSpec *volume.Spec, seLinux
}
if seLinuxFileLabel != newLabel {
fullErr := fmt.Errorf("volume %s is used with two different SELinux contexts in the same pod: %q, %q", volumeSpec.Name(), seLinuxFileLabel, newLabel)
if isRWOP {
seLinuxPodContextMismatchErrors.Add(1.0)
return "", false, fullErr
} else {
// This is not an error yet, but it will be when support for RWO and RWX volumes is added
seLinuxPodContextMismatchWarnings.Add(1.0)
klog.V(4).ErrorS(err, "Please report this error in https://github.com/kubernetes/enhancements/issues/1710, together with full Pod yaml file")
break
if err := handlerSELinuxMetricError(fullErr, isRWOP, seLinuxPodContextMismatchWarnings, seLinuxPodContextMismatchErrors); err != nil {
return "", false, err
}
}
}
Expand Down Expand Up @@ -625,3 +608,17 @@ func (dsw *desiredStateOfWorld) MarkVolumeAttachability(volumeName v1.UniqueVolu
func (dsw *desiredStateOfWorld) getSELinuxMountSupport(volumeSpec *volume.Spec) (bool, error) {
return util.SupportsSELinuxContextMount(volumeSpec, dsw.volumePluginMgr)
}

// Based on isRWOP, bump the right warning / error metric and either consume the error or return it.
func handlerSELinuxMetricError(err error, isRWOP bool, warningMetric, errorMetric *metrics.Gauge) error {
if isRWOP {
// Cannot mount with -o context if the context can't be composed.
errorMetric.Add(1.0)
return err
}

// This is not an error yet, but it will be when support for RWO and RWX volumes is added
warningMetric.Add(1.0)
klog.V(4).ErrorS(err, "Please report this error in https://github.com/kubernetes/enhancements/issues/1710, together with full Pod yaml file")
return nil
}

0 comments on commit 8d6b721

Please sign in to comment.