-
Notifications
You must be signed in to change notification settings - Fork 91
/
join.go
136 lines (130 loc) · 3.45 KB
/
join.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
package ordnode
import (
"crypto/tls"
"crypto/x509"
"encoding/json"
"fmt"
"github.com/kfsoftware/hlf-operator/kubectl-hlf/cmd/helpers"
"github.com/kfsoftware/hlf-operator/kubectl-hlf/cmd/helpers/osnadmin"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"io"
"io/ioutil"
"sigs.k8s.io/yaml"
)
type joinChannelCmd struct {
block string
name string
namespace string
identity string
}
type identity struct {
Cert Pem `json:"cert"`
Key Pem `json:"key"`
}
type Pem struct {
Pem string
}
func (c *joinChannelCmd) validate() error {
if c.namespace == "" {
return errors.Errorf("--namespace is required")
}
if c.name == "" {
return errors.Errorf("--name is required")
}
if c.identity == "" {
return errors.Errorf("--identity is required")
}
if c.block == "" {
return errors.Errorf("--block is required")
}
return nil
}
func (c *joinChannelCmd) run() error {
clientSet, err := helpers.GetKubeClient()
if err != nil {
return err
}
hlfClient, err := helpers.GetKubeOperatorClient()
if err != nil {
return err
}
log.Printf("name=%s namespace=%s", c.name, c.namespace)
ordererNode, err := helpers.GetOrdererNodeByFullName(clientSet, hlfClient, fmt.Sprintf("%s.%s", c.name, c.namespace))
if err != nil {
return err
}
certPool := x509.NewCertPool()
ok := certPool.AppendCertsFromPEM([]byte(ordererNode.Status.TlsCert))
if !ok {
return errors.Errorf("failed to add certificate")
}
adminTlsCert := ordererNode.Status.TlsAdminCert
ok = certPool.AppendCertsFromPEM([]byte(adminTlsCert))
if !ok {
return errors.Errorf("failed to add certificate")
}
identityBytes, err := ioutil.ReadFile(c.identity)
if err != nil {
return err
}
id := &identity{}
err = yaml.Unmarshal(identityBytes, id)
if err != nil {
return err
}
tlsClientCert, err := tls.X509KeyPair(
[]byte(id.Cert.Pem),
[]byte(id.Key.Pem),
)
if err != nil {
return err
}
ordererHostName, adminPort, err := helpers.GetOrdererAdminHostAndPort(clientSet, ordererNode.Spec, ordererNode.Status)
if err != nil {
return err
}
osnUrl := fmt.Sprintf("https://%s:%d", ordererHostName, adminPort)
blockBytes, err := ioutil.ReadFile(c.block)
if err != nil {
return err
}
chResponse, err := osnadmin.Join(osnUrl, blockBytes, certPool, tlsClientCert)
if err != nil {
return err
}
defer chResponse.Body.Close()
log.Infof("Status code=%d", chResponse.StatusCode)
if chResponse.StatusCode != 201 {
return errors.Errorf("error joining channel, got status code=%d", chResponse.StatusCode)
}
chInfo := &osnadmin.ChannelInfo{}
err = json.NewDecoder(chResponse.Body).Decode(chInfo)
if err != nil {
return err
}
return nil
}
func newJoinChannelCMD(io.Writer, io.Writer) *cobra.Command {
c := &joinChannelCmd{}
cmd := &cobra.Command{
Use: "join",
RunE: func(cmd *cobra.Command, args []string) error {
if err := c.validate(); err != nil {
return err
}
return c.run()
},
}
persistentFlags := cmd.PersistentFlags()
persistentFlags.StringVarP(&c.identity, "identity", "", "", "Admin org to invoke the updates")
persistentFlags.StringVarP(&c.block, "block", "", "", "Block")
persistentFlags.StringVarP(&c.name, "name", "", "", "Orderer Service name")
persistentFlags.StringVarP(&c.namespace, "namespace", "", "default", "Namespace scope for this request")
cmd.MarkPersistentFlagRequired("identity")
cmd.MarkPersistentFlagRequired("block")
cmd.MarkPersistentFlagRequired("name")
cmd.MarkPersistentFlagRequired("namespace")
return cmd
}