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

⭐ Allow enabling pod discovery with an env var #473

Merged
merged 3 commits into from
Jul 28, 2022
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
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 @@ -269,10 +270,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