Repository: microsoft/retina
File: pkg/capture/crd_to_job.go (Go)

Code before:
		outputEnv[captureConstants.CaptureOutputLocationEnvKeyS3Region] = outputConfiguration.S3Upload.Region
		outputEnv[captureConstants.CaptureOutputLocationEnvKeyS3Bucket] = outputConfiguration.S3Upload.Bucket
		outputEnv[captureConstants.CaptureOutputLocationEnvKeyS3Path] = outputConfiguration.S3Upload.Path
	}

	if len(outputEnv) == 0 && (outputConfiguration.BlobUpload == nil || *outputConfiguration.BlobUpload == "") {
		return nil, fmt.Errorf("need to specify at least one outputConfiguration.")
	}

	return outputEnv, nil
}

// obtainCaptureOptionEnv translates CaptureOption to Environment variables to capture job Pod.
func (translator *CaptureToPodTranslator) obtainCaptureOptionEnv(option retinav1alpha1.CaptureOption) (map[string]string, error) {
	outputEnv := map[string]string{}
	if option.Duration != nil {
		outputEnv[captureConstants.CaptureDurationEnvKey] = option.Duration.Duration.String()
	}
	if option.MaxCaptureSize != nil {
		outputEnv[captureConstants.CaptureMaxSizeEnvKey] = strconv.Itoa(*option.MaxCaptureSize)
	}
	if len(option.Interfaces) > 0 {
		outputEnv[captureConstants.CaptureInterfacesEnvKey] = strings.Join(option.Interfaces, ",")
	} else {
		// Default is true (capture on all interfaces) when no specific interfaces are provided
		outputEnv[captureConstants.AllInterfacesEnvKey] = "true"
	}
	return outputEnv, nil
}

// ObtainCaptureJobPodEnv translates Capture object to Environment variables to capture job Pod.
func (translator *CaptureToPodTranslator) ObtainCaptureJobPodEnv(capture retinav1alpha1.Capture) (map[string]string, error) {
	jobPodEnv := map[string]string{}

	captureOutputEnv, err := translator.obtainCaptureOutputEnv(capture.Spec.OutputConfiguration)
	if err != nil {
		return nil, err
	}
	for key, val := range captureOutputEnv {
		jobPodEnv[string(key)] = val
	}

	captureOptionEnv, err := translator.obtainCaptureOptionEnv(capture.Spec.CaptureConfiguration.CaptureOption)
	if err != nil {
		return nil, err
	}
	for key, val := range captureOptionEnv {
		jobPodEnv[string(key)] = val
	}

	tcpdumpFilter, err := translator.obtainTcpdumpFilters(capture.Spec.CaptureConfiguration)

Review comment (suggestion):
You assign this to be true here, but the value of this is never actually checked anywhere else. In `network_capture_unix.go` this is treated within the else condition as a default.

Code after:
		outputEnv[captureConstants.CaptureOutputLocationEnvKeyS3Region] = outputConfiguration.S3Upload.Region
		outputEnv[captureConstants.CaptureOutputLocationEnvKeyS3Bucket] = outputConfiguration.S3Upload.Bucket
		outputEnv[captureConstants.CaptureOutputLocationEnvKeyS3Path] = outputConfiguration.S3Upload.Path
	}

	if len(outputEnv) == 0 && (outputConfiguration.BlobUpload == nil || *outputConfiguration.BlobUpload == "") {
		return nil, fmt.Errorf("need to specify at least one outputConfiguration.")
	}

	return outputEnv, nil
}

// obtainCaptureOptionEnv translates CaptureOption to Environment variables to capture job Pod.
func (translator *CaptureToPodTranslator) obtainCaptureOptionEnv(option retinav1alpha1.CaptureOption) (map[string]string, error) {
	outputEnv := map[string]string{}
	if option.Duration != nil {
		outputEnv[captureConstants.CaptureDurationEnvKey] = option.Duration.Duration.String()
	}
	if option.MaxCaptureSize != nil {
		outputEnv[captureConstants.CaptureMaxSizeEnvKey] = strconv.Itoa(*option.MaxCaptureSize)
	}
	if len(option.Interfaces) > 0 {
		outputEnv[captureConstants.CaptureInterfacesEnvKey] = strings.Join(option.Interfaces, ",")
	}
	return outputEnv, nil
}

// ObtainCaptureJobPodEnv translates Capture object to Environment variables to capture job Pod.
func (translator *CaptureToPodTranslator) ObtainCaptureJobPodEnv(capture retinav1alpha1.Capture) (map[string]string, error) {
	jobPodEnv := map[string]string{}

	captureOutputEnv, err := translator.obtainCaptureOutputEnv(capture.Spec.OutputConfiguration)
	if err != nil {
		return nil, err
	}
	for key, val := range captureOutputEnv {
		jobPodEnv[string(key)] = val
	}

	captureOptionEnv, err := translator.obtainCaptureOptionEnv(capture.Spec.CaptureConfiguration.CaptureOption)
	if err != nil {
		return nil, err
	}
	for key, val := range captureOptionEnv {
		jobPodEnv[string(key)] = val
	}

	tcpdumpFilter, err := translator.obtainTcpdumpFilters(capture.Spec.CaptureConfiguration)
	if err != nil {
		return nil, err
	}