diff --git a/.gitignore b/.gitignore index 6dc8f05..ce9860d 100644 --- a/.gitignore +++ b/.gitignore @@ -38,3 +38,6 @@ go.work # Built Visual Studio Code Extensions *.vsix + +## Jetbrains IDE +.idea diff --git a/go.mod b/go.mod index 441c7d7..4f30ab1 100644 --- a/go.mod +++ b/go.mod @@ -95,7 +95,7 @@ require ( gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect - k8s.io/apimachinery v0.28.1 // indirect + k8s.io/apimachinery v0.28.1 k8s.io/apiserver v0.28.1 // indirect k8s.io/client-go v0.28.1 // indirect k8s.io/component-base v0.28.1 // indirect diff --git a/internal/provider/constants.go b/internal/provider/constants.go new file mode 100644 index 0000000..24ad055 --- /dev/null +++ b/internal/provider/constants.go @@ -0,0 +1,13 @@ +package provider + +// Kubernetes default environment variables +var k8sDefaultEnvVars = []string{ + "KUBERNETES_PORT", + "KUBERNETES_PORT_443_TCP", + "KUBERNETES_PORT_443_TCP_ADDR", + "KUBERNETES_PORT_443_TCP_PORT", + "KUBERNETES_PORT_443_TCP_PROTO", + "KUBERNETES_SERVICE_HOST", + "KUBERNETES_SERVICE_PORT", + "KUBERNETES_SERVICE_PORT_HTTPS", +} diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 59658c5..aca895b 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -306,32 +306,49 @@ func (p *SaladCloudProvider) PortForward(ctx context.Context, namespace, pod str return nil } -func (p *SaladCloudProvider) createContainersObject(pod *corev1.Pod) []saladclient.CreateContainer { +func (p *SaladCloudProvider) getContainerEnvironment(podMetadata metav1.ObjectMeta, container corev1.Container) map[string]string { + marshallerObjectMetadata, err := json.Marshal(podMetadata) + if err != nil { + log.G(context.Background()).Errorf("Failed Marshalling ", err) + } + envMap := make(map[string]string) + if marshallerObjectMetadata != nil { + envMap["POD_METADATA_YAM"] = string(marshallerObjectMetadata) + } + for _, env := range container.Env { + if env.ValueFrom == nil { + ignore := false + for _, ignoreEnv := range k8sDefaultEnvVars { + if ignoreEnv == env.Name { + ignore = true + break + } + } + if !ignore { + envMap[env.Name] = env.Value + } + } else { + // TODO Handle environment variable from source + } + } + return envMap +} +func (p *SaladCloudProvider) createContainersObject(pod *corev1.Pod) []saladclient.CreateContainer { cpu, memory := utils.GetPodResource(pod.Spec) - creteContainersArray := make([]saladclient.CreateContainer, 0) - for _, container := range pod.Spec.Containers { + for _, container := range pod.Spec.Containers { containerResourceRequirement := saladclient.NewContainerResourceRequirements(int32(cpu), int32(memory)) createContainer := saladclient.NewCreateContainer(container.Image, *containerResourceRequirement) - - marshallerObjectMetadata, err := json.Marshal(pod.ObjectMeta) - if err != nil { - log.G(context.Background()).Errorf("Failed Marshalling ", err) - } - - var mapString = make(map[string]string) - if marshallerObjectMetadata != nil { - mapString["POD_METADATA_YAM"] = string(marshallerObjectMetadata) - } - createContainer.SetEnvironmentVariables(mapString) + createContainer.SetEnvironmentVariables(p.getContainerEnvironment(pod.ObjectMeta, container)) if container.Command != nil { createContainer.SetCommand(container.Command) } creteContainersArray = append(creteContainersArray, *createContainer) // TODO Add support for container Registry auth } + return creteContainersArray } diff --git a/sample-deployment.yaml b/sample-deployment.yaml index fe3e13c..56fbbaf 100644 --- a/sample-deployment.yaml +++ b/sample-deployment.yaml @@ -14,6 +14,11 @@ spec: spec: containers: - name: my-container + env: + - name: test + value: test + - name: another + value: env image: docker.io/heygordian/node-app:latest resources: requests: