Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
emosbaugh committed Jun 18, 2024
1 parent 9a6df48 commit 46b72f6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
4 changes: 2 additions & 2 deletions pkg/embeddedcluster/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var stateMut = sync.Mutex{}
// - The app has an embedded cluster configuration.
// - The app embedded cluster configuration differs from the current embedded cluster config.
// - The current cluster config (as part of the Installation object) already exists in the cluster.
func MaybeStartClusterUpgrade(ctx context.Context, store store.Store, kotsKinds *kotsutil.KotsKinds, appID string) error {
func MaybeStartClusterUpgrade(ctx context.Context, store store.Store, kotsKinds *kotsutil.KotsKinds, appID string, versionLabel string) error {
if kotsKinds == nil || kotsKinds.EmbeddedClusterConfig == nil {
return nil
}
Expand Down Expand Up @@ -75,7 +75,7 @@ func MaybeStartClusterUpgrade(ctx context.Context, store store.Store, kotsKinds

artifacts := getArtifactsFromInstallation(kotsKinds.Installation, kotsKinds.License.Spec.AppSlug)

if err := startClusterUpgrade(ctx, spec, artifacts, *kotsKinds.License); err != nil {
if err := startClusterUpgrade(ctx, spec, artifacts, *kotsKinds.License, versionLabel); err != nil {
return fmt.Errorf("failed to start cluster upgrade: %w", err)
}
logger.Info("started cluster upgrade")
Expand Down
36 changes: 26 additions & 10 deletions pkg/embeddedcluster/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
embeddedclusterv1beta1 "github.com/replicatedhq/embedded-cluster-kinds/apis/v1beta1"
"github.com/replicatedhq/kots/pkg/k8sutil"
"github.com/replicatedhq/kots/pkg/kotsutil"
"github.com/replicatedhq/kots/pkg/util"
kotsv1beta1 "github.com/replicatedhq/kotskinds/apis/kots/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"oras.land/oras-go/v2/registry/remote/auth"
Expand All @@ -29,7 +30,7 @@ const (
)

// startClusterUpgrade will create a new installation with the provided config.
func startClusterUpgrade(ctx context.Context, newcfg embeddedclusterv1beta1.ConfigSpec, artifacts *embeddedclusterv1beta1.ArtifactsLocation, license kotsv1beta1.License) error {
func startClusterUpgrade(ctx context.Context, newcfg embeddedclusterv1beta1.ConfigSpec, artifacts *embeddedclusterv1beta1.ArtifactsLocation, license kotsv1beta1.License, versionLabel string) error {
// TODO(upgrade): put a lock here to prevent multiple upgrades at the same time

kbClient, err := k8sutil.GetKubeClient(ctx)
Expand Down Expand Up @@ -66,7 +67,7 @@ func startClusterUpgrade(ctx context.Context, newcfg embeddedclusterv1beta1.Conf

log.Printf("Starting cluster upgrade to version %s...", newcfg.Version)

err = runClusterUpgrade(ctx, newins)
err = runClusterUpgrade(ctx, newins, license, versionLabel)
if err != nil {
return fmt.Errorf("run cluster upgrade: %w", err)
}
Expand All @@ -78,17 +79,15 @@ func startClusterUpgrade(ctx context.Context, newcfg embeddedclusterv1beta1.Conf

const (
// TODO(upgrade)
localArtifactMirrorImage = "ttl.sh/ethan/embedded-cluster-local-artifact-mirror:24h"
// curl "https://staging.replicated.app/embedded/embedded-cluster-smoke-test-staging-app/ci/appver-dev-72693f7" -H "Authorization: $EC_SMOKE_TEST_LICENSE_ID" -o embedded-cluster-smoke-test-staging-app-ci.tgz
upgradeBinaryOnlineURL = "http://file-server.embedded-cluster.svc.cluster.local:3000/manager"
localArtifactMirrorImage = "ttl.sh/ethan/embedded-cluster-local-artifact-mirror:24h"
upgradeBinaryAirgapOCIArtifact = "ttl.sh/ethan/embedded-cluster-bin:latest"
)

// runClusterUpgrade will download the new embedded cluster operator binary and run the upgrade
// command with the provided installation data. This is needed to get the latest
// embeddedclusterv1beta1 API version. The upgrade command will first upgrade the embedded cluster
// operator, wait for the CRD to be up-to-date, and then apply the installation object.
func runClusterUpgrade(ctx context.Context, in *embeddedclusterv1beta1.Installation) error {
func runClusterUpgrade(ctx context.Context, in *embeddedclusterv1beta1.Installation, license kotsv1beta1.License, versionLabel string) error {
var bin string
if in.Spec.AirGap {
log.Println("Downloading upgrade binary from registry...")
Expand All @@ -101,7 +100,7 @@ func runClusterUpgrade(ctx context.Context, in *embeddedclusterv1beta1.Installat
} else {
log.Println("Downloading upgrade binary from http...")

b, err := downloadUpgradeBinary(ctx, upgradeBinaryOnlineURL)
b, err := downloadUpgradeBinary(ctx, license, versionLabel)
if err != nil {
return fmt.Errorf("download upgrade binary: %w", err)
}
Expand Down Expand Up @@ -149,10 +148,15 @@ func runClusterUpgrade(ctx context.Context, in *embeddedclusterv1beta1.Installat
return nil
}

func downloadUpgradeBinary(ctx context.Context, url string) (string, error) {
resp, err := http.Get(url)
func downloadUpgradeBinary(ctx context.Context, license kotsv1beta1.License, versionLabel string) (string, error) {
req, err := newDownloadUpgradeBinaryRequest(ctx, license, versionLabel)
if err != nil {
return "", fmt.Errorf("http get: %w", err)
return "", fmt.Errorf("new download upgrade binary request: %w", err)
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", fmt.Errorf("do request: %w", err)
}
defer resp.Body.Close()

Expand All @@ -170,6 +174,18 @@ func downloadUpgradeBinary(ctx context.Context, url string) (string, error) {
return f.Name(), nil
}

func newDownloadUpgradeBinaryRequest(ctx context.Context, license kotsv1beta1.License, versionLabel string) (*http.Request, error) {
url := fmt.Sprintf("%s/clusterconfig/artifact/operator?versionLabel=%s", license.Spec.Endpoint, versionLabel)
req, err := util.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, fmt.Errorf("new request: %w", err)
}
req.SetBasicAuth(license.Spec.LicenseID, license.Spec.LicenseID)
req.WithContext(ctx)

return req, nil
}

func pullUpgradeBinaryFromRegistry(ctx context.Context, repo string) (string, error) {
tmpdir, err := os.MkdirTemp("", "embedded-cluster-artifact-*")
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ func (o *Operator) DeployApp(appID string, sequence int64) (deployed bool, deplo

if deployed {
go func() {
err := embeddedcluster.MaybeStartClusterUpgrade(context.TODO(), o.store, kotsKinds, app.ID)
err := embeddedcluster.MaybeStartClusterUpgrade(context.TODO(), o.store, kotsKinds, app.ID, "TODO_VERSION_LABEL")
if err != nil {
logger.Error(errors.Wrapf(err, "failed to start cluster upgrade for sequence %d", sequence))
}
Expand Down

0 comments on commit 46b72f6

Please sign in to comment.