<commit_before>package kumpose

import (
	"encoding/json"
	"log"
	"strings"

	"github.com/docker/libcompose/project"
	"k8s.io/kubernetes/pkg/api"
	"k8s.io/kubernetes/pkg/api/unversioned"
	"k8s.io/kubernetes/pkg/apis/extensions"
)

func CompToDeployment(proj *project.Project) ([]byte, error) {
	var kubeData []byte

	for _, name := range proj.ServiceConfigs.Keys() {
		s, _ := proj.ServiceConfigs.Get(name)

		con, _ := kubeContainer(name, s)

		dep := &extensions.Deployment{
			TypeMeta: unversioned.TypeMeta{
				Kind:       "Deployment",
				APIVersion: "extensions/v1beta1",
			},
			ObjectMeta: api.ObjectMeta{
				Name:   name,
				Labels: map[string]string{"service": name},
			},
			Spec: extensions.DeploymentSpec{
				Replicas: 1,
				Template: api.PodTemplateSpec{
					ObjectMeta: api.ObjectMeta{
						Labels: map[string]string{"service": name},
					},
					Spec: api.PodSpec{
						Containers: []api.Container{
							con,
						},
						Hostname: s.Hostname,
					},
				},
			},
		}

		data, err := json.MarshalIndent(dep, "", "  ")
		if err != nil {
			log.Fatalf("Failed to marshal rc to json %v", err)
		} else {
			kubeData = append(kubeData, data...)
		}
	}

	return kubeData, nil
}

func CompToKube(proj *project.Project) ([]byte, error) {
	var kubeData []byte

	for _, name := range proj.ServiceConfigs.Keys() {
		s, _ := proj.ServiceConfigs.Get(name)

		con, _ := kubeContainer(name, s)

		rc := &api.ReplicationController{
			TypeMeta: unversioned.TypeMeta{
				Kind:       "ReplicationController",
				APIVersion: "v1",
			},
			ObjectMeta: api.ObjectMeta{
				Name:   name,
				Labels: map[string]string{"service": name},
			},
			Spec: api.ReplicationControllerSpec{
				Replicas: 1,
				Selector: map[string]string{"service": name},
				Template: &api.PodTemplateSpec{
					ObjectMeta: api.ObjectMeta{
						Labels: map[string]string{"service": name},
					},
					Spec: api.PodSpec{
						Containers: []api.Container{
							con,
						},
					},
				},
			},
		}

		data, err := json.MarshalIndent(rc, "", "  ")
		if err != nil {
			log.Fatalf("Failed to marshal rc to json %v", err)
		} else {
			kubeData = append(kubeData, data...)
		}
	}
	return kubeData, nil
}

func kubeEnv(environment []string) ([]api.EnvVar, error) {
	var kubeEnv []api.EnvVar

	for _, e := range environment {
		if strings.Contains(e, "=") {
			parts := strings.Split(e, "=")
			kubeEnv = append(kubeEnv, api.EnvVar{
				Name:  parts[0],
				Value: parts[1],
			})
		} else {
			kubeEnv = append(kubeEnv, api.EnvVar{
				Name: e,
			})
		}
	}

	return kubeEnv, nil
}

func Run(composeFile string, target string) ([]byte, error) {

	proj := project.NewProject(
		&project.Context{
			ProjectName:  "k",
			ComposeFiles: []string{composeFile},
		}, nil, nil)

	if err := proj.Parse(); err != nil {
		log.Fatal("Error parsing compose file")
		return []byte{}, err
	}

	switch target {
	case "rc":
		return CompToKube(proj)
	default:
		return CompToDeployment(proj)
	}
}
<commit_msg>Add support for looking up environment variables from host environment<commit_after>package kumpose

import (
	"encoding/json"
	"log"
	"os"
	"strings"

	"github.com/docker/libcompose/project"
	"k8s.io/kubernetes/pkg/api"
	"k8s.io/kubernetes/pkg/api/unversioned"
	"k8s.io/kubernetes/pkg/apis/extensions"
)

func CompToDeployment(proj *project.Project) ([]byte, error) {
	var kubeData []byte

	for _, name := range proj.ServiceConfigs.Keys() {
		s, _ := proj.ServiceConfigs.Get(name)

		con, _ := kubeContainer(name, s)

		dep := &extensions.Deployment{
			TypeMeta: unversioned.TypeMeta{
				Kind:       "Deployment",
				APIVersion: "extensions/v1beta1",
			},
			ObjectMeta: api.ObjectMeta{
				Name:   name,
				Labels: map[string]string{"service": name},
			},
			Spec: extensions.DeploymentSpec{
				Replicas: 1,
				Template: api.PodTemplateSpec{
					ObjectMeta: api.ObjectMeta{
						Labels: map[string]string{"service": name},
					},
					Spec: api.PodSpec{
						Containers: []api.Container{
							con,
						},
						Hostname: s.Hostname,
					},
				},
			},
		}

		data, err := json.MarshalIndent(dep, "", "  ")
		if err != nil {
			log.Fatalf("Failed to marshal rc to json %v", err)
		} else {
			kubeData = append(kubeData, data...)
		}
	}

	return kubeData, nil
}

func CompToKube(proj *project.Project) ([]byte, error) {
	var kubeData []byte

	for _, name := range proj.ServiceConfigs.Keys() {
		s, _ := proj.ServiceConfigs.Get(name)

		con, _ := kubeContainer(name, s)

		rc := &api.ReplicationController{
			TypeMeta: unversioned.TypeMeta{
				Kind:       "ReplicationController",
				APIVersion: "v1",
			},
			ObjectMeta: api.ObjectMeta{
				Name:   name,
				Labels: map[string]string{"service": name},
			},
			Spec: api.ReplicationControllerSpec{
				Replicas: 1,
				Selector: map[string]string{"service": name},
				Template: &api.PodTemplateSpec{
					ObjectMeta: api.ObjectMeta{
						Labels: map[string]string{"service": name},
					},
					Spec: api.PodSpec{
						Containers: []api.Container{
							con,
						},
					},
				},
			},
		}

		data, err := json.MarshalIndent(rc, "", "  ")
		if err != nil {
			log.Fatalf("Failed to marshal rc to json %v", err)
		} else {
			kubeData = append(kubeData, data...)
		}
	}
	return kubeData, nil
}

func kubeEnv(environment []string) ([]api.EnvVar, error) {
	var kubeEnv []api.EnvVar

	for _, e := range environment {
		envVar := api.EnvVar{}
		if strings.Contains(e, "=") {
			parts := strings.Split(e, "=")
			envVar.Name = parts[0]
			envVar.Value = parts[1]
		} else {
			envVar.Name = e
			v, ok := os.LookupEnv(e)
			if ok {
				envVar.Value = v
			}
		}
		kubeEnv = append(kubeEnv, envVar)
	}

	return kubeEnv, nil
}

func Run(composeFile string, target string) ([]byte, error) {

	proj := project.NewProject(
		&project.Context{
			ProjectName:  "k",
			ComposeFiles: []string{composeFile},
		}, nil, nil)

	if err := proj.Parse(); err != nil {
		log.Fatal("Error parsing compose file")
		return []byte{}, err
	}

	switch target {
	case "rc":
		return CompToKube(proj)
	default:
		return CompToDeployment(proj)
	}
}

{'fraction_non_alphanumeric': 0.15292378449408672, 'fraction_numerical': 0.0032851511169513796, 'mean_word_length': 3.0975773889636606, 'pattern_counts': {'":': 12, '<': 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': '1258902', 'n_tokens_mistral': 2652, 'n_tokens_neox': 2193, 'n_words': 460}