Skip to content

Commit

Permalink
kubecmd: retry any k8s errors in CLI and Terraform (#3028)
Browse files Browse the repository at this point in the history
* Retry any k8s errors in CLI and Terraform
* Use structured logging in `kubecmd` package

---------

Signed-off-by: Daniel Weiße <[email protected]>
  • Loading branch information
daniel-weisse authored Apr 16, 2024
1 parent f189aa1 commit 485ebb1
Show file tree
Hide file tree
Showing 3 changed files with 209 additions and 127 deletions.
12 changes: 6 additions & 6 deletions internal/constellation/kubecmd/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (k *KubeCmd) BackupCRDs(ctx context.Context, fileHandler file.Handler, upgr
for i := range crds {
path := filepath.Join(crdBackupFolder, crds[i].Name+".yaml")

k.log.Debug(fmt.Sprintf("Creating CRD backup: %q", path))
k.log.Debug("Creating CRD backup", "path", path)

// We have to manually set kind/apiversion because of a long-standing limitation of the API:
// https://github.com/kubernetes/kubernetes/issues/3030#issuecomment-67543738
Expand All @@ -64,23 +64,23 @@ func (k *KubeCmd) BackupCRDs(ctx context.Context, fileHandler file.Handler, upgr
func (k *KubeCmd) BackupCRs(ctx context.Context, fileHandler file.Handler, crds []apiextensionsv1.CustomResourceDefinition, upgradeDir string) error {
k.log.Debug("Starting CR backup")
for _, crd := range crds {
k.log.Debug(fmt.Sprintf("Creating backup for resource type: %q", crd.Name))
k.log.Debug("Creating backup", "crdName", crd.Name)

// Iterate over all versions of the CRD
// TODO(daniel-weisse): Consider iterating over crd.Status.StoredVersions instead
// Currently, we have to ignore not-found errors, because a CRD might define
// a version that is not installed in the cluster.
// With the StoredVersions field, we could only iterate over the installed versions.
for _, version := range crd.Spec.Versions {
k.log.Debug(fmt.Sprintf("Creating backup of CRs for %q at version %q", crd.Name, version.Name))
k.log.Debug("Starting CustomResource backup", "crdName", crd.Name, "version", version.Name)

gvr := schema.GroupVersionResource{Group: crd.Spec.Group, Version: version.Name, Resource: crd.Spec.Names.Plural}
crs, err := k.kubectl.ListCRs(ctx, gvr)
if err != nil {
if !k8serrors.IsNotFound(err) {
return fmt.Errorf("retrieving CR %s: %w", crd.Name, err)
}
k.log.Debug(fmt.Sprintf("No CRs found for %q at version %q, skipping...", crd.Name, version.Name))
k.log.Debug("No CustomResources found. Skipping...", "crdName", crd.Name, "version", version.Name)
continue
}

Expand All @@ -101,9 +101,9 @@ func (k *KubeCmd) BackupCRs(ctx context.Context, fileHandler file.Handler, crds
}
}

k.log.Debug(fmt.Sprintf("Backup for resource type %q complete", crd.Name))
k.log.Debug("CustomResource backup complete", "crdName", crd.Name)
}
k.log.Debug("CR backup complete")
k.log.Debug("All CustomResource backups completed")
return nil
}

Expand Down
Loading

0 comments on commit 485ebb1

Please sign in to comment.