Skip to content

Conversation

ahsayde
Copy link
Contributor

@ahsayde ahsayde commented Jan 25, 2023

See weaveworks/weave-gitops-enterprise#2331

Bootstrapping secret from management cluster to leaf clusters

To do

  • Track secret versions in CR status
  • Add unit tests
  • Update docs

see https://github.com/weaveworks/weave-gitops-private/pull/101/files#diff-b3fa087fff01a7268952b24df072a5163b7cfa0aa295216b2d736b6e2da31a11R297-R354

@ahsayde ahsayde force-pushed the secret-sync-experiment branch from e4282e7 to 785cb93 Compare February 5, 2023 14:42
@ahsayde ahsayde changed the title POC - bootstrapping secrets from management cluster to leaf clusters bootstrapping secrets from management cluster to leaf clusters Feb 7, 2023
@ahsayde ahsayde marked this pull request as ready for review February 7, 2023 11:33
@ahsayde ahsayde requested review from bigkevmcd and foot February 7, 2023 11:34
Copy link
Collaborator

@bigkevmcd bigkevmcd left a comment

Choose a reason for hiding this comment

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

Looks good, I think we should improve the CRD documentation at least.

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type SecretSyncSpec struct {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we document these?

This has a direct impact on the output from the CRD (and the documentation generated from the CRD).

See https://github.com/fluxcd/source-controller/blob/main/api/v1beta2/gitrepository_types.go for a great example.

}

// SetClusterSecretVersion set secret's ResourceVersion
func (s *SecretSyncStatus) SetClusterSecretVersion(cluster, secret, version string) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe we could document what these parameters are for?

What is "cluster" and "secret" in this case?

Given that they're repeated in the parameters below, maybe they are a DataClump ?

Copy link
Contributor Author

@ahsayde ahsayde Feb 7, 2023

Choose a reason for hiding this comment

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

i found that we don't need to use the secret name in the key since the CR reference only one secret
so the key will be just the cluster name

//+kubebuilder:rbac:groups=capi.weave.works,resources=secretsyncs,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=capi.weave.works,resources=secretsyncs/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=capi.weave.works,resources=secretsyncs/finalizers,verbs=update
//+kubebuilder:rbac:groups=batch,resources=jobs,verbs=get;list;watch;create;update;patch;delete
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do you need the permissions on jobs here?


if !cluster.DeletionTimestamp.IsZero() {
logger.Info("skipping cluster", "cluster", cluster.Name, "namespace", req.Namespace, "reason", "Deleted")
continue
Copy link
Collaborator

Choose a reason for hiding this comment

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

Great logging here!

One thing to be careful of, is that the context logger already has namespace as a key?

I think in this case it's fine, because the namespaces are the same, but something to be aware of, we could be losing data here.

secretSync.Status.SetClusterSecretVersion(cluster.Name, secret.Name, secret.ResourceVersion)
}

if err := r.Status().Patch(ctx, &secretSync, patch); err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since the original bootstrapper was written, Flux has added the Patch Helper

https://github.com/fluxcd/pkg/blob/main/runtime/patch/patch.go#L63

This simplifies things a bit (it will retry changes).

if err := cl.Create(ctx, &newSecret); err != nil {
if apierrors.IsAlreadyExists(err) {
if err := cl.Update(ctx, &newSecret); err != nil {
return fmt.Errorf("failed to update secret %w", err)
Copy link
Collaborator

Choose a reason for hiding this comment

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

@ahsayde ahsayde requested a review from bigkevmcd February 7, 2023 14:49
Copy link
Collaborator

@bigkevmcd bigkevmcd left a comment

Choose a reason for hiding this comment

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

Couple of minor tidyups that I'd like to see improved before merging, but looks good to me.

// TargetNamespace specifies the namespace which the secret should be bootstrapped in
// The default value is the namespace of the referenced secret
//+optional
TargetNamespace string `json:"targetNamespace"`
Copy link
Collaborator

Choose a reason for hiding this comment

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

,omitempty for this?

type SecretSyncSpec struct {
ClusterSelector metav1.LabelSelector `json:"clusterSelector"`
SecretRef v1.LocalObjectReference `json:"secretRef"`
// ClusterSelector specifies the label selector to match clusters with
Copy link
Collaborator

Choose a reason for hiding this comment

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

How about something like this?

	// Label selector for Clusters. The Clusters that are
	// selected by this will be the ones affected by this SecretSync.
	// It must match the Cluster labels. This field is immutable.
	// Label selector cannot be empty.
	ClusterSelector metav1.LabelSelector `json:"clusterSelector"`

return ctrl.Result{}, nil
}
if err := patchHelper.Patch(ctx, &secretSync); err != nil {
return ctrl.Result{}, err
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is there any more information we can wrap this error in?

@ahsayde ahsayde merged commit 6fed75b into main Feb 12, 2023
@ahsayde ahsayde deleted the secret-sync-experiment branch February 12, 2023 09:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants