Skip to content

Commit

Permalink
Add changes to support container environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
lucklypriyansh-2 committed Sep 25, 2023
1 parent b6845cd commit 7631bde
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 15 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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
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",
}
45 changes: 31 additions & 14 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

}
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 7631bde

Please sign in to comment.