Skip to content

fix: use image of container in MigratorSpec #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,18 @@ func (hook *initInjector) handleInner(ctx context.Context, req admission.Request

// For each migrator, inject an initContainer.
for _, m := range migrators {
// Look for the container named in the migrator and pull the image from that. If no container
// matches, fall back to using the first container.
podIdx := 0
for i, p := range pod.Spec.Containers {
if p.Name == m.Spec.Container {
podIdx = i
}
}
initContainer := corev1.Container{
Name: fmt.Sprintf("migrate-wait-%s", m.Name),
Image: os.Getenv("WAITER_IMAGE"),
Command: []string{"/waiter", pod.Spec.Containers[0].Image, m.Namespace, m.Name, os.Getenv("API_HOSTNAME")},
Command: []string{"/waiter", pod.Spec.Containers[podIdx].Image, m.Namespace, m.Name, os.Getenv("API_HOSTNAME")},
Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceMemory: resource.MustParse("16M"),
Expand Down
107 changes: 107 additions & 0 deletions webhook/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,113 @@ var _ = Describe("InitInjector", func() {
Expect(pod.Spec.InitContainers[0].Command).To(Equal([]string{"/waiter", "fake", helper.Namespace, "testing", "migrations-operator.migration-operator.svc"}))
})

It("selects the specified container with a multi-container Pod", func() {
c := helper.TestClient

migrator := &migrationsv1beta1.Migrator{
ObjectMeta: metav1.ObjectMeta{Name: "testing"},
Spec: migrationsv1beta1.MigratorSpec{
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{"app": "testing"},
},
Container: "second",
},
}
c.Create(migrator)

pod := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{Name: "testing", Labels: map[string]string{"app": "testing"}},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "main",
Image: "fake",
},
{
Name: "second",
Image: "foo",
},
},
},
}
c.Create(pod)

c.EventuallyGetName("testing", pod)
Expect(pod.Spec.InitContainers).To(HaveLen(1))
Expect(pod.Spec.InitContainers[0].Command).To(Equal([]string{"/waiter", "foo", helper.Namespace, "testing", "migrations-operator.migration-operator.svc"}))
})

It("falls back to the first container if the specified container doesn't exist", func() {
c := helper.TestClient

migrator := &migrationsv1beta1.Migrator{
ObjectMeta: metav1.ObjectMeta{Name: "testing"},
Spec: migrationsv1beta1.MigratorSpec{
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{"app": "testing"},
},
Container: "third",
},
}
c.Create(migrator)

pod := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{Name: "testing", Labels: map[string]string{"app": "testing"}},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "main",
Image: "bar",
},
{
Name: "second",
Image: "foo",
},
},
},
}
c.Create(pod)

c.EventuallyGetName("testing", pod)
Expect(pod.Spec.InitContainers).To(HaveLen(1))
Expect(pod.Spec.InitContainers[0].Command).To(Equal([]string{"/waiter", "bar", helper.Namespace, "testing", "migrations-operator.migration-operator.svc"}))
})

It("uses the first container image if no container name is supplied with a multi-container Pod", func() {
c := helper.TestClient

migrator := &migrationsv1beta1.Migrator{
ObjectMeta: metav1.ObjectMeta{Name: "testing"},
Spec: migrationsv1beta1.MigratorSpec{
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{"app": "testing"},
},
},
}
c.Create(migrator)

pod := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{Name: "testing", Labels: map[string]string{"app": "testing"}},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "main",
Image: "bar",
},
{
Name: "second",
Image: "foo",
},
},
},
}
c.Create(pod)

c.EventuallyGetName("testing", pod)
Expect(pod.Spec.InitContainers).To(HaveLen(1))
Expect(pod.Spec.InitContainers[0].Command).To(Equal([]string{"/waiter", "bar", helper.Namespace, "testing", "migrations-operator.migration-operator.svc"}))
})

It("doesn't inject with a non-matching migrator", func() {
c := helper.TestClient

Expand Down