Skip to content

Commit

Permalink
feat: adds feature flag to state (#909)
Browse files Browse the repository at this point in the history
  • Loading branch information
UncleGedd authored Sep 6, 2024
1 parent 2dabac7 commit 8185c37
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 53 deletions.
5 changes: 5 additions & 0 deletions src/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,8 @@ var (
// BundleAlwaysPull is a list of paths that will always be pulled from the remote repository.
BundleAlwaysPull = []string{BundleYAML, BundleYAMLSignature}
)

// feature flag to enable/disable features
const (
FF_STATE_ENABLED = false
)
70 changes: 41 additions & 29 deletions src/pkg/bundle/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,30 @@ func (b *Bundle) Deploy() error {
}

// get bundle state
enabledState := true
kc, err := cluster.NewCluster()
if err != nil {
// common scenario for Zarf actions run before cluster is available
enabledState = false
}
sc, err := state.NewClient(kc, enabledState)
if err != nil {
return err
}
var sc *state.Client
var kc *cluster.Cluster
if config.FF_STATE_ENABLED {
message.Debugf("state management disabled, skipping bundle state management")
var err error
var enabledState bool
kc, err = cluster.NewCluster()
if err != nil {
// common scenario for Zarf actions run before cluster is available
enabledState = false
}
sc, err = state.NewClient(kc, enabledState)
if err != nil {
return err
}

err = sc.InitBundleState(&b.bundle, state.Deploying)
if err != nil {
return err
err = sc.InitBundleState(&b.bundle, state.Deploying)
if err != nil {
return err
}
} else {
sc = &state.Client{
Enabled: false,
}
}

// if resume, filter for packages not yet deployed
Expand All @@ -94,7 +104,7 @@ func (b *Bundle) Deploy() error {
}

// update bundle state with success
err = sc.UpdateBundleState(&b.bundle, state.Success)
err := sc.UpdateBundleState(&b.bundle, state.Success)
if err != nil {
return err
}
Expand Down Expand Up @@ -214,29 +224,31 @@ func deployPackages(sc *state.Client, packagesToDeploy []types.Package, b *Bundl
bundleExportedVars[pkg.Name] = pkgExportedVars

// if state client is still disabled, check for cluster connection
if !sc.Enabled {
kc, err := cluster.NewCluster()
if err != nil {
message.Debugf("not connected to cluster, skipping bundle state management")
} else {
message.Debugf("connected to cluster, enabling bundle state management")
sc.Client = kc.Clientset
sc.Enabled = true
err = sc.InitBundleState(&b.bundle, state.Deploying)
if config.FF_STATE_ENABLED {
if !sc.Enabled {
kc, err := cluster.NewCluster()
if err != nil {
return err
}
// got a cluster now! update UDS state with the pkgs that were deployed before the cluster was up
for j := 0; j <= i; j++ {
err = sc.UpdateBundlePkgState(&b.bundle, packagesToDeploy[j], state.Success)
message.Debugf("not connected to cluster, skipping bundle state management")
} else {
message.Debugf("connected to cluster, enabling bundle state management")
sc.Client = kc.Clientset
sc.Enabled = true
err = sc.InitBundleState(&b.bundle, state.Deploying)
if err != nil {
return err
}
// got a cluster now! update UDS state with the pkgs that were deployed before the cluster was up
for j := 0; j <= i; j++ {
err = sc.UpdateBundlePkgState(&b.bundle, packagesToDeploy[j], state.Success)
if err != nil {
return err
}
}
}
}
}
}

}
return nil
}

Expand Down
56 changes: 34 additions & 22 deletions src/pkg/bundle/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,24 @@ func (b *Bundle) Remove() error {
}

// get bundle state
kc, err := cluster.NewCluster()
sc, err := state.NewClient(kc, true)
if err != nil {
return err
}
var sc *state.Client
var kc *cluster.Cluster
if config.FF_STATE_ENABLED {
var err error
kc, err = cluster.NewCluster()
sc, err = state.NewClient(kc, true)
if err != nil {
return err
}

err = sc.InitBundleState(&b.bundle, state.Removing)
if err != nil {
return err
err = sc.InitBundleState(&b.bundle, state.Removing)
if err != nil {
return err
}
} else {
sc = &state.Client{
Enabled: false,
}
}

// remove packages
Expand Down Expand Up @@ -112,11 +121,13 @@ func removePackages(sc *state.Client, packagesToRemove []types.Package, b *Bundl
for i := len(packagesToRemove) - 1; i >= 0; i-- {
pkg := packagesToRemove[i]

// check if disconnected from cluster
_, err = cluster.NewCluster()
if err != nil {
// cluster no longer available, disable state client (common scenario when running Zarf actions after cluster has been deleted)
sc.Enabled = false
if config.FF_STATE_ENABLED {
// check if disconnected from cluster
_, err = cluster.NewCluster()
if err != nil {
// cluster no longer available, disable state client (common scenario when running Zarf actions after cluster has been deleted)
sc.Enabled = false
}
}

if slices.Contains(deployedPackageNames, pkg.Name) {
Expand Down Expand Up @@ -160,19 +171,20 @@ func removePackages(sc *state.Client, packagesToRemove []types.Package, b *Bundl
if err != nil {
return err
}

} else {
// update bundle state if exists in bundle but not in cluster (ie. simple Zarf pkgs with no artifacts)
for _, pkgState := range bundleState.PkgStatuses {
if pkgState.Name == pkg.Name {
err = sc.UpdateBundlePkgState(&b.bundle, pkg, state.Removed)
if err != nil {
return err
if config.FF_STATE_ENABLED {
// update bundle state if exists in bundle but not in cluster (ie. simple Zarf pkgs with no artifacts)
for _, pkgState := range bundleState.PkgStatuses {
if pkgState.Name == pkg.Name {
err = sc.UpdateBundlePkgState(&b.bundle, pkg, state.Removed)
if err != nil {
return err
}
break
}
break
}
message.Debugf("Skipped removal of %s, package not found in Zarf or UDS state", pkg.Name)
}
message.Debugf("Skipped removal of %s, package not found in Zarf or UDS state", pkg.Name)
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/test/e2e/bundle_deploy_flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ func TestPackagesFlag(t *testing.T) {

func TestResumeFlag(t *testing.T) {
// delete nginx, podinfo, and uds (state) namespaces if they exist
runCmdWithErr("zarf tools kubectl delete ns nginx podinfo uds")
// todo: add 'uds' to this list of namespaces once the state feature is turned on
runCmdWithErr("zarf tools kubectl delete ns nginx podinfo") // intentionally not err checking this cmd
deployZarfInit(t)
e2e.CreateZarfPkg(t, "src/test/packages/podinfo", false)
bundleDir := "src/test/bundles/03-local-and-remote"
Expand Down
4 changes: 3 additions & 1 deletion tasks/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ tasks:
- name: dev-and-state
description: only run tests in dev_test.go and state_test.go (grouped bc neither require zarf init)
actions:
- cmd: cd src/test/e2e && go test -failfast -v -timeout 30m dev_test.go state_test.go commands_test.go main_test.go
# commenting out state tests for now as its behind a feature flag and not ready for CI
#- cmd: cd src/test/e2e && go test -failfast -v -timeout 30m dev_test.go state_test.go commands_test.go main_test.go
- cmd: cd src/test/e2e && go test -failfast -v -timeout 30m dev_test.go commands_test.go main_test.go

- name: variable
description: only run tests in variable_test.go
Expand Down

0 comments on commit 8185c37

Please sign in to comment.