2022-12-19 02:08:46 -05:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
|
|
|
package helm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
|
2023-05-22 07:31:20 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/constants"
|
2022-12-19 02:08:46 -05:00
|
|
|
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
|
|
|
"sigs.k8s.io/yaml"
|
|
|
|
)
|
|
|
|
|
2023-06-21 03:22:32 -04:00
|
|
|
func (c *Client) backupCRDs(ctx context.Context, upgradeID string) ([]apiextensionsv1.CustomResourceDefinition, error) {
|
2023-07-03 09:13:36 -04:00
|
|
|
c.log.Debugf("Starting CRD backup")
|
2022-12-19 02:08:46 -05:00
|
|
|
crds, err := c.kubectl.GetCRDs(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("getting CRDs: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-06-21 03:22:32 -04:00
|
|
|
crdBackupFolder := c.crdBackupFolder(upgradeID)
|
2022-12-19 02:08:46 -05:00
|
|
|
if err := c.fs.MkdirAll(crdBackupFolder); err != nil {
|
|
|
|
return nil, fmt.Errorf("creating backup dir: %w", err)
|
|
|
|
}
|
|
|
|
for i := range crds {
|
|
|
|
path := filepath.Join(crdBackupFolder, crds[i].Name+".yaml")
|
|
|
|
|
2023-07-03 09:13:36 -04:00
|
|
|
c.log.Debugf("Creating CRD backup: %s", path)
|
|
|
|
|
2022-12-19 02:08:46 -05:00
|
|
|
// 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
|
|
|
|
// The comment states that kind/version are encoded in the type.
|
|
|
|
// The package holding the CRD type encodes the version.
|
|
|
|
crds[i].Kind = "CustomResourceDefinition"
|
|
|
|
crds[i].APIVersion = "apiextensions.k8s.io/v1"
|
|
|
|
|
|
|
|
yamlBytes, err := yaml.Marshal(crds[i])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := c.fs.Write(path, yamlBytes); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2023-07-03 09:13:36 -04:00
|
|
|
c.log.Debugf("CRD backup complete")
|
2022-12-19 02:08:46 -05:00
|
|
|
return crds, nil
|
|
|
|
}
|
|
|
|
|
2023-06-21 03:22:32 -04:00
|
|
|
func (c *Client) backupCRs(ctx context.Context, crds []apiextensionsv1.CustomResourceDefinition, upgradeID string) error {
|
2023-07-03 09:13:36 -04:00
|
|
|
c.log.Debugf("Starting CR backup")
|
2022-12-19 02:08:46 -05:00
|
|
|
for _, crd := range crds {
|
2023-07-03 09:13:36 -04:00
|
|
|
c.log.Debugf("Creating backup for resource type: %s", crd.Name)
|
2022-12-19 02:08:46 -05:00
|
|
|
for _, version := range crd.Spec.Versions {
|
|
|
|
gvr := schema.GroupVersionResource{Group: crd.Spec.Group, Version: version.Name, Resource: crd.Spec.Names.Plural}
|
|
|
|
crs, err := c.kubectl.GetCRs(ctx, gvr)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("retrieving CR %s: %w", crd.Name, err)
|
|
|
|
}
|
|
|
|
|
2023-06-21 03:22:32 -04:00
|
|
|
backupFolder := c.backupFolder(upgradeID)
|
2022-12-19 02:08:46 -05:00
|
|
|
for _, cr := range crs {
|
2023-05-03 08:36:57 -04:00
|
|
|
targetFolder := filepath.Join(backupFolder, gvr.Group, gvr.Version, cr.GetNamespace(), cr.GetKind())
|
2023-04-28 04:33:10 -04:00
|
|
|
if err := c.fs.MkdirAll(targetFolder); err != nil {
|
|
|
|
return fmt.Errorf("creating resource dir: %w", err)
|
|
|
|
}
|
|
|
|
path := filepath.Join(targetFolder, cr.GetName()+".yaml")
|
2022-12-19 02:08:46 -05:00
|
|
|
yamlBytes, err := yaml.Marshal(cr.Object)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := c.fs.Write(path, yamlBytes); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-03 09:13:36 -04:00
|
|
|
c.log.Debugf("Backup for resource type %q complete", crd.Name)
|
2022-12-19 02:08:46 -05:00
|
|
|
}
|
2023-07-03 09:13:36 -04:00
|
|
|
c.log.Debugf("CR backup complete")
|
2022-12-19 02:08:46 -05:00
|
|
|
return nil
|
|
|
|
}
|
2023-06-21 03:22:32 -04:00
|
|
|
|
|
|
|
func (c *Client) backupFolder(upgradeID string) string {
|
|
|
|
return filepath.Join(constants.UpgradeDir, upgradeID, "backups") + string(filepath.Separator)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) crdBackupFolder(upgradeID string) string {
|
|
|
|
return filepath.Join(c.backupFolder(upgradeID), "crds") + string(filepath.Separator)
|
|
|
|
}
|