Skip to content

Commit

Permalink
Handle Environment variables (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucklypriyansh-2 authored Sep 27, 2023
1 parent 4076a14 commit 9bf565c
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 10 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@ go.work

# Built Visual Studio Code Extensions
*.vsix

## Jetbrains IDE
.idea
13 changes: 13 additions & 0 deletions internal/provider/constants.go
Original file line number Diff line number Diff line change
@@ -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",
}
40 changes: 30 additions & 10 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,35 @@ func (p *SaladCloudProvider) PortForward(ctx context.Context, namespace, pod str
return nil
}

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
log.G(context.Background()).Debugf("Environment variable support from %s is not yet implemented", env.ValueFrom.String())
}
}
return envMap
}

func (p *SaladCloudProvider) createContainersObject(pod *corev1.Pod) []saladclient.CreateContainer {

cpu, memory := utils.GetPodResource(pod.Spec)
Expand All @@ -316,16 +345,7 @@ func (p *SaladCloudProvider) createContainersObject(pod *corev1.Pod) []saladclie
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)
}
Expand Down
5 changes: 5 additions & 0 deletions sample-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 9bf565c

Please sign in to comment.