github.com/IBM-Blockchain/fabric-operator@v1.0.4/integration/helper/peer.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 helper
    20  
    21  import (
    22  	"context"
    23  	"fmt"
    24  	"io/ioutil"
    25  	"os"
    26  	"path/filepath"
    27  	"strings"
    28  
    29  	current "github.com/IBM-Blockchain/fabric-operator/api/v1beta1"
    30  	"github.com/IBM-Blockchain/fabric-operator/integration"
    31  	ibpclient "github.com/IBM-Blockchain/fabric-operator/pkg/client"
    32  
    33  	k8serrors "k8s.io/apimachinery/pkg/api/errors"
    34  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    35  	"k8s.io/client-go/kubernetes"
    36  
    37  	"sigs.k8s.io/yaml"
    38  )
    39  
    40  func CreatePeer(crClient *ibpclient.IBPClient, peer *current.IBPPeer) error {
    41  	result := crClient.Post().Namespace(peer.Namespace).Resource("ibppeers").Body(peer).Do(context.TODO())
    42  	err := result.Error()
    43  	if !k8serrors.IsAlreadyExists(err) {
    44  		return err
    45  	}
    46  	return nil
    47  }
    48  
    49  type Peer struct {
    50  	Domain     string
    51  	Name       string
    52  	Namespace  string
    53  	WorkingDir string
    54  
    55  	CR       *current.IBPPeer
    56  	CRClient *ibpclient.IBPClient
    57  	KClient  *kubernetes.Clientset
    58  
    59  	integration.NativeResourcePoller
    60  }
    61  
    62  func (p *Peer) PollForCRStatus() current.IBPCRStatusType {
    63  	crStatus := &current.IBPPeer{}
    64  
    65  	result := p.CRClient.Get().Namespace(p.Namespace).Resource("ibppeers").Name(p.Name).Do(context.TODO())
    66  	// Not handling this as this is integration test
    67  	_ = result.Into(crStatus)
    68  
    69  	return crStatus.Status.Type
    70  }
    71  
    72  func (p *Peer) TLSToFile(cert []byte) error {
    73  	err := os.MkdirAll(filepath.Dir(p.TLSPath()), 0750)
    74  	if err != nil {
    75  		return err
    76  	}
    77  	return ioutil.WriteFile(p.TLSPath(), cert, 0600)
    78  }
    79  
    80  func (p *Peer) TLSPath() string {
    81  	return filepath.Join(p.WorkingDir, p.Name, "tls-cert.pem")
    82  }
    83  
    84  func (p *Peer) ConnectionProfile() (*current.CAConnectionProfile, error) {
    85  	cm, err := p.KClient.CoreV1().ConfigMaps(p.Namespace).Get(context.TODO(), fmt.Sprintf("%s-connection-profile", p.CR.Name), metav1.GetOptions{})
    86  	if err != nil {
    87  		return nil, err
    88  	}
    89  
    90  	data := cm.BinaryData["profile.json"]
    91  
    92  	profile := &current.CAConnectionProfile{}
    93  	err = yaml.Unmarshal(data, profile)
    94  	if err != nil {
    95  		return nil, err
    96  	}
    97  
    98  	return profile, nil
    99  }
   100  
   101  func (p *Peer) JobWithPrefixFound(prefix, namespace string) bool {
   102  	jobs, err := p.KClient.BatchV1().Jobs(namespace).List(context.TODO(), metav1.ListOptions{})
   103  	if err != nil {
   104  		return false
   105  	}
   106  
   107  	for _, job := range jobs.Items {
   108  		if strings.HasPrefix(job.GetName(), prefix) {
   109  			return true
   110  		}
   111  	}
   112  
   113  	return false
   114  }