Skip to content

Commit

Permalink
fix(pkg/cluster): partially revert 9c8e3e1 to remove changes to Packa…
Browse files Browse the repository at this point in the history
…geSecretNeedsWait

Signed-off-by: Kit Patella <kit@defenseunicorns.com>
  • Loading branch information
mkcp committed Sep 10, 2024
1 parent f8216e5 commit c4b138d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
21 changes: 10 additions & 11 deletions src/pkg/cluster/zarf.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,29 +109,28 @@ func (c *Cluster) StripZarfLabelsAndSecretsFromNamespaces(ctx context.Context) {
spinner.Success()
}

// PackageSecretNeedsWait checks if a package component has a running webhook that needs to be waited on. Returns the
// number of seconds remaining to wait and the name of the webhook. If seconds is zero there's no need to wait.
func (c *Cluster) PackageSecretNeedsWait(deployedPackage *types.DeployedPackage, component v1alpha1.ZarfComponent, skipWebhooks bool) (int, string) {
// PackageSecretNeedsWait checks if a package component has a running webhook that needs to be waited on.
func (c *Cluster) PackageSecretNeedsWait(deployedPackage *types.DeployedPackage, component v1alpha1.ZarfComponent, skipWebhooks bool) (needsWait bool, waitSeconds int, hookName string) {
// Skip checking webhook status when '--skip-webhooks' flag is provided and for YOLO packages
if skipWebhooks || deployedPackage == nil || deployedPackage.Data.Metadata.YOLO {
return 0, ""
return false, 0, ""
}

// Look for the specified component
hookMap, componentExists := deployedPackage.ComponentWebhooks[component.Name]
if !componentExists {
return 0, "" // Component not found, no need to wait
return false, 0, "" // Component not found, no need to wait
}

// Check if there are any "Running" webhooks for the component that we need to wait for
for hookName, webhook := range hookMap {
if webhook.Status == types.WebhookStatusRunning {
return webhook.WaitDurationSeconds, hookName
return true, webhook.WaitDurationSeconds, hookName
}
}

// If we get here, the component doesn't need to wait for a webhook to run
return 0, ""
return false, 0, ""
}

// RecordPackageDeploymentAndWait records the deployment of a package to the cluster and waits for any webhooks to complete.
Expand All @@ -141,9 +140,9 @@ func (c *Cluster) RecordPackageDeploymentAndWait(ctx context.Context, pkg v1alph
return nil, err
}

waitSeconds, hookName := c.PackageSecretNeedsWait(deployedPackage, component, skipWebhooks)
packageNeedsWait, waitSeconds, hookName := c.PackageSecretNeedsWait(deployedPackage, component, skipWebhooks)
// If no webhooks need to complete, we can return immediately.
if waitSeconds == 0 {
if !packageNeedsWait {
return deployedPackage, nil
}

Expand All @@ -161,8 +160,8 @@ func (c *Cluster) RecordPackageDeploymentAndWait(ctx context.Context, pkg v1alph
if err != nil {
return nil, err
}
waitSeconds, _ = c.PackageSecretNeedsWait(deployedPackage, component, skipWebhooks)
if waitSeconds == 0 {
packageNeedsWait, _, _ = c.PackageSecretNeedsWait(deployedPackage, component, skipWebhooks)
if !packageNeedsWait {
return deployedPackage, nil
}
return deployedPackage, nil
Expand Down
12 changes: 11 additions & 1 deletion src/pkg/cluster/zarf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func TestPackageSecretNeedsWait(t *testing.T) {
deployedPackage *types.DeployedPackage
component v1alpha1.ZarfComponent
skipWebhooks bool
needsWait bool
waitSeconds int
hookName string
}
Expand All @@ -48,6 +49,7 @@ func TestPackageSecretNeedsWait(t *testing.T) {
Name: packageName,
ComponentWebhooks: map[string]map[string]types.Webhook{},
},
needsWait: false,
waitSeconds: 0,
hookName: "",
},
Expand All @@ -65,6 +67,7 @@ func TestPackageSecretNeedsWait(t *testing.T) {
},
},
},
needsWait: true,
waitSeconds: 10,
hookName: webhookName,
},
Expand All @@ -83,6 +86,7 @@ func TestPackageSecretNeedsWait(t *testing.T) {
},
},
},
needsWait: false,
waitSeconds: 0,
hookName: "",
},
Expand All @@ -99,6 +103,7 @@ func TestPackageSecretNeedsWait(t *testing.T) {
},
},
},
needsWait: false,
waitSeconds: 0,
hookName: "",
},
Expand All @@ -115,6 +120,7 @@ func TestPackageSecretNeedsWait(t *testing.T) {
},
},
},
needsWait: false,
waitSeconds: 0,
hookName: "",
},
Expand All @@ -131,6 +137,7 @@ func TestPackageSecretNeedsWait(t *testing.T) {
},
},
},
needsWait: false,
waitSeconds: 0,
hookName: "",
},
Expand All @@ -153,6 +160,7 @@ func TestPackageSecretNeedsWait(t *testing.T) {
},
},
},
needsWait: false,
waitSeconds: 0,
hookName: "",
},
Expand All @@ -171,6 +179,7 @@ func TestPackageSecretNeedsWait(t *testing.T) {
},
},
},
needsWait: false,
waitSeconds: 0,
hookName: "",
},
Expand All @@ -184,8 +193,9 @@ func TestPackageSecretNeedsWait(t *testing.T) {

c := &Cluster{}

waitSeconds, hookName := c.PackageSecretNeedsWait(testCase.deployedPackage, testCase.component, testCase.skipWebhooks)
needsWait, waitSeconds, hookName := c.PackageSecretNeedsWait(testCase.deployedPackage, testCase.component, testCase.skipWebhooks)

require.Equal(t, testCase.needsWait, needsWait)
require.Equal(t, testCase.waitSeconds, waitSeconds)
require.Equal(t, testCase.hookName, hookName)
})
Expand Down

0 comments on commit c4b138d

Please sign in to comment.