-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for security context
Signed-off-by: Moritz Wiesinger <[email protected]>
- Loading branch information
Showing
9 changed files
with
216 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
pkg/processor/security-context/container_security_context.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package security_context | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/arttor/helmify/pkg/helmify" | ||
"github.com/iancoleman/strcase" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
) | ||
|
||
const ( | ||
sc = "securityContext" | ||
cscValueName = "containerSecurityContext" | ||
helmTemplate = "{{- toYaml .Values.%[1]s.%[2]s.containerSecurityContext | nindent 8 }}" | ||
) | ||
|
||
// ProcessContainerSecurityContext adds 'securityContext' to the podSpec in specMap, if it doesn't have one already defined. | ||
func ProcessContainerSecurityContext(nameCamel string, specMap map[string]interface{}, values *helmify.Values) { | ||
if _, defined := specMap["containers"]; defined { | ||
containers, _, _ := unstructured.NestedSlice(specMap, "containers") | ||
for _, container := range containers { | ||
castedContainer := container.(map[string]interface{}) | ||
containerName := strcase.ToLowerCamel(castedContainer["name"].(string)) | ||
if _, defined2 := castedContainer["securityContext"]; defined2 { | ||
setSecContextValue(nameCamel, containerName, castedContainer, values) | ||
} | ||
} | ||
unstructured.SetNestedSlice(specMap, containers, "containers") | ||
} | ||
} | ||
|
||
func setSecContextValue(resourceName string, containerName string, castedContainer map[string]interface{}, values *helmify.Values) { | ||
if castedContainer["securityContext"] != nil { | ||
unstructured.SetNestedField(*values, castedContainer["securityContext"], resourceName, containerName, cscValueName) | ||
|
||
valueString := fmt.Sprintf(helmTemplate, resourceName, containerName) | ||
|
||
unstructured.SetNestedField(castedContainer, valueString, sc) | ||
} | ||
} |
147 changes: 147 additions & 0 deletions
147
pkg/processor/security-context/container_security_context_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
package security_context | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/arttor/helmify/pkg/helmify" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestProcessContainerSecurityContext(t *testing.T) { | ||
type args struct { | ||
nameCamel string | ||
specMap map[string]interface{} | ||
values *helmify.Values | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want *helmify.Values | ||
}{ | ||
{ | ||
name: "test with empty specMap", | ||
args: args{ | ||
nameCamel: "someResourceName", | ||
specMap: map[string]interface{}{}, | ||
values: &helmify.Values{}, | ||
}, | ||
want: &helmify.Values{}, | ||
}, | ||
{ | ||
name: "test with single container", | ||
args: args{ | ||
nameCamel: "someResourceName", | ||
specMap: map[string]interface{}{ | ||
"containers": []interface{}{ | ||
map[string]interface{}{ | ||
"name": "SomeContainerName", | ||
"securityContext": map[string]interface{}{ | ||
"privileged": true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
values: &helmify.Values{}, | ||
}, | ||
want: &helmify.Values{ | ||
"someResourceName": map[string]interface{}{ | ||
"someContainerName": map[string]interface{}{ | ||
"containerSecurityContext": map[string]interface{}{ | ||
"privileged": true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "test with multiple containers", | ||
args: args{ | ||
nameCamel: "someResourceName", | ||
specMap: map[string]interface{}{ | ||
"containers": []interface{}{ | ||
map[string]interface{}{ | ||
"name": "FirstContainer", | ||
"securityContext": map[string]interface{}{ | ||
"privileged": true, | ||
}, | ||
}, | ||
map[string]interface{}{ | ||
"name": "SecondContainer", | ||
"securityContext": map[string]interface{}{ | ||
"allowPrivilegeEscalation": true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
values: &helmify.Values{}, | ||
}, | ||
want: &helmify.Values{ | ||
"someResourceName": map[string]interface{}{ | ||
"firstContainer": map[string]interface{}{ | ||
"containerSecurityContext": map[string]interface{}{ | ||
"privileged": true, | ||
}, | ||
}, | ||
"secondContainer": map[string]interface{}{ | ||
"containerSecurityContext": map[string]interface{}{ | ||
"allowPrivilegeEscalation": true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
ProcessContainerSecurityContext(tt.args.nameCamel, tt.args.specMap, tt.args.values) | ||
assert.Equal(t, tt.want, tt.args.values) | ||
}) | ||
} | ||
} | ||
|
||
func Test_setSecContextValue(t *testing.T) { | ||
type args struct { | ||
resourceName string | ||
containerName string | ||
castedContainer map[string]interface{} | ||
values *helmify.Values | ||
fieldName string | ||
useRenderedHelmTemplate bool | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want *helmify.Values | ||
}{ | ||
{ | ||
name: "simple test with single container and single value", | ||
args: args{ | ||
resourceName: "someResource", | ||
containerName: "someContainer", | ||
castedContainer: map[string]interface{}{ | ||
"securityContext": map[string]interface{}{ | ||
"someField": "someValue", | ||
}, | ||
}, | ||
values: &helmify.Values{}, | ||
fieldName: "someField", | ||
useRenderedHelmTemplate: false, | ||
}, | ||
want: &helmify.Values{ | ||
"someResource": map[string]interface{}{ | ||
"someContainer": map[string]interface{}{ | ||
"containerSecurityContext": map[string]interface{}{ | ||
"someField": "someValue", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
setSecContextValue(tt.args.resourceName, tt.args.containerName, tt.args.castedContainer, tt.args.values) | ||
assert.Equal(t, tt.want, tt.args.values) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters