Skip to content

Commit

Permalink
pkg/pod: extracting function from the package…
Browse files Browse the repository at this point in the history
… to smaller packages.

Signed-off-by: Vincent Demeester <vdemeest@redhat.com>
  • Loading branch information
vdemeester committed Oct 22, 2021
1 parent e73bfb1 commit d2e9d01
Show file tree
Hide file tree
Showing 14 changed files with 415 additions and 316 deletions.
114 changes: 114 additions & 0 deletions pkg/internal/sidecars/sidecars.go
@@ -0,0 +1,114 @@
/*
Copyright 2021 The Tekton 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 sidecars

import (
"context"
"fmt"
"strings"

"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
"github.com/tektoncd/pipeline/pkg/pod"
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)

const (
sidecarPrefix = "sidecar-"
)

// Ready returns true if all of the Pod's sidecars are Ready or
// Terminated.
func Ready(podStatus corev1.PodStatus) bool {
if podStatus.Phase != corev1.PodRunning {
return false
}
for _, s := range podStatus.ContainerStatuses {
// If the step indicates that it's a step, skip it.
// An injected sidecar might not have the "sidecar-" prefix, so
// we can't just look for that prefix, we need to look at any
// non-step container.
if pod.IsContainerStep(s.Name) {
continue
}
if s.State.Running != nil && s.Ready {
continue
}
if s.State.Terminated != nil {
continue
}
return false
}
return true
}

// Stop updates sidecar containers in the Pod to a nop image, which
// exits successfully immediately.
func Stop(ctx context.Context, nopImage string, kubeclient kubernetes.Interface, namespace, name string) (*corev1.Pod, error) {
newPod, err := kubeclient.CoreV1().Pods(namespace).Get(ctx, name, metav1.GetOptions{})
if k8serrors.IsNotFound(err) {
// return NotFound as-is, since the K8s error checks don't handle wrapping.
return nil, err
} else if err != nil {
return nil, fmt.Errorf("error getting Pod %q when stopping sidecars: %w", name, err)
}

updated := false
if newPod.Status.Phase == corev1.PodRunning {
for _, s := range newPod.Status.ContainerStatuses {
// Stop any running container that isn't a step.
// An injected sidecar container might not have the
// "sidecar-" prefix, so we can't just look for that
// prefix.
if !pod.IsContainerStep(s.Name) && s.State.Running != nil {
for j, c := range newPod.Spec.Containers {
if c.Name == s.Name && c.Image != nopImage {
updated = true
newPod.Spec.Containers[j].Image = nopImage
}
}
}
}
}
if updated {
if newPod, err = kubeclient.CoreV1().Pods(newPod.Namespace).Update(ctx, newPod, metav1.UpdateOptions{}); err != nil {
return nil, fmt.Errorf("error stopping sidecars of Pod %q: %w", name, err)
}
}
return newPod, nil
}

// IsRunning determines if any SidecarStatus on a TaskRun
// is still running.
func IsRunning(tr *v1beta1.TaskRun) bool {
for _, sidecar := range tr.Status.Sidecars {
if sidecar.Terminated == nil {
return true
}
}

return false
}

// IsContainerSidecar returns true if the container name indicates that it
// represents a sidecar.
func IsContainerSidecar(name string) bool { return strings.HasPrefix(name, sidecarPrefix) }

// TrimPrefix returns the container name, stripped of its sidecar
// prefix.
func TrimPrefix(name string) string { return strings.TrimPrefix(name, sidecarPrefix) }
146 changes: 146 additions & 0 deletions pkg/internal/sidecars/sidecars_test.go
@@ -0,0 +1,146 @@
/*
Copyright 2021 The Tekton 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 sidecars_test

import (
"context"
"testing"
"time"

"github.com/google/go-cmp/cmp"
"github.com/tektoncd/pipeline/pkg/internal/sidecars"
"github.com/tektoncd/pipeline/test/diff"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
fakek8s "k8s.io/client-go/kubernetes/fake"
)

const (
stepPrefix = "step-" // FIXME(vdemeester) deduplicate this
sidecarPrefix = "sidecar-" // FIXME(vdemeester) deduplicate this
nopImage = "nop-image"
)

// TestStopSidecars tests stopping sidecars by updating their images to a nop
// image.
func TestStopSidecars(t *testing.T) {
stepContainer := corev1.Container{
Name: stepPrefix + "my-step",
Image: "foo",
}
sidecarContainer := corev1.Container{
Name: sidecarPrefix + "my-sidecar",
Image: "original-image",
}
stoppedSidecarContainer := corev1.Container{
Name: sidecarContainer.Name,
Image: nopImage,
}

// This is a container that doesn't start with the "sidecar-" prefix,
// which indicates it was injected into the Pod by a Mutating Webhook
// Admission Controller. Injected sidecars should also be stopped if
// they're running.
injectedSidecar := corev1.Container{
Name: "injected",
Image: "original-injected-image",
}
stoppedInjectedSidecar := corev1.Container{
Name: injectedSidecar.Name,
Image: nopImage,
}

for _, c := range []struct {
desc string
pod corev1.Pod
wantContainers []corev1.Container
}{{
desc: "Running sidecars (incl injected) should be stopped",
pod: corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "test-pod",
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{stepContainer, sidecarContainer, injectedSidecar},
},
Status: corev1.PodStatus{
Phase: corev1.PodRunning,
ContainerStatuses: []corev1.ContainerStatus{{
// Step state doesn't matter.
}, {
Name: sidecarContainer.Name,
// Sidecar is running.
State: corev1.ContainerState{Running: &corev1.ContainerStateRunning{StartedAt: metav1.NewTime(time.Now())}},
}, {
Name: injectedSidecar.Name,
// Injected sidecar is running.
State: corev1.ContainerState{Running: &corev1.ContainerStateRunning{StartedAt: metav1.NewTime(time.Now())}},
}},
},
},
wantContainers: []corev1.Container{stepContainer, stoppedSidecarContainer, stoppedInjectedSidecar},
}, {
desc: "Pending Pod should not be updated",
pod: corev1.Pod{
ObjectMeta: metav1.ObjectMeta{Name: "test-pod"},
Spec: corev1.PodSpec{
Containers: []corev1.Container{stepContainer, sidecarContainer},
},
Status: corev1.PodStatus{
Phase: corev1.PodPending,
// Container states don't matter.
},
},
wantContainers: []corev1.Container{stepContainer, sidecarContainer},
}, {
desc: "Non-Running sidecars should not be stopped",
pod: corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "test-pod",
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{stepContainer, sidecarContainer, injectedSidecar},
},
Status: corev1.PodStatus{
Phase: corev1.PodRunning,
ContainerStatuses: []corev1.ContainerStatus{{
// Step state doesn't matter.
}, {
Name: sidecarContainer.Name,
// Sidecar is waiting.
State: corev1.ContainerState{Waiting: &corev1.ContainerStateWaiting{}},
}, {
Name: injectedSidecar.Name,
// Injected sidecar is waiting.
State: corev1.ContainerState{Waiting: &corev1.ContainerStateWaiting{}},
}},
},
},
wantContainers: []corev1.Container{stepContainer, sidecarContainer, injectedSidecar},
}} {
t.Run(c.desc, func(t *testing.T) {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
kubeclient := fakek8s.NewSimpleClientset(&c.pod)
if got, err := sidecars.Stop(ctx, nopImage, kubeclient, c.pod.Namespace, c.pod.Name); err != nil {
t.Errorf("error stopping sidecar: %v", err)
} else if d := cmp.Diff(c.wantContainers, got.Spec.Containers); d != "" {
t.Errorf("Containers Diff %s", diff.PrintWantGot(d))
}
})
}
}
73 changes: 8 additions & 65 deletions pkg/pod/entrypoint.go
Expand Up @@ -28,21 +28,21 @@ import (

"github.com/tektoncd/pipeline/pkg/apis/pipeline"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
"github.com/tektoncd/pipeline/pkg/pod/script"
"gomodules.xyz/jsonpatch/v2"
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
)

const (
binVolumeName = "tekton-internal-bin"
binDir = "/tekton/bin"
binVolumeName = "tekton-internal-bin" // FIXME(vdemeester) remove duplication with script package
binDir = "/tekton/bin" // FIXME(vdemeester) remove duplication with script package
entrypointBinary = binDir + "/entrypoint"

runVolumeName = "tekton-internal-run"
runDir = "/tekton/run"
runDir = "/tekton/run" // FIXME(vdemeester) remove duplication with script package

downwardVolumeName = "tekton-internal-downward"
downwardMountPoint = "/tekton/downward"
Expand All @@ -53,12 +53,11 @@ const (

stepPrefix = "step-"
sidecarPrefix = "sidecar-"

breakpointOnFailure = "onFailure"
)

var (
// TODO(#1605): Generate volumeMount names, to avoid collisions.
// FIXME(vdemeester) remove duplication with script package
binMount = corev1.VolumeMount{
Name: binVolumeName,
MountPath: binDir,
Expand Down Expand Up @@ -160,7 +159,7 @@ func orderContainers(commonExtraEntrypointArgs []string, steps []corev1.Containe
breakpoints := breakpointConfig.Breakpoint
for _, b := range breakpoints {
// TODO(TEP #0042): Add other breakpoints
if b == breakpointOnFailure {
if b == script.BreakpointOnFailure {
argsForEntrypoint = append(argsForEntrypoint, "-breakpoint_on_failure")
}
}
Expand Down Expand Up @@ -219,68 +218,12 @@ func UpdateReady(ctx context.Context, kubeclient kubernetes.Interface, pod corev
return err
}

// StopSidecars updates sidecar containers in the Pod to a nop image, which
// exits successfully immediately.
func StopSidecars(ctx context.Context, nopImage string, kubeclient kubernetes.Interface, namespace, name string) (*corev1.Pod, error) {
newPod, err := kubeclient.CoreV1().Pods(namespace).Get(ctx, name, metav1.GetOptions{})
if k8serrors.IsNotFound(err) {
// return NotFound as-is, since the K8s error checks don't handle wrapping.
return nil, err
} else if err != nil {
return nil, fmt.Errorf("error getting Pod %q when stopping sidecars: %w", name, err)
}

updated := false
if newPod.Status.Phase == corev1.PodRunning {
for _, s := range newPod.Status.ContainerStatuses {
// Stop any running container that isn't a step.
// An injected sidecar container might not have the
// "sidecar-" prefix, so we can't just look for that
// prefix.
if !IsContainerStep(s.Name) && s.State.Running != nil {
for j, c := range newPod.Spec.Containers {
if c.Name == s.Name && c.Image != nopImage {
updated = true
newPod.Spec.Containers[j].Image = nopImage
}
}
}
}
}
if updated {
if newPod, err = kubeclient.CoreV1().Pods(newPod.Namespace).Update(ctx, newPod, metav1.UpdateOptions{}); err != nil {
return nil, fmt.Errorf("error stopping sidecars of Pod %q: %w", name, err)
}
}
return newPod, nil
}

// IsSidecarStatusRunning determines if any SidecarStatus on a TaskRun
// is still running.
func IsSidecarStatusRunning(tr *v1beta1.TaskRun) bool {
for _, sidecar := range tr.Status.Sidecars {
if sidecar.Terminated == nil {
return true
}
}

return false
}

// IsContainerStep returns true if the container name indicates that it
// isContainerStep returns true if the container name indicates that it
// represents a step.
func IsContainerStep(name string) bool { return strings.HasPrefix(name, stepPrefix) }

// isContainerSidecar returns true if the container name indicates that it
// represents a sidecar.
func isContainerSidecar(name string) bool { return strings.HasPrefix(name, sidecarPrefix) }

// trimStepPrefix returns the container name, stripped of its step prefix.
func trimStepPrefix(name string) string { return strings.TrimPrefix(name, stepPrefix) }

// TrimSidecarPrefix returns the container name, stripped of its sidecar
// prefix.
func TrimSidecarPrefix(name string) string { return strings.TrimPrefix(name, sidecarPrefix) }
func TrimStepPrefix(name string) string { return strings.TrimPrefix(name, stepPrefix) }

// StepName returns the step name after adding "step-" prefix to the actual step name or
// returns "step-unnamed-<step-index>" if not specified
Expand Down

0 comments on commit d2e9d01

Please sign in to comment.