Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: change UpdateZarfAgentValues to rolling restart #2644

Merged
merged 2 commits into from
Jun 21, 2024
Merged
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
71 changes: 34 additions & 37 deletions src/internal/packager/helm/zarf.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,42 +74,26 @@
spinner := message.NewProgressSpinner("Gathering information to update Zarf Agent TLS")
defer spinner.Stop()

err := h.createActionConfig(cluster.ZarfNamespaceName, spinner)
if err != nil {
return fmt.Errorf("unable to initialize the K8s client: %w", err)
}

// Get the current agent image from one of its pods.
selector, err := metav1.LabelSelectorAsSelector(&metav1.LabelSelector{MatchLabels: map[string]string{"app": "agent-hook"}})
deployment, err := h.cluster.Clientset.AppsV1().Deployments(cluster.ZarfNamespaceName).Get(ctx, "agent-hook", metav1.GetOptions{})

Check warning on line 77 in src/internal/packager/helm/zarf.go

View check run for this annotation

Codecov / codecov/patch

src/internal/packager/helm/zarf.go#L77

Added line #L77 was not covered by tests
if err != nil {
return err
}
listOpts := metav1.ListOptions{
LabelSelector: selector.String(),
}
podList, err := h.cluster.Clientset.CoreV1().Pods(cluster.ZarfNamespaceName).List(ctx, listOpts)
agentImage, err := transform.ParseImageRef(deployment.Spec.Template.Spec.Containers[0].Image)

Check warning on line 81 in src/internal/packager/helm/zarf.go

View check run for this annotation

Codecov / codecov/patch

src/internal/packager/helm/zarf.go#L81

Added line #L81 was not covered by tests
if err != nil {
return err
}

var currentAgentImage transform.Image
if len(podList.Items) > 0 && len(podList.Items[0].Spec.Containers) > 0 {
currentAgentImage, err = transform.ParseImageRef(podList.Items[0].Spec.Containers[0].Image)
if err != nil {
return fmt.Errorf("unable to parse current agent image reference: %w", err)
}
} else {
return fmt.Errorf("unable to get current agent pod")
err = h.createActionConfig(cluster.ZarfNamespaceName, spinner)
if err != nil {
return err

Check warning on line 88 in src/internal/packager/helm/zarf.go

View check run for this annotation

Codecov / codecov/patch

src/internal/packager/helm/zarf.go#L86-L88

Added lines #L86 - L88 were not covered by tests
}

// List the releases to find the current agent release name.
listClient := action.NewList(h.actionConfig)

releases, err := listClient.Run()
if err != nil {
return fmt.Errorf("unable to list helm releases: %w", err)
}

spinner.Success()

for _, release := range releases {
Expand All @@ -122,11 +106,11 @@
h.variableConfig.SetConstants([]variables.Constant{
{
Name: "AGENT_IMAGE",
Value: currentAgentImage.Path,
Value: agentImage.Path,

Check warning on line 109 in src/internal/packager/helm/zarf.go

View check run for this annotation

Codecov / codecov/patch

src/internal/packager/helm/zarf.go#L109

Added line #L109 was not covered by tests
},
{
Name: "AGENT_IMAGE_TAG",
Value: currentAgentImage.Tag,
Value: agentImage.Tag,

Check warning on line 113 in src/internal/packager/helm/zarf.go

View check run for this annotation

Codecov / codecov/patch

src/internal/packager/helm/zarf.go#L113

Added line #L113 was not covered by tests
},
})
applicationTemplates, err := template.GetZarfTemplates("zarf-agent", h.state)
Expand All @@ -142,30 +126,43 @@
}
}

spinner = message.NewProgressSpinner("Cleaning up Zarf Agent pods after update")
// Trigger a rolling update for the TLS secret update to take effect.
// https://kubernetes.io/docs/concepts/workloads/controllers/deployment/#updating-a-deployment
spinner = message.NewProgressSpinner("Performing a rolling update for the Zarf Agent deployment")

Check warning on line 131 in src/internal/packager/helm/zarf.go

View check run for this annotation

Codecov / codecov/patch

src/internal/packager/helm/zarf.go#L131

Added line #L131 was not covered by tests
defer spinner.Stop()

// Force pods to be recreated to get the updated secret.
// TODO: Explain why no grace period is given.
deleteGracePeriod := int64(0)
deletePolicy := metav1.DeletePropagationForeground
deleteOpts := metav1.DeleteOptions{
GracePeriodSeconds: &deleteGracePeriod,
PropagationPolicy: &deletePolicy,
// Re-fetch the agent deployment before we update since the resourceVersion has changed after updating the Helm release values.
// Avoids this error: https://github.com/kubernetes/kubernetes/issues/28149
deployment, err = h.cluster.Clientset.AppsV1().Deployments(cluster.ZarfNamespaceName).Get(ctx, "agent-hook", metav1.GetOptions{})
if err != nil {
return err

Check warning on line 138 in src/internal/packager/helm/zarf.go

View check run for this annotation

Codecov / codecov/patch

src/internal/packager/helm/zarf.go#L136-L138

Added lines #L136 - L138 were not covered by tests
}
if deployment.Spec.Template.Annotations == nil {
deployment.Spec.Template.Annotations = map[string]string{}

Check warning on line 141 in src/internal/packager/helm/zarf.go

View check run for this annotation

Codecov / codecov/patch

src/internal/packager/helm/zarf.go#L140-L141

Added lines #L140 - L141 were not covered by tests
}
selector, err = metav1.LabelSelectorAsSelector(&metav1.LabelSelector{MatchLabels: map[string]string{"app": "agent-hook"}})
deployment.Spec.Template.Annotations["zarf.dev/restartedAt"] = time.Now().UTC().Format(time.RFC3339)
_, err = h.cluster.Clientset.AppsV1().Deployments(cluster.ZarfNamespaceName).Update(ctx, deployment, metav1.UpdateOptions{})

Check warning on line 144 in src/internal/packager/helm/zarf.go

View check run for this annotation

Codecov / codecov/patch

src/internal/packager/helm/zarf.go#L143-L144

Added lines #L143 - L144 were not covered by tests
if err != nil {
return err
}
listOpts = metav1.ListOptions{
LabelSelector: selector.String(),

objs := []object.ObjMetadata{

Check warning on line 149 in src/internal/packager/helm/zarf.go

View check run for this annotation

Codecov / codecov/patch

src/internal/packager/helm/zarf.go#L149

Added line #L149 was not covered by tests
{
GroupKind: schema.GroupKind{
Group: "apps",
Kind: "Deployment",
},
Namespace: cluster.ZarfNamespaceName,
Name: "agent-hook",
},

Check warning on line 157 in src/internal/packager/helm/zarf.go

View check run for this annotation

Codecov / codecov/patch

src/internal/packager/helm/zarf.go#L151-L157

Added lines #L151 - L157 were not covered by tests
}
err = h.cluster.Clientset.CoreV1().Pods(cluster.ZarfNamespaceName).DeleteCollection(ctx, deleteOpts, listOpts)
waitCtx, waitCancel := context.WithTimeout(ctx, 60*time.Second)
defer waitCancel()
err = pkgkubernetes.WaitForReady(waitCtx, h.cluster.Watcher, objs)

Check warning on line 161 in src/internal/packager/helm/zarf.go

View check run for this annotation

Codecov / codecov/patch

src/internal/packager/helm/zarf.go#L159-L161

Added lines #L159 - L161 were not covered by tests
if err != nil {
return fmt.Errorf("error recycling pods for the Zarf Agent: %w", err)
return err

Check warning on line 163 in src/internal/packager/helm/zarf.go

View check run for this annotation

Codecov / codecov/patch

src/internal/packager/helm/zarf.go#L163

Added line #L163 was not covered by tests
}

spinner.Success()

return nil
}
Loading