Skip to content

Commit 2aead8e

Browse files
committed
export default SecurityContextConstraints in OCP clusters
this commit exports SecurityContextConstraints to let the user deploy the ceph-csi-operator in OCP clusters Signed-off-by: Divyansh Kamboj <[email protected]>
1 parent e6494a0 commit 2aead8e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+32004
-1
lines changed

api/go.mod

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,32 @@
11
module github.com/ceph/ceph-csi-operator/api
22

3-
go 1.22.5
3+
go 1.22.0
4+
5+
toolchain go1.22.5
46

57
require (
8+
github.com/openshift/api v0.0.0-20240724184751-84047ef4a2ce
9+
github.com/stretchr/testify v1.8.4
610
k8s.io/api v0.30.3
711
k8s.io/apimachinery v0.30.3
812
sigs.k8s.io/controller-runtime v0.18.4
13+
sigs.k8s.io/yaml v1.3.0
914
)
1015

1116
require (
17+
github.com/davecgh/go-spew v1.1.1 // indirect
1218
github.com/go-logr/logr v1.4.1 // indirect
1319
github.com/gogo/protobuf v1.3.2 // indirect
1420
github.com/google/gofuzz v1.2.0 // indirect
1521
github.com/json-iterator/go v1.1.12 // indirect
1622
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
1723
github.com/modern-go/reflect2 v1.0.2 // indirect
24+
github.com/pmezard/go-difflib v1.0.0 // indirect
1825
golang.org/x/net v0.23.0 // indirect
1926
golang.org/x/text v0.14.0 // indirect
2027
gopkg.in/inf.v0 v0.9.1 // indirect
2128
gopkg.in/yaml.v2 v2.4.0 // indirect
29+
gopkg.in/yaml.v3 v3.0.1 // indirect
2230
k8s.io/klog/v2 v2.120.1 // indirect
2331
k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect
2432
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect

api/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ github.com/onsi/ginkgo/v2 v2.17.1 h1:V++EzdbhI4ZV4ev0UTIj0PzhzOcReJFyJaLjtSF55M8
3232
github.com/onsi/ginkgo/v2 v2.17.1/go.mod h1:llBI3WDLL9Z6taip6f33H76YcWtJv+7R3HigUjbIBOs=
3333
github.com/onsi/gomega v1.32.0 h1:JRYU78fJ1LPxlckP6Txi/EYqJvjtMrDC04/MM5XRHPk=
3434
github.com/onsi/gomega v1.32.0/go.mod h1:a4x4gW6Pz2yK1MAmvluYme5lvYTn61afQ2ETw/8n4Lg=
35+
github.com/openshift/api v0.0.0-20240724184751-84047ef4a2ce h1:AR9XMlwc7akIN13KDx4L0tI04zHf8jEZ1z1RMRbz1J0=
36+
github.com/openshift/api v0.0.0-20240724184751-84047ef4a2ce/go.mod h1:OOh6Qopf21pSzqNVCB5gomomBXb8o5sGKZxG2KNpaXM=
3537
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
3638
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
3739
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=

api/ocp/scc.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package ocp
2+
3+
import (
4+
"bytes"
5+
_ "embed"
6+
"fmt"
7+
"text/template"
8+
9+
secv1 "github.com/openshift/api/security/v1"
10+
"sigs.k8s.io/yaml"
11+
)
12+
13+
//go:embed scc.yaml
14+
var sccYAMLTemplate string
15+
16+
// NewSecurityContextConstraints loads the embedded SCC YAML template, replaces the namespace,
17+
// and returns it as a SecurityContextConstraints object
18+
func NewSecurityContextConstraints(name string, namespace string) (*secv1.SecurityContextConstraints, error) {
19+
tmpl, err := template.New("scc").Parse(sccYAMLTemplate)
20+
if err != nil {
21+
return nil, fmt.Errorf("error parsing template: %v", err)
22+
}
23+
24+
var buf bytes.Buffer
25+
err = tmpl.Execute(&buf, map[string]string{"Name": name, "Namespace": namespace})
26+
if err != nil {
27+
return nil, fmt.Errorf("error executing template: %v", err)
28+
}
29+
30+
scc := &secv1.SecurityContextConstraints{}
31+
err = yaml.UnmarshalStrict(buf.Bytes(), scc)
32+
if err != nil {
33+
return nil, fmt.Errorf("error unmarshaling YAML: %v", err)
34+
}
35+
36+
return scc, nil
37+
}

api/ocp/scc.yaml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
kind: SecurityContextConstraints
2+
metadata:
3+
name: {{ .Name }}
4+
allowHostDirVolumePlugin: true
5+
allowHostIPC: true
6+
allowHostNetwork: false
7+
allowHostPID: true
8+
allowHostPorts: true
9+
allowPrivilegedContainer: true
10+
allowedCapabilities:
11+
- SYS_ADMIN
12+
apiVersion: security.openshift.io/v1
13+
defaultAddCapabilities: []
14+
fsGroup:
15+
type: RunAsAny
16+
priority:
17+
readOnlyRootFilesystem: false
18+
requiredDropCapabilities:
19+
- ALL
20+
runAsUser:
21+
type: RunAsAny
22+
seLinuxContext:
23+
type: RunAsAny
24+
supplementalGroups:
25+
type: RunAsAny
26+
users:
27+
- system:serviceaccount:{{.Namespace}}:csi-rbd-ctrlplugin-sa
28+
- system:serviceaccount:{{.Namespace}}:csi-cephfs-ctrlplugin-sa
29+
- system:serviceaccount:{{.Namespace}}:csi-nfs-ctrlplugin-sa
30+
- system:serviceaccount:{{.Namespace}}:csi-rbd-nodeplugin-sa
31+
- system:serviceaccount:{{.Namespace}}:csi-cephfs-nodeplugin-sa
32+
- system:serviceaccount:{{.Namespace}}:csi-nfs-nodeplugin-sa
33+
volumes:
34+
- configMap
35+
- emptyDir
36+
- hostPath
37+
- projected

api/ocp/scc_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package ocp
2+
3+
import (
4+
"strings"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestNewSecurityContextConstraints(t *testing.T) {
12+
testNamespace := "test-namespace"
13+
testName := "test"
14+
scc, err := NewSecurityContextConstraints(testName, testNamespace)
15+
require.NoError(t, err, "NewSecurityContextConstraints should not return an error")
16+
assert.NotNil(t, scc, "SCC should not be nil")
17+
18+
assert.Equal(t, scc.Name, testName)
19+
assert.NotEmpty(t, scc.Users, "Users should not be empty")
20+
for _, user := range scc.Users {
21+
assert.True(t, strings.Contains(user, testNamespace),
22+
"Each user should contain the specified namespace")
23+
assert.False(t, strings.Contains(user, "{{.Namespace}}"),
24+
"Template placeholders should be replaced")
25+
}
26+
}

api/vendor/github.com/davecgh/go-spew/LICENSE

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/vendor/github.com/davecgh/go-spew/spew/bypass.go

Lines changed: 145 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/vendor/github.com/davecgh/go-spew/spew/bypasssafe.go

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)