Skip to content
Open
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
1 change: 0 additions & 1 deletion pkg/action/chart_lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,6 @@ func ChartLint(ctx context.Context, opts ChartLintOptions) error {

transformedResSpecs, err := spec.BuildTransformedResourceSpecs(ctx, opts.ReleaseNamespace, renderChartResult.ResourceSpecs, []spec.ResourceTransformer{
spec.NewResourceListsTransformer(),
spec.NewDropInvalidAnnotationsAndLabelsTransformer(),
})
if err != nil {
return fmt.Errorf("build transformed resource specs: %w", err)
Expand Down
1 change: 0 additions & 1 deletion pkg/action/chart_render.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,6 @@ func ChartRender(ctx context.Context, opts ChartRenderOptions) (*ChartRenderResu

transformedResSpecs, err := spec.BuildTransformedResourceSpecs(ctx, opts.ReleaseNamespace, renderChartResult.ResourceSpecs, []spec.ResourceTransformer{
spec.NewResourceListsTransformer(),
spec.NewDropInvalidAnnotationsAndLabelsTransformer(),
})
if err != nil {
return nil, fmt.Errorf("build transformed resource specs: %w", err)
Expand Down
2 changes: 0 additions & 2 deletions pkg/action/release_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,6 @@ func releaseInstall(ctx context.Context, ctxCancelFn context.CancelCauseFunc, re

transformedResSpecs, err := spec.BuildTransformedResourceSpecs(ctx, releaseNamespace, renderChartResult.ResourceSpecs, []spec.ResourceTransformer{
spec.NewResourceListsTransformer(),
spec.NewDropInvalidAnnotationsAndLabelsTransformer(),
})
if err != nil {
return fmt.Errorf("build transformed resource specs: %w", err)
Expand Down Expand Up @@ -788,7 +787,6 @@ func runRollbackPlan(ctx context.Context, releaseName, releaseNamespace string,

transformedResSpecs, err := spec.BuildTransformedResourceSpecs(ctx, releaseNamespace, resSpecs, []spec.ResourceTransformer{
spec.NewResourceListsTransformer(),
spec.NewDropInvalidAnnotationsAndLabelsTransformer(),
})
if err != nil {
return nil, nonCritErrs, critErrs.Add(fmt.Errorf("build transformed resource specs: %w", err))
Expand Down
1 change: 0 additions & 1 deletion pkg/action/release_plan_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,6 @@ func releasePlanInstall(ctx context.Context, ctxCancelFn context.CancelCauseFunc

transformedResSpecs, err := spec.BuildTransformedResourceSpecs(ctx, releaseNamespace, renderChartResult.ResourceSpecs, []spec.ResourceTransformer{
spec.NewResourceListsTransformer(),
spec.NewDropInvalidAnnotationsAndLabelsTransformer(),
})
if err != nil {
return nil, fmt.Errorf("build transformed resource specs: %w", err)
Expand Down
9 changes: 9 additions & 0 deletions pkg/action/release_uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ func releaseUninstall(ctx context.Context, ctxCancelFn context.CancelCauseFunc,
}

log.Default.Debug(ctx, "Build release history")

history, err := release.BuildHistory(releaseName, releaseStorage, release.HistoryOptions{})
if err != nil {
return fmt.Errorf("build release history: %w", err)
Expand All @@ -203,6 +204,7 @@ func releaseUninstall(ctx context.Context, ctxCancelFn context.CancelCauseFunc,
deployType := common.DeployTypeUninstall

log.Default.Debug(ctx, "Convert previous release to resource specs")

prevRelResSpecs, err := release.ReleaseToResourceSpecs(prevRelease, releaseNamespace, false)
if err != nil {
return fmt.Errorf("convert previous release to resource specs: %w", err)
Expand All @@ -217,6 +219,7 @@ func releaseUninstall(ctx context.Context, ctxCancelFn context.CancelCauseFunc,
}

log.Default.Debug(ctx, "Build resources")

instResources, delResources, err := resource.BuildResources(ctx, deployType, releaseNamespace, prevRelResSpecs, nil, patchers, clientFactory, resource.BuildResourcesOptions{
Remote: true,
DefaultDeletePropagation: metav1.DeletionPropagation(opts.DefaultDeletePropagation),
Expand All @@ -226,6 +229,7 @@ func releaseUninstall(ctx context.Context, ctxCancelFn context.CancelCauseFunc,
}

log.Default.Debug(ctx, "Build resource infos")

instResInfos, delResInfos, err := plan.BuildResourceInfos(ctx, deployType, releaseName, releaseNamespace, instResources, delResources, prevReleaseFailed, clientFactory, plan.BuildResourceInfosOptions{
NetworkParallelism: opts.NetworkParallelism,
NoRemoveManualChanges: opts.NoRemoveManualChanges,
Expand All @@ -236,12 +240,14 @@ func releaseUninstall(ctx context.Context, ctxCancelFn context.CancelCauseFunc,
}

log.Default.Debug(ctx, "Build release infos")

relInfos, err := plan.BuildReleaseInfos(ctx, deployType, releases, nil)
if err != nil {
return fmt.Errorf("build release infos: %w", err)
}

log.Default.Debug(ctx, "Build delete plan")

deletePlan, err := plan.BuildPlan(instResInfos, delResInfos, relInfos, plan.BuildPlanOptions{
NoFinalTracking: opts.NoFinalTracking,
})
Expand All @@ -261,6 +267,7 @@ func releaseUninstall(ctx context.Context, ctxCancelFn context.CancelCauseFunc,
logStore := kdutil.NewConcurrent(logstore.NewLogStore())

log.Default.Debug(ctx, "Start tracking")

var progressPrinter *track.ProgressTablesPrinter
if !opts.NoProgressTablePrint {
progressPrinter = track.NewProgressTablesPrinter(taskStore, logStore, track.ProgressTablesPrinterOptions{
Expand All @@ -278,6 +285,7 @@ func releaseUninstall(ctx context.Context, ctxCancelFn context.CancelCauseFunc,
}

log.Default.Debug(ctx, "Execute release delete plan")

executePlanErr := plan.ExecutePlan(ctx, releaseNamespace, deletePlan, taskStore, logStore, informerFactory, history, clientFactory, plan.ExecutePlanOptions{
LegacyProgressReporter: reporter,
TrackingOptions: opts.TrackingOptions,
Expand Down Expand Up @@ -312,6 +320,7 @@ func releaseUninstall(ctx context.Context, ctxCancelFn context.CancelCauseFunc,

criticalErrs.Add(critErrs)
nonCriticalErrs.Add(nonCritErrs)

if runFailurePlanResult != nil {
completedResourceOps = append(completedResourceOps, runFailurePlanResult.CompletedResourceOps...)
canceledResourceOps = append(canceledResourceOps, runFailurePlanResult.CanceledResourceOps...)
Expand Down
12 changes: 11 additions & 1 deletion pkg/resource/spec/resource_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,17 @@ func NewResourceSpecFromManifest(manifest, releaseNamespace string, opts Resourc
return nil, fmt.Errorf("decode resource (file: %q): %w", opts.FilePath, err)
}

return NewResourceSpec(obj.(*unstructured.Unstructured), releaseNamespace, opts), nil
unstruct := obj.(*unstructured.Unstructured)

if _, _, err := unstructured.NestedNullCoercingStringMap(unstruct.Object, "metadata", "annotations"); err != nil {
return nil, fmt.Errorf("decode resource (file: %q): %w", opts.FilePath, err)
}

if _, _, err := unstructured.NestedNullCoercingStringMap(unstruct.Object, "metadata", "labels"); err != nil {
return nil, fmt.Errorf("decode resource (file: %q): %w", opts.FilePath, err)
}

return NewResourceSpec(unstruct, releaseNamespace, opts), nil
}

func (s *ResourceSpec) SetAnnotations(annotations map[string]string) {
Expand Down
60 changes: 2 additions & 58 deletions pkg/resource/spec/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,11 @@ import (

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"

"github.com/werf/nelm/pkg/log"
)

const (
TypeDropInvalidAnnotationsAndLabelsTransformer ResourceTransformerType = "drop-invalid-annotations-and-labels-transformer"
TypeResourceListsTransformer ResourceTransformerType = "resource-lists-transformer"
)
const TypeResourceListsTransformer ResourceTransformerType = "resource-lists-transformer"

var (
_ ResourceTransformer = (*DropInvalidAnnotationsAndLabelsTransformer)(nil)
_ ResourceTransformer = (*ResourceListsTransformer)(nil)
)
var _ ResourceTransformer = (*ResourceListsTransformer)(nil)

type ResourceTransformerType string

Expand All @@ -32,54 +24,6 @@ type ResourceTransformerResourceInfo struct {
Obj *unstructured.Unstructured
}

// TODO(major): remove this transformer. Replace it with proper early validation of resource Heads.
type DropInvalidAnnotationsAndLabelsTransformer struct{}

func NewDropInvalidAnnotationsAndLabelsTransformer() *DropInvalidAnnotationsAndLabelsTransformer {
return &DropInvalidAnnotationsAndLabelsTransformer{}
}

func (t *DropInvalidAnnotationsAndLabelsTransformer) Match(ctx context.Context, info *ResourceTransformerResourceInfo) (matched bool, err error) {
return true, nil
}

func (t *DropInvalidAnnotationsAndLabelsTransformer) Transform(ctx context.Context, info *ResourceTransformerResourceInfo) ([]*unstructured.Unstructured, error) {
annotations, _, _ := unstructured.NestedMap(info.Obj.Object, "metadata", "annotations")

resultAnnotations := make(map[string]string)
for annoKey, rawAnnoValue := range annotations {
annoValue, valIsString := rawAnnoValue.(string)
if !valIsString {
log.Default.Warn(ctx, "Dropped invalid annotation %q in resource %q (%s): key is not a string", annoKey, info.Obj.GetName(), info.Obj.GroupVersionKind().String())
continue
}

resultAnnotations[annoKey] = annoValue
}

labels, _, _ := unstructured.NestedMap(info.Obj.Object, "metadata", "labels")

resultLabels := make(map[string]string)
for labelKey, rawLabelValue := range labels {
labelValue, valIsString := rawLabelValue.(string)
if !valIsString {
log.Default.Warn(ctx, "Dropped invalid label %q in resource %q (%s): key is not a string", labelKey, info.Obj.GetName(), info.Obj.GroupVersionKind().String())
continue
}

resultLabels[labelKey] = labelValue
}

info.Obj.SetAnnotations(resultAnnotations)
info.Obj.SetLabels(resultLabels)

return []*unstructured.Unstructured{info.Obj}, nil
}

func (t *DropInvalidAnnotationsAndLabelsTransformer) Type() ResourceTransformerType {
return TypeDropInvalidAnnotationsAndLabelsTransformer
}

type ResourceListsTransformer struct{}

func NewResourceListsTransformer() *ResourceListsTransformer {
Expand Down
Loading