Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add secret annotations' backward translation #23

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions cert-manager-plugin/pkg/syncers/secrets/syncer.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package secrets

import (
ctxt "context"

certmanagerv1 "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1"
"github.com/loft-sh/vcluster-cert-manager-plugin/pkg/constants"
"github.com/loft-sh/vcluster-sdk/clienthelper"
"github.com/loft-sh/vcluster-sdk/syncer"
"github.com/loft-sh/vcluster-sdk/syncer/context"
"github.com/loft-sh/vcluster-sdk/syncer/translator"
Expand Down Expand Up @@ -29,6 +33,29 @@ type secretSyncer struct {
physicalClient client.Client
}

func (s *secretSyncer) getCertVirtualName(certPhysicalName string) (string, error) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure, but I think this function and function below could be transformed to single function getObjectVirtualName or something like that.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when Oleg is back from holiday he may have thoughts, but for me this seems fine as two simple little functions like this!


virtualCert := &certmanagerv1.Certificate{}
err := clienthelper.GetByIndex(ctxt.TODO(), s.virtualClient, virtualCert, translator.IndexByPhysicalName, certPhysicalName)

if err != nil {
return "", err
} else {
philimonoff marked this conversation as resolved.
Show resolved Hide resolved
return virtualCert.Name, nil
}
}

func (s *secretSyncer) getIssuerVirtualName(issuerPhysicalName string) (string, error) {
virtualIssuer := &certmanagerv1.Issuer{}
err := clienthelper.GetByIndex(ctxt.TODO(), s.virtualClient, virtualIssuer, translator.IndexByPhysicalName, issuerPhysicalName)

if err != nil {
return "", err
} else {
philimonoff marked this conversation as resolved.
Show resolved Hide resolved
return virtualIssuer.Name, nil
}
}

func (s *secretSyncer) SyncDown(ctx *context.SyncContext, vObj client.Object) (ctrl.Result, error) {
vSecret := vObj.(*corev1.Secret)

Expand Down Expand Up @@ -124,6 +151,29 @@ func (s *secretSyncer) SyncUp(ctx *context.SyncContext, pObj client.Object) (ctr
vSecret.Labels[k] = v
}
vSecret.Annotations[constants.BackwardSyncAnnotation] = "true"

if vSecret.Annotations["cert-manager.io/certificate-name"] != "" {
certPhysicalName := vSecret.Annotations["cert-manager.io/certificate-name"]
certVirtualName, err := s.getCertVirtualName(certPhysicalName)

if err != nil {
return ctrl.Result{}, err
} else {
philimonoff marked this conversation as resolved.
Show resolved Hide resolved
vSecret.Annotations["cert-manager.io/certificate-name"] = certVirtualName
}
}

if vSecret.Annotations["cert-manager.io/issuer-name"] != "" {
issuerPhysicalName := vSecret.Annotations["cert-manager.io/issuer-name"]
issuerVirtualName, err := s.getIssuerVirtualName(issuerPhysicalName)

if err != nil {
return ctrl.Result{}, err
} else {
philimonoff marked this conversation as resolved.
Show resolved Hide resolved
vSecret.Annotations["cert-manager.io/issuer-name"] = issuerVirtualName
}
}

vSecret.Labels[translate.ControllerLabel] = constants.PluginName
ctx.Log.Infof("create virtual secret %s/%s because physical secret exists", vSecret.Namespace, vSecret.Name)
return ctrl.Result{}, ctx.VirtualClient.Create(ctx.Context, vSecret)
Expand Down