github.com/IBM-Blockchain/fabric-operator@v1.0.4/pkg/initializer/peer/coreconfigmap.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 initializer 20 21 import ( 22 "context" 23 "fmt" 24 "io/ioutil" 25 "path/filepath" 26 27 current "github.com/IBM-Blockchain/fabric-operator/api/v1beta1" 28 "github.com/IBM-Blockchain/fabric-operator/pkg/initializer/common" 29 "github.com/IBM-Blockchain/fabric-operator/pkg/initializer/common/config" 30 configv1 "github.com/IBM-Blockchain/fabric-operator/pkg/initializer/peer/config/v1" 31 configv2 "github.com/IBM-Blockchain/fabric-operator/pkg/initializer/peer/config/v2" 32 k8sclient "github.com/IBM-Blockchain/fabric-operator/pkg/k8s/controllerclient" 33 "github.com/IBM-Blockchain/fabric-operator/pkg/util" 34 "github.com/IBM-Blockchain/fabric-operator/version" 35 "github.com/pkg/errors" 36 corev1 "k8s.io/api/core/v1" 37 k8serrors "k8s.io/apimachinery/pkg/api/errors" 38 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 39 "k8s.io/apimachinery/pkg/runtime" 40 ) 41 42 //go:generate counterfeiter -o mocks/client.go -fake-name Client ../../k8s/controllerclient Client 43 44 type CoreConfigMap struct { 45 Config *Config 46 Scheme *runtime.Scheme 47 GetLabels func(instance metav1.Object) map[string]string 48 Client k8sclient.Client 49 } 50 51 func (c *CoreConfigMap) GetCoreConfig(instance *current.IBPPeer) (*corev1.ConfigMap, error) { 52 return common.GetConfigFromConfigMap(c.Client, instance) 53 } 54 55 func (c *CoreConfigMap) CreateOrUpdate(instance *current.IBPPeer, peer CoreConfig) error { 56 cm := &corev1.ConfigMap{ 57 ObjectMeta: metav1.ObjectMeta{ 58 Name: fmt.Sprintf("%s-config", instance.GetName()), 59 Namespace: instance.GetNamespace(), 60 Labels: c.GetLabels(instance), 61 }, 62 BinaryData: map[string][]byte{}, 63 } 64 65 existing, err := c.GetCoreConfig(instance) 66 if err != nil { 67 if !k8serrors.IsNotFound(err) { 68 return err 69 } 70 } 71 if existing != nil { 72 cm.BinaryData = existing.BinaryData 73 } 74 75 peerBytes, err := peer.ToBytes() 76 if err != nil { 77 return err 78 } 79 cm.BinaryData["core.yaml"] = peerBytes 80 81 err = c.addNodeOU(instance, cm) 82 if err != nil { 83 return err 84 } 85 86 err = c.Client.CreateOrUpdate(context.TODO(), cm, k8sclient.CreateOrUpdateOption{ 87 Owner: instance, 88 Scheme: c.Scheme, 89 }) 90 if err != nil { 91 return errors.Wrap(err, "failed to create or update Peer config map") 92 } 93 94 return nil 95 96 } 97 98 func (c *CoreConfigMap) AddNodeOU(instance *current.IBPPeer) error { 99 cm := &corev1.ConfigMap{ 100 ObjectMeta: metav1.ObjectMeta{ 101 Name: fmt.Sprintf("%s-config", instance.GetName()), 102 Namespace: instance.GetNamespace(), 103 Labels: c.GetLabels(instance), 104 }, 105 BinaryData: map[string][]byte{}, 106 } 107 108 existing, err := c.GetCoreConfig(instance) 109 if err != nil { 110 if !k8serrors.IsNotFound(err) { 111 return err 112 } 113 } 114 if existing != nil { 115 cm.BinaryData = existing.BinaryData 116 } 117 118 err = c.addNodeOU(instance, cm) 119 if err != nil { 120 return err 121 } 122 123 err = c.Client.CreateOrUpdate(context.TODO(), cm, k8sclient.CreateOrUpdateOption{ 124 Owner: instance, 125 Scheme: c.Scheme, 126 }) 127 if err != nil { 128 return errors.Wrap(err, "failed to create or update Peer config map") 129 } 130 131 return nil 132 } 133 134 func (c *CoreConfigMap) addNodeOU(instance *current.IBPPeer, cm *corev1.ConfigMap) error { 135 if !instance.Spec.NodeOUDisabled() { 136 configFilePath := c.Config.OUFile 137 138 // Check if both intermediate ecerts and tlscerts exists 139 if util.IntermediateSecretExists(c.Client, instance.Namespace, fmt.Sprintf("ecert-%s-intercerts", instance.Name)) && 140 util.IntermediateSecretExists(c.Client, instance.Namespace, fmt.Sprintf("tls-%s-intercerts", instance.Name)) { 141 configFilePath = c.Config.InterOUFile 142 } 143 144 ouBytes, err := ioutil.ReadFile(filepath.Clean(configFilePath)) 145 if err != nil { 146 return errors.Wrapf(err, "failed to read OU config file from '%s'", configFilePath) 147 } 148 149 cm.BinaryData["config.yaml"] = ouBytes 150 } else { 151 // Set enabled to false in config 152 nodeOUConfig, err := config.NodeOUConfigFromBytes(cm.BinaryData["config.yaml"]) 153 if err != nil { 154 return err 155 } 156 157 nodeOUConfig.NodeOUs.Enable = false 158 ouBytes, err := config.NodeOUConfigToBytes(nodeOUConfig) 159 if err != nil { 160 return err 161 } 162 163 cm.BinaryData["config.yaml"] = ouBytes 164 } 165 166 return nil 167 } 168 169 func GetCoreFromConfigMap(client k8sclient.Client, instance *current.IBPPeer) (*corev1.ConfigMap, error) { 170 return common.GetConfigFromConfigMap(client, instance) 171 } 172 173 func GetCoreConfigFromBytes(instance *current.IBPPeer, bytes []byte) (CoreConfig, error) { 174 switch version.GetMajorReleaseVersion(instance.Spec.FabricVersion) { 175 case version.V2: 176 v2config, err := configv2.ReadCoreFromBytes(bytes) 177 if err != nil { 178 return nil, err 179 } 180 return v2config, nil 181 case version.V1: 182 fallthrough 183 default: 184 // Choosing to default to v1.4 to not break backwards comptability, if coming 185 // from a previous version of operator the 'FabricVersion' field would not be set and would 186 // result in an error. 187 v1config, err := configv1.ReadCoreFromBytes(bytes) 188 if err != nil { 189 return nil, err 190 } 191 return v1config, nil 192 } 193 } 194 195 func GetCoreConfigFromFile(instance *current.IBPPeer, file string) (CoreConfig, error) { 196 switch version.GetMajorReleaseVersion(instance.Spec.FabricVersion) { 197 case version.V2: 198 log.Info("v2 Fabric Peer requested") 199 v2config, err := configv2.ReadCoreFile(file) 200 if err != nil { 201 return nil, err 202 } 203 return v2config, nil 204 case version.V1: 205 fallthrough 206 default: 207 // Choosing to default to v1.4 to not break backwards comptability, if coming 208 // from a previous version of operator the 'FabricVersion' field would not be set and would 209 // result in an error. // TODO: Determine if we want to throw error or handle setting 210 // FabricVersion as part of migration logic. 211 log.Info("v1 Fabric Peer requested") 212 pconfig, err := configv1.ReadCoreFile(file) 213 if err != nil { 214 return nil, err 215 } 216 return pconfig, nil 217 } 218 }