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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ run-test-e2e: ## Run end-to-end tests (requires roxie to be available in PATH, k

@echo ""; \
echo "Invoking go test..."
$(GOTEST) -v -tags=e2e -timeout=120m -parallel=1 ./tests/e2e/...
$(GOTEST) -v -tags=e2e -timeout=150m -parallel=1 ./tests/e2e/...

.PHONY: test-e2e
test-e2e: build ## Run end-to-end tests (requires kubectl context and cluster access)
Expand Down
18 changes: 6 additions & 12 deletions cmd/teardown.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ package main

import (
"context"
"errors"
"fmt"
"time"

"github.com/spf13/cobra"
"github.com/stackrox/roxie/internal/component"
"github.com/stackrox/roxie/internal/deployer"
"github.com/stackrox/roxie/internal/env"
"github.com/stackrox/roxie/internal/k8s"
log "github.com/stackrox/roxie/internal/logger"
"github.com/stackrox/roxie/internal/manifest"
)
Expand Down Expand Up @@ -60,16 +58,12 @@ func runTeardown(cmd *cobra.Command, args []string) error {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Minute)
defer cancel()

var clusterConfig *deployer.Config
clusterManifest, err := manifest.LoadManifestSecret(ctx)
if errors.Is(err, k8s.ErrResourceNotFound) {
log.Infof("No roxie manifest found in cluster, proceeding without it")
} else if err != nil {
log.Warningf("Failed to load manifest from cluster: %v", err)
} else {
clusterConfig = &clusterManifest.Config
}

clusterConfig := new(deployer.DefaultConfig())
// A configured namespace restricts teardown to that namespace. Drop the built-in
// default namespaces, so that only explicit user configuration (user config, --set, ...)
// restricts teardown and roxie deployments are otherwise discovered cluster-wide.
clusterConfig.Central.Namespace = ""
clusterConfig.SecuredCluster.Namespace = ""
deploySettings, err := assembleConfigForCommand(clusterConfig, deploySettingsFromArgs, skipUserConfig)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ require (
go.opentelemetry.io/otel/trace v1.45.0 // indirect
go.yaml.in/yaml/v2 v2.4.4 // indirect
golang.org/x/net v0.60.0 // indirect
golang.org/x/sync v0.23.0 // indirect
golang.org/x/sync v0.23.0
golang.org/x/sys v0.49.0 // indirect
golang.org/x/text v0.42.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
Expand Down
60 changes: 47 additions & 13 deletions internal/deployer/addons.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"fmt"
"regexp"
"slices"
"strconv"

"github.com/stackrox/roxie/internal/helm"
log "github.com/stackrox/roxie/internal/logger"
)

Expand Down Expand Up @@ -52,24 +54,56 @@ func (d *Deployer) deployAddOns(ctx context.Context, addOns []AddOn) error {
return nil
}

func (d *Deployer) teardownAddOns(ctx context.Context, addOns []AddOn) {
if len(addOns) == 0 {
return
// teardownDiscoveredAddOns uninstalls add-on Helm releases in the given
// namespace. Add-ons are lifecycle-aligned with Central, so the caller
// passes the Central namespace.
func (d *Deployer) teardownDiscoveredAddOns(ctx context.Context, namespace string) error {
releases, err := helm.ListByPrefix(ctx, addOnReleasePrefix, namespace)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
if err != nil {
return fmt.Errorf("listing add-on Helm releases in namespace %s: %w", namespace, err)
}
if len(releases) == 0 {
return nil
}

log.Infof("Tearing down %d add-on(s)...", len(addOns))
sortReleasesForTeardown(releases)

for _, addon := range addOns {
if err := addon.Teardown(ctx); err != nil {
if !addon.IsOptional() {
log.Errorf("Failed to tear down non-optional add-on %q: %v", addon.Name(), err)
} else {
log.Warningf("Failed to tear down optional add-on %q: %v", addon.Name(), err)
}
} else {
log.Successf("Add-on %q torn down", addon.Name())
log.Infof("Tearing down %d add-on(s)...", len(releases))
var errs []error
for _, release := range releases {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
if err := ctx.Err(); err != nil {
errs = append(errs, err)
break
}
if err := helm.Uninstall(ctx, release.Name, namespace); err != nil {
errs = append(errs, fmt.Errorf("tearing down add-on release %q: %w", release.Name, err))
continue
}
log.Successf("Add-on release %q torn down", release.Name)
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
return errors.Join(errs...)
}

// sortReleasesForTeardown orders add-on releases in reverse installation order
// (see sortAddOns): ascending priority, ties broken by descending name.
func sortReleasesForTeardown(releases []helm.Release) {
slices.SortFunc(releases, func(a, b helm.Release) int {
return cmp.Or(
cmp.Compare(releasePriority(a), releasePriority(b)),
cmp.Compare(b.Name, a.Name), // ties: descending name
)
})
}

// releasePriority returns the priority stored on the release, falling back to the
// lowest priority if the label is missing or invalid (e.g. releases installed by
// older roxie versions).
func releasePriority(release helm.Release) uint64 {
prio, err := strconv.ParseUint(release.Labels[addOnPriorityLabel], 10, 0)
if err != nil {
return 0
}
return prio
}

// ResolveEnabledAddOns returns the enabled add-ons sorted by descending priority (name for ties at zero).
Expand Down
7 changes: 7 additions & 0 deletions internal/deployer/addons_helm_chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package deployer
import (
"context"
"fmt"
"strconv"

"github.com/stackrox/roxie/internal/helm"
log "github.com/stackrox/roxie/internal/logger"
Expand All @@ -11,6 +12,9 @@ import (
const (
addOnReleasePrefix = "roxie-addon-"
maxHelmReleaseNameLen = 53
// addOnPriorityLabel is stored on the add-on's Helm release, so that teardown can
// restore the reverse installation order without consulting local config.
addOnPriorityLabel = "roxie.stackrox.io/addon-priority"
)

type helmAddOn struct {
Expand Down Expand Up @@ -54,6 +58,9 @@ func newHelmAddOn(
}
opts.ReleaseName = releaseName
opts.Namespace = namespace
opts.Labels = map[string]string{
addOnPriorityLabel: strconv.FormatUint(uint64(commonProperties.Priority), 10),
}
return &helmAddOn{
name: name,
releaseName: releaseName,
Expand Down
36 changes: 36 additions & 0 deletions internal/deployer/addons_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"strings"
"testing"

"github.com/stackrox/roxie/internal/helm"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -86,6 +87,7 @@ func TestResolveAddOns(t *testing.T) {
assert.Equal(t, "example", ha.installOpts.ChartName)
assert.Equal(t, "1.0.0", ha.installOpts.ChartVersion)
assert.Equal(t, "acs-central", ha.installOpts.Namespace)
assert.Equal(t, map[string]string{addOnPriorityLabel: "0"}, ha.installOpts.Labels)
},
},
{
Expand Down Expand Up @@ -244,3 +246,37 @@ func TestResolveAddOns(t *testing.T) {
})
}
}

func TestSortReleasesForTeardown(t *testing.T) {
withPrio := func(name, prio string) helm.Release {
return helm.Release{Name: name, Labels: map[string]string{"owner": "helm", addOnPriorityLabel: prio}}
}
releases := []helm.Release{
{Name: "roxie-addon-legacy-b"},
withPrio("roxie-addon-hi-prio", "10"),
withPrio("roxie-addon-alpha", "0"),
withPrio("roxie-addon-garbage", "not-a-number"),
withPrio("roxie-addon-mid-prio", "5"),
withPrio("roxie-addon-hi-prio-2", "10"),
{Name: "roxie-addon-legacy-a"},
withPrio("roxie-addon-zebra", "0"),
}

sortReleasesForTeardown(releases)

names := make([]string, len(releases))
for i, r := range releases {
names[i] = r.Name
}
// Exact reverse of the installation order; unlabeled/invalid releases count as lowest priority.
assert.Equal(t, []string{
"roxie-addon-zebra",
"roxie-addon-legacy-b",
"roxie-addon-legacy-a",
"roxie-addon-garbage",
"roxie-addon-alpha",
"roxie-addon-mid-prio",
"roxie-addon-hi-prio-2",
"roxie-addon-hi-prio",
}, names)
}
Loading
Loading