github.com/IBM-Blockchain/fabric-operator@v1.0.4/integration/operator.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 integration 20 21 import ( 22 "context" 23 "fmt" 24 "os" 25 "path/filepath" 26 27 config "github.com/IBM-Blockchain/fabric-operator/operatorconfig" 28 29 "github.com/IBM-Blockchain/fabric-operator/pkg/apis/common" 30 "github.com/IBM-Blockchain/fabric-operator/pkg/apis/deployer" 31 cainit "github.com/IBM-Blockchain/fabric-operator/pkg/initializer/ca" 32 ordererinit "github.com/IBM-Blockchain/fabric-operator/pkg/initializer/orderer" 33 peerinit "github.com/IBM-Blockchain/fabric-operator/pkg/initializer/peer" 34 uzap "go.uber.org/zap" 35 corev1 "k8s.io/api/core/v1" 36 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 37 "sigs.k8s.io/controller-runtime/pkg/log/zap" 38 ) 39 40 // GetOperatorConfig returns the operator configuration with the default templating files population 41 // and with default versions set for components. 42 func GetOperatorConfig(configs, caFiles, peerFiles, ordererFiles, consoleFiles string) *config.Config { 43 ulevel := uzap.NewAtomicLevelAt(2) 44 if os.Getenv("LOG_LEVEL") == "debug" { 45 ulevel = uzap.NewAtomicLevelAt(-1) 46 } 47 level := zap.Level(&ulevel) 48 logger := zap.New(zap.Opts(level)) 49 50 cfg := &config.Config{ 51 CAInitConfig: &cainit.Config{ 52 CADefaultConfigPath: filepath.Join(configs, "ca/ca.yaml"), 53 TLSCADefaultConfigPath: filepath.Join(configs, "ca/tlsca.yaml"), 54 DeploymentFile: filepath.Join(caFiles, "deployment.yaml"), 55 PVCFile: filepath.Join(caFiles, "pvc.yaml"), 56 ServiceFile: filepath.Join(caFiles, "service.yaml"), 57 RoleFile: filepath.Join(caFiles, "role.yaml"), 58 ServiceAccountFile: filepath.Join(caFiles, "serviceaccount.yaml"), 59 RoleBindingFile: filepath.Join(caFiles, "rolebinding.yaml"), 60 ConfigMapFile: filepath.Join(caFiles, "configmap-caoverride.yaml"), 61 IngressFile: filepath.Join(caFiles, "ingress.yaml"), 62 Ingressv1beta1File: filepath.Join(caFiles, "ingressv1beta1.yaml"), 63 RouteFile: filepath.Join(caFiles, "route.yaml"), 64 SharedPath: "/tmp/data", 65 }, 66 PeerInitConfig: &peerinit.Config{ 67 CorePeerFile: filepath.Join(configs, "peer/core.yaml"), 68 CorePeerV2File: filepath.Join(configs, "peer/v2/core.yaml"), 69 OUFile: filepath.Join(configs, "peer/ouconfig.yaml"), 70 InterOUFile: filepath.Join(configs, "peer/ouconfig-inter.yaml"), 71 DeploymentFile: filepath.Join(peerFiles, "deployment.yaml"), 72 PVCFile: filepath.Join(peerFiles, "pvc.yaml"), 73 CouchDBPVCFile: filepath.Join(peerFiles, "couchdb-pvc.yaml"), 74 ServiceFile: filepath.Join(peerFiles, "service.yaml"), 75 RoleFile: filepath.Join(peerFiles, "role.yaml"), 76 ServiceAccountFile: filepath.Join(peerFiles, "serviceaccount.yaml"), 77 RoleBindingFile: filepath.Join(peerFiles, "rolebinding.yaml"), 78 FluentdConfigMapFile: filepath.Join(peerFiles, "fluentd-configmap.yaml"), 79 CouchContainerFile: filepath.Join(peerFiles, "couchdb.yaml"), 80 CouchInitContainerFile: filepath.Join(peerFiles, "couchdb-init.yaml"), 81 IngressFile: filepath.Join(peerFiles, "ingress.yaml"), 82 Ingressv1beta1File: filepath.Join(peerFiles, "ingressv1beta1.yaml"), 83 CCLauncherFile: filepath.Join(peerFiles, "chaincode-launcher.yaml"), 84 RouteFile: filepath.Join(peerFiles, "route.yaml"), 85 StoragePath: "/tmp/peerinit", 86 }, 87 OrdererInitConfig: &ordererinit.Config{ 88 OrdererV2File: filepath.Join(configs, "orderer/v2/orderer.yaml"), 89 OrdererV24File: filepath.Join(configs, "orderer/v24/orderer.yaml"), 90 OrdererFile: filepath.Join(configs, "orderer/orderer.yaml"), 91 ConfigTxFile: filepath.Join(configs, "orderer/configtx.yaml"), 92 OUFile: filepath.Join(configs, "orderer/ouconfig.yaml"), 93 InterOUFile: filepath.Join(configs, "orderer/ouconfig-inter.yaml"), 94 DeploymentFile: filepath.Join(ordererFiles, "deployment.yaml"), 95 PVCFile: filepath.Join(ordererFiles, "pvc.yaml"), 96 ServiceFile: filepath.Join(ordererFiles, "service.yaml"), 97 CMFile: filepath.Join(ordererFiles, "configmap.yaml"), 98 RoleFile: filepath.Join(ordererFiles, "role.yaml"), 99 ServiceAccountFile: filepath.Join(ordererFiles, "serviceaccount.yaml"), 100 RoleBindingFile: filepath.Join(ordererFiles, "rolebinding.yaml"), 101 IngressFile: filepath.Join(ordererFiles, "ingress.yaml"), 102 Ingressv1beta1File: filepath.Join(ordererFiles, "ingressv1beta1.yaml"), 103 RouteFile: filepath.Join(ordererFiles, "route.yaml"), 104 StoragePath: "/tmp/ordererinit", 105 }, 106 ConsoleInitConfig: &config.ConsoleConfig{ 107 DeploymentFile: filepath.Join(consoleFiles, "deployment.yaml"), 108 PVCFile: filepath.Join(consoleFiles, "pvc.yaml"), 109 ServiceFile: filepath.Join(consoleFiles, "service.yaml"), 110 CMFile: filepath.Join(consoleFiles, "configmap.yaml"), 111 ConsoleCMFile: filepath.Join(consoleFiles, "console-configmap.yaml"), 112 DeployerCMFile: filepath.Join(consoleFiles, "deployer-configmap.yaml"), 113 RoleFile: filepath.Join(consoleFiles, "role.yaml"), 114 RoleBindingFile: filepath.Join(consoleFiles, "rolebinding.yaml"), 115 ServiceAccountFile: filepath.Join(consoleFiles, "serviceaccount.yaml"), 116 IngressFile: filepath.Join(consoleFiles, "ingress.yaml"), 117 Ingressv1beta1File: filepath.Join(consoleFiles, "ingressv1beta1.yaml"), 118 NetworkPolicyIngressFile: filepath.Join(consoleFiles, "networkpolicy-ingress.yaml"), 119 NetworkPolicyDenyAllFile: filepath.Join(consoleFiles, "networkpolicy-denyall.yaml"), 120 }, 121 Logger: &logger, 122 Operator: config.Operator{ 123 Restart: config.Restart{ 124 Timeout: common.MustParseDuration("5m"), 125 }, 126 }, 127 } 128 129 setDefaultVersions(cfg) 130 return cfg 131 } 132 133 func setDefaultVersions(operatorCfg *config.Config) { 134 operatorCfg.Operator.Versions = &deployer.Versions{ 135 CA: map[string]deployer.VersionCA{ 136 FabricCAVersion + "-1": { 137 Default: true, 138 Version: FabricCAVersion + "-1", 139 Image: deployer.CAImages{ 140 CAInitImage: InitImage, 141 CAInitTag: InitTag, 142 CAImage: CaImage, 143 CATag: CaTag, 144 }, 145 }, 146 }, 147 Peer: map[string]deployer.VersionPeer{ 148 FabricVersion + "-1": { 149 Default: true, 150 Version: FabricVersion + "-1", 151 Image: deployer.PeerImages{ 152 PeerInitImage: InitImage, 153 PeerInitTag: InitTag, 154 PeerImage: PeerImage, 155 PeerTag: PeerTag, 156 CouchDBImage: CouchdbImage, 157 CouchDBTag: CouchdbTag, 158 GRPCWebImage: GrpcwebImage, 159 GRPCWebTag: GrpcwebTag, 160 }, 161 }, 162 }, 163 Orderer: map[string]deployer.VersionOrderer{ 164 FabricVersion + "-1": { 165 Default: true, 166 Version: FabricVersion + "-1", 167 Image: deployer.OrdererImages{ 168 OrdererInitImage: InitImage, 169 OrdererInitTag: InitTag, 170 OrdererImage: OrdererImage, 171 OrdererTag: OrdererTag, 172 GRPCWebImage: GrpcwebImage, 173 GRPCWebTag: GrpcwebTag, 174 }, 175 }, 176 }, 177 } 178 } 179 180 type Operator struct { 181 NativeResourcePoller 182 } 183 184 func (o *Operator) GetPod() (*corev1.Pod, error) { 185 opts := metav1.ListOptions{ 186 LabelSelector: fmt.Sprintf("name=%s", o.Name), 187 } 188 podList, err := o.Client.CoreV1().Pods(o.Namespace).List(context.TODO(), opts) 189 if err != nil { 190 return nil, err 191 } 192 return &podList.Items[0], nil 193 } 194 195 func (o *Operator) Restart() error { 196 pod, err := o.GetPod() 197 if err != nil { 198 return err 199 } 200 201 err = o.Client.CoreV1().Pods(o.Namespace).Delete(context.TODO(), pod.Name, metav1.DeleteOptions{}) 202 if err != nil { 203 return err 204 } 205 206 return nil 207 }