github.com/IBM-Blockchain/fabric-operator@v1.0.4/pkg/offering/openshift/orderer/orderer.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 openshiftorderer
    20  
    21  import (
    22  	"fmt"
    23  
    24  	current "github.com/IBM-Blockchain/fabric-operator/api/v1beta1"
    25  	config "github.com/IBM-Blockchain/fabric-operator/operatorconfig"
    26  	k8sclient "github.com/IBM-Blockchain/fabric-operator/pkg/k8s/controllerclient"
    27  	baseorderer "github.com/IBM-Blockchain/fabric-operator/pkg/offering/base/orderer"
    28  	baseordereroverride "github.com/IBM-Blockchain/fabric-operator/pkg/offering/base/orderer/override"
    29  	"github.com/IBM-Blockchain/fabric-operator/pkg/offering/common"
    30  	"github.com/IBM-Blockchain/fabric-operator/pkg/offering/openshift/orderer/override"
    31  	"github.com/IBM-Blockchain/fabric-operator/version"
    32  	"github.com/pkg/errors"
    33  	"k8s.io/apimachinery/pkg/runtime"
    34  	logf "sigs.k8s.io/controller-runtime/pkg/log"
    35  	"sigs.k8s.io/controller-runtime/pkg/reconcile"
    36  )
    37  
    38  const (
    39  	defaultRoute = "./definitions/orderer/route.yaml"
    40  )
    41  
    42  var log = logf.Log.WithName("openshift_orderer")
    43  
    44  var _ baseorderer.IBPOrderer = &Orderer{}
    45  
    46  type Orderer struct {
    47  	*baseorderer.Orderer
    48  }
    49  
    50  func New(client k8sclient.Client, scheme *runtime.Scheme, config *config.Config) *Orderer {
    51  	o := &override.Override{
    52  		Override: baseordereroverride.Override{
    53  			Client: client,
    54  			Config: config,
    55  		},
    56  	}
    57  
    58  	orderer := &Orderer{
    59  		Orderer: baseorderer.New(client, scheme, config, o),
    60  	}
    61  
    62  	return orderer
    63  }
    64  
    65  func (o *Orderer) Reconcile(instance *current.IBPOrderer, update baseorderer.Update) (common.Result, error) {
    66  
    67  	if instance.Spec.NodeNumber == nil {
    68  		versionSet, err := o.SetVersion(instance)
    69  		if err != nil {
    70  			return common.Result{}, errors.Wrap(err, fmt.Sprintf("failed updating CR '%s' to version '%s'", instance.Name, version.Operator))
    71  		}
    72  		if versionSet {
    73  			log.Info("Instance version updated, requeuing request...")
    74  			return common.Result{
    75  				Result: reconcile.Result{
    76  					Requeue: true,
    77  				},
    78  			}, nil
    79  		}
    80  
    81  		if instance.Status.Status == "" || instance.Status.Status == current.False || (instance.Status.Version != "" && version.String(instance.Status.Version).GreaterThan(version.V210)) {
    82  			instanceUpdated, err := o.PreReconcileChecks(instance, update)
    83  			if err != nil {
    84  				return common.Result{}, errors.Wrap(err, "failed pre reconcile checks")
    85  			}
    86  
    87  			if instanceUpdated {
    88  				log.Info("Instance updated, requeuing request...")
    89  				return common.Result{
    90  					Result: reconcile.Result{
    91  						Requeue: true,
    92  					},
    93  					OverrideUpdateStatus: true,
    94  				}, nil
    95  			}
    96  		}
    97  	}
    98  
    99  	// TODO: Major rehaul is needed of versioning and migration strategy. Need a way to
   100  	// migrate as first step to get CR spec in appropriate state to avoid versioning checks
   101  	// like below and above
   102  	if (instance.Status.Version == "" && instance.Status.Status == current.True) || (instance.Status.Version != "" && version.String(instance.Status.Version).Equal(version.V210)) {
   103  		if instance.Spec.NodeNumber == nil {
   104  			number := 1
   105  			instance.Spec.NodeNumber = &number
   106  		}
   107  	}
   108  
   109  	if instance.Spec.NodeNumber == nil {
   110  		result, err := o.ReconcileCluster(instance, update, o.AddHostPortToProfile)
   111  		if err != nil {
   112  			return common.Result{}, errors.Wrap(err, "failed to reconcile cluster")
   113  		}
   114  		return result, nil
   115  	}
   116  
   117  	result, err := o.ReconcileNode(instance, update)
   118  	if err != nil {
   119  		return common.Result{}, errors.Wrap(err, "failed to reconcile node")
   120  	}
   121  
   122  	return result, nil
   123  }
   124  
   125  func (o *Orderer) ReconcileNode(instance *current.IBPOrderer, update baseorderer.Update) (common.Result, error) {
   126  	var err error
   127  
   128  	hostAPI := fmt.Sprintf("%s-%s-orderer.%s", instance.Namespace, instance.Name, instance.Spec.Domain)
   129  	hostOperations := fmt.Sprintf("%s-%s-operations.%s", instance.Namespace, instance.Name, instance.Spec.Domain)
   130  	hostGrpc := fmt.Sprintf("%s-%s-grpcweb.%s", instance.Namespace, instance.Name, instance.Spec.Domain)
   131  	hosts := []string{}
   132  	currentVer := version.String(instance.Spec.FabricVersion)
   133  	if currentVer.EqualWithoutTag(version.V2_4_1) || currentVer.GreaterThan(version.V2_4_1) {
   134  		hostAdmin := fmt.Sprintf("%s-%s-admin.%s", instance.Namespace, instance.Name, instance.Spec.Domain)
   135  		hosts = append(hosts, hostAPI, hostOperations, hostGrpc, hostAdmin, "127.0.0.1")
   136  	} else {
   137  		hosts = append(hosts, hostAPI, hostOperations, hostGrpc, "127.0.0.1")
   138  	}
   139  
   140  	o.CheckCSRHosts(instance, hosts)
   141  
   142  	log.Info(fmt.Sprintf("Reconciling Orderer node %s", instance.GetName()))
   143  
   144  	openshiftnode := NewNode(baseorderer.NewNode(o.Client, o.Scheme, o.Config, instance.GetName(), o.RenewCertTimers, o.RestartManager))
   145  
   146  	if !instance.Spec.IsUsingChannelLess() && instance.Spec.GenesisBlock == "" && !(instance.Spec.IsPrecreateOrderer()) {
   147  		return common.Result{}, fmt.Errorf("Genesis block not provided for orderer node: %s", instance.GetName())
   148  	}
   149  
   150  	result, err := openshiftnode.Reconcile(instance, update)
   151  	if err != nil {
   152  		return common.Result{}, err
   153  	}
   154  
   155  	return result, nil
   156  }