Skip to content

Commit

Permalink
⭐ Allow enabling pod discovery with an env var (#473)
Browse files Browse the repository at this point in the history
  • Loading branch information
imilchev authored Jul 28, 2022
1 parent d680f90 commit 858616e
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 2 deletions.
2 changes: 2 additions & 0 deletions controllers/k8s_scan/container_image/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"go.mondoo.com/mondoo-operator/api/v1alpha2"
"go.mondoo.com/mondoo-operator/controllers/scanapi"
"go.mondoo.com/mondoo-operator/pkg/feature_flags"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
Expand Down Expand Up @@ -97,6 +98,7 @@ func CronJob(image, integrationMrn string, m v1alpha2.MondooAuditConfig) *batchv
ReadOnly: true,
},
},
Env: feature_flags.AllFeatureFlagsAsEnv(),
},
},
Volumes: []corev1.Volume{
Expand Down
2 changes: 2 additions & 0 deletions controllers/k8s_scan/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"go.mondoo.com/mondoo-operator/api/v1alpha2"
"go.mondoo.com/mondoo-operator/controllers/scanapi"
"go.mondoo.com/mondoo-operator/pkg/feature_flags"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
Expand Down Expand Up @@ -95,6 +96,7 @@ func CronJob(image, integrationMrn string, m v1alpha2.MondooAuditConfig) *batchv
ReadOnly: true,
},
},
Env: feature_flags.AllFeatureFlagsAsEnv(),
},
},
Volumes: []corev1.Volume{
Expand Down
50 changes: 50 additions & 0 deletions pkg/feature_flags/feature_flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package feature_flags

import (
"os"
"strings"

corev1 "k8s.io/api/core/v1"
)

const FeatureFlagPrefix = "FEATURE_"

var (
enablePodDiscovery bool
allFeatureFlags = make(map[string]string)
)

func init() {
envs := os.Environ()
for _, e := range envs {
// If it has the feature flag prefix, then parse the env var.
if strings.HasPrefix(e, FeatureFlagPrefix) {
val := strings.Split(e, "=")
allFeatureFlags[val[0]] = val[1]
setGlobalFlags(val[0], val[1])
}
}
}

func AllFeatureFlags() map[string]string {
return allFeatureFlags
}

func AllFeatureFlagsAsEnv() []corev1.EnvVar {
var env []corev1.EnvVar
for k, v := range allFeatureFlags {
env = append(env, corev1.EnvVar{Name: k, Value: v})
}
return env
}

func GetEnablePodDiscovery() bool {
return enablePodDiscovery
}

func setGlobalFlags(k, v string) {
switch k {
case "FEATURE_DISCOVER_PODS":
enablePodDiscovery = true
}
}
13 changes: 11 additions & 2 deletions pkg/mondooclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"time"

"go.mondoo.com/mondoo-operator/pkg/constants"
"go.mondoo.com/mondoo-operator/pkg/feature_flags"
"go.mondoo.com/mondoo-operator/pkg/inventory"
)

Expand Down Expand Up @@ -270,10 +271,18 @@ func (s *mondooClient) ScanKubernetesResources(ctx context.Context, integrationM
scanJob.Inventory.Spec.Assets[0].Labels[constants.MondooAssetsIntegrationLabel] = integrationMrn
}

if scanContainerImages {
scanJob.Inventory.Spec.Assets[0].Connections[0].Discover.Targets = []string{"container-images"}
if scanContainerImages || feature_flags.GetEnablePodDiscovery() {
scanJob.Inventory.Spec.Assets[0].Connections[0].Options = make(map[string]string)
scanJob.Inventory.Spec.Assets[0].Connections[0].Options["all-namespaces"] = "true"

}

if scanContainerImages {
scanJob.Inventory.Spec.Assets[0].Connections[0].Discover.Targets = append(scanJob.Inventory.Spec.Assets[0].Connections[0].Discover.Targets, "container-images")
}

if feature_flags.GetEnablePodDiscovery() {
scanJob.Inventory.Spec.Assets[0].Connections[0].Discover.Targets = append(scanJob.Inventory.Spec.Assets[0].Connections[0].Discover.Targets, "pods")
}

reqBodyBytes, err := json.Marshal(scanJob)
Expand Down

0 comments on commit 858616e

Please sign in to comment.