Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pkg/pod: extracting function from the package…
… to smaller packages. Signed-off-by: Vincent Demeester <vdemeest@redhat.com>
- Loading branch information
1 parent
a159e75
commit 9d738c0
Showing
14 changed files
with
415 additions
and
316 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.