@@ -21,8 +21,11 @@ import (
2121 pkgerr "errors"
2222 "fmt"
2323 "net/http"
24+ "sort"
2425 "time"
2526
27+ profilev1alpha1 "github.com/kluster-manager/cluster-profile/apis/profile/v1alpha1"
28+ "github.com/kluster-manager/cluster-profile/pkg/common"
2629 "github.com/kluster-manager/cluster-profile/pkg/utils"
2730
2831 fluxhelm "github.com/fluxcd/helm-controller/api/v2"
@@ -203,7 +206,7 @@ func waitForReleaseToBeCreated(kc client.Client, name []string) error {
203206 })
204207}
205208
206- func updateManifestWork (ctx context.Context , fakeServer * FakeServer , kc client.Client , mw * workv1.ManifestWork ) error {
209+ func updateManifestWork (ctx context.Context , fakeServer * FakeServer , kc client.Client , mw * workv1.ManifestWork , profile * profilev1alpha1. ManagedClusterSetProfile ) error {
207210 logger := log .FromContext (ctx )
208211 // fake-apiserver shutdown
209212 if err := fakeServer .FakeSrv .Shutdown (ctx ); err != nil {
@@ -265,6 +268,11 @@ func updateManifestWork(ctx context.Context, fakeServer *FakeServer, kc client.C
265268 mw .Spec .ManifestConfigs = configOptions
266269 _ , err := cu .CreateOrPatch (ctx , kc , mw , func (obj client.Object , createOp bool ) client.Object {
267270 in := obj .(* workv1.ManifestWork )
271+ if in .Labels == nil {
272+ in .Labels = map [string ]string {}
273+ }
274+ in .Labels [kmapi .ClusterProfileLabel ] = profile .Name
275+ in .Labels [common .LabelAceFeatureSet ] = "true"
268276 in .Spec = mw .Spec
269277 return in
270278 })
@@ -356,9 +364,7 @@ func sanitizeFeatures(kc client.Client, clusterName string, features []string) (
356364 return nil , err
357365 }
358366
359- featureExclusionTracker := make (map [string ]bool ) // Tracks if an exclusion group already has an enabled feature
360367 exclusionGroupFeatures := make (map [string ][]string ) // Tracks features by their exclusion group
361-
362368 var featureList uiapi.FeatureList
363369 if err = kc .List (context .Background (), & featureList ); err != nil {
364370 return nil , err
@@ -368,28 +374,31 @@ func sanitizeFeatures(kc client.Client, clusterName string, features []string) (
368374 if exclusionGroup != "" {
369375 // Mark the exclusion group as having an enabled feature if this feature is enabled
370376 if strings .Contains (featuresMap .EnabledFeatures , f .Name ) {
371- featureExclusionTracker [exclusionGroup ] = true
372377 exclusionGroupFeatures [exclusionGroup ] = append (exclusionGroupFeatures [exclusionGroup ], f .Name )
373378 }
374379 }
375380 }
376381
382+ sort .Strings (features )
377383 var sanitizedFeatures []string
378384 for _ , f := range features {
379385 var feature uiapi.Feature
380386 if err := kc .Get (context .Background (), types.NamespacedName {Name : f }, & feature ); err != nil {
381387 return nil , err
382388 }
383389
390+ if strings .Contains (featuresMap .ExternallyManagedFeatures , f ) || strings .Contains (featuresMap .DisabledFeatures , f ) {
391+ continue
392+ }
393+
384394 exclusionGroup := feature .Spec .FeatureExclusionGroup
385395 // Skip if an exclusion group already has an enabled feature or features are already present
386- if exclusionGroup != "" {
387- if featureExclusionTracker [exclusionGroup ] || len (exclusionGroupFeatures [exclusionGroup ]) > 0 {
388- continue
389- }
396+ exist , err := existInManifestWork (kc , clusterName , feature .Spec .FeatureSet , feature .Name )
397+ if err != nil {
398+ return nil , err
390399 }
391400
392- if strings . Contains ( featuresMap . ExternallyManagedFeatures , f ) || strings . Contains ( featuresMap . DisabledFeatures , f ) {
401+ if exclusionGroup != "" && len ( exclusionGroupFeatures [ exclusionGroup ]) > 0 && ! exist {
393402 continue
394403 }
395404
@@ -417,3 +426,22 @@ func getFeatureStatus(cluster *v1.ManagedCluster) (*kmapi.ClusterClaimFeatures,
417426
418427 return nil , pkgerr .New ("features cluster claim not found" )
419428}
429+
430+ func existInManifestWork (kc client.Client , clusterName , fSetName , featureName string ) (bool , error ) {
431+ var mw workv1.ManifestWork
432+ err := kc .Get (context .Background (), client.ObjectKey {Name : fSetName , Namespace : clusterName }, & mw )
433+ if err != nil && ! errors .IsNotFound (err ) {
434+ return false , err
435+ }
436+
437+ for _ , m := range mw .Spec .Workload .Manifests {
438+ hr := fluxhelm.HelmRelease {}
439+ if err = utils .Copy (m , & hr ); err != nil {
440+ return false , err
441+ }
442+ if hr .Name == featureName {
443+ return true , nil
444+ }
445+ }
446+ return false , nil
447+ }
0 commit comments