github.com/IBM-Blockchain/fabric-operator@v1.0.4/pkg/client/client.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 client
    20  
    21  import (
    22  	current "github.com/IBM-Blockchain/fabric-operator/api/v1beta1"
    23  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  	"k8s.io/apimachinery/pkg/runtime"
    25  	"k8s.io/apimachinery/pkg/runtime/schema"
    26  	"k8s.io/apimachinery/pkg/runtime/serializer"
    27  	"k8s.io/client-go/rest"
    28  )
    29  
    30  const (
    31  	CRDGroup   string = "ibp.com"
    32  	CRDVersion string = "v1beta1"
    33  )
    34  
    35  var SchemeGroupVersion = schema.GroupVersion{Group: CRDGroup, Version: CRDVersion}
    36  
    37  type IBPClient struct {
    38  	rest.Interface
    39  }
    40  
    41  func New(cfg *rest.Config) (*IBPClient, error) {
    42  	scheme := runtime.NewScheme()
    43  	SchemeBuilder := runtime.NewSchemeBuilder(addKnownTypes)
    44  	err := SchemeBuilder.AddToScheme(scheme)
    45  	if err != nil {
    46  		return nil, err
    47  	}
    48  
    49  	config := *cfg
    50  	config.GroupVersion = &SchemeGroupVersion
    51  	config.APIPath = "/apis"
    52  	config.ContentType = runtime.ContentTypeJSON
    53  	config.NegotiatedSerializer = serializer.NewCodecFactory(scheme)
    54  	client, err := rest.RESTClientFor(&config)
    55  	if err != nil {
    56  		return nil, err
    57  	}
    58  	return &IBPClient{client}, nil
    59  }
    60  
    61  func addKnownTypes(scheme *runtime.Scheme) error {
    62  	scheme.AddKnownTypes(SchemeGroupVersion,
    63  		&current.IBPCA{},
    64  		&current.IBPCAList{},
    65  		&current.IBPPeer{},
    66  		&current.IBPPeerList{},
    67  		&current.IBPOrderer{},
    68  		&current.IBPOrdererList{},
    69  		&current.IBPConsole{},
    70  		&current.IBPConsoleList{},
    71  	)
    72  	metav1.AddToGroupVersion(scheme, SchemeGroupVersion)
    73  	return nil
    74  }