github.com/IBM-Blockchain/fabric-operator@v1.0.4/pkg/offering/k8s/peer/override/ingressv1beta1_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  	networkingv1beta1 "k8s.io/api/networking/v1beta1"
    30  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    31  	"k8s.io/apimachinery/pkg/util/intstr"
    32  )
    33  
    34  var _ = Describe("K8s Peer Overrides", func() {
    35  	var (
    36  		overrider *override.Override
    37  		instance  *current.IBPPeer
    38  	)
    39  
    40  	BeforeEach(func() {
    41  		overrider = &override.Override{}
    42  	})
    43  
    44  	Context("Ingress", func() {
    45  		var (
    46  			err            error
    47  			ingress        *networkingv1beta1.Ingress
    48  			apihost        string
    49  			operationshost string
    50  			grpcwebhost    string
    51  		)
    52  
    53  		BeforeEach(func() {
    54  			ingress, err = util.GetIngressv1beta1FromFile("../../../../../definitions/peer/ingressv1beta1.yaml")
    55  			Expect(err).NotTo(HaveOccurred())
    56  
    57  			instance = &current.IBPPeer{
    58  				ObjectMeta: metav1.ObjectMeta{
    59  					Name:      "ingress1",
    60  					Namespace: "namespace1",
    61  				},
    62  				Spec: current.IBPPeerSpec{
    63  					Domain: "domain1",
    64  				},
    65  			}
    66  
    67  			apihost = instance.Namespace + "-" + instance.Name + "-peer" + "." + instance.Spec.Domain
    68  			operationshost = instance.Namespace + "-" + instance.Name + "-operations" + "." + instance.Spec.Domain
    69  			grpcwebhost = instance.Namespace + "-" + instance.Name + "-grpcweb" + "." + instance.Spec.Domain
    70  		})
    71  
    72  		When("creating ingress", func() {
    73  			It("sets appropriate values", func() {
    74  				err := overrider.Ingressv1beta1(instance, ingress, resources.Create)
    75  				Expect(err).NotTo(HaveOccurred())
    76  				VerifyIngressCommonOverridesv1beta1(instance, ingress, apihost, operationshost, grpcwebhost)
    77  			})
    78  		})
    79  
    80  		When("creating ingress with custom class", func() {
    81  			It("sets appropriate values", func() {
    82  				instance.Spec.Ingress = current.Ingress{
    83  					Class: "custom",
    84  				}
    85  				err := overrider.Ingressv1beta1(instance, ingress, resources.Create)
    86  				Expect(err).NotTo(HaveOccurred())
    87  				VerifyIngressCommonOverridesv1beta1(instance, ingress, apihost, operationshost, grpcwebhost)
    88  			})
    89  		})
    90  
    91  		When("updating ingress", func() {
    92  			It("sets appropriate values", func() {
    93  				err := overrider.Ingressv1beta1(instance, ingress, resources.Update)
    94  				Expect(err).NotTo(HaveOccurred())
    95  				VerifyIngressCommonOverridesv1beta1(instance, ingress, apihost, operationshost, grpcwebhost)
    96  			})
    97  		})
    98  
    99  		When("updating ingress with custom class", func() {
   100  			It("sets appropriate values", func() {
   101  				instance.Spec.Ingress = current.Ingress{
   102  					Class: "custom",
   103  				}
   104  				err := overrider.Ingressv1beta1(instance, ingress, resources.Update)
   105  				Expect(err).NotTo(HaveOccurred())
   106  				VerifyIngressCommonOverridesv1beta1(instance, ingress, apihost, operationshost, grpcwebhost)
   107  			})
   108  		})
   109  	})
   110  })
   111  
   112  func VerifyIngressCommonOverridesv1beta1(instance *current.IBPPeer, ingress *networkingv1beta1.Ingress, apihost, operationshost, grpcwebhost string) {
   113  	By("setting annotation for custom ingress class", func() {
   114  		if instance.Spec.Ingress.Class != "" {
   115  			Expect(ingress.ObjectMeta.Annotations["kubernetes.io/ingress.class"]).To(Equal(instance.Spec.Ingress.Class))
   116  		} else {
   117  			Expect(ingress.ObjectMeta.Annotations["kubernetes.io/ingress.class"]).To(Equal("nginx"))
   118  		}
   119  	})
   120  	By("setting rules", func() {
   121  		Expect(ingress.Spec.Rules).To(HaveLen(3))
   122  		Expect(ingress.Spec.Rules[0]).To(Equal(networkingv1beta1.IngressRule{
   123  			Host: apihost,
   124  			IngressRuleValue: networkingv1beta1.IngressRuleValue{
   125  				HTTP: &networkingv1beta1.HTTPIngressRuleValue{
   126  					Paths: []networkingv1beta1.HTTPIngressPath{
   127  						networkingv1beta1.HTTPIngressPath{
   128  							Backend: networkingv1beta1.IngressBackend{
   129  								ServiceName: instance.GetName(),
   130  								ServicePort: intstr.FromString("peer-api"),
   131  							},
   132  							Path: "/",
   133  						},
   134  					},
   135  				},
   136  			},
   137  		}))
   138  		Expect(ingress.Spec.Rules[1]).To(Equal(networkingv1beta1.IngressRule{
   139  			Host: operationshost,
   140  			IngressRuleValue: networkingv1beta1.IngressRuleValue{
   141  				HTTP: &networkingv1beta1.HTTPIngressRuleValue{
   142  					Paths: []networkingv1beta1.HTTPIngressPath{
   143  						networkingv1beta1.HTTPIngressPath{
   144  							Backend: networkingv1beta1.IngressBackend{
   145  								ServiceName: instance.GetName(),
   146  								ServicePort: intstr.FromString("operations"),
   147  							},
   148  							Path: "/",
   149  						},
   150  					},
   151  				},
   152  			},
   153  		}))
   154  		Expect(ingress.Spec.Rules[2]).To(Equal(networkingv1beta1.IngressRule{
   155  			Host: grpcwebhost,
   156  			IngressRuleValue: networkingv1beta1.IngressRuleValue{
   157  				HTTP: &networkingv1beta1.HTTPIngressRuleValue{
   158  					Paths: []networkingv1beta1.HTTPIngressPath{
   159  						networkingv1beta1.HTTPIngressPath{
   160  							Backend: networkingv1beta1.IngressBackend{
   161  								ServiceName: instance.GetName(),
   162  								ServicePort: intstr.FromString("grpcweb"),
   163  							},
   164  							Path: "/",
   165  						},
   166  					},
   167  				},
   168  			},
   169  		}))
   170  	})
   171  
   172  	By("setting TLS hosts", func() {
   173  		Expect(ingress.Spec.TLS).To(HaveLen(3))
   174  		Expect(ingress.Spec.TLS[0].Hosts).To(Equal([]string{apihost}))
   175  		Expect(ingress.Spec.TLS[1].Hosts).To(Equal([]string{operationshost}))
   176  		Expect(ingress.Spec.TLS[2].Hosts).To(Equal([]string{grpcwebhost}))
   177  	})
   178  }