github.com/IBM-Blockchain/fabric-operator@v1.0.4/pkg/offering/k8s/peer/override/ingress_test.go (about)

     1  /*
     2   * Copyright contributors to the Hyperledger Fabric Operator project
     3   *
     4   * SPDX-License-Identifier: Apache-2.0
     5   *
     6   * Licensed under the Apache License, Version 2.0 (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at:
     9   *
    10   * 	  http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   * Unless required by applicable law or agreed to in writing, software
    13   * distributed under the License is distributed on an "AS IS" BASIS,
    14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15   * See the License for the specific language governing permissions and
    16   * limitations under the License.
    17   */
    18  
    19  package override_test
    20  
    21  import (
    22  	. "github.com/onsi/ginkgo/v2"
    23  	. "github.com/onsi/gomega"
    24  
    25  	current "github.com/IBM-Blockchain/fabric-operator/api/v1beta1"
    26  	"github.com/IBM-Blockchain/fabric-operator/pkg/manager/resources"
    27  	"github.com/IBM-Blockchain/fabric-operator/pkg/offering/k8s/peer/override"
    28  	"github.com/IBM-Blockchain/fabric-operator/pkg/util"
    29  	networkingv1 "k8s.io/api/networking/v1"
    30  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    31  )
    32  
    33  var _ = Describe("K8s Peer Overrides", func() {
    34  	var (
    35  		overrider *override.Override
    36  		instance  *current.IBPPeer
    37  	)
    38  
    39  	BeforeEach(func() {
    40  		overrider = &override.Override{}
    41  	})
    42  
    43  	Context("Ingress", func() {
    44  		var (
    45  			err            error
    46  			ingress        *networkingv1.Ingress
    47  			apihost        string
    48  			operationshost string
    49  			grpcwebhost    string
    50  		)
    51  
    52  		BeforeEach(func() {
    53  			ingress, err = util.GetIngressFromFile("../../../../../definitions/peer/ingress.yaml")
    54  			Expect(err).NotTo(HaveOccurred())
    55  
    56  			instance = &current.IBPPeer{
    57  				ObjectMeta: metav1.ObjectMeta{
    58  					Name:      "ingress1",
    59  					Namespace: "namespace1",
    60  				},
    61  				Spec: current.IBPPeerSpec{
    62  					Domain: "domain1",
    63  				},
    64  			}
    65  
    66  			apihost = instance.Namespace + "-" + instance.Name + "-peer" + "." + instance.Spec.Domain
    67  			operationshost = instance.Namespace + "-" + instance.Name + "-operations" + "." + instance.Spec.Domain
    68  			grpcwebhost = instance.Namespace + "-" + instance.Name + "-grpcweb" + "." + instance.Spec.Domain
    69  		})
    70  
    71  		When("creating ingress", func() {
    72  			It("sets appropriate values", func() {
    73  				err := overrider.Ingress(instance, ingress, resources.Create)
    74  				Expect(err).NotTo(HaveOccurred())
    75  				VerifyIngressCommonOverrides(instance, ingress, apihost, operationshost, grpcwebhost)
    76  			})
    77  		})
    78  
    79  		When("creating ingress with custom class", func() {
    80  			It("sets appropriate values", func() {
    81  				instance.Spec.Ingress = current.Ingress{
    82  					Class: "custom",
    83  				}
    84  				err := overrider.Ingress(instance, ingress, resources.Create)
    85  				Expect(err).NotTo(HaveOccurred())
    86  				VerifyIngressCommonOverrides(instance, ingress, apihost, operationshost, grpcwebhost)
    87  			})
    88  		})
    89  
    90  		When("updating ingress", func() {
    91  			It("sets appropriate values", func() {
    92  				err := overrider.Ingress(instance, ingress, resources.Update)
    93  				Expect(err).NotTo(HaveOccurred())
    94  				VerifyIngressCommonOverrides(instance, ingress, apihost, operationshost, grpcwebhost)
    95  			})
    96  		})
    97  
    98  		When("updating ingress with custom class", func() {
    99  			It("sets appropriate values", func() {
   100  				instance.Spec.Ingress = current.Ingress{
   101  					Class: "custom",
   102  				}
   103  				err := overrider.Ingress(instance, ingress, resources.Update)
   104  				Expect(err).NotTo(HaveOccurred())
   105  				VerifyIngressCommonOverrides(instance, ingress, apihost, operationshost, grpcwebhost)
   106  			})
   107  		})
   108  	})
   109  })
   110  
   111  func VerifyIngressCommonOverrides(instance *current.IBPPeer, ingress *networkingv1.Ingress, apihost, operationshost, grpcwebhost string) {
   112  	By("setting annotation for custom ingress class", func() {
   113  		if instance.Spec.Ingress.Class != "" {
   114  			Expect(ingress.ObjectMeta.Annotations["kubernetes.io/ingress.class"]).To(Equal(instance.Spec.Ingress.Class))
   115  		} else {
   116  			Expect(ingress.ObjectMeta.Annotations["kubernetes.io/ingress.class"]).To(Equal("nginx"))
   117  		}
   118  	})
   119  	By("setting rules", func() {
   120  		pathType := networkingv1.PathTypeImplementationSpecific
   121  		Expect(ingress.Spec.Rules).To(HaveLen(3))
   122  		Expect(ingress.Spec.Rules[0]).To(Equal(networkingv1.IngressRule{
   123  			Host: apihost,
   124  			IngressRuleValue: networkingv1.IngressRuleValue{
   125  				HTTP: &networkingv1.HTTPIngressRuleValue{
   126  					Paths: []networkingv1.HTTPIngressPath{
   127  						networkingv1.HTTPIngressPath{
   128  							Backend: networkingv1.IngressBackend{
   129  								Service: &networkingv1.IngressServiceBackend{
   130  									Name: instance.GetName(),
   131  									Port: networkingv1.ServiceBackendPort{
   132  										Name: "peer-api",
   133  									},
   134  								},
   135  							},
   136  							Path:     "/",
   137  							PathType: &pathType,
   138  						},
   139  					},
   140  				},
   141  			},
   142  		}))
   143  		Expect(ingress.Spec.Rules[1]).To(Equal(networkingv1.IngressRule{
   144  			Host: operationshost,
   145  			IngressRuleValue: networkingv1.IngressRuleValue{
   146  				HTTP: &networkingv1.HTTPIngressRuleValue{
   147  					Paths: []networkingv1.HTTPIngressPath{
   148  						networkingv1.HTTPIngressPath{
   149  							Backend: networkingv1.IngressBackend{
   150  								Service: &networkingv1.IngressServiceBackend{
   151  									Name: instance.GetName(),
   152  									Port: networkingv1.ServiceBackendPort{
   153  										Name: "operations",
   154  									},
   155  								},
   156  							},
   157  							Path:     "/",
   158  							PathType: &pathType,
   159  						},
   160  					},
   161  				},
   162  			},
   163  		}))
   164  		Expect(ingress.Spec.Rules[2]).To(Equal(networkingv1.IngressRule{
   165  			Host: grpcwebhost,
   166  			IngressRuleValue: networkingv1.IngressRuleValue{
   167  				HTTP: &networkingv1.HTTPIngressRuleValue{
   168  					Paths: []networkingv1.HTTPIngressPath{
   169  						networkingv1.HTTPIngressPath{
   170  							Backend: networkingv1.IngressBackend{
   171  								Service: &networkingv1.IngressServiceBackend{
   172  									Name: instance.GetName(),
   173  									Port: networkingv1.ServiceBackendPort{
   174  										Name: "grpcweb",
   175  									},
   176  								},
   177  							},
   178  							Path:     "/",
   179  							PathType: &pathType,
   180  						},
   181  					},
   182  				},
   183  			},
   184  		}))
   185  	})
   186  
   187  	By("setting TLS hosts", func() {
   188  		Expect(ingress.Spec.TLS).To(HaveLen(3))
   189  		Expect(ingress.Spec.TLS[0].Hosts).To(Equal([]string{apihost}))
   190  		Expect(ingress.Spec.TLS[1].Hosts).To(Equal([]string{operationshost}))
   191  		Expect(ingress.Spec.TLS[2].Hosts).To(Equal([]string{grpcwebhost}))
   192  	})
   193  }