Skip to content

Commit e8d404a

Browse files
committed
add network selection
Signed-off-by: Oleg <[email protected]>
1 parent 18d3dc0 commit e8d404a

21 files changed

+528
-385
lines changed

api/v1alpha1/circuitrelay_types.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ limitations under the License.
1717
package v1alpha1
1818

1919
import (
20-
"github.com/libp2p/go-libp2p-core/peer"
20+
"github.com/libp2p/go-libp2p/core/peer"
2121
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2222

2323
ma "github.com/multiformats/go-multiaddr"

api/v1alpha1/ipfscluster_types.go

+19-3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ const (
4444
ReproviderStrategyRoots ReproviderStrategy = "roots"
4545
)
4646

47+
type NetworkMode string
48+
49+
const (
50+
// NetworkModePublic Defines an IPFSCluster running in public mode with its
51+
// content available to everyone.
52+
NetworkModePublic NetworkMode = "public"
53+
// NetworkModePrivate Defines an IPFSCluster running in a private network
54+
// with its content only available to other authorized nodes.
55+
NetworkModePrivate NetworkMode = "private"
56+
)
57+
4758
type ReprovideSettings struct {
4859
// Strategy specifies the reprovider strategy, defaults to 'all'.
4960
// +kubebuilder:validation:Enum={all,pinned,roots}
@@ -60,9 +71,14 @@ type followParams struct {
6071
Template string `json:"template"`
6172
}
6273

63-
// networkConfig defines the configuration structure used for networking.
64-
type networkConfig struct {
74+
// NetworkConfig defines the configuration structure used for networking.
75+
type NetworkConfig struct {
76+
// circuitRelays defines how many CircuitRelays should be created.
6577
CircuitRelays int32 `json:"circuitRelays"`
78+
// networkMode is a switch which defines whether this IPFSCluster will use
79+
// the global IPFS network or create its own.
80+
// +kubebuilder:validation:Enum={public,private}
81+
NetworkMode NetworkMode `json:"networkMode,omitempty"`
6682
}
6783

6884
// IpfsClusterSpec defines the desired state of the IpfsCluster.
@@ -74,7 +90,7 @@ type IpfsClusterSpec struct {
7490
// replicas sets the number of replicas of IPFS Cluster nodes we should be running.
7591
Replicas int32 `json:"replicas"`
7692
// networking defines network configuration settings.
77-
Networking networkConfig `json:"networking"`
93+
Networking NetworkConfig `json:"networking"`
7894
// follows defines the list of other IPFS Clusters this one should follow.
7995
Follows []followParams `json:"follows"`
8096
// ipfsResources specifies the resource requirements for each IPFS container. If this

api/v1alpha1/zz_generated.deepcopy.go

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bundle/manifests/cluster.ipfs.io_ipfsclusters.yaml

+9
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,17 @@ spec:
9797
description: networking defines network configuration settings.
9898
properties:
9999
circuitRelays:
100+
description: circuitRelays defines how many CircuitRelays should
101+
be created.
100102
format: int32
101103
type: integer
104+
networkMode:
105+
description: networkMode is a switch which defines whether this
106+
IPFSCluster will use the global IPFS network or create its own.
107+
enum:
108+
- public
109+
- private
110+
type: string
102111
required:
103112
- circuitRelays
104113
type: object

bundle/manifests/ipfs-operator.clusterserviceversion.yaml

+19-7
Large diffs are not rendered by default.

config/crd/bases/cluster.ipfs.io_ipfsclusters.yaml

+9
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,17 @@ spec:
9898
description: networking defines network configuration settings.
9999
properties:
100100
circuitRelays:
101+
description: circuitRelays defines how many CircuitRelays should
102+
be created.
101103
format: int32
102104
type: integer
105+
networkMode:
106+
description: networkMode is a switch which defines whether this
107+
IPFSCluster will use the global IPFS network or create its own.
108+
enum:
109+
- public
110+
- private
111+
type: string
103112
required:
104113
- circuitRelays
105114
type: object

config/manager/kustomization.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,3 @@ kind: Kustomization
1313
images:
1414
- name: controller
1515
newName: quay.io/redhat-et-ipfs/ipfs-operator
16-
newTag: 0.0.1

config/manifests/bases/ipfs-operator.clusterserviceversion.yaml

+5-5
Large diffs are not rendered by default.

controllers/circuitrelay_controller.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ import (
3636
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
3737
"sigs.k8s.io/controller-runtime/pkg/log"
3838

39-
"github.com/libp2p/go-libp2p-core/crypto"
40-
peer "github.com/libp2p/go-libp2p-core/peer"
4139
relaydaemon "github.com/libp2p/go-libp2p-relay-daemon"
40+
"github.com/libp2p/go-libp2p/core/crypto"
41+
peer "github.com/libp2p/go-libp2p/core/peer"
4242
relayv2 "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/relay"
4343
clusterv1alpha1 "github.com/redhat-et/ipfs-operator/api/v1alpha1"
4444
"github.com/redhat-et/ipfs-operator/controllers/utils"
@@ -181,7 +181,7 @@ func (r *CircuitRelayReconciler) generateNewIdentity(
181181
instance.Status.AddrInfo.Addrs = addrStrings
182182
var privkey crypto.PrivKey
183183
var pubkey peer.ID
184-
privkey, pubkey, err = newKey()
184+
privkey, pubkey, err = utils.NewKey()
185185
if err != nil {
186186
return nil, fmt.Errorf("error during key generation: %w", err)
187187
}

controllers/ipfs_util.go

+38-45
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,43 @@
11
package controllers
22

3-
import (
4-
"crypto/rand"
5-
"encoding/base64"
6-
"encoding/hex"
7-
"fmt"
3+
// ci "github.com/libp2p/go-libp2p-core/crypto"
4+
// peer "github.com/libp2p/go-libp2p-core/peer"
85

9-
ci "github.com/libp2p/go-libp2p-core/crypto"
10-
peer "github.com/libp2p/go-libp2p-core/peer"
11-
)
6+
// func newClusterSecret() (string, error) {
7+
// const secretLen = 32
8+
// buf := make([]byte, secretLen)
9+
// _, err := rand.Read(buf)
10+
// if err != nil {
11+
// return "", err
12+
// }
13+
// return hex.EncodeToString(buf), nil
14+
// }
1215

13-
func newClusterSecret() (string, error) {
14-
const secretLen = 32
15-
buf := make([]byte, secretLen)
16-
_, err := rand.Read(buf)
17-
if err != nil {
18-
return "", err
19-
}
20-
return hex.EncodeToString(buf), nil
21-
}
16+
// // newKey Generates a new private key and returns that along with the identity.
17+
// func newKey() (ci.PrivKey, peer.ID, error) {
18+
// const edDSAKeyLen = 4096
19+
// priv, pub, err := ci.GenerateKeyPair(ci.Ed25519, edDSAKeyLen)
20+
// if err != nil {
21+
// return nil, "", err
22+
// }
23+
// peerid, err := peer.IDFromPublicKey(pub)
24+
// if err != nil {
25+
// return nil, "", err
26+
// }
27+
// return priv, peerid, nil
28+
// }
2229

23-
// newKey Generates a new private key and returns that along with the identity.
24-
func newKey() (ci.PrivKey, peer.ID, error) {
25-
const edDSAKeyLen = 4096
26-
priv, pub, err := ci.GenerateKeyPair(ci.Ed25519, edDSAKeyLen)
27-
if err != nil {
28-
return nil, "", err
29-
}
30-
peerid, err := peer.IDFromPublicKey(pub)
31-
if err != nil {
32-
return nil, "", err
33-
}
34-
return priv, peerid, nil
35-
}
36-
37-
// generateIdentity Generates a new key and returns the peer ID and private key
38-
// encoded as a base64 string using standard encoding, or an error if the key could not be generated.
39-
func generateIdentity() (peer.ID, string, error) {
40-
priv, peerid, err := newKey()
41-
if err != nil {
42-
return "", "", fmt.Errorf("cannot generate new key: %w", err)
43-
}
44-
privBytes, err := ci.MarshalPrivateKey(priv)
45-
if err != nil {
46-
return "", "", fmt.Errorf("cannot get bytes from private key: %w", err)
47-
}
48-
privStr := base64.StdEncoding.EncodeToString(privBytes)
49-
return peerid, privStr, nil
50-
}
30+
// // generateIdentity Generates a new key and returns the peer ID and private key
31+
// // encoded as a base64 string using standard encoding, or an error if the key could not be generated.
32+
// func generateIdentity() (peer.ID, string, error) {
33+
// priv, peerid, err := newKey()
34+
// if err != nil {
35+
// return "", "", fmt.Errorf("cannot generate new key: %w", err)
36+
// }
37+
// privBytes, err := ci.MarshalPrivateKey(priv)
38+
// if err != nil {
39+
// return "", "", fmt.Errorf("cannot get bytes from private key: %w", err)
40+
// }
41+
// privStr := base64.StdEncoding.EncodeToString(privBytes)
42+
// return peerid, privStr, nil
43+
// }

controllers/ipfscluster_controller.go

+7-22
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import (
3232
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
3333
ctrllog "sigs.k8s.io/controller-runtime/pkg/log"
3434

35-
"github.com/libp2p/go-libp2p-core/peer"
3635
clusterv1alpha1 "github.com/redhat-et/ipfs-operator/api/v1alpha1"
3736
"github.com/redhat-et/ipfs-operator/controllers/utils"
3837
)
@@ -82,20 +81,6 @@ func (r *IpfsClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request)
8281
return ctrl.Result{}, r.Update(ctx, instance)
8382
}
8483

85-
// generate a new ID
86-
var peerid peer.ID
87-
var privStr string
88-
if peerid, privStr, err = generateIdentity(); err != nil {
89-
log.Error(err, "failed to generate identity")
90-
return ctrl.Result{}, err
91-
}
92-
93-
clusSec, err := newClusterSecret()
94-
if err != nil {
95-
log.Error(err, "cannot generate new cluster secret")
96-
return ctrl.Result{}, err
97-
}
98-
9984
if err = r.createCircuitRelays(ctx, instance); err != nil {
10085
log.Error(err, "cannot create circuit relays")
10186
return ctrl.Result{}, err
@@ -121,7 +106,7 @@ func (r *IpfsClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request)
121106
}
122107

123108
// Reconcile the tracked objects
124-
trackedObjects := r.createTrackedObjects(ctx, instance, peerid, clusSec, privStr)
109+
trackedObjects := r.createTrackedObjects(ctx, instance)
125110
shouldRequeue := utils.CreateOrPatchTrackedObjects(ctx, trackedObjects, r.Client, log)
126111
return ctrl.Result{Requeue: shouldRequeue}, nil
127112
}
@@ -130,9 +115,9 @@ func (r *IpfsClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request)
130115
func (r *IpfsClusterReconciler) createTrackedObjects(
131116
ctx context.Context,
132117
instance *clusterv1alpha1.IpfsCluster,
133-
peerID peer.ID,
134-
clusterSecret string,
135-
privateString string,
118+
// peerID peer.ID,
119+
// clusterSecret string,
120+
// privateString string,
136121
) map[client.Object]controllerutil.MutateFn {
137122
sa := corev1.ServiceAccount{}
138123
svc := corev1.Service{}
@@ -148,9 +133,9 @@ func (r *IpfsClusterReconciler) createTrackedObjects(
148133
ctx,
149134
instance,
150135
&secConfig,
151-
[]byte(clusterSecret),
152-
[]byte(privateString),
153-
peerID.String(),
136+
// []byte(clusterSecret),
137+
// []byte(privateString),
138+
// peerID.String(),
154139
)
155140
mutSts := r.StatefulSet(instance, &sts, svcName, secConfigName, cmScriptName)
156141

controllers/ipfscluster_controller_test.go

+4-7
Original file line numberDiff line numberDiff line change
@@ -71,29 +71,26 @@ var _ = Describe("IPFS Reconciler", func() {
7171
When("replicas are edited", func() {
7272
// we always expect there to be cluster secrets, which have two values
7373
const (
74-
clusterPeerID = "meow meow meow"
75-
alwaysKeys = 3
74+
alwaysKeys = 3
7675
)
7776
var (
78-
clusterSec = []byte("cluster secret")
79-
bootstrapKey = []byte("bootstrap private key")
80-
replicas int32
77+
replicas int32
8178
)
8279
BeforeEach(func() {
8380
replicas = rand.Int31n(100)
8481
ipfs.Spec.Replicas = replicas
8582
})
8683
It("creates a new peer ids", func() {
8784
secretConfig := &v1.Secret{}
88-
fn, _ := ipfsReconciler.SecretConfig(ctx, ipfs, secretConfig, clusterSec, bootstrapKey, clusterPeerID)
85+
fn, _ := ipfsReconciler.SecretConfig(ctx, ipfs, secretConfig)
8986
Expect(fn()).To(BeNil())
9087
secretStringToData(secretConfig)
9188
expectedKeys := int(replicas)*2 + alwaysKeys
9289
Expect(len(secretConfig.Data)).To(Equal(expectedKeys))
9390

9491
// increase the replica count. Expect to see new keys generated.
9592
ipfs.Spec.Replicas++
96-
fn, _ = ipfsReconciler.SecretConfig(ctx, ipfs, secretConfig, clusterSec, bootstrapKey, clusterPeerID)
93+
fn, _ = ipfsReconciler.SecretConfig(ctx, ipfs, secretConfig)
9794
Expect(fn()).To(BeNil())
9895
secretStringToData(secretConfig)
9996
Expect(len(secretConfig.Data)).To(Equal(expectedKeys + 2))

controllers/scripts.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010

1111
"github.com/alecthomas/units"
1212
"github.com/ipfs/kubo/config"
13-
"github.com/libp2p/go-libp2p-core/peer"
13+
"github.com/libp2p/go-libp2p/core/peer"
1414
ma "github.com/multiformats/go-multiaddr"
1515
clusterv1alpha1 "github.com/redhat-et/ipfs-operator/api/v1alpha1"
1616
"github.com/redhat-et/ipfs-operator/controllers/scripts"

controllers/scripts/config.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010

1111
"github.com/alecthomas/units"
1212
"github.com/ipfs/kubo/config"
13-
"github.com/libp2p/go-libp2p-core/peer"
13+
"github.com/libp2p/go-libp2p/core/peer"
1414
)
1515

1616
type configureIpfsOpts struct {

0 commit comments

Comments
 (0)