...

Source file src/edge-infra.dev/pkg/edge/linkerd/k8s/controllers/integration/workloadinjection_controller_test.go

Documentation: edge-infra.dev/pkg/edge/linkerd/k8s/controllers/integration

     1  package integration
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  	"github.com/stretchr/testify/require"
     8  	"k8s.io/apimachinery/pkg/types"
     9  
    10  	l5dv1alpha1 "edge-infra.dev/pkg/edge/linkerd/k8s/apis/linkerd/v1alpha1"
    11  	"edge-infra.dev/pkg/k8s/runtime/conditions"
    12  	"edge-infra.dev/pkg/k8s/runtime/inventory"
    13  	"edge-infra.dev/test/f2"
    14  	"edge-infra.dev/test/f2/x/ktest"
    15  )
    16  
    17  func TestLinkerdWorkloadInjectionController(t *testing.T) {
    18  	var (
    19  		k *ktest.K8s
    20  	)
    21  	feature := f2.NewFeature("Workload Injection Controller").
    22  		Setup("setup", func(ctx f2.Context, t *testing.T) f2.Context {
    23  			k = ktest.FromContextT(ctx, t)
    24  			return ctx
    25  		}).
    26  		Test("workload injection empty spec injects proxy into all namespaces", func(ctx f2.Context, t *testing.T) f2.Context {
    27  			workloadInjection := l5dreinjectionjob(l5dv1alpha1.LinkerdWorkloadInjectionSpec{})
    28  			require.NoError(t, k.Client.Create(ctx, workloadInjection))
    29  			assert.Eventually(t, func() bool {
    30  				_ = k.Client.Get(ctx, types.NamespacedName{Name: workloadInjection.GetName()}, workloadInjection)
    31  				return workloadInjection.IsCompleted() && conditions.IsReady(workloadInjection)
    32  			}, ktest.Timeout, ktest.Tick, "Wait for linkerd reinjection job to complete")
    33  
    34  			assert.Contains(t, workloadInjection.Status.Inventory.Entries, inventory.ResourceRef{ID: expectedInventoryID1, Version: expectedInventoryVersion})
    35  			assert.Contains(t, workloadInjection.Status.Inventory.Entries, inventory.ResourceRef{ID: expectedInventoryID2, Version: expectedInventoryVersion})
    36  			assert.Equal(t, 2, len(workloadInjection.Status.Inventory.Entries))
    37  			assert.Equal(t, inventory.ResourceInventory{}, *workloadInjection.Status.FailedInventory)
    38  
    39  			require.NoError(t, k.Client.Delete(ctx, workloadInjection))
    40  			return ctx
    41  		}).
    42  		Test("proxy injected into specified namespaces", func(ctx f2.Context, t *testing.T) f2.Context {
    43  			workloadInjection := l5dreinjectionjob(l5dv1alpha1.LinkerdWorkloadInjectionSpec{
    44  				Namespaces: []string{testNS1Name},
    45  			})
    46  			require.NoError(t, k.Client.Create(ctx, workloadInjection))
    47  			assert.Eventually(t, func() bool {
    48  				_ = k.Client.Get(ctx, types.NamespacedName{Name: workloadInjection.GetName()}, workloadInjection)
    49  				return workloadInjection.IsCompleted() && conditions.IsReady(workloadInjection)
    50  			}, ktest.Timeout, ktest.Tick, "Wait for linkerd reinjection job to complete")
    51  
    52  			assert.Contains(t, workloadInjection.Status.Inventory.Entries, inventory.ResourceRef{ID: expectedInventoryID1, Version: expectedInventoryVersion})
    53  			assert.Equal(t, 1, len(workloadInjection.Status.Inventory.Entries))
    54  			assert.Equal(t, inventory.ResourceInventory{}, *workloadInjection.Status.FailedInventory)
    55  
    56  			require.NoError(t, k.Client.Delete(ctx, workloadInjection))
    57  			return ctx
    58  		}).
    59  		Test("recreating workload injection CR should inject proxies into new namespaces", func(ctx f2.Context, t *testing.T) f2.Context {
    60  			workloadInjection := l5dreinjectionjob(l5dv1alpha1.LinkerdWorkloadInjectionSpec{
    61  				Namespaces: []string{testNS1Name},
    62  			})
    63  			require.NoError(t, k.Client.Create(ctx, workloadInjection))
    64  
    65  			assert.Eventually(t, func() bool {
    66  				_ = k.Client.Get(ctx, types.NamespacedName{Name: workloadInjection.GetName()}, workloadInjection)
    67  				return workloadInjection.IsCompleted() && conditions.IsReady(workloadInjection)
    68  			}, ktest.Timeout, ktest.Tick, "Wait for linkerd reinjection job to complete")
    69  
    70  			assert.Contains(t, workloadInjection.Status.Inventory.Entries, inventory.ResourceRef{ID: expectedInventoryID1, Version: expectedInventoryVersion})
    71  			assert.Equal(t, 1, len(workloadInjection.Status.Inventory.Entries))
    72  			assert.Equal(t, inventory.ResourceInventory{}, *workloadInjection.Status.FailedInventory)
    73  
    74  			require.NoError(t, k.Client.Delete(ctx, workloadInjection))
    75  
    76  			newWorkloadinjection := l5dreinjectionjob(l5dv1alpha1.LinkerdWorkloadInjectionSpec{
    77  				Namespaces: []string{testNS1Name, testNS2Name},
    78  			})
    79  			require.NoError(t, k.Client.Create(ctx, newWorkloadinjection))
    80  
    81  			assert.Eventually(t, func() bool {
    82  				_ = k.Client.Get(ctx, types.NamespacedName{Name: newWorkloadinjection.GetName()}, newWorkloadinjection)
    83  				return newWorkloadinjection.IsCompleted() && conditions.IsReady(newWorkloadinjection)
    84  			}, ktest.Timeout, ktest.Tick, "Wait for linkerd reinjection job to complete")
    85  
    86  			assert.Contains(t, newWorkloadinjection.Status.Inventory.Entries, inventory.ResourceRef{ID: expectedInventoryID1, Version: expectedInventoryVersion})
    87  			assert.Contains(t, newWorkloadinjection.Status.Inventory.Entries, inventory.ResourceRef{ID: expectedInventoryID2, Version: expectedInventoryVersion})
    88  			assert.Equal(t, 2, len(newWorkloadinjection.Status.Inventory.Entries))
    89  			assert.Equal(t, inventory.ResourceInventory{}, *newWorkloadinjection.Status.FailedInventory)
    90  
    91  			require.NoError(t, k.Client.Delete(ctx, newWorkloadinjection))
    92  			return ctx
    93  		}).
    94  		Test("proxy not injected into pods on host network", func(ctx f2.Context, t *testing.T) f2.Context {
    95  			hostDeployment := createDeployment(testNS1Name, "host1")
    96  			require.NoError(t, k.Client.Create(ctx, hostDeployment.DeepCopy()))
    97  			k.WaitOn(t, k.ObjExists(hostDeployment))
    98  
    99  			pod := createPod(testNS1Name, "hostpod", true, ownerReference(hostDeployment))
   100  			require.NoError(t, k.Client.Create(ctx, pod))
   101  			workloadInjection := l5dreinjectionjob(l5dv1alpha1.LinkerdWorkloadInjectionSpec{
   102  				Namespaces: []string{testNS1Name},
   103  			})
   104  			require.NoError(t, k.Client.Create(ctx, workloadInjection))
   105  			assert.Eventually(t, func() bool {
   106  				_ = k.Client.Get(ctx, types.NamespacedName{Name: workloadInjection.GetName()}, workloadInjection)
   107  				return workloadInjection.IsCompleted() && conditions.IsReady(workloadInjection)
   108  			}, ktest.Timeout, ktest.Tick, "Wait for linkerd reinjection job to complete")
   109  
   110  			assert.Contains(t, workloadInjection.Status.Inventory.Entries, inventory.ResourceRef{ID: expectedInventoryID1, Version: expectedInventoryVersion})
   111  			assert.Equal(t, 1, len(workloadInjection.Status.Inventory.Entries))
   112  			assert.Equal(t, inventory.ResourceInventory{}, *workloadInjection.Status.FailedInventory)
   113  
   114  			require.NoError(t, k.Client.Delete(ctx, workloadInjection))
   115  			return ctx
   116  		}).
   117  		Feature()
   118  
   119  	f.Test(t, feature)
   120  }
   121  

View as plain text