-
Notifications
You must be signed in to change notification settings - Fork 35
/
peer.go
286 lines (241 loc) · 8.77 KB
/
peer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
* Copyright contributors to the Hyperledger Fabric Operator project
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package v2
import (
"context"
"fmt"
"reflect"
"strings"
"github.com/pkg/errors"
current "github.com/IBM-Blockchain/fabric-operator/api/v1beta1"
config "github.com/IBM-Blockchain/fabric-operator/operatorconfig"
"github.com/IBM-Blockchain/fabric-operator/pkg/action"
"github.com/IBM-Blockchain/fabric-operator/pkg/apis/common"
"github.com/IBM-Blockchain/fabric-operator/pkg/apis/deployer"
v2peer "github.com/IBM-Blockchain/fabric-operator/pkg/apis/peer/v2"
initializer "github.com/IBM-Blockchain/fabric-operator/pkg/initializer/peer"
v2config "github.com/IBM-Blockchain/fabric-operator/pkg/initializer/peer/config/v2"
k8sclient "github.com/IBM-Blockchain/fabric-operator/pkg/k8s/controllerclient"
ver "github.com/IBM-Blockchain/fabric-operator/version"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/yaml"
)
var log = logf.Log.WithName("peer_fabric_migrator")
//go:generate counterfeiter -o mocks/configmapmanager.go -fake-name ConfigMapManager . ConfigMapManager
type ConfigMapManager interface {
GetCoreConfig(*current.IBPPeer) (*corev1.ConfigMap, error)
CreateOrUpdate(*current.IBPPeer, initializer.CoreConfig) error
}
//go:generate counterfeiter -o mocks/deploymentmanager.go -fake-name DeploymentManager . DeploymentManager
type DeploymentManager interface {
Get(metav1.Object) (client.Object, error)
Delete(metav1.Object) error
DeploymentStatus(metav1.Object) (appsv1.DeploymentStatus, error)
GetScheme() *runtime.Scheme
}
type Migrate struct {
DeploymentManager DeploymentManager
ConfigMapManager ConfigMapManager
Client k8sclient.Client
}
func (m *Migrate) MigrationNeeded(instance metav1.Object) bool {
// Check for DinD container, if DinD container not found this is
// v2 fabric IBP instance
obj, err := m.DeploymentManager.Get(instance)
if err != nil {
// If deployment does not exist, this instance is not a healthy
// state and migration should be avoided
return false
}
var deploymentUpdated bool
var configUpdated bool
dep := obj.(*appsv1.Deployment)
for _, cont := range dep.Spec.Template.Spec.Containers {
if strings.ToLower(cont.Name) == "dind" {
// DinD container found, instance is not at v2
deploymentUpdated = false
}
}
cm, err := m.ConfigMapManager.GetCoreConfig(instance.(*current.IBPPeer))
if err != nil {
// If config map does not exist, this instance is not a healthy
// state and migration should be avoided
return false
}
v1corebytes := cm.BinaryData["core.yaml"]
core := &v2config.Core{}
err = yaml.Unmarshal(v1corebytes, core)
if err != nil {
return false
}
configUpdated = configHasBeenUpdated(core)
return !deploymentUpdated || !configUpdated
}
func (m *Migrate) UpgradeDBs(instance metav1.Object, timeouts config.DBMigrationTimeouts) error {
log.Info(fmt.Sprintf("Resetting Peer '%s'", instance.GetName()))
return action.UpgradeDBs(m.DeploymentManager, m.Client, instance.(*current.IBPPeer), timeouts)
}
func (m *Migrate) UpdateConfig(instance metav1.Object, version string) error {
log.Info("Updating config to v2")
cm, err := m.ConfigMapManager.GetCoreConfig(instance.(*current.IBPPeer))
if err != nil {
return errors.Wrap(err, "failed to get config map")
}
v1corebytes := cm.BinaryData["core.yaml"]
core := &v2config.Core{}
err = yaml.Unmarshal(v1corebytes, core)
if err != nil {
return err
}
// resetting VM endpoint
// As per this PR #2165, VM and Ledger structs been added to Peer. endpoint is not required for v2 peer as there is no DinD
core.VM.Endpoint = ""
core.Chaincode.ExternalBuilders = []v2peer.ExternalBuilder{
v2peer.ExternalBuilder{
Name: "ibp-builder",
Path: "/usr/local",
EnvironmentWhiteList: []string{
"IBP_BUILDER_ENDPOINT",
"IBP_BUILDER_SHARED_DIR",
},
PropogateEnvironment: []string{
"IBP_BUILDER_ENDPOINT",
"IBP_BUILDER_SHARED_DIR",
"PEER_NAME",
},
},
}
core.Chaincode.InstallTimeout = common.MustParseDuration("300s")
if core.Chaincode.System == nil {
core.Chaincode.System = make(map[string]string)
}
core.Chaincode.System["_lifecycle"] = "enable"
core.Peer.Limits.Concurrency.DeliverService = 2500
core.Peer.Limits.Concurrency.EndorserService = 2500
core.Peer.Gossip.PvtData.ImplicitCollectionDisseminationPolicy.RequiredPeerCount = 0
core.Peer.Gossip.PvtData.ImplicitCollectionDisseminationPolicy.MaxPeerCount = 1
currentVer := ver.String(version)
if currentVer.EqualWithoutTag(ver.V2_4_1) || currentVer.GreaterThan(ver.V2_4_1) {
trueVal := true
core.Peer.Gateway = v2peer.Gateway{
Enabled: &trueVal,
EndorsementTimeout: common.MustParseDuration("30s"),
DialTimeout: common.MustParseDuration("120s"),
}
core.Peer.Limits.Concurrency.GatewayService = 500
core.Ledger.State.SnapShots = v2peer.SnapShots{
RootDir: "/data/peer/ledgersData/snapshots/",
}
}
core.Ledger.State.CouchdbConfig.CacheSize = 64
core.Ledger.State.CouchdbConfig.MaxRetries = 10
err = m.ConfigMapManager.CreateOrUpdate(instance.(*current.IBPPeer), core)
if err != nil {
return err
}
return nil
}
// SetChaincodeLauncherResourceOnCR will update the peer's CR by adding chaincode launcher
// resources. The default resources are defined in deployer's config map, which is part
// IBPConsole resource. The default resources are extracted for the chaincode launcher
// by reading the deployer's config map and updating the CR.
func (m *Migrate) SetChaincodeLauncherResourceOnCR(instance metav1.Object) error {
log.Info("Setting chaincode launcher resource on CR")
cr := instance.(*current.IBPPeer)
if cr.Spec.Resources != nil && cr.Spec.Resources.CCLauncher != nil {
// No need to proceed further if Chaincode launcher resources already set
return nil
}
consoleList := ¤t.IBPConsoleList{}
if err := m.Client.List(context.TODO(), consoleList); err != nil {
return err
}
consoles := consoleList.Items
// If no consoles found, set default resource for chaincode launcher container
rr := &corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("0.1"),
corev1.ResourceMemory: resource.MustParse("100Mi"),
},
Limits: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("2"),
corev1.ResourceMemory: resource.MustParse("2Gi"),
},
}
if len(consoles) > 0 {
log.Info("Setting chaincode launcher resource on CR based on deployer config from config map")
// Get config map associated with console
cm := &corev1.ConfigMap{}
nn := types.NamespacedName{
Name: fmt.Sprintf("%s-deployer", consoles[0].GetName()),
Namespace: instance.GetNamespace(),
}
if err := m.Client.Get(context.TODO(), nn, cm); err != nil {
return err
}
settingsBytes := []byte(cm.Data["settings.yaml"])
settings := &deployer.Config{}
if err := yaml.Unmarshal(settingsBytes, settings); err != nil {
return err
}
if settings.Defaults != nil && settings.Defaults.Resources != nil &&
settings.Defaults.Resources.Peer != nil && settings.Defaults.Resources.Peer.CCLauncher != nil {
rr = settings.Defaults.Resources.Peer.CCLauncher
}
}
log.Info(fmt.Sprintf("Setting chaincode launcher resource on CR to %+v", rr))
if cr.Spec.Resources == nil {
cr.Spec.Resources = ¤t.PeerResources{}
}
cr.Spec.Resources.CCLauncher = rr
if err := m.Client.Update(context.TODO(), cr); err != nil {
return err
}
return nil
}
// Updates required from v1.4 to v2.x:
// - External builders
// - Limits
// - Install timeout
// - Implicit collection dissemination policy
func configHasBeenUpdated(core *v2config.Core) bool {
if len(core.Chaincode.ExternalBuilders) == 0 {
return false
}
if core.Chaincode.ExternalBuilders[0].Name != "ibp-builder" {
return false
}
// Check if install timeout was set
if reflect.DeepEqual(core.Chaincode.InstallTimeout, common.Duration{}) {
return false
}
if core.Peer.Limits.Concurrency.DeliverService != 2500 {
return false
}
if core.Peer.Limits.Concurrency.EndorserService != 2500 {
return false
}
return true
}