Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if seccomp profile is set before assign; Check only once #567

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 33 additions & 25 deletions policy/secomp/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package secomp

import (
"fmt"
"sync"

"kmodules.xyz/client-go/discovery"
meta_util "kmodules.xyz/client-go/meta"
Expand All @@ -28,35 +29,42 @@ import (
"k8s.io/client-go/rest"
)

var profile string
var (
profile string
seccomp *core.SeccompProfile
once sync.Once
)

func init() {
if meta_util.PossiblyInCluster() {
cfg, err := rest.InClusterConfig()
if err != nil {
panic(err)
}
kc := kubernetes.NewForConfigOrDie(cfg)
yes, err := discovery.CheckAPIVersion(kc.Discovery(), ">= 1.27")
if err != nil {
panic(err)
}
if yes {
profile = string(core.SeccompProfileTypeRuntimeDefault)
}
}
pflag.StringVar(&profile, "default-seccomp-profile-type", profile, "Default seccomp profile")
}

func DefaultSeccompProfile() *core.SeccompProfile {
if profile == "" {
return nil
} else if profile != string(core.SeccompProfileTypeUnconfined) &&
profile != string(core.SeccompProfileTypeRuntimeDefault) &&
profile != string(core.SeccompProfileTypeLocalhost) {
panic(fmt.Errorf("unknown seccomp profile type %s", profile))
}
return &core.SeccompProfile{
Type: core.SeccompProfileType(profile),
}
once.Do(func() {
if meta_util.PossiblyInCluster() {
cfg, err := rest.InClusterConfig()
if err != nil {
panic(err)
}
kc := kubernetes.NewForConfigOrDie(cfg)
yes, err := discovery.CheckAPIVersion(kc.Discovery(), ">= 1.27")
if err != nil {
panic(err)
}
if yes && profile == "" {
profile = string(core.SeccompProfileTypeRuntimeDefault)
}
}
if profile == "" {
seccomp = nil
} else if profile != string(core.SeccompProfileTypeUnconfined) &&
profile != string(core.SeccompProfileTypeRuntimeDefault) &&
profile != string(core.SeccompProfileTypeLocalhost) {
panic(fmt.Errorf("unknown seccomp profile type %s", profile))
}
seccomp = &core.SeccompProfile{
Type: core.SeccompProfileType(profile),
}
})
return seccomp
}
Loading