@@ -20,6 +20,7 @@ import (
2020 "sigs.k8s.io/controller-runtime/pkg/client"
2121 "sigs.k8s.io/controller-runtime/pkg/client/apiutil"
2222 "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
23+ "sigs.k8s.io/controller-runtime/pkg/log"
2324 "sigs.k8s.io/yaml"
2425
2526 helmclient "github.com/operator-framework/helm-operator-plugins/pkg/client"
@@ -35,8 +36,9 @@ const (
3536)
3637
3738type ClusterExtensionRevisionGenerator interface {
38- GenerateRevision (bundleFS fs.FS , ext * ocv1.ClusterExtension , objectLabels , revisionAnnotations map [string ]string ) (* ocv1.ClusterExtensionRevision , error )
39+ GenerateRevision (ctx context. Context , bundleFS fs.FS , ext * ocv1.ClusterExtension , objectLabels , revisionAnnotations map [string ]string ) (* ocv1.ClusterExtensionRevision , error )
3940 GenerateRevisionFromHelmRelease (
41+ ctx context.Context ,
4042 helmRelease * release.Release , ext * ocv1.ClusterExtension ,
4143 objectLabels map [string ]string ,
4244 ) (* ocv1.ClusterExtensionRevision , error )
@@ -48,6 +50,7 @@ type SimpleRevisionGenerator struct {
4850}
4951
5052func (r * SimpleRevisionGenerator ) GenerateRevisionFromHelmRelease (
53+ ctx context.Context ,
5154 helmRelease * release.Release , ext * ocv1.ClusterExtension ,
5255 objectLabels map [string ]string ,
5356) (* ocv1.ClusterExtensionRevision , error ) {
@@ -64,11 +67,11 @@ func (r *SimpleRevisionGenerator) GenerateRevisionFromHelmRelease(
6467 maps .Copy (labels , existingLabels )
6568 maps .Copy (labels , objectLabels )
6669 obj .SetLabels (labels )
67- obj .SetOwnerReferences (nil ) // reset OwnerReferences for migration.
6870
6971 // Memory optimization: strip large annotations
7072 // Note: ApplyStripTransform never returns an error in practice
7173 _ = cache .ApplyStripAnnotationsTransform (& obj )
74+ sanitizedUnstructured (ctx , & obj )
7275
7376 objs = append (objs , ocv1.ClusterExtensionRevisionObject {
7477 Object : obj ,
@@ -88,6 +91,7 @@ func (r *SimpleRevisionGenerator) GenerateRevisionFromHelmRelease(
8891}
8992
9093func (r * SimpleRevisionGenerator ) GenerateRevision (
94+ ctx context.Context ,
9195 bundleFS fs.FS , ext * ocv1.ClusterExtension ,
9296 objectLabels , revisionAnnotations map [string ]string ,
9397) (* ocv1.ClusterExtensionRevision , error ) {
@@ -122,6 +126,7 @@ func (r *SimpleRevisionGenerator) GenerateRevision(
122126 if err := cache .ApplyStripAnnotationsTransform (& unstr ); err != nil {
123127 return nil , err
124128 }
129+ sanitizedUnstructured (ctx , & unstr )
125130
126131 objs = append (objs , ocv1.ClusterExtensionRevisionObject {
127132 Object : unstr ,
@@ -135,6 +140,48 @@ func (r *SimpleRevisionGenerator) GenerateRevision(
135140 return r .buildClusterExtensionRevision (objs , ext , revisionAnnotations ), nil
136141}
137142
143+ // sanitizedUnstructured takes an unstructured obj, removes status if present, and returns a sanitized copy containing only the allowed metadata entries set below.
144+ // If any unallowed entries are removed, a warning will be logged.
145+ func sanitizedUnstructured (ctx context.Context , unstr * unstructured.Unstructured ) {
146+ l := log .FromContext (ctx )
147+ obj := unstr .Object
148+
149+ // remove status
150+ if _ , ok := obj ["status" ]; ok {
151+ l .Info ("warning: extraneous status removed from manifest" )
152+ delete (obj , "status" )
153+ }
154+
155+ var allowedMetadata = []string {
156+ "annotations" ,
157+ "labels" ,
158+ "name" ,
159+ "namespace" ,
160+ }
161+
162+ var metadata map [string ]any
163+ if metaRaw , ok := obj ["metadata" ]; ok {
164+ metadata , ok = metaRaw .(map [string ]any )
165+ if ! ok {
166+ return
167+ }
168+ } else {
169+ return
170+ }
171+
172+ metadataSanitized := map [string ]any {}
173+ for _ , key := range allowedMetadata {
174+ if val , ok := metadata [key ]; ok {
175+ metadataSanitized [key ] = val
176+ }
177+ }
178+
179+ if len (metadataSanitized ) != len (metadata ) {
180+ l .Info ("warning: extraneous values removed from manifest metadata" , "allowed metadata" , allowedMetadata )
181+ }
182+ obj ["metadata" ] = metadataSanitized
183+ }
184+
138185func (r * SimpleRevisionGenerator ) buildClusterExtensionRevision (
139186 objects []ocv1.ClusterExtensionRevisionObject ,
140187 ext * ocv1.ClusterExtension ,
@@ -190,7 +237,7 @@ func (m *BoxcutterStorageMigrator) Migrate(ctx context.Context, ext *ocv1.Cluste
190237 return err
191238 }
192239
193- rev , err := m .RevisionGenerator .GenerateRevisionFromHelmRelease (helmRelease , ext , objectLabels )
240+ rev , err := m .RevisionGenerator .GenerateRevisionFromHelmRelease (ctx , helmRelease , ext , objectLabels )
194241 if err != nil {
195242 return err
196243 }
@@ -236,7 +283,7 @@ func (bc *Boxcutter) createOrUpdate(ctx context.Context, obj client.Object) erro
236283
237284func (bc * Boxcutter ) apply (ctx context.Context , contentFS fs.FS , ext * ocv1.ClusterExtension , objectLabels , revisionAnnotations map [string ]string ) (bool , string , error ) {
238285 // Generate desired revision
239- desiredRevision , err := bc .RevisionGenerator .GenerateRevision (contentFS , ext , objectLabels , revisionAnnotations )
286+ desiredRevision , err := bc .RevisionGenerator .GenerateRevision (ctx , contentFS , ext , objectLabels , revisionAnnotations )
240287 if err != nil {
241288 return false , "" , err
242289 }
0 commit comments