mirror of
https://github.com/YuzuZensai/netbird-kubernetes-operator.git
synced 2026-09-13 10:49:15 +00:00
Fix sidecar injection when pod annotations is nil. (#250)
When a pod does not have any annotations the injector will fail. This change adds a test for the case and also fixes the issue. Fixes #246 Signed-off-by: Philip Laine <philip.laine@gmail.com>
This commit is contained in:
@@ -172,6 +172,9 @@ func (d *PodNetbirdInjector) Default(ctx context.Context, pod *corev1.Pod) error
|
|||||||
return fmt.Errorf("unknown injection mode %s", sidecarProfile.Spec.InjectionMode)
|
return fmt.Errorf("unknown injection mode %s", sidecarProfile.Spec.InjectionMode)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if pod.Annotations == nil {
|
||||||
|
pod.Annotations = map[string]string{}
|
||||||
|
}
|
||||||
pod.Annotations[SidecarProfileAnnotation] = sidecarProfile.Name
|
pod.Annotations[SidecarProfileAnnotation] = sidecarProfile.Name
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -4,16 +4,73 @@ package v1
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/go-openapi/testify/v2/require"
|
||||||
. "github.com/onsi/ginkgo/v2"
|
. "github.com/onsi/ginkgo/v2"
|
||||||
. "github.com/onsi/gomega"
|
. "github.com/onsi/gomega"
|
||||||
corev1 "k8s.io/api/core/v1"
|
corev1 "k8s.io/api/core/v1"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
|
kruntime "k8s.io/apimachinery/pkg/runtime"
|
||||||
|
"sigs.k8s.io/controller-runtime/pkg/client/fake"
|
||||||
|
|
||||||
nbv1 "github.com/netbirdio/kubernetes-operator/api/v1"
|
nbv1 "github.com/netbirdio/kubernetes-operator/api/v1"
|
||||||
nbv1alpha1 "github.com/netbirdio/kubernetes-operator/api/v1alpha1"
|
nbv1alpha1 "github.com/netbirdio/kubernetes-operator/api/v1alpha1"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestPodInjectorSidecarProfile(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
setupKey := &nbv1alpha1.SetupKey{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "test",
|
||||||
|
Namespace: "test",
|
||||||
|
},
|
||||||
|
Spec: nbv1alpha1.SetupKeySpec{
|
||||||
|
Name: "test",
|
||||||
|
Ephemeral: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
sidecarProfile := &nbv1alpha1.SidecarProfile{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "test",
|
||||||
|
Namespace: "test",
|
||||||
|
},
|
||||||
|
Spec: nbv1alpha1.SidecarProfileSpec{
|
||||||
|
SetupKeyRef: corev1.LocalObjectReference{
|
||||||
|
Name: "test",
|
||||||
|
},
|
||||||
|
InjectionMode: nbv1alpha1.InjectionModeContainer,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
scheme := kruntime.NewScheme()
|
||||||
|
err := corev1.AddToScheme(scheme)
|
||||||
|
require.NoError(t, err)
|
||||||
|
err = nbv1alpha1.AddToScheme(scheme)
|
||||||
|
require.NoError(t, err)
|
||||||
|
k8sClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(sidecarProfile, setupKey).Build()
|
||||||
|
injector := PodNetbirdInjector{
|
||||||
|
client: k8sClient,
|
||||||
|
managementURL: "https://api.netbird.io",
|
||||||
|
clientImage: "netbirdio/netbird:latest",
|
||||||
|
}
|
||||||
|
|
||||||
|
pod := &corev1.Pod{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "test",
|
||||||
|
Namespace: "test",
|
||||||
|
},
|
||||||
|
Spec: corev1.PodSpec{
|
||||||
|
Containers: []corev1.Container{},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
err = injector.Default(t.Context(), pod)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Len(t, pod.Spec.Containers, 1)
|
||||||
|
require.EqualT(t, "netbird", pod.Spec.Containers[0].Name)
|
||||||
|
}
|
||||||
|
|
||||||
var _ = Describe("Pod Webhook", func() {
|
var _ = Describe("Pod Webhook", func() {
|
||||||
var (
|
var (
|
||||||
obj *corev1.Pod
|
obj *corev1.Pod
|
||||||
@@ -141,59 +198,4 @@ var _ = Describe("Pod Webhook", func() {
|
|||||||
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
Context("When creating Pod with SidecarProfile", func() {
|
|
||||||
BeforeEach(func() {
|
|
||||||
sidecarProfile := &nbv1alpha1.SidecarProfile{
|
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
|
||||||
Name: "test",
|
|
||||||
Namespace: "test",
|
|
||||||
},
|
|
||||||
Spec: nbv1alpha1.SidecarProfileSpec{
|
|
||||||
SetupKeyRef: corev1.LocalObjectReference{
|
|
||||||
Name: "test",
|
|
||||||
},
|
|
||||||
InjectionMode: nbv1alpha1.InjectionModeContainer,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
Expect(k8sClient.Create(context.Background(), sidecarProfile)).To(Succeed())
|
|
||||||
})
|
|
||||||
|
|
||||||
AfterEach(func() {
|
|
||||||
sidecarProfile := &nbv1alpha1.SidecarProfile{
|
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
|
||||||
Name: "test",
|
|
||||||
Namespace: "test",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
Expect(k8sClient.Delete(ctx, sidecarProfile)).To(Succeed())
|
|
||||||
})
|
|
||||||
|
|
||||||
When("SetupKey doesn't exist", func() {
|
|
||||||
It("Should fail", func() {
|
|
||||||
Expect(defaulter.Default(context.Background(), obj)).To(HaveOccurred())
|
|
||||||
Expect(obj.Spec.Containers).To(HaveLen(1))
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
When("SetupKey exists", func() {
|
|
||||||
It("Should succeed", func() {
|
|
||||||
setupKey := &nbv1alpha1.SetupKey{
|
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
|
||||||
Name: "test",
|
|
||||||
Namespace: "test",
|
|
||||||
},
|
|
||||||
Spec: nbv1alpha1.SetupKeySpec{
|
|
||||||
Name: "test",
|
|
||||||
Ephemeral: true,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
Expect(k8sClient.Create(context.Background(), setupKey)).To(Succeed())
|
|
||||||
|
|
||||||
Expect(defaulter.Default(context.Background(), obj)).NotTo(HaveOccurred())
|
|
||||||
Expect(obj.Spec.Containers).To(HaveLen(2))
|
|
||||||
Expect(obj.Spec.Containers[1].Name).To(Equal("netbird"))
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user