Skip to content

Commit

Permalink
chore(pkg/controllers): remove unused secret and no-op comparisons (#168
Browse files Browse the repository at this point in the history
)

Fixes: #167
  • Loading branch information
uhthomas authored May 11, 2024
1 parent 1d1bef4 commit a11b890
Showing 1 changed file with 2 additions and 40 deletions.
42 changes: 2 additions & 40 deletions pkg/controllers/wireguard_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,8 +460,6 @@ func (r *WireguardReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
return ctrl.Result{}, err
}

bytes.Equal(b, secret.Data["state.json"])

if !bytes.Equal(b, secret.Data["state.json"]) {
log.Info("Updating secret with new config")
publicKey := string(secret.Data["publicKey"])
Expand Down Expand Up @@ -519,33 +517,14 @@ func (r *WireguardReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
return ctrl.Result{}, err
}

bytes.Equal(b, secret.Data["state"])

secret := r.secretForWireguard(wireguard, b, privateKey, publicKey)

log.Info("Creating a new secret", "secret.Namespace", secret.Namespace, "secret.Name", secret.Name)
err = r.Create(ctx, secret)
if err != nil {
log.Error(err, "Failed to create new secret", "secret.Namespace", secret.Namespace, "secret.Name", secret.Name)
return ctrl.Result{}, err
}

clientKey, err := wgtypes.GeneratePrivateKey()

if err != nil {
log.Error(err, "Failed to generate private key")
return ctrl.Result{}, err
}

clientSecret := r.secretForClient(wireguard, clientKey.String(), clientKey.PublicKey().String())

log.Info("Creating a new secret", "secret.Namespace", clientSecret.Namespace, "secret.Name", clientSecret.Name)
err = r.Create(ctx, clientSecret)
if err != nil {
log.Error(err, "Failed to create new secret", "secret.Namespace", clientSecret.Namespace, "secret.Name", clientSecret.Name)
if err := r.Create(ctx, secret); err != nil {
log.Error(err, "Failed to create new secret", "secret.Namespace", secret.Namespace, "secret.Name", secret.Name)
return ctrl.Result{}, err
}

return ctrl.Result{}, err
} else if err != nil {
log.Error(err, "Failed to get secret")
Expand Down Expand Up @@ -697,23 +676,6 @@ func (r *WireguardReconciler) secretForWireguard(m *v1alpha1.Wireguard, state []

}

func (r *WireguardReconciler) secretForClient(m *v1alpha1.Wireguard, privateKey string, publicKey string) *corev1.Secret {
ls := labelsForWireguard(m.Name)
dep := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: m.Name + "-client",
Namespace: m.Namespace,
Labels: ls,
},
Data: map[string][]byte{"privateKey": []byte(privateKey), "publicKey": []byte(publicKey)},
}

ctrl.SetControllerReference(m, dep, r.Scheme)

return dep

}

func (r *WireguardReconciler) deploymentForWireguard(m *v1alpha1.Wireguard) *appsv1.Deployment {
ls := labelsForWireguard(m.Name)
replicas := int32(1)
Expand Down

0 comments on commit a11b890

Please sign in to comment.