github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/docker/debugger/debug.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 debugger 18 19 import ( 20 "context" 21 "sync" 22 23 "github.com/docker/docker/api/types/container" 24 "github.com/docker/docker/api/types/mount" 25 26 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/debug/types" 27 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/event" 28 eventV2 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/event/v2" 29 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/graph" 30 ) 31 32 var ( 33 // For testing 34 notifyDebuggingContainerStarted = event.DebuggingContainerStarted 35 notifyDebuggingContainerTerminated = event.DebuggingContainerTerminated 36 debuggingContainerStartedV2 = eventV2.DebuggingContainerStarted 37 debuggingContainerTerminatedV2 = eventV2.DebuggingContainerTerminated 38 ) 39 40 type DebugManager struct { 41 insecureRegistries map[string]bool 42 debugHelpersRegistry string 43 44 images []string 45 configurations map[string]types.ContainerDebugConfiguration 46 47 supportMounts map[string]mount.Mount 48 mountLock sync.Mutex 49 } 50 51 func NewDebugManager(insecureRegistries map[string]bool, debugHelpersRegistry string) *DebugManager { 52 return &DebugManager{ 53 insecureRegistries: insecureRegistries, 54 debugHelpersRegistry: debugHelpersRegistry, 55 configurations: make(map[string]types.ContainerDebugConfiguration), 56 supportMounts: make(map[string]mount.Mount), 57 } 58 } 59 60 func (d *DebugManager) ConfigurationForImage(image string) types.ContainerDebugConfiguration { 61 if d == nil { 62 return types.ContainerDebugConfiguration{} 63 } 64 return d.configurations[image] 65 } 66 67 func (d *DebugManager) AddSupportMount(image, mountID string) { 68 d.mountLock.Lock() 69 defer d.mountLock.Unlock() 70 d.supportMounts[image] = mount.Mount{ 71 Type: mount.TypeVolume, 72 Source: mountID, 73 Target: "/dbg", 74 } 75 } 76 77 func (d *DebugManager) HasMount(image string) bool { 78 d.mountLock.Lock() 79 defer d.mountLock.Unlock() 80 _, ok := d.supportMounts[image] 81 return ok 82 } 83 84 func (d *DebugManager) SupportMounts() map[string]mount.Mount { 85 if d == nil { 86 return nil 87 } 88 return d.supportMounts 89 } 90 91 func (d *DebugManager) Start(context.Context) error { 92 if d == nil { 93 return nil 94 } 95 for _, image := range d.images { 96 config := d.configurations[image] 97 notifyDebuggingContainerStarted( 98 "", // no pod 99 image, 100 "", // no namespace 101 config.Artifact, 102 config.Runtime, 103 config.WorkingDir, 104 config.Ports) 105 debuggingContainerStartedV2("", image, "", config.Artifact, config.Runtime, config.WorkingDir, config.Ports) 106 } 107 return nil 108 } 109 110 func (d *DebugManager) Stop() { 111 if d == nil { 112 return 113 } 114 for _, image := range d.images { 115 config := d.configurations[image] 116 notifyDebuggingContainerTerminated( 117 "", // no pod 118 image, 119 "", // no namespace 120 config.Artifact, 121 config.Runtime, 122 config.WorkingDir, 123 config.Ports) 124 debuggingContainerTerminatedV2("", image, "", config.Artifact, config.Runtime, config.WorkingDir, config.Ports) 125 } 126 d.images = nil 127 d.configurations = make(map[string]types.ContainerDebugConfiguration) 128 } 129 130 func (d *DebugManager) Name() string { return "Docker Debug Manager" } 131 132 func (d *DebugManager) TransformImage(ctx context.Context, artifact graph.Artifact, cfg *container.Config) ([]*container.Config, error) { 133 if d == nil { 134 return nil, nil 135 } 136 configurations, initContainers, err := TransformImage(ctx, artifact, cfg, d.insecureRegistries, d.debugHelpersRegistry) 137 if err != nil { 138 return nil, err 139 } 140 d.images = append(d.images, cfg.Image) 141 for k, v := range configurations { 142 d.configurations[k] = v 143 } 144 145 return initContainers, nil 146 }