Skip to content

Commit

Permalink
trigger embedded airgap upgrade (#4524)
Browse files Browse the repository at this point in the history
  • Loading branch information
Craig O'Donnell authored Apr 4, 2024
1 parent e5053be commit 8612e42
Show file tree
Hide file tree
Showing 22 changed files with 233 additions and 82 deletions.
2 changes: 1 addition & 1 deletion cmd/kots/cli/admin-console-push-images.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func AdminPushImagesCmd() *cobra.Command {
}

if _, err := os.Stat(imageSource); err == nil {
err = image.TagAndPushImagesFromBundle(imageSource, *options)
_, err = image.TagAndPushImagesFromBundle(imageSource, *options)
if err != nil {
return errors.Wrap(err, "failed to push images")
}
Expand Down
8 changes: 3 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
module github.com/replicatedhq/kots

go 1.21.0

toolchain go1.21.3
go 1.21.7

require (
cloud.google.com/go/storage v1.35.1
Expand Down Expand Up @@ -49,8 +47,8 @@ require (
github.com/phayes/freeport v0.0.0-20220201140144-74d24b5ae9f5
github.com/pkg/errors v0.9.1
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2
github.com/replicatedhq/embedded-cluster-operator v0.20.0
github.com/replicatedhq/kotskinds v0.0.0-20240209205029-4b4312dee0e4
github.com/replicatedhq/embedded-cluster-kinds v1.1.1
github.com/replicatedhq/kotskinds v0.0.0-20240326213823-6a0ed11e7397
github.com/replicatedhq/kurlkinds v1.3.6
github.com/replicatedhq/troubleshoot v0.83.0
github.com/replicatedhq/yaml/v3 v3.0.0-beta5-replicatedhq
Expand Down
14 changes: 8 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1311,10 +1311,12 @@ github.com/quasilyte/go-consistent v0.0.0-20190521200055-c6f3937de18c/go.mod h1:
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 h1:N/ElC8H3+5XpJzTSTfLsJV/mx9Q9g7kxmchpfZyxgzM=
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
github.com/remyoudompheng/bigfft v0.0.0-20170806203942-52369c62f446/go.mod h1:uYEyJGbgTkfkS4+E/PavXkNJcbFIpEtjt2B0KDQ5+9M=
github.com/replicatedhq/embedded-cluster-operator v0.20.0 h1:s9tlPeeOn6faj76bmW5PuMndj/uvUhrS/07iaGqIC0Q=
github.com/replicatedhq/embedded-cluster-operator v0.20.0/go.mod h1:qkCOGMKxiCzSkqM+3IWACbAB/32G0V67HYreJ5iaa30=
github.com/replicatedhq/kotskinds v0.0.0-20240209205029-4b4312dee0e4 h1:gVPYIjTxxtPs58pc+9euQNmI1jA/xExfam7bAYNUH5c=
github.com/replicatedhq/kotskinds v0.0.0-20240209205029-4b4312dee0e4/go.mod h1:QjhIUu3+OmHZ09u09j3FCoTt8F3BYtQglS+OLmftu9I=
github.com/replicatedhq/embedded-cluster-kinds v1.1.1 h1:300bVpCBHtZ9acJgEHL3/S4CapvxCRfcSwg/adMNWW8=
github.com/replicatedhq/embedded-cluster-kinds v1.1.1/go.mod h1:LheSDOgMngMRAbwAj0sVZUVv2ciKIVR2bYTMeOBGwlg=
github.com/replicatedhq/kotskinds v0.0.0-20240326192117-c171d8810b55 h1:jMMARRH885QgPR8LBFGjRxYGBQgzADsyjZyQLZ/TKlc=
github.com/replicatedhq/kotskinds v0.0.0-20240326192117-c171d8810b55/go.mod h1:QjhIUu3+OmHZ09u09j3FCoTt8F3BYtQglS+OLmftu9I=
github.com/replicatedhq/kotskinds v0.0.0-20240326213823-6a0ed11e7397 h1:JNuBcFH9D3Osyi+1QUwdvaAklEd6HXznqZDfpWlr73M=
github.com/replicatedhq/kotskinds v0.0.0-20240326213823-6a0ed11e7397/go.mod h1:QjhIUu3+OmHZ09u09j3FCoTt8F3BYtQglS+OLmftu9I=
github.com/replicatedhq/kurlkinds v1.3.6 h1:/dhS32cSSZR4yS4vA8EquBvz+VgJCyTqBO9Xw+6eI4M=
github.com/replicatedhq/kurlkinds v1.3.6/go.mod h1:c5+hoAkuARgftB2Ft3RCyWRZZPhL0clHEaw7XoGDAg4=
github.com/replicatedhq/termui/v3 v3.1.1-0.20200811145416-f40076d26851 h1:eRlNDHxGfVkPCRXbA4BfQJvt5DHjFiTtWy3R/t4djyY=
Expand Down Expand Up @@ -2302,8 +2304,8 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
gotest.tools/v3 v3.5.1 h1:EENdUnS3pdur5nybKYIh2Vfgc8IUNBjxDPSjtiJcOzU=
gotest.tools/v3 v3.5.1/go.mod h1:isy3WKz7GK6uNw/sbHzfKBLvlvXwUyV06n6brMxxopU=
gotest.tools/v3 v3.5.0 h1:Ljk6PdHdOhAb5aDMWXjDLMMhph+BpztA4v1QdqEW2eY=
gotest.tools/v3 v3.5.0/go.mod h1:isy3WKz7GK6uNw/sbHzfKBLvlvXwUyV06n6brMxxopU=
helm.sh/helm/v3 v3.14.3 h1:HmvRJlwyyt9HjgmAuxHbHv3PhMz9ir/XNWHyXfmnOP4=
helm.sh/helm/v3 v3.14.3/go.mod h1:v6myVbyseSBJTzhmeE39UcPLNv6cQK6qss3dvgAySaE=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
17 changes: 10 additions & 7 deletions pkg/embeddedcluster/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import (
"sync"
"time"

"github.com/replicatedhq/embedded-cluster-operator/api/v1beta1"
embeddedclusterv1beta1 "github.com/replicatedhq/embedded-cluster-kinds/apis/v1beta1"
appstatetypes "github.com/replicatedhq/kots/pkg/appstate/types"
"github.com/replicatedhq/kots/pkg/kotsutil"
"github.com/replicatedhq/kots/pkg/logger"
"github.com/replicatedhq/kots/pkg/store"
"github.com/replicatedhq/kots/pkg/util"
Expand All @@ -22,16 +23,16 @@ 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, conf *v1beta1.Config, appID string) error {
if conf == nil {
func MaybeStartClusterUpgrade(ctx context.Context, store store.Store, kotsKinds *kotsutil.KotsKinds, appID string) error {
if kotsKinds == nil || kotsKinds.EmbeddedClusterConfig == nil {
return nil
}

if !util.IsEmbeddedCluster() {
return nil
}

spec := conf.Spec
spec := kotsKinds.EmbeddedClusterConfig.Spec
if upgrade, err := RequiresUpgrade(ctx, spec); err != nil {
// if there is no installation object we can't start an upgrade. this is a valid
// scenario specially during cluster bootstrap. as we do not need to upgrade the
Expand Down Expand Up @@ -66,7 +67,9 @@ func MaybeStartClusterUpgrade(ctx context.Context, store store.Store, conf *v1be
continue
}

if err := startClusterUpgrade(ctx, spec); err != nil {
artifacts := getArtifactsFromInstallation(kotsKinds.Installation, kotsKinds.License.Spec.AppSlug)

if err := startClusterUpgrade(ctx, spec, artifacts); err != nil {
return fmt.Errorf("failed to start cluster upgrade: %w", err)
}
logger.Info("started cluster upgrade")
Expand Down Expand Up @@ -107,7 +110,7 @@ func watchClusterState(ctx context.Context, store store.Store) {
logger.Errorf("embeddedcluster monitor: fail updating state: %v", err)
}

if state == v1beta1.InstallationStateInstalled {
if state == embeddedclusterv1beta1.InstallationStateInstalled {
numReady++
} else {
numReady = 0
Expand All @@ -124,7 +127,7 @@ func updateClusterState(ctx context.Context, store store.Store, lastState string
if err != nil {
return "", fmt.Errorf("failed to get current installation: %w", err)
}
state := v1beta1.InstallationStateUnknown
state := embeddedclusterv1beta1.InstallationStateUnknown
if installation.Status.State != "" {
state = installation.Status.State
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/embeddedcluster/roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"sort"
"strings"

embeddedclusterv1beta1 "github.com/replicatedhq/embedded-cluster-operator/api/v1beta1"
embeddedclusterv1beta1 "github.com/replicatedhq/embedded-cluster-kinds/apis/v1beta1"
)

const DEFAULT_CONTROLLER_ROLE_NAME = "controller"
Expand Down
2 changes: 1 addition & 1 deletion pkg/embeddedcluster/roles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package embeddedcluster
import (
"testing"

embeddedclusterv1beta1 "github.com/replicatedhq/embedded-cluster-operator/api/v1beta1"
embeddedclusterv1beta1 "github.com/replicatedhq/embedded-cluster-kinds/apis/v1beta1"
"github.com/stretchr/testify/require"
)

Expand Down
39 changes: 37 additions & 2 deletions pkg/embeddedcluster/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import (
"context"
"encoding/json"
"fmt"
"regexp"
"sort"
"time"

embeddedclusterv1beta1 "github.com/replicatedhq/embedded-cluster-operator/api/v1beta1"
embeddedclusterv1beta1 "github.com/replicatedhq/embedded-cluster-kinds/apis/v1beta1"
"github.com/replicatedhq/kots/pkg/k8sutil"
"github.com/replicatedhq/kots/pkg/logger"
kotsv1beta1 "github.com/replicatedhq/kotskinds/apis/kots/v1beta1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -23,6 +26,13 @@ const configMapNamespace = "embedded-cluster"
// ErrNoInstallations is returned when no installation object is found in the cluster.
var ErrNoInstallations = fmt.Errorf("no installations found")

var (
chartsArtifactRegex = regexp.MustCompile(`\/embedded-cluster\/(charts\.tar\.gz):`)
imagesArtifactRegex = regexp.MustCompile(`\/embedded-cluster\/(images-.+\.tar):`)
binaryArtifactRegex = regexp.MustCompile(`\/embedded-cluster\/(embedded-cluster-.+):`)
metadataArtifactRegex = regexp.MustCompile(`\/embedded-cluster\/(version-metadata\.json):`)
)

// ReadConfigMap will read the Kurl config from a configmap
func ReadConfigMap(client kubernetes.Interface) (*corev1.ConfigMap, error) {
return client.CoreV1().ConfigMaps(configMapNamespace).Get(context.TODO(), configMapName, metav1.GetOptions{})
Expand Down Expand Up @@ -102,8 +112,32 @@ func ClusterConfig(ctx context.Context) (*embeddedclusterv1beta1.ConfigSpec, err
return latest.Spec.Config, nil
}

func getArtifactsFromInstallation(installation kotsv1beta1.Installation, appSlug string) *embeddedclusterv1beta1.ArtifactsLocation {
if len(installation.Spec.EmbeddedClusterArtifacts) == 0 {
return nil
}

artifacts := &embeddedclusterv1beta1.ArtifactsLocation{}
for _, artifact := range installation.Spec.EmbeddedClusterArtifacts {
switch {
case chartsArtifactRegex.MatchString(artifact):
artifacts.HelmCharts = artifact
case imagesArtifactRegex.MatchString(artifact):
artifacts.Images = artifact
case binaryArtifactRegex.MatchString(artifact):
artifacts.EmbeddedClusterBinary = artifact
case metadataArtifactRegex.MatchString(artifact):
artifacts.EmbeddedClusterMetadata = artifact
default:
logger.Warnf("unknown artifact in installation: %s", artifact)
}
}

return artifacts
}

// startClusterUpgrade will create a new installation with the provided config.
func startClusterUpgrade(ctx context.Context, newcfg embeddedclusterv1beta1.ConfigSpec) error {
func startClusterUpgrade(ctx context.Context, newcfg embeddedclusterv1beta1.ConfigSpec, artifacts *embeddedclusterv1beta1.ArtifactsLocation) error {
clientConfig, err := k8sutil.GetClusterConfig()
if err != nil {
return fmt.Errorf("failed to get cluster config: %w", err)
Expand All @@ -126,6 +160,7 @@ func startClusterUpgrade(ctx context.Context, newcfg embeddedclusterv1beta1.Conf
ClusterID: current.Spec.ClusterID,
MetricsBaseURL: current.Spec.MetricsBaseURL,
AirGap: current.Spec.AirGap,
Artifacts: artifacts,
Config: &newcfg,
EndUserK0sConfigOverrides: current.Spec.EndUserK0sConfigOverrides,
},
Expand Down
60 changes: 60 additions & 0 deletions pkg/embeddedcluster/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package embeddedcluster

import (
"reflect"
"testing"

embeddedclusterv1beta1 "github.com/replicatedhq/embedded-cluster-kinds/apis/v1beta1"
kotsv1beta1 "github.com/replicatedhq/kotskinds/apis/kots/v1beta1"
)

func Test_getArtifactsFromInstallation(t *testing.T) {
type args struct {
installation kotsv1beta1.Installation
appSlug string
}
tests := []struct {
name string
args args
want *embeddedclusterv1beta1.ArtifactsLocation
}{
{
name: "no artifacts",
args: args{
installation: kotsv1beta1.Installation{},
appSlug: "my-app",
},
want: nil,
},
{
name: "has all artifacts",
args: args{
installation: kotsv1beta1.Installation{
Spec: kotsv1beta1.InstallationSpec{
EmbeddedClusterArtifacts: []string{
"onprem.registry.com/my-app/embedded-cluster/charts.tar.gz:v1",
"onprem.registry.com/my-app/embedded-cluster/images-amd64.tar:v1",
"onprem.registry.com/my-app/embedded-cluster/embedded-cluster-amd64:v1",
"onprem.registry.com/my-app/embedded-cluster/version-metadata.json:v1",
},
},
},
appSlug: "my-app",
},
want: &embeddedclusterv1beta1.ArtifactsLocation{
Images: "onprem.registry.com/my-app/embedded-cluster/images-amd64.tar:v1",
HelmCharts: "onprem.registry.com/my-app/embedded-cluster/charts.tar.gz:v1",
EmbeddedClusterBinary: "onprem.registry.com/my-app/embedded-cluster/embedded-cluster-amd64:v1",
EmbeddedClusterMetadata: "onprem.registry.com/my-app/embedded-cluster/version-metadata.json:v1",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := getArtifactsFromInstallation(tt.args.installation, tt.args.appSlug)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("getArtifactsFromInstallation() = %v, want %v", got, tt.want)
}
})
}
}
2 changes: 1 addition & 1 deletion pkg/handlers/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

"github.com/gorilla/mux"
"github.com/pkg/errors"
embeddedclusterv1beta1 "github.com/replicatedhq/embedded-cluster-operator/api/v1beta1"
embeddedclusterv1beta1 "github.com/replicatedhq/embedded-cluster-kinds/apis/v1beta1"
"github.com/replicatedhq/kots/pkg/airgap"
downstreamtypes "github.com/replicatedhq/kots/pkg/api/downstream/types"
"github.com/replicatedhq/kots/pkg/api/handlers/types"
Expand Down
Loading

0 comments on commit 8612e42

Please sign in to comment.