/
create.go
221 lines (215 loc) · 6.7 KB
/
create.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
package ordnode
import (
"context"
"encoding/base64"
"fmt"
"github.com/kfsoftware/hlf-operator/api/hlf.kungfusoftware.es/v1alpha1"
"github.com/kfsoftware/hlf-operator/controllers/utils"
"github.com/kfsoftware/hlf-operator/kubectl-hlf/cmd/helpers"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"io"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/yaml"
"strconv"
"strings"
)
type OrdererOptions struct {
Name string
StorageClass string
Capacity string
NS string
Image string
Version string
MspID string
EnrollID string
EnrollPW string
CAName string
SystemChannelName string
NumOrderers int
Hosts []string
Output bool
}
func (o OrdererOptions) Validate() error {
return nil
}
type createCmd struct {
out io.Writer
errOut io.Writer
ordererOpts OrdererOptions
}
func (c *createCmd) validate() error {
c.ordererOpts.Image = helpers.DefaultOrdererImage
return c.ordererOpts.Validate()
}
func (c *createCmd) run(args []string) error {
oclient, err := helpers.GetKubeOperatorClient()
if err != nil {
return err
}
certAuth, err := helpers.GetCertAuthByFullName(oclient, c.ordererOpts.CAName)
if err != nil {
return err
}
clientSet, err := helpers.GetKubeClient()
if err != nil {
return err
}
k8sIP, err := utils.GetPublicIPKubernetes(clientSet)
if err != nil {
return err
}
includeHosts := len(c.ordererOpts.Hosts) > 0
if includeHosts && len(c.ordererOpts.Hosts) != c.ordererOpts.NumOrderers {
return errors.Errorf("there are %d orderers but %d hosts", c.ordererOpts.NumOrderers, len(c.ordererOpts.Hosts))
}
var nodes []v1alpha1.OrdererNode
for i := 0; i < c.ordererOpts.NumOrderers; i++ {
hosts := []string{}
host := ""
port := 0
if includeHosts {
ordHost := c.ordererOpts.Hosts[i]
chunks := strings.Split(ordHost, ":")
host = chunks[0]
if len(chunks) < 2 {
return errors.Errorf("Host %s doesn't have port", ordHost)
}
port, err = strconv.Atoi(chunks[1])
if err != nil {
return err
}
hosts = append(hosts, host)
}
nodes = append(nodes, v1alpha1.OrdererNode{
ID: fmt.Sprintf("orderer%d", i),
Host: host,
Port: port,
Enrollment: v1alpha1.OrdererNodeEnrollment{
TLS: v1alpha1.OrdererNodeEnrollmentTLS{
Csr: v1alpha1.Csr{
Hosts: hosts,
},
},
},
})
}
if len(nodes) == 0 {
return errors.Errorf("Orderers are empty")
}
fabricOrderer := &v1alpha1.FabricOrdererNode{
TypeMeta: v1.TypeMeta{
Kind: "FabricOrderingService",
APIVersion: v1alpha1.GroupVersion.String(),
},
ObjectMeta: v1.ObjectMeta{
Name: c.ordererOpts.Name,
Namespace: c.ordererOpts.NS,
},
Spec: v1alpha1.FabricOrdererNodeSpec{
ServiceMonitor: nil,
HostAliases: []corev1.HostAlias{},
Resources: corev1.ResourceRequirements{},
Replicas: 1,
Image: c.ordererOpts.Image,
Tag: c.ordererOpts.Version,
PullPolicy: corev1.PullAlways,
MspID: c.ordererOpts.MspID,
Genesis: "",
BootstrapMethod: v1alpha1.BootstrapMethodNone,
ChannelParticipationEnabled: true,
Storage: v1alpha1.Storage{
Size: c.ordererOpts.Capacity,
StorageClass: c.ordererOpts.StorageClass,
AccessMode: "ReadWriteOnce",
},
Service: v1alpha1.OrdererNodeService{
Type: "NodePort",
},
Secret: &v1alpha1.Secret{
Enrollment: v1alpha1.Enrollment{
Component: v1alpha1.Component{
Cahost: k8sIP,
Caname: certAuth.Spec.CA.Name,
Caport: certAuth.Status.NodePort,
Catls: v1alpha1.Catls{
Cacert: base64.StdEncoding.EncodeToString([]byte(certAuth.Status.TlsCert)),
},
Enrollid: c.ordererOpts.EnrollID,
Enrollsecret: c.ordererOpts.EnrollPW,
},
TLS: v1alpha1.TLS{
Cahost: k8sIP,
Caname: certAuth.Spec.TLSCA.Name,
Caport: certAuth.Status.NodePort,
Catls: v1alpha1.Catls{
Cacert: base64.StdEncoding.EncodeToString([]byte(certAuth.Status.TlsCert)),
},
Csr: v1alpha1.Csr{
Hosts: []string{
"127.0.0.1",
"localhost",
k8sIP,
},
CN: "",
},
Enrollid: c.ordererOpts.EnrollID,
Enrollsecret: c.ordererOpts.EnrollPW,
},
},
},
Istio: nil,
AdminIstio: nil,
},
}
if c.ordererOpts.Output {
ot, err := yaml.Marshal(&fabricOrderer)
if err != nil {
return err
}
fmt.Println(string(ot))
} else {
ctx := context.Background()
ordService, err := oclient.HlfV1alpha1().FabricOrdererNodes(c.ordererOpts.NS).Create(
ctx,
fabricOrderer,
v1.CreateOptions{},
)
if err != nil {
return err
}
log.Infof("Ordering service %s created on namespace %s", ordService.Name, ordService.Namespace)
}
return nil
}
func newCreateOrdererNodeCmd(out io.Writer, errOut io.Writer) *cobra.Command {
c := createCmd{out: out, errOut: errOut}
cmd := &cobra.Command{
Use: "create",
Short: "Create a Fabric Ordering Service Node(OSN)",
RunE: func(cmd *cobra.Command, args []string) error {
if err := c.validate(); err != nil {
return err
}
return c.run(args)
},
}
f := cmd.Flags()
f.StringVar(&c.ordererOpts.Name, "name", "", "name of the Fabric Orderer to create")
f.StringVar(&c.ordererOpts.CAName, "ca-name", "", "name of the Fabric Orderer")
f.StringVar(&c.ordererOpts.EnrollID, "enroll-id", "", "user to enroll orderer certificates")
f.StringVar(&c.ordererOpts.EnrollPW, "enroll-pw", "", "password to enroll orderer certificates")
f.StringVar(&c.ordererOpts.Capacity, "capacity", "5Gi", "total raw capacity of Fabric Orderer in this zone, e.g. 16Ti")
f.StringVarP(&c.ordererOpts.NS, "namespace", "n", helpers.DefaultNamespace, "namespace scope for this request")
f.StringVarP(&c.ordererOpts.StorageClass, "storage-class", "s", helpers.DefaultStorageclass, "storage class for this Fabric Orderer")
f.StringVarP(&c.ordererOpts.Image, "image", "", helpers.DefaultOrdererImage, "version of the Fabric Orderer")
f.StringVarP(&c.ordererOpts.Version, "version", "", helpers.DefaultOrdererVersion, "version of the Fabric Orderer")
f.StringVarP(&c.ordererOpts.MspID, "mspid", "", "", "MSP ID of the organization")
f.StringVarP(&c.ordererOpts.SystemChannelName, "system-channel", "", "system-channel", "System channel name")
f.IntVarP(&c.ordererOpts.NumOrderers, "num-orderers", "", 3, "Orderers nodes to create")
f.StringArrayVarP(&c.ordererOpts.Hosts, "hosts", "", []string{}, "Hosts")
f.BoolVarP(&c.ordererOpts.Output, "output", "o", false, "output in yaml")
return cmd
}