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

feat(ec): migration from v1 to v2 #5100

Merged
merged 4 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
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
48 changes: 46 additions & 2 deletions pkg/embeddedcluster/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,18 @@ import (
registrytypes "github.com/replicatedhq/kots/pkg/registry/types"
"github.com/replicatedhq/kots/pkg/util"
kotsv1beta1 "github.com/replicatedhq/kotskinds/apis/kots/v1beta1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"oras.land/oras-go/v2/registry/remote/auth"
"oras.land/oras-go/v2/registry/remote/credentials"
k8syaml "sigs.k8s.io/yaml"
)

const (
V2MigrationSecretName = "migratev2-secret"
)

// startClusterUpgrade will create a new installation with the provided config.
func startClusterUpgrade(
ctx context.Context, newcfg embeddedclusterv1beta1.ConfigSpec,
Expand Down Expand Up @@ -135,8 +140,6 @@ func runClusterUpgrade(
return fmt.Errorf("marshal installation: %w", err)
}

log.Println("Running upgrade command...")

args := []string{"upgrade"}
if in.Spec.AirGap {
// TODO(upgrade): local-artifact-mirror-image should be included in the installation object
Expand All @@ -148,6 +151,20 @@ func runClusterUpgrade(
}
args = append(args, "--installation", "-")

if os.Getenv("ENABLE_V2_MIGRATION") == "true" {
err := createV2MigrationSecret(ctx, k8sClient, license)
if err != nil {
return fmt.Errorf("create v2 migration secret: %w", err)
}

args = append(args, "--migrate-v2")
args = append(args, "--migrate-v2-secret", V2MigrationSecretName)
args = append(args, "--app-slug", license.Spec.AppSlug)
args = append(args, "--app-version-label", versionLabel)
}

log.Printf("Running upgrade command with args %q ...", args)

cmd := exec.CommandContext(ctx, bin, args...)
cmd.Stdin = strings.NewReader(string(installationData))
pr, pw := io.Pipe()
Expand Down Expand Up @@ -375,3 +392,30 @@ func embeddedRegistryImageName(registrySettings registrytypes.RegistrySettings,

return imageutil.DestECImage(destRegistry, srcImage)
}

func createV2MigrationSecret(ctx context.Context, k8sClient kubernetes.Interface, license kotsv1beta1.License) error {
encoded, err := k8syaml.Marshal(license)
if err != nil {
return fmt.Errorf("encode license: %w", err)
}

secret := &corev1.Secret{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "Secret",
},
ObjectMeta: metav1.ObjectMeta{
Name: V2MigrationSecretName,
Namespace: "embedded-cluster",
},
Data: map[string][]byte{
"license": encoded,
},
}
_, err = k8sClient.CoreV1().Secrets("embedded-cluster").Create(ctx, secret, metav1.CreateOptions{})
if err != nil {
return fmt.Errorf("create secret: %w", err)
}

return nil
}
2 changes: 1 addition & 1 deletion pkg/embeddedcluster/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func ListCMInstallations(ctx context.Context, kbClient kbclient.Client) ([]embed
),
}
var cmList corev1.ConfigMapList
if err := kbClient.List(ctx, &cmList, opts); err != nil {
if err := kbClient.List(ctx, &cmList, kbclient.InNamespace("embedded-cluster"), opts); err != nil {
return nil, fmt.Errorf("list configmaps: %w", err)
}

Expand Down
9 changes: 1 addition & 8 deletions pkg/reporting/distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,9 @@ func distributionFromServerGroupAndResources(clientset kubernetes.Interface) Dis

func distributionFromProviderId(clientset kubernetes.Interface) Distribution {
nodes, err := clientset.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{})
nodeCount := len(nodes.Items)
if nodeCount > 1 {
logger.Infof("Found %d nodes", nodeCount)
} else {
logger.Infof("Found %d node", nodeCount)
}
if err != nil {
logger.Infof("got error listing node: %v", err.Error())
}
if len(nodes.Items) >= 1 {
} else if len(nodes.Items) >= 1 {
node := nodes.Items[0]
if strings.HasPrefix(node.Spec.ProviderID, "kind:") {
return Kind
Expand Down
1 change: 1 addition & 0 deletions pkg/upgradeservice/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ func Deploy(opts DeployOptions) error {
go func() (finalError error) {
defer func() {
if finalError != nil {
logger.Error(errors.Wrap(finalError, "failed to deploy"))
if err := task.SetStatusUpgradeFailed(opts.Params.AppSlug, finalError.Error()); err != nil {
logger.Error(errors.Wrap(err, "failed to set task status to upgrade failed"))
}
Expand Down
Loading