Skip to content

Commit

Permalink
port DebuggingContainerEvents from API V1 to V2 (#6276)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarlonGamez authored Jul 22, 2021
1 parent 6f7acec commit 6713a61
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 1 deletion.
55 changes: 55 additions & 0 deletions pkg/skaffold/event/v2/debugging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
Copyright 2021 The Skaffold Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v2

import proto "github.com/GoogleContainerTools/skaffold/proto/v2"

// DebuggingContainerStarted notifies that a debuggable container has appeared.
func DebuggingContainerStarted(podName, containerName, namespace, artifact, runtime, workingDir string, debugPorts map[string]uint32) {
handler.handle(&proto.Event{
EventType: &proto.Event_DebuggingContainerEvent{
DebuggingContainerEvent: &proto.DebuggingContainerEvent{
Status: Started,
PodName: podName,
ContainerName: containerName,
Namespace: namespace,
Artifact: artifact,
Runtime: runtime,
WorkingDir: workingDir,
DebugPorts: debugPorts,
},
},
})
}

// DebuggingContainerTerminated notifies that a debuggable container has disappeared.
func DebuggingContainerTerminated(podName, containerName, namespace, artifact, runtime, workingDir string, debugPorts map[string]uint32) {
handler.handle(&proto.Event{
EventType: &proto.Event_DebuggingContainerEvent{
DebuggingContainerEvent: &proto.DebuggingContainerEvent{
Status: Terminated,
PodName: podName,
ContainerName: containerName,
Namespace: namespace,
Artifact: artifact,
Runtime: runtime,
WorkingDir: workingDir,
DebugPorts: debugPorts,
},
},
})
}
45 changes: 45 additions & 0 deletions pkg/skaffold/event/v2/debugging_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
Copyright 2021 The Skaffold Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v2

import (
"testing"

latestV1 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest/v1"
)

func TestDebuggingContainer(t *testing.T) {
defer func() { handler = newHandler() }()

handler = newHandler()
handler.state = emptyState(mockCfg([]latestV1.Pipeline{{}}, "test"))

found := func() bool {
for _, dc := range handler.getState().DebuggingContainers {
if dc.Namespace == "ns" && dc.PodName == "pod" && dc.ContainerName == "container" {
return true
}
}
return false
}
notFound := func() bool { return !found() }
wait(t, notFound)
DebuggingContainerStarted("pod", "container", "ns", "artifact", "runtime", "/", nil)
wait(t, found)
DebuggingContainerTerminated("pod", "container", "ns", "artifact", "runtime", "/", nil)
wait(t, notFound)
}
5 changes: 5 additions & 0 deletions pkg/skaffold/kubernetes/debugging/container_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@ import (

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/debug/annotations"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/event"
eventV2 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/event/v2"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes"
)

var (
// For testing
notifyDebuggingContainerStarted = event.DebuggingContainerStarted
notifyDebuggingContainerTerminated = event.DebuggingContainerTerminated
debuggingContainerStartedV2 = eventV2.DebuggingContainerStarted
debuggingContainerTerminatedV2 = eventV2.DebuggingContainerTerminated
)

type ContainerManager struct {
Expand Down Expand Up @@ -126,6 +129,7 @@ func (d *ContainerManager) checkPod(pod *v1.Pod) {
config.Runtime,
config.WorkingDir,
config.Ports)
debuggingContainerStartedV2(pod.Name, c.Name, pod.Namespace, config.Artifact, config.Runtime, config.WorkingDir, config.Ports)

case c.State.Terminated != nil && seen:
delete(d.active, key)
Expand All @@ -134,6 +138,7 @@ func (d *ContainerManager) checkPod(pod *v1.Pod) {
config.Runtime,
config.WorkingDir,
config.Ports)
debuggingContainerTerminatedV2(pod.Name, c.Name, pod.Namespace, config.Artifact, config.Runtime, config.WorkingDir, config.Ports)
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion pkg/skaffold/kubernetes/debugging/container_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,15 @@ func TestContainerManager(t *testing.T) {
testutil.Run(t, "simulation", func(t *testutil.T) {
startCount := 0
terminatedCount := 0
// Override event v1 funcs to do nothing to avoid additional overhead
t.Override(&notifyDebuggingContainerStarted, func(podName string, containerName string, namespace string, artifactImage string, runtime string, workingDir string, debugPorts map[string]uint32) {
startCount++
})
t.Override(&notifyDebuggingContainerTerminated, func(podName string, containerName string, namespace string, artifactImage string, runtime string, workingDir string, debugPorts map[string]uint32) {
})
t.Override(&debuggingContainerStartedV2, func(podName string, containerName string, namespace string, artifactImage string, runtime string, workingDir string, debugPorts map[string]uint32) {
startCount++
})
t.Override(&debuggingContainerTerminatedV2, func(podName string, containerName string, namespace string, artifactImage string, runtime string, workingDir string, debugPorts map[string]uint32) {
terminatedCount++
})
pod := v1.Pod{
Expand Down

0 comments on commit 6713a61

Please sign in to comment.