github.com/IBM-Blockchain/fabric-operator@v1.0.4/controllers/common/common.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 common
    20  
    21  import (
    22  	"context"
    23  	"fmt"
    24  	"reflect"
    25  
    26  	current "github.com/IBM-Blockchain/fabric-operator/api/v1beta1"
    27  
    28  	"sigs.k8s.io/controller-runtime/pkg/client"
    29  )
    30  
    31  const (
    32  	IBPCA      = "IBPCA"
    33  	IBPPEER    = "IBPPeer"
    34  	IBPORDERER = "IBPOrderer"
    35  	IBPCONSOLE = "IBPConsole"
    36  )
    37  
    38  type Client interface {
    39  	List(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error
    40  }
    41  
    42  // 1. Only one existing instance (of the same type as 'instance') should have
    43  //    the name 'instance.GetName()'; if more than one is present, return error
    44  // 2. If any instance of a different type share the same name, return error
    45  func ValidateCRName(k8sclient Client, name, namespace, kind string) error {
    46  	listOptions := &client.ListOptions{
    47  		Namespace: namespace,
    48  	}
    49  
    50  	count := 0
    51  
    52  	caList := &current.IBPCAList{}
    53  	err := k8sclient.List(context.TODO(), caList, listOptions)
    54  	if err != nil {
    55  		return err
    56  	}
    57  	for _, ca := range caList.Items {
    58  		if name == ca.Name {
    59  			if kind == IBPCA {
    60  				count++
    61  			} else {
    62  				return fmt.Errorf("custom resource with name '%s' already exists", name)
    63  			}
    64  		}
    65  	}
    66  
    67  	ordererList := &current.IBPOrdererList{}
    68  	err = k8sclient.List(context.TODO(), ordererList, listOptions)
    69  	if err != nil {
    70  		return err
    71  	}
    72  	for _, o := range ordererList.Items {
    73  		if name == o.Name {
    74  			if kind == IBPORDERER {
    75  				count++
    76  			} else {
    77  				return fmt.Errorf("custom resource with name %s already exists", name)
    78  			}
    79  		}
    80  	}
    81  
    82  	peerList := &current.IBPPeerList{}
    83  	err = k8sclient.List(context.TODO(), peerList, listOptions)
    84  	if err != nil {
    85  		return err
    86  	}
    87  	for _, p := range peerList.Items {
    88  		if name == p.Name {
    89  			if kind == IBPPEER {
    90  				count++
    91  			} else {
    92  				return fmt.Errorf("custom resource with name %s already exists", name)
    93  			}
    94  		}
    95  	}
    96  
    97  	consoleList := &current.IBPConsoleList{}
    98  	err = k8sclient.List(context.TODO(), consoleList, listOptions)
    99  	if err != nil {
   100  		return err
   101  	}
   102  	for _, c := range consoleList.Items {
   103  		if name == c.Name {
   104  			if kind == IBPCONSOLE {
   105  				count++
   106  			} else {
   107  				return fmt.Errorf("custom resource with name %s already exists", name)
   108  			}
   109  		}
   110  	}
   111  
   112  	if count > 1 {
   113  		return fmt.Errorf("custom resource with name %s already exists", name)
   114  	}
   115  
   116  	return nil
   117  }
   118  
   119  func MSPInfoUpdateDetected(oldSecret, newSecret *current.SecretSpec) bool {
   120  	if newSecret == nil || newSecret.MSP == nil {
   121  		return false
   122  	}
   123  
   124  	if oldSecret == nil || oldSecret.MSP == nil {
   125  		if newSecret.MSP.Component != nil || newSecret.MSP.TLS != nil || newSecret.MSP.ClientAuth != nil {
   126  			return true
   127  		}
   128  	} else {
   129  		// For comparison purpose ignoring admin certs - admin cert updates
   130  		// detected in Initialize() code
   131  		if oldSecret.MSP.Component != nil && newSecret.MSP.Component != nil {
   132  			oldSecret.MSP.Component.AdminCerts = newSecret.MSP.Component.AdminCerts
   133  		}
   134  		if oldSecret.MSP.TLS != nil && newSecret.MSP.TLS != nil {
   135  			oldSecret.MSP.TLS.AdminCerts = newSecret.MSP.TLS.AdminCerts
   136  		}
   137  		if oldSecret.MSP.ClientAuth != nil && newSecret.MSP.ClientAuth != nil {
   138  			oldSecret.MSP.ClientAuth.AdminCerts = newSecret.MSP.ClientAuth.AdminCerts
   139  		}
   140  
   141  		return !reflect.DeepEqual(oldSecret.MSP, newSecret.MSP)
   142  	}
   143  
   144  	return false
   145  }