@@ -2,18 +2,19 @@ package controllers
22
33import (
44 "context"
5- "strconv "
5+ "fmt "
66
77 corev1 "k8s.io/api/core/v1"
8- "k8s.io/apimachinery/pkg/api/resource"
98 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
109 ctrl "sigs.k8s.io/controller-runtime"
1110
11+ "github.com/alecthomas/units"
1212 "github.com/ipfs/kubo/config"
1313 "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"
17+ "github.com/redhat-et/ipfs-operator/controllers/utils"
1718 "sigs.k8s.io/controller-runtime/pkg/client"
1819 "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
1920 ctrllog "sigs.k8s.io/controller-runtime/pkg/log"
@@ -35,68 +36,57 @@ func (r *IpfsReconciler) ConfigMapScripts(
3536 m * clusterv1alpha1.Ipfs ,
3637 cm * corev1.ConfigMap ,
3738) (controllerutil.MutateFn , string ) {
39+ var err error
3840 log := ctrllog .FromContext (ctx )
39- relayPeers := []peer.AddrInfo {}
40- relayStatic := []string {}
41- for _ , relayName := range m .Status .CircuitRelays {
42- relay := clusterv1alpha1.CircuitRelay {}
43- relay .Name = relayName
44- relay .Namespace = m .Namespace
45- err := r .Get (ctx , client .ObjectKeyFromObject (& relay ), & relay )
46- if err != nil {
47- log .Error (err , "could not lookup circuitRelay during confgMapScripts" , "relay" , relayName )
48- return nil , ""
49- }
50- if err = relay .Status .AddrInfo .Parse (); err != nil {
51- log .Error (err , "could not parse AddrInfo. Information will not be included in config" , "relay" , relayName )
52- continue
53- }
54- ai := relay .Status .AddrInfo .AddrInfo ()
55- relayPeers = append (relayPeers , * ai )
56- p2ppart , err := ma .NewMultiaddr ("/p2p/" + ai .ID .String ())
57- if err != nil {
58- log .Error (err , "could not create p2p component during configMapScripts" , "relay" , relayName )
59- }
60- for _ , addr := range ai .Addrs {
61- fullMa := addr .Encapsulate (p2ppart ).String ()
62- relayStatic = append (relayStatic , fullMa )
63- }
64- }
6541
66- cmName := "ipfs-cluster-scripts-" + m .Name
67- var storageMaxGB string
68- parsed , err := resource .ParseQuantity (m .Spec .IpfsStorage )
42+ relayPeers , err := r .getCircuitInfo (ctx , m )
6943 if err != nil {
70- storageMaxGB = "100"
71- } else {
72- sizei64 , _ := parsed . AsInt64 ()
73- sizeGB := sizei64 / 1024 / 1024 / 1024
74- var reducedSize int64
75- // if the disk is big, use a bigger percentage of it.
76- if sizeGB > 1024 * 8 {
77- reducedSize = sizeGB * 9 / 10
78- } else {
79- reducedSize = sizeGB * 8 / 10
80- }
81- storageMaxGB = strconv . Itoa ( int ( reducedSize ) )
44+ log . Error ( err , "could not get relay circuit info" )
45+ return utils . ErrFunc ( fmt . Errorf ( "error when getting relay circuit info: %w" , err )), ""
46+ }
47+ relayStatic , err := staticAddrsFromRelayPeers ( relayPeers )
48+ if err != nil {
49+ log . Error ( err , "could not get static addresses from relayPeers" )
50+ return utils . ErrFunc ( fmt . Errorf ( "could not get static addresses: %w" , err )), ""
51+ }
52+ // convert multiaddrs to strings
53+ relayStaticStrs := make ([] string , len ( relayStatic ))
54+ for i , maddr := range relayStatic {
55+ relayStaticStrs [ i ] = maddr . String ( )
8256 }
8357
8458 relayConfig := config.RelayClient {
8559 Enabled : config .True ,
86- StaticRelays : relayStatic ,
60+ StaticRelays : relayStaticStrs ,
8761 }
8862
89- // get the config script
90- configScript , err := scripts .CreateConfigureScript (
91- storageMaxGB ,
92- relayPeers ,
93- relayConfig ,
94- )
63+ cmName := "ipfs-cluster-scripts-" + m .Name
64+
65+ // configure storage variables
66+ if err != nil {
67+ return utils .ErrFunc (err ), ""
68+ }
69+
70+ // compute storage sizes of IPFS volumes
71+ sizei64 , ok := m .Spec .IpfsStorage .AsInt64 ()
72+ if ! ok {
73+ sizei64 = m .Spec .IpfsStorage .ToDec ().Value ()
74+ }
75+ maxStorage := MaxIPFSStorage (sizei64 )
76+ maxStorageS := fmt .Sprintf ("%dB" , maxStorage )
77+ bloomFilterSize := scripts .CalculateBloomFilterSize (maxStorage )
9578 if err != nil {
9679 return func () error {
9780 return err
9881 }, ""
9982 }
83+ // get the config script
84+ configScript , err := scripts .CreateConfigureScript (
85+ maxStorageS ,
86+ relayPeers ,
87+ relayConfig ,
88+ bloomFilterSize ,
89+ )
10090
10191 expected := & corev1.ConfigMap {
10292 ObjectMeta : metav1.ObjectMeta {
@@ -118,3 +108,60 @@ func (r *IpfsReconciler) ConfigMapScripts(
118108 return nil
119109 }, cmName
120110}
111+
112+ // staticAddrsFromRelayPeers Extracts all of the static addresses from the
113+ // given list of relayPeers.
114+ func staticAddrsFromRelayPeers (relayPeers []peer.AddrInfo ) ([]ma.Multiaddr , error ) {
115+ relayStatic := make ([]ma.Multiaddr , 0 )
116+ for _ , addrInfo := range relayPeers {
117+ p2ppart , err := ma .NewMultiaddr ("/p2p/" + addrInfo .ID .String ())
118+ if err != nil {
119+ return nil , fmt .Errorf ("could not create p2p component: %w" , err )
120+ }
121+ for _ , addr := range addrInfo .Addrs {
122+ fullMa := addr .Encapsulate (p2ppart )
123+ relayStatic = append (relayStatic , fullMa )
124+ }
125+ }
126+ return relayStatic , nil
127+ }
128+
129+ // getCircuitInfo Gets address info from the list of CircuitRelays
130+ // and returns a list of AddrInfo.
131+ func (r * IpfsReconciler ) getCircuitInfo (
132+ ctx context.Context ,
133+ ipfs * clusterv1alpha1.Ipfs ,
134+ ) ([]peer.AddrInfo , error ) {
135+ log := ctrllog .FromContext (ctx )
136+ relayPeers := []peer.AddrInfo {}
137+ for _ , relayName := range ipfs .Status .CircuitRelays {
138+ relay := clusterv1alpha1.CircuitRelay {}
139+ relay .Name = relayName
140+ relay .Namespace = ipfs .Namespace
141+ // OPTIMIZE: do this asynchronously?
142+ if err := r .Get (ctx , client .ObjectKeyFromObject (& relay ), & relay ); err != nil {
143+ return nil , fmt .Errorf ("could not lookup circuitRelay: %w" , err )
144+ }
145+ if err := relay .Status .AddrInfo .Parse (); err != nil {
146+ log .Error (err , "could not parse AddrInfo. Information will not be included in config" , "relay" , relayName )
147+ continue
148+ }
149+ addrInfo := relay .Status .AddrInfo .AddrInfo ()
150+ relayPeers = append (relayPeers , * addrInfo )
151+ }
152+ return relayPeers , nil
153+ }
154+
155+ // MaxIPSStorage Accepts a storage quantity and returns with a
156+ // calculated value to be used for setting the Max IPFS storage value
157+ // in bytes.
158+ func MaxIPFSStorage (ipfsStorage int64 ) (storageMaxGB int64 ) {
159+ var reducedSize units.Base2Bytes
160+ // if the disk is big, use a bigger percentage of it.
161+ if units .Base2Bytes (ipfsStorage ) > units .Tebibyte * 8 {
162+ reducedSize = units .Base2Bytes (ipfsStorage ) * 9 / 10
163+ } else {
164+ reducedSize = units .Base2Bytes (ipfsStorage ) * 8 / 10
165+ }
166+ return int64 (reducedSize )
167+ }
0 commit comments