|
1 | 1 | package controllers |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
| 5 | + "strconv" |
| 6 | + "strings" |
| 7 | + |
4 | 8 | corev1 "k8s.io/api/core/v1" |
| 9 | + "k8s.io/apimachinery/pkg/api/errors" |
5 | 10 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
6 | 11 | ctrl "sigs.k8s.io/controller-runtime" |
| 12 | + "sigs.k8s.io/controller-runtime/pkg/client" |
7 | 13 | "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" |
8 | 14 |
|
9 | 15 | clusterv1alpha1 "github.com/redhat-et/ipfs-operator/api/v1alpha1" |
10 | 16 | ) |
11 | 17 |
|
| 18 | +const ( |
| 19 | + peerIDPrefix = "peerID-" |
| 20 | + privateKeyPrefix = "privateKey-" |
| 21 | +) |
| 22 | + |
| 23 | +// errorFunc Returns a function which returns the provided |
| 24 | +// error when it is called. |
| 25 | +func errorFunc(err error) controllerutil.MutateFn { |
| 26 | + return func() error { |
| 27 | + return err |
| 28 | + } |
| 29 | +} |
| 30 | + |
12 | 31 | func (r *IpfsReconciler) secretConfig( |
| 32 | + ctx context.Context, |
13 | 33 | m *clusterv1alpha1.Ipfs, |
14 | 34 | sec *corev1.Secret, |
15 | 35 | clusterSecret, |
16 | 36 | bootstrapPrivateKey []byte, |
17 | 37 | ) (controllerutil.MutateFn, string) { |
18 | 38 | secName := "ipfs-cluster-" + m.Name |
19 | | - expected := &corev1.Secret{ |
| 39 | + |
| 40 | + expectedSecret := &corev1.Secret{ |
20 | 41 | ObjectMeta: metav1.ObjectMeta{ |
21 | 42 | Name: secName, |
22 | 43 | Namespace: m.Namespace, |
23 | 44 | }, |
24 | | - Data: map[string][]byte{ |
25 | | - "CLUSTER_SECRET": clusterSecret, |
26 | | - "BOOTSTRAP_PEER_PRIV_KEY": bootstrapPrivateKey, |
27 | | - }, |
28 | 45 | } |
29 | | - expected.DeepCopyInto(sec) |
| 46 | + // find secret |
| 47 | + err := r.Get(ctx, client.ObjectKeyFromObject(expectedSecret), expectedSecret) |
| 48 | + if err != nil && !errors.IsNotFound(err) { |
| 49 | + return errorFunc(err), "" |
| 50 | + } |
| 51 | + // initialize the secret, if needed |
| 52 | + if err != nil && errors.IsNotFound(err) { |
| 53 | + expectedSecret.Data = make(map[string][]byte, 0) |
| 54 | + expectedSecret.StringData = make(map[string]string, 0) |
| 55 | + // secret doesn't exist |
| 56 | + err = generateNewIdentities(expectedSecret, 0, m.Spec.Replicas) |
| 57 | + if err != nil { |
| 58 | + return errorFunc(err), "" |
| 59 | + } |
| 60 | + expectedSecret.Data["CLUSTER_SECRET"] = clusterSecret |
| 61 | + expectedSecret.Data["BOOTSTRAP_PEER_PRIV_KEY"] = bootstrapPrivateKey |
| 62 | + } |
| 63 | + |
| 64 | + // secret does exist |
| 65 | + numIdentities := countIdentities(expectedSecret) |
| 66 | + if numIdentities != m.Spec.Replicas { |
| 67 | + // create more identities if needed, otherwise they will be reused |
| 68 | + // when scaling down and then up again |
| 69 | + if numIdentities < m.Spec.Replicas { |
| 70 | + // create more |
| 71 | + err = generateNewIdentities(expectedSecret, numIdentities, m.Spec.Replicas) |
| 72 | + if err != nil { |
| 73 | + return errorFunc(err), "" |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + expectedSecret.DeepCopyInto(sec) |
30 | 79 | // FIXME: catch this error before we run the function being returned |
31 | | - if err := ctrl.SetControllerReference(m, sec, r.Scheme); err != nil { |
32 | | - return func() error { return err }, "" |
| 80 | + if err = ctrl.SetControllerReference(m, sec, r.Scheme); err != nil { |
| 81 | + return errorFunc(err), "" |
33 | 82 | } |
34 | 83 | return func() error { |
| 84 | + sec.Data = expectedSecret.Data |
35 | 85 | return nil |
36 | 86 | }, secName |
37 | 87 | } |
| 88 | + |
| 89 | +// countIdentities Counts the amount of unique peer identities present in the secret. |
| 90 | +func countIdentities(secret *corev1.Secret) int32 { |
| 91 | + var count int32 |
| 92 | + for key := range secret.Data { |
| 93 | + if strings.Contains(key, peerIDPrefix) { |
| 94 | + count++ |
| 95 | + } |
| 96 | + } |
| 97 | + return count |
| 98 | +} |
| 99 | + |
| 100 | +// generateNewIdentities Populates the secret data with new Peer IDs |
| 101 | +// and private keys which are mapped based on the replica number. |
| 102 | +func generateNewIdentities(secret *corev1.Secret, start, n int32) error { |
| 103 | + for i := start; i < n; i++ { |
| 104 | + // generate new private key & peer id |
| 105 | + peerID, privKey, err := generateIdentity() |
| 106 | + if err != nil { |
| 107 | + return err |
| 108 | + } |
| 109 | + peerIDKey := peerIDPrefix + strconv.Itoa(int(i)) |
| 110 | + secret.StringData[peerIDKey] = peerID.String() |
| 111 | + secretKey := privateKeyPrefix + strconv.Itoa(int(i)) |
| 112 | + secret.StringData[secretKey] = privKey |
| 113 | + } |
| 114 | + return nil |
| 115 | +} |
0 commit comments