github.com/IBM-Blockchain/fabric-operator@v1.0.4/pkg/offering/k8s/peer/override/ingress.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
    20  
    21  import (
    22  	current "github.com/IBM-Blockchain/fabric-operator/api/v1beta1"
    23  	"github.com/IBM-Blockchain/fabric-operator/pkg/manager/resources"
    24  	networkingv1 "k8s.io/api/networking/v1"
    25  	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    26  )
    27  
    28  func (o *Override) Ingress(object v1.Object, ingress *networkingv1.Ingress, action resources.Action) error {
    29  	instance := object.(*current.IBPPeer)
    30  
    31  	switch action {
    32  	case resources.Create:
    33  		return o.CreateIngress(instance, ingress)
    34  	case resources.Update:
    35  		return o.UpdateIngress(instance, ingress)
    36  	}
    37  
    38  	return nil
    39  }
    40  
    41  func (o *Override) CreateIngress(instance *current.IBPPeer, ingress *networkingv1.Ingress) error {
    42  	return o.CommonIngress(instance, ingress)
    43  }
    44  
    45  func (o *Override) UpdateIngress(instance *current.IBPPeer, ingress *networkingv1.Ingress) error {
    46  	return o.CommonIngress(instance, ingress)
    47  }
    48  
    49  func (o *Override) CommonIngress(instance *current.IBPPeer, ingress *networkingv1.Ingress) error {
    50  	ingressClass := "nginx"
    51  	if instance.Spec.Ingress.Class != "" {
    52  		ingressClass = instance.Spec.Ingress.Class
    53  	}
    54  	ingress.ObjectMeta.Annotations["kubernetes.io/ingress.class"] = ingressClass
    55  
    56  	apihost := instance.Namespace + "-" + instance.Name + "-peer" + "." + instance.Spec.Domain
    57  	operationshost := instance.Namespace + "-" + instance.Name + "-operations" + "." + instance.Spec.Domain
    58  	grpcwebhost := instance.Namespace + "-" + instance.Name + "-grpcweb" + "." + instance.Spec.Domain
    59  
    60  	pathType := networkingv1.PathTypeImplementationSpecific
    61  	ingress.Spec = networkingv1.IngressSpec{
    62  		Rules: []networkingv1.IngressRule{
    63  			networkingv1.IngressRule{
    64  				Host: apihost,
    65  				IngressRuleValue: networkingv1.IngressRuleValue{
    66  					HTTP: &networkingv1.HTTPIngressRuleValue{
    67  						Paths: []networkingv1.HTTPIngressPath{
    68  							networkingv1.HTTPIngressPath{
    69  								Backend: networkingv1.IngressBackend{
    70  									Service: &networkingv1.IngressServiceBackend{
    71  										Name: instance.GetName(),
    72  										Port: networkingv1.ServiceBackendPort{
    73  											Name: "peer-api",
    74  										},
    75  									},
    76  								},
    77  								Path:     "/",
    78  								PathType: &pathType,
    79  							},
    80  						},
    81  					},
    82  				},
    83  			},
    84  			networkingv1.IngressRule{
    85  				Host: operationshost,
    86  				IngressRuleValue: networkingv1.IngressRuleValue{
    87  					HTTP: &networkingv1.HTTPIngressRuleValue{
    88  						Paths: []networkingv1.HTTPIngressPath{
    89  							networkingv1.HTTPIngressPath{
    90  								Backend: networkingv1.IngressBackend{
    91  									Service: &networkingv1.IngressServiceBackend{
    92  										Name: instance.GetName(),
    93  										Port: networkingv1.ServiceBackendPort{
    94  											Name: "operations",
    95  										},
    96  									},
    97  								},
    98  								Path:     "/",
    99  								PathType: &pathType,
   100  							},
   101  						},
   102  					},
   103  				},
   104  			},
   105  			networkingv1.IngressRule{
   106  				Host: grpcwebhost,
   107  				IngressRuleValue: networkingv1.IngressRuleValue{
   108  					HTTP: &networkingv1.HTTPIngressRuleValue{
   109  						Paths: []networkingv1.HTTPIngressPath{
   110  							networkingv1.HTTPIngressPath{
   111  								Backend: networkingv1.IngressBackend{
   112  									Service: &networkingv1.IngressServiceBackend{
   113  										Name: instance.GetName(),
   114  										Port: networkingv1.ServiceBackendPort{
   115  											Name: "grpcweb",
   116  										},
   117  									},
   118  								},
   119  								Path:     "/",
   120  								PathType: &pathType,
   121  							},
   122  						},
   123  					},
   124  				},
   125  			},
   126  		},
   127  		TLS: []networkingv1.IngressTLS{
   128  			networkingv1.IngressTLS{
   129  				Hosts: []string{apihost},
   130  			},
   131  			networkingv1.IngressTLS{
   132  				Hosts: []string{operationshost},
   133  			},
   134  			networkingv1.IngressTLS{
   135  				Hosts: []string{grpcwebhost},
   136  			},
   137  		},
   138  	}
   139  
   140  	return nil
   141  }