Skip to content

Commit 540db7b

Browse files
Fix manifestwork label and feature installation issue (#24)
Signed-off-by: RokibulHasan7 <mdrokibulhasan@appscode.com>
1 parent e6aa506 commit 540db7b

File tree

4 files changed

+41
-14
lines changed

4 files changed

+41
-14
lines changed

pkg/common/constants.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package common
1919
const (
2020
ClusterClaimClusterInfo = "cluster.ace.info"
2121
LabelAceFeatureSet = "featureset.appscode.com/managed"
22-
ProfileLabel = "profile.appscode.com/name"
2322

2423
ACEUpgrader = "ace.cloud.com/upgrader"
2524
ACEUpgraderVersion = "ace.cloud.com/version"

pkg/controller/managedclustersetprofile_controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ import (
2020
"context"
2121

2222
profilev1alpha1 "github.com/kluster-manager/cluster-profile/apis/profile/v1alpha1"
23-
"github.com/kluster-manager/cluster-profile/pkg/common"
2423

2524
core "k8s.io/api/core/v1"
2625
"k8s.io/apimachinery/pkg/api/errors"
2726
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2827
"k8s.io/apimachinery/pkg/runtime"
2928
"k8s.io/apimachinery/pkg/types"
29+
kmapi "kmodules.xyz/client-go/api/v1"
3030
cu "kmodules.xyz/client-go/client"
3131
clusterv1 "open-cluster-management.io/api/cluster/v1"
3232
clusterv1beta2 "open-cluster-management.io/api/cluster/v1beta2"
@@ -100,7 +100,7 @@ func (r *ManagedClusterSetProfileReconciler) Reconcile(ctx context.Context, req
100100
Name: cluster.Name,
101101
Namespace: cluster.Name,
102102
Labels: map[string]string{
103-
common.ProfileLabel: profile.Name,
103+
kmapi.ClusterProfileLabel: profile.Name,
104104
},
105105
OwnerReferences: []metav1.OwnerReference{
106106
*metav1.NewControllerRef(profile, profilev1alpha1.SchemeGroupVersion.WithKind(profilev1alpha1.ResourceKindManagedClusterSetProfile)),

pkg/feature_installer/enable_featureset.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func enableFeatureSet(ctx context.Context, kc client.Client, featureSet string,
139139
Namespace: profileBinding.Namespace,
140140
Labels: map[string]string{
141141
common.LabelAceFeatureSet: "true",
142-
common.ProfileLabel: profile.Name,
142+
kmapi.ClusterProfileLabel: profile.Name,
143143
},
144144
OwnerReferences: []metav1.OwnerReference{
145145
*metav1.NewControllerRef(profileBinding, profilev1alpha1.SchemeGroupVersion.WithKind(profilev1alpha1.ResourceKindManagedClusterProfileBinding)),
@@ -282,7 +282,7 @@ func applyFeatureSet(ctx context.Context, kc client.Client, mw *workv1.ManifestW
282282
return err
283283
}
284284

285-
if err = updateManifestWork(ctx, fakeServer, kc, mw); err != nil {
285+
if err = updateManifestWork(ctx, fakeServer, kc, mw, profile); err != nil {
286286
return err
287287
}
288288
return nil

pkg/feature_installer/helpers.go

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)