Skip to content

Commit

Permalink
Allow in cluster config
Browse files Browse the repository at this point in the history
  • Loading branch information
dviejokfs committed Apr 12, 2022
1 parent d1c6ffb commit d8eeb5e
Show file tree
Hide file tree
Showing 10 changed files with 528 additions and 68 deletions.
4 changes: 3 additions & 1 deletion cmd/channel/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,13 @@ func (c syncCmd) run() error {
err = channel.SyncChannel(
ctx,
channelConfig,
channelManagerConfig,
map[string]*appconfig.DCClient{},
sdk,
coreBackends,
c.saveOrderer,
c.savePeer,
false,
false,
)
if err != nil {
return err
Expand Down
69 changes: 55 additions & 14 deletions cmd/serve/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@ package serve

import (
"context"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/core"
"github.com/hyperledger/fabric-sdk-go/pkg/core/config"
"github.com/hyperledger/fabric-sdk-go/pkg/fabsdk"
appconfig "github.com/kfsoftware/hlf-channel-manager/config"
"github.com/kfsoftware/hlf-channel-manager/log"
"github.com/kfsoftware/hlf-channel-manager/nc"
"github.com/kfsoftware/hlf-channel-manager/server"
operatorv1 "github.com/kfsoftware/hlf-operator/pkg/client/clientset/versioned"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
"io/ioutil"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)

Expand Down Expand Up @@ -58,27 +62,28 @@ func (c *serveCmd) validate() error {
if c.metricsAddress == "" {
return errors.New("--metrics-address is required for the server")
}
if c.config == "" {
return errors.New("--config is required for the server")
}
if c.hlfConfig == "" {
return errors.New("--hlf-config is required for the server")
}
return nil
}

func (c *serveCmd) run() error {
ctx := context.Background()
channelManagerConfig := appconfig.HLFChannelManagerConfig{}
hlfManagerConfigBytes, err := ioutil.ReadFile(c.config)
if err != nil {
return err
if c.config != "" {
hlfManagerConfigBytes, err := ioutil.ReadFile(c.config)
if err != nil {
return err
}
err = yaml.Unmarshal(hlfManagerConfigBytes, &channelManagerConfig)
if err != nil {
return err
}
}
err = yaml.Unmarshal(hlfManagerConfigBytes, &channelManagerConfig)
restConfigInCluster, err := rest.InClusterConfig()
if err != nil {
return err
log.Warnf("Failed to get in cluster config: %s", err)
}
dcs := map[string]*operatorv1.Clientset{}
log.Infof("Creating a new SDK instance to connect to %s", restConfigInCluster)
dcs := map[string]*appconfig.DCClient{}
for _, dc := range channelManagerConfig.DCs {
restConfig, err := clientcmd.BuildConfigFromFlags("", dc.KubeConfig)
if err != nil {
Expand All @@ -90,9 +95,45 @@ func (c *serveCmd) run() error {
log.Errorf("failed to build hlf client from %v", err)
return err
}
dcs[dc.Name] = hlfClient
kubeClientSet, err := kubernetes.NewForConfig(restConfig)
if err != nil {
log.Errorf("failed to build kube client from %v", err)
return err
}
dcs[dc.Name] = &appconfig.DCClient{
HLFClient: hlfClient,
KubeClient: kubeClientSet,
KubeConfig: restConfigInCluster,
}
}
var configBackend core.ConfigProvider
if c.hlfConfig != "" {
configBackend = config.FromFile(c.hlfConfig)
} else if restConfigInCluster != nil {
hlfClient, err := operatorv1.NewForConfig(restConfigInCluster)
if err != nil {
log.Errorf("failed to build hlf client from %v", err)
return err
}
kubeClientSet, err := kubernetes.NewForConfig(restConfigInCluster)
if err != nil {
log.Errorf("failed to build kube client from %v", err)
return err
}
ncResponse, err := nc.GenerateNetworkConfig(kubeClientSet, hlfClient, "")
if err != nil {
log.Errorf("failed to generate network config from %v", err)
return err
}
configBackend = config.FromRaw([]byte(ncResponse.NetworkConfig), "yaml")
dcs["default"] = &appconfig.DCClient{
HLFClient: hlfClient,
KubeClient: kubeClientSet,
KubeConfig: restConfigInCluster,
}
} else {
return errors.New("no network config configured")
}
configBackend := config.FromFile(c.hlfConfig)
sdk, err := fabsdk.New(configBackend)
if err != nil {
return err
Expand Down
39 changes: 30 additions & 9 deletions config/common.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package config

import "time"
import (
operatorv1 "github.com/kfsoftware/hlf-operator/pkg/client/clientset/versioned"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"time"
)

type HLFChannelManagerConfig struct {
DCs []DC `yaml:"dcs"`
Expand All @@ -10,6 +15,12 @@ type DC struct {
KubeConfig string `yaml:"kubeconfig"`
}

type DCClient struct {
HLFClient *operatorv1.Clientset
KubeClient *kubernetes.Clientset
KubeConfig *rest.Config
}

type ChannelConfig struct {
Name string `yaml:"name"`
Consenters []Consenter `yaml:"consenters"`
Expand Down Expand Up @@ -70,27 +81,37 @@ type Policy struct {
type AdminOrg struct {
MSPID string `yaml:"mspid"`
CA string `yaml:"ca"`
TLSCA string `yaml:"tlsCA"`
}

type PeerOrg struct {
MSPID string `yaml:"mspid"`
CA CA `yaml:"ca"`
MSPID string `yaml:"mspid"`
CA CA `yaml:"ca"`
SignCA string `yaml:"signCA"`
Peers []Peer `yaml:"peers"`
}
type Peer struct {
Name string `yaml:"name"`
DC string `yaml:"dc"`
}
type OrdererOrg struct {
MSPID string `yaml:"mspid"`
CA CA `yaml:"ca"`
Orderers []Orderer `yaml:"orderers"`
}
type Orderer struct {
Name string `yaml:"name"`
DC string `yaml:"dc"`
Name string `yaml:"name"`
Namespace string `yaml:"namespace"`
DC string `yaml:"dc"`
}

type CA struct {
Name string `yaml:"name"`
DC string `yaml:"dc"`
Name string `yaml:"name"`
Namespace string `yaml:"namespace"`
DC string `yaml:"dc"`
}
type Consenter struct {
Name string `yaml:"name"`
DC string `yaml:"dc"`
Name string `yaml:"name"`
DC string `yaml:"dc"`
Namespace string `yaml:"namespace"`
}
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.17

require (
github.com/99designs/gqlgen v0.17.1
github.com/Masterminds/sprig/v3 v3.1.0
github.com/golang/protobuf v1.5.2
github.com/hyperledger/fabric v2.1.1+incompatible
github.com/hyperledger/fabric-config v0.1.0
Expand All @@ -25,6 +26,8 @@ require (

require (
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible // indirect
github.com/Masterminds/goutils v1.1.0 // indirect
github.com/Masterminds/semver/v3 v3.1.0 // indirect
github.com/agnivade/levenshtein v1.1.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
Expand All @@ -42,14 +45,17 @@ require (
github.com/gorilla/websocket v1.4.2 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/huandu/xstrings v1.3.1 // indirect
github.com/hyperledger/fabric-lib-go v1.0.0 // indirect
github.com/imdario/mergo v0.3.9 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/magiconair/properties v1.8.5 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/miekg/pkcs11 v1.1.1 // indirect
github.com/mitchellh/copystructure v1.0.0 // indirect
github.com/mitchellh/mapstructure v1.4.3 // indirect
github.com/mitchellh/reflectwalk v1.0.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/operator-framework/operator-lib v0.1.0 // indirect
Expand Down Expand Up @@ -83,6 +89,7 @@ require (
gopkg.in/ini.v1 v1.66.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
k8s.io/api v0.18.4 // indirect
k8s.io/apiextensions-apiserver v0.18.4 // indirect
k8s.io/klog v1.0.0 // indirect
k8s.io/utils v0.0.0-20200603063816-c1c6865ac451 // indirect
sigs.k8s.io/controller-runtime v0.6.1 // indirect
Expand Down
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,15 @@ github.com/Knetic/govaluate v3.0.0+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8L
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible h1:1G1pk05UrOh0NlF1oeaaix1x8XzrfjIDK47TY0Zehcw=
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
github.com/MakeNowJust/heredoc v0.0.0-20170808103936-bb23615498cd/go.mod h1:64YHyfSL2R96J44Nlwm39UHepQbyR5q10x7iYa1ks2E=
github.com/Masterminds/goutils v1.1.0 h1:zukEsf/1JZwCMgHiK3GZftabmxiCw4apj3a28RPBiVg=
github.com/Masterminds/goutils v1.1.0/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3QEww=
github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
github.com/Masterminds/semver/v3 v3.1.0 h1:Y2lUDsFKVRSYGojLJ1yLxSXdMmMYTYls0rCvoqmMUQk=
github.com/Masterminds/semver/v3 v3.1.0/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
github.com/Masterminds/sprig v2.22.0+incompatible h1:z4yfnGrZ7netVz+0EDJ0Wi+5VZCSYp4Z0m2dk6cEM60=
github.com/Masterminds/sprig v2.22.0+incompatible/go.mod h1:y6hNFY5UBTIWBxnzTeuNhlNS5hqE0NB0E6fgfo2Br3o=
github.com/Masterminds/sprig/v3 v3.1.0 h1:j7GpgZ7PdFqNsmncycTHsLmVPf5/3wJtlgW9TNDYD9Y=
github.com/Masterminds/sprig/v3 v3.1.0/go.mod h1:ONGMf7UfYGAbMXCZmQLy8x3lCDIPrEZE/rU8pmrbihA=
github.com/Masterminds/squirrel v1.2.0/go.mod h1:yaPeOnPG5ZRwL9oKdTsO/prlkPbXWZlRVMQ/gGlzIuA=
github.com/Masterminds/vcs v1.13.1/go.mod h1:N09YCmOQr6RLxC6UNHzuVwAdodYbbnycGHSmwVJjcKA=
Expand Down Expand Up @@ -506,6 +511,7 @@ github.com/hashicorp/mdns v1.0.4/go.mod h1:mtBihi+LeNXGtG8L9dX59gAEa12BDtBQSp4v/
github.com/hashicorp/memberlist v0.3.0/go.mod h1:MS2lj3INKhZjWNqd3N0m3J+Jxf3DAOnAH9VT3Sh9MUE=
github.com/hashicorp/serf v0.9.6/go.mod h1:TXZNMjZQijwlDvp+r0b63xZ45H7JmCmgg4gpTwn9UV4=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/huandu/xstrings v1.3.1 h1:4jgBlKK6tLKFvO8u5pmYjG91cqytmDCDvGh7ECVFfFs=
github.com/huandu/xstrings v1.3.1/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
github.com/hyperledger/fabric v2.1.1+incompatible h1:cYYRv3vVg4kA6DmrixLxwn1nwBEUuYda8DsMwlaMKbY=
github.com/hyperledger/fabric v2.1.1+incompatible/go.mod h1:tGFAOCT696D3rG0Vofd2dyWYLySHlh0aQjf7Q1HAju0=
Expand Down Expand Up @@ -655,6 +661,7 @@ github.com/miekg/pkcs11 v1.1.1 h1:Ugu9pdy6vAYku5DEpVWVFPYnzV+bxB+iRdbuFSu7TvU=
github.com/miekg/pkcs11 v1.1.1/go.mod h1:XsNlhZGX73bx86s2hdc/FuaLm2CPZJemRLMA+WTFxgs=
github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc=
github.com/mitchellh/cli v1.1.0/go.mod h1:xcISNoH86gajksDmfB23e/pu+B+GeFRMYmoHXxx3xhI=
github.com/mitchellh/copystructure v1.0.0 h1:Laisrj+bAB6b/yJwB5Bt3ITZhGJdqmxquMKeZ+mmkFQ=
github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/go-testing-interface v1.0.0/go.mod h1:kRemZodwjscx+RGhAo8eIhFbs2+BFgRtFPeD/KE+zxI=
Expand All @@ -666,6 +673,7 @@ github.com/mitchellh/mapstructure v1.3.2/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RR
github.com/mitchellh/mapstructure v1.4.3 h1:OVowDSCllw/YjdLkam3/sm7wEtOy59d8ndGgCcyj8cs=
github.com/mitchellh/mapstructure v1.4.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mitchellh/osext v0.0.0-20151018003038-5e2d6d41470f/go.mod h1:OkQIRizQZAeMln+1tSwduZz7+Af5oFlKirV/MSYes2A=
github.com/mitchellh/reflectwalk v1.0.0 h1:9D+8oIskB4VJBN5SFlmc27fSlIBZaov1Wpk/IfikLNY=
github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
Expand Down Expand Up @@ -1486,6 +1494,7 @@ k8s.io/api v0.18.2/go.mod h1:SJCWI7OLzhZSvbY7U8zwNl9UA4o1fizoug34OV/2r78=
k8s.io/api v0.18.4 h1:8x49nBRxuXGUlDlwlWd3RMY1SayZrzFfxea3UZSkFw4=
k8s.io/api v0.18.4/go.mod h1:lOIQAKYgai1+vz9J7YcDZwC26Z0zQewYOGWdyIPUUQ4=
k8s.io/apiextensions-apiserver v0.18.0/go.mod h1:18Cwn1Xws4xnWQNC00FLq1E350b9lUF+aOdIWDOZxgo=
k8s.io/apiextensions-apiserver v0.18.4 h1:Y3HGERmS8t9u12YNUFoOISqefaoGRuTc43AYCLzWmWE=
k8s.io/apiextensions-apiserver v0.18.4/go.mod h1:NYeyeYq4SIpFlPxSAB6jHPIdvu3hL0pc36wuRChybio=
k8s.io/apimachinery v0.18.0/go.mod h1:9SnR/e11v5IbyPCGbvJViimtJ0SwHG4nfZFjU77ftcA=
k8s.io/apimachinery v0.18.2/go.mod h1:9SnR/e11v5IbyPCGbvJViimtJ0SwHG4nfZFjU77ftcA=
Expand Down
4 changes: 3 additions & 1 deletion gql/resolvers/mutation.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,11 +333,13 @@ func (r *mutationResolver) SyncChannel(ctx context.Context, channelConfigStr str
err = channel.SyncChannel(
ctx,
channelConfig,
r.ChannelManagerConfig,
r.DCS,
r.FabricSDK,
r.ConfigBackend,
saveOrderer,
saveApplication,
true,
true,
)
if err != nil {
return nil, err
Expand Down
3 changes: 1 addition & 2 deletions gql/resolvers/resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ import (
"github.com/hyperledger/fabric-sdk-go/pkg/fabsdk"
appconfig "github.com/kfsoftware/hlf-channel-manager/config"
"github.com/kfsoftware/hlf-channel-manager/gql"
operatorv1 "github.com/kfsoftware/hlf-operator/pkg/client/clientset/versioned"
)

type Resolver struct {
DCS map[string]*operatorv1.Clientset
DCS map[string]*appconfig.DCClient
ChannelManagerConfig appconfig.HLFChannelManagerConfig
FabricSDK *fabsdk.FabricSDK
ConfigBackend []core.ConfigBackend
Expand Down
Loading

0 comments on commit d8eeb5e

Please sign in to comment.