Skip to content

Commit

Permalink
[Feature] Expose core.PodSecurityContext Sysctl options (#1360)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajanikow authored Jul 20, 2023
1 parent 8b6395a commit 2bd002e
Show file tree
Hide file tree
Showing 11 changed files with 439 additions and 88 deletions.
3 changes: 2 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pkg/generated/** linguist-generated
**/zz_generated.deepcopy.go linguist-generated
**/zz_generated.deepcopy.go linguist-generated
pkg/api/** linguist-generated
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- (Feature) Backup lifetime - remove Backup once its lifetime has been reached
- (Feature) Add Feature dependency
- (Feature) Run secured containers as a feature
- (Feature) Expose core.PodSecurityContext Sysctl options

## [1.2.31](https://github.com/arangodb/kube-arangodb/tree/1.2.31) (2023-07-14)
- (Improvement) Block traffic on the services if there is more than 1 active leader in ActiveFailover mode
Expand Down
315 changes: 231 additions & 84 deletions docs/api/ArangoDeployment.V1.md

Large diffs are not rendered by default.

34 changes: 34 additions & 0 deletions pkg/apis/deployment/v1/server_group_security_context_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
package v1

import (
"sort"

core "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/intstr"

"github.com/arangodb/kube-arangodb/pkg/util"
)
Expand Down Expand Up @@ -51,6 +54,17 @@ type ServerGroupSpecSecurityContext struct {
SupplementalGroups []int64 `json:"supplementalGroups,omitempty"`
FSGroup *int64 `json:"fsGroup,omitempty"`

// Sysctls hold a list of namespaced sysctls used for the pod. Pods with unsupported
// sysctls (by the container runtime) might fail to launch.
// Map Value can be String or Int
// +doc/example: sysctls:
// +doc/example: "kernel.shm_rmid_forced": "0"
// +doc/example: "net.core.somaxconn": 1024
// +doc/example: "kernel.msgmax": "65536"
// +doc/type: map[string]intstr.IntOrString
// +doc/link: Documentation|https://kubernetes.io/docs/tasks/administer-cluster/sysctl-cluster/
Sysctls map[string]intstr.IntOrString `json:"sysctls,omitempty"`

// SeccompProfile defines a pod/container's seccomp profile settings. Only one profile source may be set.
// +doc/type: core.SeccompProfile
// +doc/link: Documentation of core.SeccompProfile|https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.26/#seccompprofile-v1-core
Expand Down Expand Up @@ -96,6 +110,26 @@ func (s *ServerGroupSpecSecurityContext) NewPodSecurityContext(secured bool) *co
}
}

if s != nil && len(s.Sysctls) > 0 {
var sysctls []core.Sysctl
for k, v := range s.Sysctls {
sysctls = append(sysctls, core.Sysctl{
Name: k,
Value: v.String(),
})
}

sort.Slice(sysctls, func(i, j int) bool {
return sysctls[i].Name < sysctls[j].Name
})

if psc == nil {
psc = &core.PodSecurityContext{}
}

psc.Sysctls = sysctls
}

if secured {
if psc == nil {
psc = &core.PodSecurityContext{}
Expand Down
59 changes: 59 additions & 0 deletions pkg/apis/deployment/v1/server_group_security_context_spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@
package v1

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
core "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/intstr"

"github.com/arangodb/kube-arangodb/pkg/util"
)
Expand Down Expand Up @@ -76,6 +79,27 @@ func TestServerGroupSpecSecurityContext_NewPodSecurityContext(t *testing.T) {
SupplementalGroups: []int64{1},
},
},
"pass sysctl opts": {
sc: &ServerGroupSpecSecurityContext{
Sysctls: map[string]intstr.IntOrString{
"opt.1": intstr.FromInt(1),
"opt.2": intstr.FromString("2"),
},
},
secured: false,
want: &core.PodSecurityContext{
Sysctls: []core.Sysctl{
{
Name: "opt.1",
Value: "1",
},
{
Name: "opt.2",
Value: "2",
},
},
},
},
}

for testName, testCase := range testCases {
Expand All @@ -86,6 +110,41 @@ func TestServerGroupSpecSecurityContext_NewPodSecurityContext(t *testing.T) {
}
}

func TestServerGroupSpecSecurityContext_NewPodSecurityContextFromJSON(t *testing.T) {
testCases := map[string]struct {
sc string
secured bool
want *core.PodSecurityContext
}{
"pass sysctl opts": {
sc: `{"sysctls":{"opt.1":1, "opt.2":"2"}}`,
secured: false,
want: &core.PodSecurityContext{
Sysctls: []core.Sysctl{
{
Name: "opt.1",
Value: "1",
},
{
Name: "opt.2",
Value: "2",
},
},
},
},
}

for testName, testCase := range testCases {
t.Run(testName, func(t *testing.T) {
var p ServerGroupSpecSecurityContext
require.NoError(t, json.Unmarshal([]byte(testCase.sc), &p))

actual := p.NewPodSecurityContext(testCase.secured)
assert.Equalf(t, testCase.want, actual, "NewPodSecurityContext(%v)", testCase.secured)
})
}
}

func TestServerGroupSpecSecurityContext_NewSecurityContext(t *testing.T) {
tests := map[string]struct {
sc *ServerGroupSpecSecurityContext
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/deployment/v1/timeouts.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// DISCLAIMER
//
// Copyright 2016-2022 ArangoDB GmbH, Cologne, Germany
// Copyright 2016-2023 ArangoDB GmbH, Cologne, Germany
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
8 changes: 8 additions & 0 deletions pkg/apis/deployment/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions pkg/apis/deployment/v2alpha1/server_group_security_context_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
package v2alpha1

import (
"sort"

core "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/intstr"

"github.com/arangodb/kube-arangodb/pkg/util"
)
Expand Down Expand Up @@ -51,6 +54,17 @@ type ServerGroupSpecSecurityContext struct {
SupplementalGroups []int64 `json:"supplementalGroups,omitempty"`
FSGroup *int64 `json:"fsGroup,omitempty"`

// Sysctls hold a list of namespaced sysctls used for the pod. Pods with unsupported
// sysctls (by the container runtime) might fail to launch.
// Map Value can be String or Int
// +doc/example: sysctls:
// +doc/example: "kernel.shm_rmid_forced": "0"
// +doc/example: "net.core.somaxconn": 1024
// +doc/example: "kernel.msgmax": "65536"
// +doc/type: map[string]intstr.IntOrString
// +doc/link: Documentation|https://kubernetes.io/docs/tasks/administer-cluster/sysctl-cluster/
Sysctls map[string]intstr.IntOrString `json:"sysctls,omitempty"`

// SeccompProfile defines a pod/container's seccomp profile settings. Only one profile source may be set.
// +doc/type: core.SeccompProfile
// +doc/link: Documentation of core.SeccompProfile|https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.26/#seccompprofile-v1-core
Expand Down Expand Up @@ -96,6 +110,26 @@ func (s *ServerGroupSpecSecurityContext) NewPodSecurityContext(secured bool) *co
}
}

if s != nil && len(s.Sysctls) > 0 {
var sysctls []core.Sysctl
for k, v := range s.Sysctls {
sysctls = append(sysctls, core.Sysctl{
Name: k,
Value: v.String(),
})
}

sort.Slice(sysctls, func(i, j int) bool {
return sysctls[i].Name < sysctls[j].Name
})

if psc == nil {
psc = &core.PodSecurityContext{}
}

psc.Sysctls = sysctls
}

if secured {
if psc == nil {
psc = &core.PodSecurityContext{}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@
package v2alpha1

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
core "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/intstr"

"github.com/arangodb/kube-arangodb/pkg/util"
)
Expand Down Expand Up @@ -76,6 +79,27 @@ func TestServerGroupSpecSecurityContext_NewPodSecurityContext(t *testing.T) {
SupplementalGroups: []int64{1},
},
},
"pass sysctl opts": {
sc: &ServerGroupSpecSecurityContext{
Sysctls: map[string]intstr.IntOrString{
"opt.1": intstr.FromInt(1),
"opt.2": intstr.FromString("2"),
},
},
secured: false,
want: &core.PodSecurityContext{
Sysctls: []core.Sysctl{
{
Name: "opt.1",
Value: "1",
},
{
Name: "opt.2",
Value: "2",
},
},
},
},
}

for testName, testCase := range testCases {
Expand All @@ -86,6 +110,41 @@ func TestServerGroupSpecSecurityContext_NewPodSecurityContext(t *testing.T) {
}
}

func TestServerGroupSpecSecurityContext_NewPodSecurityContextFromJSON(t *testing.T) {
testCases := map[string]struct {
sc string
secured bool
want *core.PodSecurityContext
}{
"pass sysctl opts": {
sc: `{"sysctls":{"opt.1":1, "opt.2":"2"}}`,
secured: false,
want: &core.PodSecurityContext{
Sysctls: []core.Sysctl{
{
Name: "opt.1",
Value: "1",
},
{
Name: "opt.2",
Value: "2",
},
},
},
},
}

for testName, testCase := range testCases {
t.Run(testName, func(t *testing.T) {
var p ServerGroupSpecSecurityContext
require.NoError(t, json.Unmarshal([]byte(testCase.sc), &p))

actual := p.NewPodSecurityContext(testCase.secured)
assert.Equalf(t, testCase.want, actual, "NewPodSecurityContext(%v)", testCase.secured)
})
}
}

func TestServerGroupSpecSecurityContext_NewSecurityContext(t *testing.T) {
tests := map[string]struct {
sc *ServerGroupSpecSecurityContext
Expand Down
4 changes: 2 additions & 2 deletions pkg/apis/deployment/v2alpha1/timeouts.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// DISCLAIMER
//
// Copyright 2016-2022 ArangoDB GmbH, Cologne, Germany
// Copyright 2016-2023 ArangoDB GmbH, Cologne, Germany
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -35,7 +35,7 @@ type Timeouts struct {
// MaintenanceGracePeriod action timeout
MaintenanceGracePeriod *Timeout `json:"maintenanceGracePeriod,omitempty"`

// Actions keep list of the actions timeouts.
// Actions keep map of the actions timeouts.
// +doc/type: map[string]meta.Duration
// +doc/link: List of supported action names|/docs/generated/actions.md
// +doc/link: Definition of meta.Duration|https://github.com/kubernetes/apimachinery/blob/v0.26.6/pkg/apis/meta/v1/duration.go
Expand Down
8 changes: 8 additions & 0 deletions pkg/apis/deployment/v2alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2bd002e

Please sign in to comment.