@@ -106,25 +106,7 @@ func (a *APISecretPublisher) PublishConnection(ctx context.Context, o resource.C
106106 s := resource .ConnectionSecretFor (o , resource .MustGetKind (o , a .typer ))
107107 s .Data = c
108108
109- err := a .secret .Apply (ctx , s ,
110- resource .ConnectionSecretMustBeControllableBy (o .GetUID ()),
111- resource .AllowUpdateIf (func (current , desired runtime.Object ) bool {
112- // We consider the update to be a no-op and don't allow it if the
113- // current and existing secret data are identical.
114- //nolint:forcetypeassert // Will always be a secret.
115- return ! cmp .Equal (current .(* corev1.Secret ).Data , desired .(* corev1.Secret ).Data , cmpopts .EquateEmpty ())
116- }),
117- )
118- if resource .IsNotAllowed (err ) {
119- // The update was not allowed because it was a no-op.
120- return false , nil
121- }
122-
123- if err != nil {
124- return false , errors .Wrap (err , errCreateOrUpdateSecret )
125- }
126-
127- return true , nil
109+ return publishConnectionSecret (ctx , a .secret , s , o .GetUID (), false )
128110}
129111
130112// UnpublishConnection is no-op since PublishConnection only creates resources
@@ -164,35 +146,48 @@ func (a *APILocalSecretPublisher) PublishConnection(ctx context.Context, o resou
164146 s := resource .LocalConnectionSecretFor (o , resource .MustGetKind (o , a .typer ))
165147 s .Data = c
166148
167- err := a .secret .Apply (ctx , s ,
168- resource .ConnectionSecretMustBeControllableBy (o .GetUID ()),
149+ return publishConnectionSecret (ctx , a .secret , s , o .GetUID (), o .GetWriteConnectionSecretToReference ().Metadata != nil )
150+ }
151+
152+ // UnpublishConnection is no-op since PublishConnection only creates resources
153+ // that will be garbage collected by Kubernetes when the managed resource is
154+ // deleted.
155+ func (a * APILocalSecretPublisher ) UnpublishConnection (_ context.Context , _ resource.LocalConnectionSecretOwner , _ ConnectionDetails ) error {
156+ return nil
157+ }
158+
159+ func connectionSecretNeedsUpdate (current , desired * corev1.Secret , manageMetadata bool ) bool {
160+ if ! cmp .Equal (current .Data , desired .Data , cmpopts .EquateEmpty ()) {
161+ return true
162+ }
163+
164+ if ! manageMetadata {
165+ return false
166+ }
167+
168+ return ! cmp .Equal (current .Labels , desired .Labels , cmpopts .EquateEmpty ()) ||
169+ ! cmp .Equal (current .Annotations , desired .Annotations , cmpopts .EquateEmpty ())
170+ }
171+
172+ // publishConnectionSecret applies the desired Secret via the given applicator,
173+ // returning whether the secret was published (i.e. changed) and any error.
174+ func publishConnectionSecret (ctx context.Context , applicator resource.Applicator , s * corev1.Secret , uid types.UID , manageMetadata bool ) (bool , error ) {
175+ err := applicator .Apply (ctx , s ,
176+ resource .ConnectionSecretMustBeControllableBy (uid ),
169177 resource .AllowUpdateIf (func (current , desired runtime.Object ) bool {
170- // We consider the update to be a no-op and don't allow it if the
171- // current and existing secret data are identical.
172178 //nolint:forcetypeassert // Will always be a secret.
173- // NOTE(erhancagirici): cmp package is not recommended for production use
174- return ! cmp .Equal (current .(* corev1.Secret ).Data , desired .(* corev1.Secret ).Data , cmpopts .EquateEmpty ())
179+ return connectionSecretNeedsUpdate (current .(* corev1.Secret ), desired .(* corev1.Secret ), manageMetadata )
175180 }),
176181 )
177182 if resource .IsNotAllowed (err ) {
178- // The update was not allowed because it was a no-op.
179183 return false , nil
180184 }
181-
182185 if err != nil {
183186 return false , errors .Wrap (err , errCreateOrUpdateSecret )
184187 }
185-
186188 return true , nil
187189}
188190
189- // UnpublishConnection is no-op since PublishConnection only creates resources
190- // that will be garbage collected by Kubernetes when the managed resource is
191- // deleted.
192- func (a * APILocalSecretPublisher ) UnpublishConnection (_ context.Context , _ resource.LocalConnectionSecretOwner , _ ConnectionDetails ) error {
193- return nil
194- }
195-
196191// An APISimpleReferenceResolver resolves references from one managed resource
197192// to others by calling the referencing resource's ResolveReferences method, if
198193// any.
0 commit comments