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

fix(core):timeout when host & cluster security policies crds are not found #1803

Merged
merged 1 commit into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
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
15 changes: 6 additions & 9 deletions KubeArmor/core/kubeArmor.go
Original file line number Diff line number Diff line change
Expand Up @@ -708,8 +708,8 @@ func KubeArmor() {

// == //

timeout, err := time.ParseDuration(cfg.GlobalCfg.InitTimeout)
if dm.K8sEnabled && cfg.GlobalCfg.Policy {
timeout, err := time.ParseDuration(cfg.GlobalCfg.InitTimeout)
if err != nil {
dm.Logger.Warnf("Not a valid InitTimeout duration: %q, defaulting to '60s'", cfg.GlobalCfg.InitTimeout)
timeout = 60 * time.Second
Expand All @@ -726,14 +726,12 @@ func KubeArmor() {
dm.Logger.Print("Started to monitor security policies")

// watch cluster security policies
clusterSecurityPoliciesSynced := dm.WatchClusterSecurityPolicies()
clusterSecurityPoliciesSynced := dm.WatchClusterSecurityPolicies(timeout)
if clusterSecurityPoliciesSynced == nil {
// destroy the daemon
dm.DestroyKubeArmorDaemon()

return
dm.Logger.Warn("error while monitoring cluster security policies, informer cache not synced")
} else {
dm.Logger.Print("Started to monitor cluster security policies")
}
dm.Logger.Print("Started to monitor cluster security policies")

// watch default posture
defaultPostureSynced := dm.WatchDefaultPosture()
Expand Down Expand Up @@ -776,8 +774,7 @@ func KubeArmor() {

if dm.K8sEnabled && cfg.GlobalCfg.HostPolicy {
// watch host security policies
go dm.WatchHostSecurityPolicies()
dm.Logger.Print("Started to monitor host security policies")
go dm.WatchHostSecurityPolicies(timeout)
}

if !dm.K8sEnabled && (enableContainerPolicy || cfg.GlobalCfg.HostPolicy) {
Expand Down
41 changes: 33 additions & 8 deletions KubeArmor/core/kubeUpdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -1676,13 +1676,22 @@ func (dm *KubeArmorDaemon) WatchSecurityPolicies() cache.InformerSynced {
}

// WatchClusterSecurityPolicies Function
func (dm *KubeArmorDaemon) WatchClusterSecurityPolicies() cache.InformerSynced {
for {
if !K8s.CheckCustomResourceDefinition("kubearmorclusterpolicies") {
time.Sleep(time.Second * 1)
continue
} else {
break
func (dm *KubeArmorDaemon) WatchClusterSecurityPolicies(timeout time.Duration) cache.InformerSynced {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

crdFound := false
for !crdFound {
select {
case <-ctx.Done():
dm.Logger.Warn("timeout while monitoring cluster security policies, kubearmorclusterpolicies CRD not found")
return nil
default:
if K8s.CheckCustomResourceDefinition("kubearmorclusterpolicies") {
crdFound = true
} else {
time.Sleep(time.Second * 1)
}
}
}

Expand Down Expand Up @@ -2260,8 +2269,24 @@ func (dm *KubeArmorDaemon) ParseAndUpdateHostSecurityPolicy(event tp.K8sKubeArmo
}

// WatchHostSecurityPolicies Function
func (dm *KubeArmorDaemon) WatchHostSecurityPolicies() {
func (dm *KubeArmorDaemon) WatchHostSecurityPolicies(timeout time.Duration) {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

for {
select {
case <-ctx.Done():
dm.Logger.Warn("timeout while monitoring host security policies, kubearmorhostpolicies CRD not found")
return
default:
if !K8s.CheckCustomResourceDefinition("kubearmorhostpolicies") {
time.Sleep(time.Second * 1)
continue
}
}

dm.Logger.Print("Started to monitor host security policies")

if !K8s.CheckCustomResourceDefinition("kubearmorhostpolicies") {
time.Sleep(time.Second * 1)
continue
Expand Down
Loading