github.com/operator-framework/operator-lifecycle-manager@v0.30.0/test/e2e/catsrc_pod_config_e2e_test.go (about)

     1  package e2e
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	. "github.com/onsi/ginkgo/v2"
     8  	. "github.com/onsi/gomega"
     9  	"github.com/operator-framework/api/pkg/operators/v1alpha1"
    10  	"github.com/operator-framework/operator-lifecycle-manager/test/e2e/ctx"
    11  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    12  	k8scontrollerclient "sigs.k8s.io/controller-runtime/pkg/client"
    13  
    14  	corev1 "k8s.io/api/core/v1"
    15  )
    16  
    17  const catalogSourceLabel = "olm.catalogSource"
    18  
    19  var _ = By
    20  
    21  var _ = Describe("CatalogSource Grpc Pod Config", func() {
    22  
    23  	var (
    24  		generatedNamespace corev1.Namespace
    25  	)
    26  
    27  	BeforeEach(func() {
    28  		generatedNamespace = SetupGeneratedTestNamespace(genName("catsrc-grpc-pod-config-e2e-"))
    29  	})
    30  
    31  	AfterEach(func() {
    32  		TeardownNamespace(generatedNamespace.GetName())
    33  	})
    34  
    35  	When("the user wants more control over where the grpc catalog source pod gets scheduled", func() {
    36  		var (
    37  			client              k8scontrollerclient.Client
    38  			catalogSource       *v1alpha1.CatalogSource
    39  			defaultNodeSelector = map[string]string{
    40  				"kubernetes.io/os": "linux",
    41  			}
    42  			defaultTolerations       []corev1.Toleration = nil
    43  			catalogSourceName                            = "test-catsrc"
    44  			defaultPriorityClassName                     = ""
    45  		)
    46  
    47  		BeforeEach(func() {
    48  			client = ctx.Ctx().Client()
    49  
    50  			By("must be a grpc source type with spec.image defined")
    51  			catalogSource = &v1alpha1.CatalogSource{
    52  				ObjectMeta: metav1.ObjectMeta{
    53  					Name:      catalogSourceName,
    54  					Namespace: generatedNamespace.GetName(),
    55  				},
    56  				Spec: v1alpha1.CatalogSourceSpec{
    57  					SourceType: v1alpha1.SourceTypeGrpc,
    58  					Image:      "repo/image:tag",
    59  					GrpcPodConfig: &v1alpha1.GrpcPodConfig{
    60  						SecurityContextConfig: v1alpha1.Restricted,
    61  					},
    62  				},
    63  			}
    64  		})
    65  
    66  		AfterEach(func() {
    67  			By("assume the catalog source was created and just delete it")
    68  			_ = client.Delete(context.TODO(), catalogSource)
    69  
    70  			By("wait for it to go away")
    71  			Expect(waitForDelete(func() error {
    72  				return client.Get(context.TODO(), k8scontrollerclient.ObjectKey{
    73  					Name:      catalogSource.GetName(),
    74  					Namespace: catalogSource.GetNamespace(),
    75  				}, &v1alpha1.CatalogSource{})
    76  			})).To(BeNil())
    77  		})
    78  
    79  		It("should override the pod's spec.priorityClassName", func() {
    80  			var overridenPriorityClassName = "system-node-critical"
    81  
    82  			By("create catalog source")
    83  			catalogSource.Spec.GrpcPodConfig = &v1alpha1.GrpcPodConfig{
    84  				PriorityClassName:     &overridenPriorityClassName,
    85  				SecurityContextConfig: v1alpha1.Restricted,
    86  			}
    87  			mustCreateCatalogSource(client, catalogSource)
    88  
    89  			By("Check overrides are present in the spec")
    90  			catalogSourcePod := mustGetCatalogSourcePod(client, catalogSource)
    91  			Expect(catalogSourcePod).ToNot(BeNil())
    92  			Expect(catalogSourcePod.Spec.NodeSelector).To(BeEquivalentTo(defaultNodeSelector))
    93  			Expect(catalogSourcePod.Spec.Tolerations).To(ContainElements(defaultTolerations))
    94  			Expect(catalogSourcePod.Spec.PriorityClassName).To(Equal(overridenPriorityClassName))
    95  		})
    96  
    97  		It("should override the pod's spec.priorityClassName when it is empty", func() {
    98  			var overridenPriorityClassName = ""
    99  
   100  			By("create catalog source")
   101  			catalogSource.Spec.GrpcPodConfig = &v1alpha1.GrpcPodConfig{
   102  				PriorityClassName:     &overridenPriorityClassName,
   103  				SecurityContextConfig: v1alpha1.Restricted,
   104  			}
   105  			mustCreateCatalogSource(client, catalogSource)
   106  
   107  			By("Check overrides are present in the spec")
   108  			catalogSourcePod := mustGetCatalogSourcePod(client, catalogSource)
   109  			Expect(catalogSourcePod).ToNot(BeNil())
   110  			Expect(catalogSourcePod.Spec.NodeSelector).To(BeEquivalentTo(defaultNodeSelector))
   111  			Expect(catalogSourcePod.Spec.Tolerations).To(ContainElements(defaultTolerations))
   112  			Expect(catalogSourcePod.Spec.PriorityClassName).To(Equal(overridenPriorityClassName))
   113  		})
   114  
   115  		It("should override the pod's spec.nodeSelector", func() {
   116  			var overridenNodeSelector = map[string]string{
   117  				"kubernetes.io/os": "linux",
   118  				"some":             "tag",
   119  			}
   120  
   121  			By("create catalog source")
   122  			catalogSource.Spec.GrpcPodConfig = &v1alpha1.GrpcPodConfig{
   123  				NodeSelector:          overridenNodeSelector,
   124  				SecurityContextConfig: v1alpha1.Restricted,
   125  			}
   126  			mustCreateCatalogSource(client, catalogSource)
   127  
   128  			By("Check overrides are present in the spec")
   129  			catalogSourcePod := mustGetCatalogSourcePod(client, catalogSource)
   130  			Expect(catalogSourcePod).ToNot(BeNil())
   131  			Expect(catalogSourcePod.Spec.NodeSelector).To(BeEquivalentTo(overridenNodeSelector))
   132  			Expect(catalogSourcePod.Spec.Tolerations).To(ContainElements(defaultTolerations))
   133  			Expect(catalogSourcePod.Spec.PriorityClassName).To(Equal(defaultPriorityClassName))
   134  		})
   135  
   136  		It("should override the pod's spec.tolerations", func() {
   137  			var tolerationSeconds int64 = 120
   138  			var overriddenTolerations = []corev1.Toleration{
   139  				{
   140  					Key:               "some/key",
   141  					Operator:          corev1.TolerationOpExists,
   142  					Effect:            corev1.TaintEffectNoExecute,
   143  					TolerationSeconds: &tolerationSeconds,
   144  				},
   145  				{
   146  					Key:      "someother/key",
   147  					Operator: corev1.TolerationOpEqual,
   148  					Effect:   corev1.TaintEffectNoSchedule,
   149  				},
   150  			}
   151  
   152  			By("create catalog source")
   153  			catalogSource.Spec.GrpcPodConfig = &v1alpha1.GrpcPodConfig{
   154  				Tolerations:           overriddenTolerations,
   155  				SecurityContextConfig: v1alpha1.Restricted,
   156  			}
   157  			mustCreateCatalogSource(client, catalogSource)
   158  
   159  			By("Check overrides are present in the spec")
   160  			catalogSourcePod := mustGetCatalogSourcePod(client, catalogSource)
   161  			Expect(catalogSourcePod).ToNot(BeNil())
   162  			Expect(catalogSourcePod.Spec.NodeSelector).To(BeEquivalentTo(defaultNodeSelector))
   163  			Expect(catalogSourcePod.Spec.Tolerations).To(ContainElements(overriddenTolerations))
   164  			Expect(catalogSourcePod.Spec.PriorityClassName).To(Equal(defaultPriorityClassName))
   165  		})
   166  	})
   167  })
   168  
   169  func mustGetCatalogSourcePod(client k8scontrollerclient.Client, catalogSource *v1alpha1.CatalogSource) *corev1.Pod {
   170  	var podList = corev1.PodList{}
   171  
   172  	var opts = []k8scontrollerclient.ListOption{
   173  		k8scontrollerclient.InNamespace(catalogSource.GetNamespace()),
   174  		// NOTE: this will fail if we stop setting the label on the catalog source pod
   175  		k8scontrollerclient.MatchingLabels{
   176  			catalogSourceLabel: catalogSource.GetName(),
   177  		},
   178  	}
   179  
   180  	// Try to get a pod until its found and there's only one of them
   181  	Eventually(func() error {
   182  		if err := client.List(context.TODO(), &podList, opts...); err != nil {
   183  			return err
   184  		}
   185  		if len(podList.Items) != 1 {
   186  			return fmt.Errorf("expecting one catalog source pod but found %d", len(podList.Items))
   187  		}
   188  		return nil
   189  	}).Should(BeNil())
   190  
   191  	return &podList.Items[0]
   192  }
   193  
   194  func mustCreateCatalogSource(client k8scontrollerclient.Client, catalogSource *v1alpha1.CatalogSource) {
   195  	// create the object
   196  	Expect(client.Create(context.TODO(), catalogSource)).To(BeNil())
   197  
   198  	// wait for object to be appear
   199  	Eventually(func() error {
   200  		return client.Get(context.TODO(), k8scontrollerclient.ObjectKey{
   201  			Name:      catalogSource.Name,
   202  			Namespace: catalogSource.GetNamespace(),
   203  		}, &v1alpha1.CatalogSource{})
   204  	}).Should(BeNil())
   205  }