Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions controllers/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,7 @@ var (
GetClusterScopingAnnotation = getClusterScopingAnnotation
AddStaleResourceScopingAnnotations = addStaleResourceScopingAnnotations

AddExtraLabels = addExtraLabels
AddExtraAnnotations = addExtraAnnotations
AdjustNamespace = adjustNamespace
AdjustNamespace = adjustNamespace

ResourcesHash = resourcesHash
ResourcesConfiguration = resourcesConfiguration
Expand Down
138 changes: 0 additions & 138 deletions controllers/handlers_helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -1943,13 +1943,6 @@ func deployHelmChart(ctx context.Context, dCtx *deploymentContext,
}
}

if currentRelease != nil {
err = addExtraMetadata(ctx, instantiatedChart, dCtx.clusterSummary, kubeconfig, registryOptions, logger)
if err != nil {
return nil, nil, err
}
}

currentRelease, err = getReleaseInfo(ctx, instantiatedChart.ReleaseName, instantiatedChart.ReleaseNamespace,
kubeconfig, registryOptions, getEnableClientCacheValue(instantiatedChart.Options))
if err != nil && !errors.Is(err, driver.ErrReleaseNotFound) {
Expand Down Expand Up @@ -4986,103 +4979,6 @@ func getHelmUninstallClient(requestedChart *configv1beta1.HelmChart, actionConfi
return uninstallClient
}

func addExtraMetadata(ctx context.Context, requestedChart *configv1beta1.HelmChart,
clusterSummary *configv1beta1.ClusterSummary, kubeconfig string,
registryOptions *registryClientOptions, logger logr.Logger) error {

// No-op in DryRun mode
if clusterSummary.Spec.ClusterProfileSpec.SyncMode == configv1beta1.SyncModeDryRun {
return nil
}

if clusterSummary.Spec.ClusterProfileSpec.ExtraLabels == nil &&
clusterSummary.Spec.ClusterProfileSpec.ExtraAnnotations == nil {

return nil
}

actionConfig, err := actionConfigInit(ctx, requestedChart.ReleaseNamespace, kubeconfig, registryOptions,
getEnableClientCacheValue(requestedChart.Options))
if err != nil {
return err
}

statusObject := action.NewStatus(actionConfig)
rawResults, err := statusObject.Run(requestedChart.ReleaseName)
if err != nil {
return err
}
results, ok := rawResults.(*releasev1.Release)
if !ok {
return fmt.Errorf("unexpected release type %T", rawResults)
}

resources, err := collectHelmContent(results, install, true, false, logger)
if err != nil {
return err
}

config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
logger.Error(err, "BuildConfigFromFlags")
return err
}

logger.V(logs.LogDebug).Info(fmt.Sprintf("adding extra labels/annotations to %d resources", len(resources)))
for i := range resources {
r := resources[i]

// For some helm charts, collectHelmContent returns an empty namespace for namespaced resources
// If resource is a namespaced one and namespace is empty, set namespace to release namespace.
namespace, err := getResourceNamespace(ctx, r, requestedChart.ReleaseNamespace,
clusterSummary.Spec.ClusterNamespace, clusterSummary.Spec.ClusterName,
clusterSummary.Spec.ClusterType, logger)
if err != nil {
return err
}

var dr dynamic.ResourceInterface
dr, err = k8s_utils.GetDynamicResourceInterface(config, r.GroupVersionKind(), namespace)
if err != nil {
return err
}

addExtraLabels(r, clusterSummary.Spec.ClusterProfileSpec.ExtraLabels)
addExtraAnnotations(r, clusterSummary.Spec.ClusterProfileSpec.ExtraAnnotations)

isDriftDetection := clusterSummary.Spec.ClusterProfileSpec.SyncMode == configv1beta1.SyncModeContinuousWithDriftDetection
isDryRun := clusterSummary.Spec.ClusterProfileSpec.SyncMode == configv1beta1.SyncModeDryRun
_, err = deployer.UpdateResource(ctx, dr, isDriftDetection, isDryRun, getForceValue(requestedChart.Options),
clusterSummary.Spec.ClusterProfileSpec.DriftExclusions, r, []string{}, logger)
if err != nil {
logger.V(logs.LogInfo).Info(fmt.Sprintf("failed to update resource %s %s/%s: %v",
r.GetKind(), r.GetNamespace(), r.GetName(), err))
return err
}
}

return nil
}

func getResourceNamespace(ctx context.Context, r *unstructured.Unstructured, releaseNamespace string,
clusterNamespace, clusterName string, clusterType libsveltosv1beta1.ClusterType,
logger logr.Logger) (string, error) {

namespace := r.GetNamespace()
if namespace == "" {
namespacedResource, err := isNamespaced(ctx, r, clusterNamespace, clusterName, clusterType,
false, logger)
if err != nil {
return "", err
}
if namespacedResource {
namespace = releaseNamespace
}
}

return namespace, nil
}

// desiredValuesAreSubset returns true when every key-value pair in desired is present
// with an equal value in full. Nested maps are checked recursively. This lets Sveltos
// confirm that the values it would set are already reflected in the deployed release's
Expand Down Expand Up @@ -5916,40 +5812,6 @@ func canManageChart(ctx context.Context, c client.Client, clusterSummary *config
return determineChartOwnership(ctx, c, clusterSummary, instantiatedChart, logger)
}

// addExtraLabels adds ExtraLabels to policy.
// If policy already has a label with a key present in `ExtraLabels`, the value from `ExtraLabels` will
// override the existing value.
func addExtraLabels(policy *unstructured.Unstructured, extraLabels map[string]string) {
if extraLabels == nil {
return
}

if len(extraLabels) == 0 {
return
}

for k := range extraLabels {
deployer.AddLabel(policy, k, extraLabels[k])
}
}

// addExtraAnnotations adds ExtraAnnotations to policy.
// If policy already has an annotation with a key present in `ExtraAnnotations`, the value from `ExtraAnnotations`
// will override the existing value.
func addExtraAnnotations(policy *unstructured.Unstructured, extraAnnotations map[string]string) {
if extraAnnotations == nil {
return
}

if len(extraAnnotations) == 0 {
return
}

for k := range extraAnnotations {
deployer.AddAnnotation(policy, k, extraAnnotations[k])
}
}

// If in pull mode either remove resources or treat it as an upgrade
func prepareChartForAgent(ctx context.Context, dCtx *deploymentContext,
instantiatedChart *configv1beta1.HelmChart,
Expand Down
56 changes: 0 additions & 56 deletions controllers/handlers_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1716,62 +1716,6 @@ var _ = Describe("HandlersUtils", func() {
}, timeout, pollingInterval).Should(BeNil())
})

It("addExtraLabels adds extra labels on unstructured", func() {
u := &unstructured.Unstructured{}
extraLabels := map[string]string{
randomString(): randomString(),
randomString(): randomString(),
}

controllers.AddExtraLabels(u, extraLabels)
labels := u.GetLabels()
Expect(labels).ToNot(BeNil())
for k := range extraLabels {
Expect(labels[k]).To(Equal(extraLabels[k]))
}

// Add extra labels again
extraLabels = map[string]string{
randomString(): randomString(),
randomString(): randomString(),
}

controllers.AddExtraLabels(u, extraLabels)
labels = u.GetLabels()
Expect(labels).ToNot(BeNil())
for k := range extraLabels {
Expect(labels[k]).To(Equal(extraLabels[k]))
}
})

It("addExtraAnnotations adds extra annotations on unstructured", func() {
u := &unstructured.Unstructured{}
extraAnnotations := map[string]string{
randomString(): randomString(),
randomString(): randomString(),
}

controllers.AddExtraAnnotations(u, extraAnnotations)
annotations := u.GetAnnotations()
Expect(annotations).ToNot(BeNil())
for k := range extraAnnotations {
Expect(annotations[k]).To(Equal(extraAnnotations[k]))
}

// Add extra annotations again
extraAnnotations = map[string]string{
randomString(): randomString(),
randomString(): randomString(),
}

controllers.AddExtraAnnotations(u, extraAnnotations)
annotations = u.GetAnnotations()
Expect(annotations).ToNot(BeNil())
for k := range extraAnnotations {
Expect(annotations[k]).To(Equal(extraAnnotations[k]))
}
})

It("adjustNamespace adjusts namespace for both namespaced and cluster wide resources", func() {
logger := textlogger.NewLogger(textlogger.NewConfig())

Expand Down