From bf57f45c8338b9f39128bafc184d2f2bf14b881e Mon Sep 17 00:00:00 2001 From: Andrew Lavery Date: Thu, 25 Jan 2024 13:37:49 -0500 Subject: [PATCH] disable snapshots and gitops on embedded cluster (#4397) * disable snapshots on embedded cluster also refactor to determine whether this is embedded cluster from an environment variable * disable gitops in embedded cluster * fix imports * use the existing 'EMBEDDED_CLUSTER_ID' env var --- pkg/embeddedcluster/helmvm_nodes.go | 11 ++++------- pkg/embeddedcluster/monitor.go | 13 +++---------- pkg/embeddedcluster/util.go | 23 ----------------------- pkg/handlers/app.go | 9 ++++++++- pkg/kotsadm/metadata.go | 5 ++--- pkg/operator/operator.go | 7 +------ pkg/util/util.go | 4 ++++ 7 files changed, 22 insertions(+), 50 deletions(-) diff --git a/pkg/embeddedcluster/helmvm_nodes.go b/pkg/embeddedcluster/helmvm_nodes.go index 7b0eb7729e..11c2ff266d 100644 --- a/pkg/embeddedcluster/helmvm_nodes.go +++ b/pkg/embeddedcluster/helmvm_nodes.go @@ -6,6 +6,7 @@ import ( "github.com/pkg/errors" "github.com/replicatedhq/kots/pkg/embeddedcluster/types" "github.com/replicatedhq/kots/pkg/k8sutil" + "github.com/replicatedhq/kots/pkg/util" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" @@ -29,7 +30,9 @@ func GetNodes(ctx context.Context, client kubernetes.Interface) (*types.Embedded return nil, errors.Wrap(err, "failed to create metrics client") } - toReturn := types.EmbeddedClusterNodes{} + toReturn := types.EmbeddedClusterNodes{ + IsEmbeddedClusterEnabled: util.IsEmbeddedCluster(), + } for _, node := range nodes.Items { nodeMet, err := nodeMetrics(ctx, client, metricsClient, node) @@ -40,12 +43,6 @@ func GetNodes(ctx context.Context, client kubernetes.Interface) (*types.Embedded toReturn.Nodes = append(toReturn.Nodes, *nodeMet) } - isEmbeddedCluster, err := IsEmbeddedCluster(client) - if err != nil { - return nil, errors.Wrap(err, "is embeddedcluster") - } - toReturn.IsEmbeddedClusterEnabled = isEmbeddedCluster - isHA, err := IsHA(client) if err != nil { return nil, errors.Wrap(err, "is ha") diff --git a/pkg/embeddedcluster/monitor.go b/pkg/embeddedcluster/monitor.go index 3d0e1d0eca..4573423f2e 100644 --- a/pkg/embeddedcluster/monitor.go +++ b/pkg/embeddedcluster/monitor.go @@ -10,6 +10,7 @@ import ( "github.com/replicatedhq/embedded-cluster-operator/api/v1beta1" "github.com/replicatedhq/kots/pkg/logger" "github.com/replicatedhq/kots/pkg/store" + "github.com/replicatedhq/kots/pkg/util" "k8s.io/client-go/kubernetes" ) @@ -25,11 +26,7 @@ func MaybeStartClusterUpgrade(ctx context.Context, client kubernetes.Interface, return nil } - isEC, err := IsEmbeddedCluster(client) - if err != nil { - return fmt.Errorf("failed to check if embedded cluster is enabled: %w", err) - } - if !isEC { + if !util.IsEmbeddedCluster() { return nil } @@ -58,11 +55,7 @@ func MaybeStartClusterUpgrade(ctx context.Context, client kubernetes.Interface, // InitClusterState initializes the cluster state in the database. This should be called when the // server launches. func InitClusterState(ctx context.Context, client kubernetes.Interface, store store.Store) error { - isEC, err := IsEmbeddedCluster(client) - if err != nil { - return fmt.Errorf("failed to check if embedded cluster is enabled: %w", err) - } - if isEC { + if util.IsEmbeddedCluster() { go watchClusterState(ctx, store) return nil } diff --git a/pkg/embeddedcluster/util.go b/pkg/embeddedcluster/util.go index 6487ee3601..4e606b5cbf 100644 --- a/pkg/embeddedcluster/util.go +++ b/pkg/embeddedcluster/util.go @@ -11,7 +11,6 @@ import ( embeddedclusterv1beta1 "github.com/replicatedhq/embedded-cluster-operator/api/v1beta1" "github.com/replicatedhq/kots/pkg/k8sutil" corev1 "k8s.io/api/core/v1" - kuberneteserrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/kubernetes" @@ -29,28 +28,6 @@ func ReadConfigMap(client kubernetes.Interface) (*corev1.ConfigMap, error) { return client.CoreV1().ConfigMaps(configMapNamespace).Get(context.TODO(), configMapName, metav1.GetOptions{}) } -func IsEmbeddedCluster(clientset kubernetes.Interface) (bool, error) { - if clientset == nil { - return false, fmt.Errorf("clientset is nil") - } - - configMapExists := false - _, err := ReadConfigMap(clientset) - if err == nil { - configMapExists = true - } else if kuberneteserrors.IsNotFound(err) { - configMapExists = false - } else if kuberneteserrors.IsUnauthorized(err) { - configMapExists = false - } else if kuberneteserrors.IsForbidden(err) { - configMapExists = false - } else if err != nil { - return false, fmt.Errorf("failed to get embedded cluster configmap: %w", err) - } - - return configMapExists, nil -} - func IsHA(clientset kubernetes.Interface) (bool, error) { return true, nil } diff --git a/pkg/handlers/app.go b/pkg/handlers/app.go index b240fe9548..75997a2083 100644 --- a/pkg/handlers/app.go +++ b/pkg/handlers/app.go @@ -281,6 +281,13 @@ func responseAppFromApp(a *apptypes.App) (*types.ResponseApp, error) { return nil, errors.Wrap(err, "failed to check if snapshots is allowed") } allowSnapshots := s && license.Spec.IsSnapshotSupported + if allowSnapshots { + if util.IsEmbeddedCluster() { + allowSnapshots = false // snapshots are not allowed in embedded cluster installations today + } + } + + isGitopsSupported := license.Spec.IsGitOpsSupported && !util.IsEmbeddedCluster() // gitops is not allowed in embedded cluster installations today links, err := version.GetRealizedLinksFromAppSpec(a.ID, parentSequence) if err != nil { @@ -340,7 +347,7 @@ func responseAppFromApp(a *apptypes.App) (*types.ResponseApp, error) { IsConfigurable: a.IsConfigurable, UpdateCheckerSpec: a.UpdateCheckerSpec, AutoDeploy: a.AutoDeploy, - IsGitOpsSupported: license.Spec.IsGitOpsSupported, + IsGitOpsSupported: isGitopsSupported, IsIdentityServiceSupported: license.Spec.IsIdentityServiceSupported, IsAppIdentityServiceSupported: isAppIdentityServiceSupported, IsGeoaxisSupported: license.Spec.IsGeoaxisSupported, diff --git a/pkg/kotsadm/metadata.go b/pkg/kotsadm/metadata.go index 9c2c09b90a..824305df9e 100644 --- a/pkg/kotsadm/metadata.go +++ b/pkg/kotsadm/metadata.go @@ -1,20 +1,19 @@ package kotsadm import ( - "github.com/replicatedhq/kots/pkg/embeddedcluster" "github.com/replicatedhq/kots/pkg/kotsadm/types" "github.com/replicatedhq/kots/pkg/kurl" + "github.com/replicatedhq/kots/pkg/util" "k8s.io/client-go/kubernetes" ) func GetMetadata(clientset kubernetes.Interface) types.Metadata { isKurl, _ := kurl.IsKurl(clientset) - isEmbeddedCluster, _ := embeddedcluster.IsEmbeddedCluster(clientset) metadata := types.Metadata{ IsAirgap: IsAirgap(), IsKurl: isKurl, - IsEmbeddedCluster: isEmbeddedCluster, + IsEmbeddedCluster: util.IsEmbeddedCluster(), } return metadata diff --git a/pkg/operator/operator.go b/pkg/operator/operator.go index 962c9e80df..9c275e232f 100644 --- a/pkg/operator/operator.go +++ b/pkg/operator/operator.go @@ -382,11 +382,6 @@ func (o *Operator) DeployApp(appID string, sequence int64) (deployed bool, deplo return false, errors.Wrap(err, "failed to apply status informers") } - isEmbeddedCluster, err := embeddedcluster.IsEmbeddedCluster(o.k8sClientset) - if err != nil { - return false, errors.Wrap(err, "failed to check if this is an embedded cluster installation") - } - o.client.ApplyNamespacesInformer(kotsKinds.KotsApplication.Spec.AdditionalNamespaces, imagePullSecrets) o.client.ApplyHooksInformer(kotsKinds.KotsApplication.Spec.AdditionalNamespaces) @@ -413,7 +408,7 @@ func (o *Operator) DeployApp(appID string, sequence int64) (deployed bool, deplo } deployed, err = o.client.DeployApp(deployArgs) if err != nil { - if isEmbeddedCluster { + if util.IsEmbeddedCluster() { go func() { logger.Info("app deploy failed, starting cluster upgrade in the background") err2 := embeddedcluster.MaybeStartClusterUpgrade(context.Background(), o.k8sClientset, o.store, kotsKinds.EmbeddedClusterConfig) diff --git a/pkg/util/util.go b/pkg/util/util.go index 5e2dae67d0..0f2cb3ab79 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -169,6 +169,10 @@ func IsHelmManaged() bool { return os.Getenv("IS_HELM_MANAGED") == "true" } +func IsEmbeddedCluster() bool { + return os.Getenv("EMBEDDED_CLUSTER_ID") != "" +} + func GetValueFromMapPath(m interface{}, path []string) interface{} { if len(path) == 0 { return nil