Skip to content

Commit 173846b

Browse files
authored
Merge pull request kubernetes#72389 from mason1kwok/feature_gate_AllowedProcMountTypes
AllowedProcMountTypes - Update DropDisabled[Alpha]Fields behaviour
2 parents 9cdfdba + 3453da2 commit 173846b

File tree

3 files changed

+86
-30
lines changed

3 files changed

+86
-30
lines changed

pkg/api/podsecuritypolicy/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ go_test(
3838
"//pkg/apis/core:go_default_library",
3939
"//pkg/apis/policy:go_default_library",
4040
"//pkg/features:go_default_library",
41+
"//staging/src/k8s.io/apimachinery/pkg/util/diff:go_default_library",
4142
"//staging/src/k8s.io/apiserver/pkg/util/feature:go_default_library",
4243
"//staging/src/k8s.io/apiserver/pkg/util/feature/testing:go_default_library",
4344
],

pkg/api/podsecuritypolicy/util.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,8 @@ import (
2525
// DropDisabledFields removes disabled fields from the pod security policy spec.
2626
// This should be called from PrepareForCreate/PrepareForUpdate for all resources containing a od security policy spec.
2727
func DropDisabledFields(pspSpec, oldPSPSpec *policy.PodSecurityPolicySpec) {
28-
if !utilfeature.DefaultFeatureGate.Enabled(features.ProcMountType) {
28+
if !utilfeature.DefaultFeatureGate.Enabled(features.ProcMountType) && !allowedProcMountTypesInUse(oldPSPSpec) {
2929
pspSpec.AllowedProcMountTypes = nil
30-
if oldPSPSpec != nil {
31-
oldPSPSpec.AllowedProcMountTypes = nil
32-
}
3330
}
3431
if !utilfeature.DefaultFeatureGate.Enabled(features.RunAsGroup) {
3532
pspSpec.RunAsGroup = nil
@@ -38,3 +35,16 @@ func DropDisabledFields(pspSpec, oldPSPSpec *policy.PodSecurityPolicySpec) {
3835
}
3936
}
4037
}
38+
39+
func allowedProcMountTypesInUse(oldPSPSpec *policy.PodSecurityPolicySpec) bool {
40+
if oldPSPSpec == nil {
41+
return false
42+
}
43+
44+
if oldPSPSpec.AllowedProcMountTypes != nil {
45+
return true
46+
}
47+
48+
return false
49+
50+
}

pkg/api/podsecuritypolicy/util_test.go

Lines changed: 71 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,48 +17,93 @@ limitations under the License.
1717
package podsecuritypolicy
1818

1919
import (
20+
"fmt"
21+
"reflect"
2022
"testing"
2123

24+
"k8s.io/apimachinery/pkg/util/diff"
2225
utilfeature "k8s.io/apiserver/pkg/util/feature"
2326
utilfeaturetesting "k8s.io/apiserver/pkg/util/feature/testing"
2427
api "k8s.io/kubernetes/pkg/apis/core"
2528
"k8s.io/kubernetes/pkg/apis/policy"
2629
"k8s.io/kubernetes/pkg/features"
2730
)
2831

29-
func TestDropAlphaProcMountType(t *testing.T) {
30-
// PodSecurityPolicy with AllowedProcMountTypes set
31-
psp := policy.PodSecurityPolicy{
32-
Spec: policy.PodSecurityPolicySpec{
33-
AllowedProcMountTypes: []api.ProcMountType{api.UnmaskedProcMount},
34-
},
32+
func TestDropAllowedProcMountTypes(t *testing.T) {
33+
allowedProcMountTypes := []api.ProcMountType{api.UnmaskedProcMount}
34+
scWithoutAllowedProcMountTypes := func() *policy.PodSecurityPolicySpec {
35+
return &policy.PodSecurityPolicySpec{}
36+
}
37+
scWithAllowedProcMountTypes := func() *policy.PodSecurityPolicySpec {
38+
return &policy.PodSecurityPolicySpec{
39+
AllowedProcMountTypes: allowedProcMountTypes,
40+
}
3541
}
3642

37-
// Enable alpha feature ProcMountType
38-
defer utilfeaturetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.ProcMountType, true)()
43+
scInfo := []struct {
44+
description string
45+
hasAllowedProcMountTypes bool
46+
sc func() *policy.PodSecurityPolicySpec
47+
}{
48+
{
49+
description: "PodSecurityPolicySpec Without AllowedProcMountTypes",
50+
hasAllowedProcMountTypes: false,
51+
sc: scWithoutAllowedProcMountTypes,
52+
},
53+
{
54+
description: "PodSecurityPolicySpec With AllowedProcMountTypes",
55+
hasAllowedProcMountTypes: true,
56+
sc: scWithAllowedProcMountTypes,
57+
},
58+
{
59+
description: "is nil",
60+
hasAllowedProcMountTypes: false,
61+
sc: func() *policy.PodSecurityPolicySpec { return nil },
62+
},
63+
}
3964

40-
// now test dropping the fields - should not be dropped
41-
DropDisabledFields(&psp.Spec, nil)
65+
for _, enabled := range []bool{true, false} {
66+
for _, oldPSPSpecInfo := range scInfo {
67+
for _, newPSPSpecInfo := range scInfo {
68+
oldPSPSpecHasAllowedProcMountTypes, oldPSPSpec := oldPSPSpecInfo.hasAllowedProcMountTypes, oldPSPSpecInfo.sc()
69+
newPSPSpecHasAllowedProcMountTypes, newPSPSpec := newPSPSpecInfo.hasAllowedProcMountTypes, newPSPSpecInfo.sc()
70+
if newPSPSpec == nil {
71+
continue
72+
}
4273

43-
// check to make sure AllowedProcMountTypes is still present
44-
// if featureset is set to true
45-
if utilfeature.DefaultFeatureGate.Enabled(features.ProcMountType) {
46-
if psp.Spec.AllowedProcMountTypes == nil {
47-
t.Error("AllowedProcMountTypes in pvc.Spec should not have been dropped based on feature-gate")
48-
}
49-
}
74+
t.Run(fmt.Sprintf("feature enabled=%v, old PodSecurityPolicySpec %v, new PodSecurityPolicySpec %v", enabled, oldPSPSpecInfo.description, newPSPSpecInfo.description), func(t *testing.T) {
75+
defer utilfeaturetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.ProcMountType, enabled)()
5076

51-
// Disable alpha feature ProcMountType
52-
defer utilfeaturetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.ProcMountType, false)()
77+
DropDisabledFields(newPSPSpec, oldPSPSpec)
5378

54-
// now test dropping the fields
55-
DropDisabledFields(&psp.Spec, nil)
79+
// old PodSecurityPolicySpec should never be changed
80+
if !reflect.DeepEqual(oldPSPSpec, oldPSPSpecInfo.sc()) {
81+
t.Errorf("old PodSecurityPolicySpec changed: %v", diff.ObjectReflectDiff(oldPSPSpec, oldPSPSpecInfo.sc()))
82+
}
5683

57-
// check to make sure AllowedProcMountTypes is nil
58-
// if featureset is set to false
59-
if utilfeature.DefaultFeatureGate.Enabled(features.ProcMountType) {
60-
if psp.Spec.AllowedProcMountTypes != nil {
61-
t.Error("DropDisabledFields AllowedProcMountTypes for psp.Spec failed")
84+
switch {
85+
case enabled || oldPSPSpecHasAllowedProcMountTypes:
86+
// new PodSecurityPolicySpec should not be changed if the feature is enabled, or if the old PodSecurityPolicySpec had AllowedProcMountTypes
87+
if !reflect.DeepEqual(newPSPSpec, newPSPSpecInfo.sc()) {
88+
t.Errorf("new PodSecurityPolicySpec changed: %v", diff.ObjectReflectDiff(newPSPSpec, newPSPSpecInfo.sc()))
89+
}
90+
case newPSPSpecHasAllowedProcMountTypes:
91+
// new PodSecurityPolicySpec should be changed
92+
if reflect.DeepEqual(newPSPSpec, newPSPSpecInfo.sc()) {
93+
t.Errorf("new PodSecurityPolicySpec was not changed")
94+
}
95+
// new PodSecurityPolicySpec should not have AllowedProcMountTypes
96+
if !reflect.DeepEqual(newPSPSpec, scWithoutAllowedProcMountTypes()) {
97+
t.Errorf("new PodSecurityPolicySpec had PodSecurityPolicySpecAllowedProcMountTypes: %v", diff.ObjectReflectDiff(newPSPSpec, scWithoutAllowedProcMountTypes()))
98+
}
99+
default:
100+
// new PodSecurityPolicySpec should not need to be changed
101+
if !reflect.DeepEqual(newPSPSpec, newPSPSpecInfo.sc()) {
102+
t.Errorf("new PodSecurityPolicySpec changed: %v", diff.ObjectReflectDiff(newPSPSpec, newPSPSpecInfo.sc()))
103+
}
104+
}
105+
})
106+
}
62107
}
63108
}
64109
}

0 commit comments

Comments
 (0)