Skip to content

Commit

Permalink
feat: exclude init containers (#1438)
Browse files Browse the repository at this point in the history
* feat: exclude init containers

Signed-off-by: chenk <hen.keinan@gmail.com>

* feat: exclude init containers

Signed-off-by: chenk <hen.keinan@gmail.com>

* feat: exclude init containers

Signed-off-by: chenk <hen.keinan@gmail.com>

---------

Signed-off-by: chenk <hen.keinan@gmail.com>
  • Loading branch information
chen-keinan authored Aug 15, 2023
1 parent 478ec85 commit d2825cd
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 6 deletions.
1 change: 1 addition & 0 deletions deploy/helm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ Keeps security report resources updated
| trivyOperator.scanJobPodTemplateLabels | string | `""` | scanJobPodTemplateLabels comma-separated representation of the labels which the user wants the scanner pods to be labeled with. Example: `foo=bar,env=stage` will labeled the scanner pods with the labels `foo: bar` and `env: stage` |
| trivyOperator.scanJobPodTemplatePodSecurityContext | object | `{}` | scanJobPodTemplatePodSecurityContext podSecurityContext the user wants the scanner and node collector pods to be amended with. Example: RunAsUser: 10000 RunAsGroup: 10000 RunAsNonRoot: true |
| trivyOperator.scanJobTolerations | list | `[]` | scanJobTolerations tolerations to be applied to the scanner pods and node-collector so that they can run on nodes with matching taints |
| trivyOperator.skipInitContainers | bool | `false` | skipInitContainers when this flag is set to true, the initContainers will be skipped for the scanner and node collector pods |
| trivyOperator.skipResourceByLabels | string | `""` | skipResourceByLabels comma-separated labels keys which trivy-operator will skip scanning on resources with matching labels |
| trivyOperator.vulnerabilityReportsPlugin | string | `"Trivy"` | vulnerabilityReportsPlugin the name of the plugin that generates vulnerability reports `Trivy` |
| volumeMounts | list | `[]` | |
Expand Down
3 changes: 3 additions & 0 deletions deploy/helm/templates/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ data:
{{- with .Values.trivyOperator.scanJobAutomountServiceAccountToken }}
scanJob.automountServiceAccountToken: {{ . | quote }}
{{- end }}
{{- with .Values.trivyOperator.skipInitContainers }}
scanJob.skipInitContainers: {{ . | quote }}
{{- end }}
{{- with .Values.nodeCollector.excludeNodes }}
nodeCollector.excludeNodes: {{ . | quote }}
{{- end }}
Expand Down
3 changes: 3 additions & 0 deletions deploy/helm/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ trivyOperator:
# labeled with. Example: `foo=bar,env=stage` will labeled the scanner pods with the labels `foo: bar` and `env: stage`
scanJobPodTemplateLabels: ""

# -- skipInitContainers when this flag is set to true, the initContainers will be skipped for the scanner and node collector pods
skipInitContainers: false

# -- scanJobPodTemplatePodSecurityContext podSecurityContext the user wants the scanner and node collector pods to be amended with.
# Example:
# RunAsUser: 10000
Expand Down
1 change: 1 addition & 0 deletions docs/getting-started/installation/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ To change the target namespace from all namespaces to the `default` namespace ed
| `nodeCollector.volumes`| see helm/values.yaml | node-collector pod volumes definition for collecting config files information |
| `scanJob.nodeSelector`| N/A| JSON representation of the [nodeSelector] to be applied to the scanner pods so that they can run on nodes with matching labels. Example: `'{"example.com/node-type":"worker", "cpu-type": "sandylake"}'` |
| `scanJob.automountServiceAccountToken` | `"false"` | the flag to enable automount for service account token on scan job. Set `"true"` to enable. |
| `scanJob.skipInitContainers` | `"false"` | when this flag is set to true, the initContainers will be skipped for the scanner and node collector pod. Set `"true"` to enable. |
| `report.additionalLabels` | `""` | additionalReportLabels comma-separated representation of the labels which the user wants the reports to be labeled with. Example: `foo=bar,env=stage` will labeled the reports with the labels `foo: bar` and `env: stage` |
| `scanJob.annotations`| N/A| One-line comma-separated representation of the annotations which the user wants the scanner pods to be annotated with. Example: `foo=bar,env=stage` will annotate the scanner pods with the annotations `foo: bar` and `env: stage` |
| `scanJob.templateLabel`| N/A| One-line comma-separated representation of the template labels which the user wants the scanner pods to be labeled with. Example: `foo=bar,env=stage` will labeled the scanner pods with the labels `foo: bar` and `env: stage` |
Expand Down
9 changes: 6 additions & 3 deletions pkg/kube/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ const KubeSystemNamespace = "kube-system"

// GetContainerImagesFromPodSpec returns a map of container names
// to container images from the specified v1.PodSpec.
func GetContainerImagesFromPodSpec(spec corev1.PodSpec) ContainerImages {
func GetContainerImagesFromPodSpec(spec corev1.PodSpec, skipInitContainers bool) ContainerImages {
images := ContainerImages{}

containers := append(spec.Containers, spec.InitContainers...)
containers := make([]corev1.Container, 0)
containers = append(containers, spec.Containers...)
if !skipInitContainers {
containers = append(containers, spec.InitContainers...)
}
for _, container := range containers {
images[container.Name] = container.Image
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/kube/resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestGetContainerImagesFromPodSpec(t *testing.T) {
},
},
},
})
}, false)
assert.Equal(t, kube.ContainerImages{
"nginx": "nginx:1.16",
"sidecar": "sidecar:1.32.7",
Expand Down
5 changes: 5 additions & 0 deletions pkg/trivyoperator/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ const (
keyScanJobAnnotations = "scanJob.annotations"
//nolint
keyscanJobAutomountServiceAccountToken = "scanJob.automountServiceAccountToken"
keySkipInitContainers = "scanJob.skipInitContainers"
KeyScanJobContainerSecurityContext = "scanJob.podTemplateContainerSecurityContext"
keyScanJobPodSecurityContext = "scanJob.podTemplatePodSecurityContext"
keyScanJobPodTemplateLabels = "scanJob.podTemplateLabels"
Expand Down Expand Up @@ -243,6 +244,10 @@ func (c ConfigData) GetScanJobAutomountServiceAccountToken() bool {
return c.getBoolKey(keyscanJobAutomountServiceAccountToken)
}

func (c ConfigData) GetSkipInitContainers() bool {
return c.getBoolKey(keySkipInitContainers)
}

func (c ConfigData) GetScanJobAnnotations() (map[string]string, error) {
scanJobAnnotationsStr, found := c[keyScanJobAnnotations]
if !found || strings.TrimSpace(scanJobAnnotationsStr) == "" {
Expand Down
8 changes: 7 additions & 1 deletion pkg/vulnerabilityreport/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type ScanJobBuilder struct {
podSecurityContext *corev1.PodSecurityContext
containerSecurityContext *corev1.SecurityContext
podPriorityClassName string
skipInitContainers bool
}

func NewScanJobBuilder() *ScanJobBuilder {
Expand All @@ -61,6 +62,11 @@ func (s *ScanJobBuilder) WithTTL(ttl *time.Duration) *ScanJobBuilder {
return s
}

func (s *ScanJobBuilder) WithSkipInitContainers(skipInitContainers bool) *ScanJobBuilder {
s.skipInitContainers = skipInitContainers
return s
}

func (s *ScanJobBuilder) WithObject(object client.Object) *ScanJobBuilder {
s.object = object
return s
Expand Down Expand Up @@ -126,7 +132,7 @@ func (s *ScanJobBuilder) Get() (*batchv1.Job, []*corev1.Secret, error) {

templateSpec.NodeSelector = s.nodeSelector

containerImagesAsJSON, err := kube.GetContainerImagesFromPodSpec(spec).AsJSON()
containerImagesAsJSON, err := kube.GetContainerImagesFromPodSpec(spec, s.skipInitContainers).AsJSON()
if err != nil {
return nil, nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/vulnerabilityreport/controller/workload.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func (r *WorkloadController) reconcileWorkload(workloadKind kube.Kind) reconcile
return ctrl.Result{}, err
}

containerImages := kube.GetContainerImagesFromPodSpec(podSpec)
containerImages := kube.GetContainerImagesFromPodSpec(podSpec, r.GetSkipInitContainers())
hash := kube.ComputeHash(podSpec)

log = log.WithValues("podSpecHash", hash)
Expand Down Expand Up @@ -302,6 +302,7 @@ func (r *WorkloadController) submitScanJob(ctx context.Context, owner client.Obj
WithNodeSelector(scanJobNodeSelector).
WithPodSecurityContext(scanJobSecurityContext).
WithSecurityContext(scanJobContainerSecurityContext).
WithSkipInitContainers(r.GetSkipInitContainers()).
WithPodTemplateLabels(scanJobPodTemplateLabels).
WithCredentials(credentials).
WithPodPriorityClassName(scanJobPodPriorityClassName).
Expand Down

0 comments on commit d2825cd

Please sign in to comment.