<commit_before>package core

import (
	"strconv"
	"strings"

	"github.com/supergiant/guber"
	"github.com/supergiant/supergiant/types"
)

// kube_helpers.go is a collection of helper methods that convert a Supergiant
// resource definition into a Kubernetes resource defition.
// (and some other assorted things that should maybe be moved out...)

func kubeVolumeMounts(m *types.ContainerBlueprint) (volMounts []*guber.VolumeMount) {
	for _, mount := range m.Mounts {
		volMounts = append(volMounts, asKubeVolumeMount(mount))
	}
	return volMounts
}

func kubeContainerPorts(m *types.ContainerBlueprint) (cPorts []*guber.ContainerPort) {
	for _, port := range m.Ports {
		cPorts = append(cPorts, asKubeContainerPort(port))
	}
	return cPorts
}

func interpolatedEnvVars(m *types.ContainerBlueprint, instance *InstanceResource) (envVars []*guber.EnvVar) {
	for _, envVar := range m.Env {
		envVars = append(envVars, asKubeEnvVar(envVar, instance))
	}
	return envVars
}

func ImageRepoName(m *types.ContainerBlueprint) string {
	return strings.Split(m.Image, "/")[0]
}

func asKubeContainer(m *types.ContainerBlueprint, instance *InstanceResource) *guber.Container { // NOTE how instance must be passed here
	return &guber.Container{
		Name:  "container", // TODO this will fail with multiple containers ------------------------------------ TODO
		Image: m.Image,
		Env:   interpolatedEnvVars(m, instance),
		Resources: &guber.Resources{
			Requests: &guber.ResourceValues{
				Memory: types.BytesFromMiB(m.RAM.Min).ToKubeMebibytes(),
				CPU:    types.CoresFromMillicores(m.CPU.Min).ToKubeMillicores(),
			},
			Limits: &guber.ResourceValues{
				Memory: types.BytesFromMiB(m.RAM.Max).ToKubeMebibytes(),
				CPU:    types.CoresFromMillicores(m.CPU.Max).ToKubeMillicores(),
			},
		},
		VolumeMounts: kubeVolumeMounts(m),
		Ports:        kubeContainerPorts(m),

		// TODO this should be an option, enabled by default with volumes
		SecurityContext: &guber.SecurityContext{
			Privileged: true,
		},
	}
}

// EnvVar
//==============================================================================
func interpolatedValue(m *types.EnvVar, instance *InstanceResource) string {
	r := strings.NewReplacer(
		"{{ instance_id }}", *instance.ID,
		"{{ other_stuff }}", "TODO")
	return r.Replace(m.Value)
}

func asKubeEnvVar(m *types.EnvVar, instance *InstanceResource) *guber.EnvVar {<commit_msg>Do not pass ResourceValues when null<commit_after>package core

import (
	"strconv"
	"strings"

	"github.com/supergiant/guber"
	"github.com/supergiant/supergiant/types"
)

// kube_helpers.go is a collection of helper methods that convert a Supergiant
// resource definition into a Kubernetes resource defition.
// (and some other assorted things that should maybe be moved out...)

func kubeVolumeMounts(m *types.ContainerBlueprint) (volMounts []*guber.VolumeMount) {
	for _, mount := range m.Mounts {
		volMounts = append(volMounts, asKubeVolumeMount(mount))
	}
	return volMounts
}

func kubeContainerPorts(m *types.ContainerBlueprint) (cPorts []*guber.ContainerPort) {
	for _, port := range m.Ports {
		cPorts = append(cPorts, asKubeContainerPort(port))
	}
	return cPorts
}

func interpolatedEnvVars(m *types.ContainerBlueprint, instance *InstanceResource) (envVars []*guber.EnvVar) {
	for _, envVar := range m.Env {
		envVars = append(envVars, asKubeEnvVar(envVar, instance))
	}
	return envVars
}

func ImageRepoName(m *types.ContainerBlueprint) string {
	return strings.Split(m.Image, "/")[0]
}

func asKubeContainer(m *types.ContainerBlueprint, instance *InstanceResource) *guber.Container { // NOTE how instance must be passed here
	// TODO
	resources := new(guber.Resources)
	if m.RAM != nil {
		if m.RAM.Min != 0 {
			resources.Requests.Memory = types.BytesFromMiB(m.RAM.Min).ToKubeMebibytes()
		}
		if m.RAM.Max != 0 {
			resources.Limits.Memory = types.BytesFromMiB(m.RAM.Max).ToKubeMebibytes()
		}
	}
	if m.CPU != nil {
		if m.CPU.Min != 0 {
			resources.Requests.CPU = types.CoresFromMillicores(m.CPU.Min).ToKubeMillicores()
		}
		if m.CPU.Max != 0 {
			resources.Limits.CPU = types.CoresFromMillicores(m.CPU.Max).ToKubeMillicores()
		}
	}

	return &guber.Container{
		Name:         "container", // TODO this will fail with multiple containers ------------------------------------ TODO
		Image:        m.Image,
		Env:          interpolatedEnvVars(m, instance),
		Resources:    resources,
		VolumeMounts: kubeVolumeMounts(m),
		Ports:        kubeContainerPorts(m),

		// TODO this should be an option, enabled by default with volumes
		SecurityContext: &guber.SecurityContext{
			Privileged: true,
		},
	}
}

// EnvVar
//==============================================================================
func interpolatedValue(m *types.EnvVar, instance *InstanceResource) string {
	r := strings.NewReplacer(
		"{{ instance_id }}", *instance.ID,
		"{{ other_stuff }}", "TODO")
	return r.Replace(m.Value)
}

func asKubeEnvVar(m *types.EnvVar, instance *InstanceResource) *guber.EnvVar {
{'fraction_non_alphanumeric': 0.1642541214314435, 'fraction_numerical': 0.0012062726176115801, 'mean_word_length': 4.220356768100735, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '726989', 'n_tokens_mistral': 1739, 'n_tokens_neox': 1648, 'n_words': 366}