github.com/IBM-Blockchain/fabric-operator@v1.0.4/pkg/crd/manager.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 crd
    20  
    21  import (
    22  	"path/filepath"
    23  
    24  	"github.com/IBM-Blockchain/fabric-operator/pkg/util"
    25  	extv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
    26  	logf "sigs.k8s.io/controller-runtime/pkg/log"
    27  )
    28  
    29  const (
    30  	CACRD      = "./config/crd/bases/ibp_v1alpha1_ibpca.yaml"
    31  	PeerCRD    = "./config/crd/bases/ibp_v1alpha1_ibppeer.yaml"
    32  	OrdererCRD = "./config/crd/bases/ibp_v1alpha1_ibporderer.yaml"
    33  	ConsoleCRD = "./config/crd/bases/ibp_v1alpha1_ibpconsole.yaml"
    34  
    35  	CACRDFile      = "ibp_v1alpha1_ibpca.yaml"
    36  	PEERCRDFIle    = "ibp_v1alpha1_ibppeer.yaml"
    37  	ORDERERCRDFILE = "ibp_v1alpha1_ibporderer.yaml"
    38  	CONSOLECRDFILE = "ibp_v1alpha1_ibpconsole.yaml"
    39  )
    40  
    41  var log = logf.Log.WithName("crd_manager")
    42  
    43  //go:generate counterfeiter -o mocks/client.go -fake-name Client . Client
    44  
    45  type Client interface {
    46  	CreateCRD(crd *extv1.CustomResourceDefinition) (*extv1.CustomResourceDefinition, error)
    47  }
    48  
    49  type Manager struct {
    50  	Client Client
    51  	crds   []*extv1.CustomResourceDefinition
    52  }
    53  
    54  func GetCRDList() []string {
    55  	return []string{CACRD, PeerCRD, OrdererCRD, ConsoleCRD}
    56  }
    57  
    58  func GetCRDListFromDir(dir string) []string {
    59  
    60  	crds := []string{
    61  		filepath.Join(dir, CACRDFile),
    62  		filepath.Join(dir, PEERCRDFIle),
    63  		filepath.Join(dir, ORDERERCRDFILE),
    64  		filepath.Join(dir, CONSOLECRDFILE),
    65  	}
    66  
    67  	return crds
    68  }
    69  
    70  func NewManager(c Client, files ...string) (*Manager, error) {
    71  	m := &Manager{
    72  		Client: c,
    73  	}
    74  	for _, file := range files {
    75  		crd, err := util.GetCRDFromFile(file)
    76  		if err != nil {
    77  			return nil, err
    78  		}
    79  		m.crds = append(m.crds, crd)
    80  	}
    81  	return m, nil
    82  }
    83  
    84  func (m *Manager) Create() error {
    85  	log.Info("Create CRDs")
    86  	for _, crd := range m.crds {
    87  		log.Info("Creating", "CRD", crd.Name)
    88  		_, err := m.Client.CreateCRD(crd)
    89  		if err != nil {
    90  			return err
    91  		}
    92  	}
    93  	return nil
    94  }