github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/kubernetes/debugging/adapter/adapter.go (about) 1 /* 2 Copyright 2021 The Skaffold Authors 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package adapter 18 19 import ( 20 v1 "k8s.io/api/core/v1" 21 22 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/debug/types" 23 ) 24 25 type Adapter struct { 26 container *v1.Container 27 executable *types.ExecutableContainer 28 } 29 30 func NewAdapter(c *v1.Container) *Adapter { 31 return &Adapter{ 32 container: c, 33 executable: ExecutableContainerFromK8sContainer(c), 34 } 35 } 36 37 func (a *Adapter) GetContainer() *types.ExecutableContainer { 38 return a.executable 39 } 40 41 // Apply takes the relevant fields from the operable container 42 // and applies them to the referenced v1.Container from the manifest's pod spec 43 func (a *Adapter) Apply() { 44 a.container.Args = a.executable.Args 45 a.container.Command = a.executable.Command 46 a.container.Env = containerEnvToK8sEnv(a.executable.Env, holdValueFrom(a.container.Env)) 47 a.container.Ports = containerPortsToK8sPorts(a.executable.Ports) 48 } 49 50 // ExecutableContainerFromK8sContainer creates an instance of an operableContainer 51 // from a v1.Container reference. This object will be passed around to accept 52 // transforms, and will eventually overwrite fields from the creating v1.Container 53 // in the manifest-under-transformation's pod spec. 54 func ExecutableContainerFromK8sContainer(c *v1.Container) *types.ExecutableContainer { 55 return &types.ExecutableContainer{ 56 Command: c.Command, 57 Args: c.Args, 58 Env: k8sEnvToContainerEnv(c.Env), 59 Ports: k8sPortsToContainerPorts(c.Ports), 60 } 61 } 62 63 func k8sEnvToContainerEnv(k8sEnv []v1.EnvVar) types.ContainerEnv { 64 env := make(map[string]string, len(k8sEnv)) 65 var order []string 66 for _, entry := range k8sEnv { 67 order = append(order, entry.Name) 68 env[entry.Name] = entry.Value 69 } 70 return types.ContainerEnv{ 71 Order: order, 72 Env: env, 73 } 74 } 75 76 // holdValueFrom stores all ValueFrom values as they are on the adapter, 77 // which will then be put back in place by Apply() later, as 78 // ValueFrom isn't handled by the debug code when altering the env vars. 79 func holdValueFrom(env []v1.EnvVar) map[string]*v1.EnvVarSource { 80 from := make(map[string]*v1.EnvVarSource, len(env)) 81 for _, entry := range env { 82 from[entry.Name] = entry.ValueFrom 83 } 84 return from 85 } 86 87 func containerEnvToK8sEnv(env types.ContainerEnv, valueFrom map[string]*v1.EnvVarSource) []v1.EnvVar { 88 var k8sEnv []v1.EnvVar 89 for _, k := range env.Order { 90 k8sEnv = append(k8sEnv, v1.EnvVar{ 91 Name: k, 92 Value: env.Env[k], 93 ValueFrom: valueFrom[k], 94 }) 95 } 96 return k8sEnv 97 } 98 99 func k8sPortsToContainerPorts(k8sPorts []v1.ContainerPort) []types.ContainerPort { 100 var containerPorts []types.ContainerPort 101 for _, port := range k8sPorts { 102 containerPorts = append(containerPorts, types.ContainerPort{ 103 Name: port.Name, 104 HostPort: port.HostPort, 105 ContainerPort: port.ContainerPort, 106 Protocol: string(port.Protocol), 107 HostIP: port.HostIP, 108 }) 109 } 110 return containerPorts 111 } 112 113 func containerPortsToK8sPorts(containerPorts []types.ContainerPort) []v1.ContainerPort { 114 var k8sPorts []v1.ContainerPort 115 for _, port := range containerPorts { 116 k8sPorts = append(k8sPorts, v1.ContainerPort{ 117 Name: port.Name, 118 HostPort: port.HostPort, 119 ContainerPort: port.ContainerPort, 120 Protocol: v1.Protocol(port.Protocol), 121 HostIP: port.HostIP, 122 }) 123 } 124 return k8sPorts 125 }