//go:build e2ev2 package lifecycle import ( "context" "fmt" "log" "os" "path/filepath" "slices" "strings" "time" awsutil "github.com/openshift/hypershift/cmd/infra/aws/util" supportawsutil "github.com/openshift/hypershift/support/awsutil" v2util "github.com/openshift/hypershift/test/e2e/v2/util" "github.com/openshift/hypershift/test/e2e/v2/util/mirrorregistry" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/kms" crclient "sigs.k8s.io/controller-runtime/pkg/client" ) type AWSPlatformConfig struct { region string zones []string additionalTags []string sharedDir string externalOIDC externalOIDCSetup newMirrorRegistry func(crclient.Client, mirrorregistry.Options) (mirrorRegistry, error) resolveKMSKeyARNFn func(ctx context.Context, alias string) (string, error) } type AWSPlatformOptions struct { Region string Zones string ProwJobId string } func NewAWSPlatformConfig(opts AWSPlatformOptions, sharedDir string) *AWSPlatformConfig { zones := strings.Split(opts.Zones, ",") // TODO: this is currently just to satisfy an assumption made by EnsureInfrastructureResourceTagsTest // and should probably be handled another way. That test assumes there is at least one pre-existing // non-kubernetes-namespaced tag on the infra. tags := []string{fmt.Sprintf("expirationDate=%s", time.Now().Add(4*time.Hour).UTC().Format(time.RFC3339))} if opts.ProwJobId != "" { tags = append(tags, supportawsutil.HypershiftProwJobIDTagKey+"="+opts.ProwJobId) } cfg := &AWSPlatformConfig{ region: opts.Region, sharedDir: sharedDir, additionalTags: tags, zones: zones, externalOIDC: externalOIDCSetup{sharedDir: sharedDir}, newMirrorRegistry: func(cl crclient.Client, opts mirrorregistry.Options) (mirrorRegistry, error) { return mirrorregistry.New(cl, opts) }, } log.Printf("AWS platform config: region=%s, zones=%v, additionalTags=%v", cfg.region, cfg.zones, cfg.additionalTags) return cfg } func (a *AWSPlatformConfig) Name() string { return "aws" } func (a *AWSPlatformConfig) DefaultBaseDomain() string { return "ci.hypershift.devcluster.openshift.com" } // ClusterSpecs returns the AWS cluster variants to create for the e2e-v2 run, // one per test group. The catalog is static (always 12 variants) regardless of // environment; only [AWSPlatformConfig.TestMatrix] decides which variants are // actually referenced, and [TestPlan.FilterClusterSpecs] prunes the rest. // // In particular: // - kms-reencryption is only referenced by TestMatrix when // HYPERSHIFT_ETCD_KMS_KEY_ARN is set (see [AWSPlatformConfig.TestMatrix]). // - public-rollouts is only referenced when E2E_MERGE_PUBLIC_ROLLOUTS is // unset; when set, rollout tests share the public cluster instead. // // Arguments: // - ctx: used for cancellable setup I/O (e.g. resolving a KMS key alias to an ARN). // - releaseImage: pullspec of the current release under test (a tag or digest // reference such as quay.io/openshift-release-dev/ocp-release:4.20.0-x86_64, // not an OCP version string). // - n1Image: pullspec of the N-1 release used as the upgrade starting point. // // Variants: // - public: default target for non-lifecycle tests. // - public-rollouts: identical config to public, dedicated to nodepool rollout tests. // - upgrade: starts on n1Image, upgraded to releaseImage. // - karpenter: AutoNode/Karpenter node provisioning. // - karpenter-upgrade: karpenter, starting on n1Image. // // Environment inputs: // - EXTRA_ARGS: whitespace-split, appended to every variant. // - HYPERSHIFT_STORAGE_KMS_KEY_ALIAS: resolved to an ARN and passed as // --initial-storage-volumes-kms-key on the public variant (for the storage-kms test). func (a *AWSPlatformConfig) ClusterSpecs(ctx context.Context, releaseImage, n1Image string) []ClusterSpec { baseDomain := envOrDefault("HYPERSHIFT_BASE_DOMAIN", a.DefaultBaseDomain()) destroyArgs := []string{ "--region=" + a.region, "--base-domain=" + baseDomain, } commonArgs := []string{ "--region=" + a.region, "--zones=" + strings.Join(a.zones, ","), "--root-volume-size=64", "--root-volume-type=gp3", "--pods-labels=hypershift-e2e-test-label=test", "--toleration=key=hypershift-e2e-test-toleration,operator=Equal,value=true,effect=NoSchedule", "--annotations=hypershift.openshift.io/cleanup-cloud-resources=true", "--annotations=hypershift.openshift.io/skip-release-image-validation=true", } for _, tag := range a.additionalTags { commonArgs = append(commonArgs, "--additional-tags="+tag) } // Parse EXTRA_ARGS from environment if provided var extraArgs []string if envArgs := os.Getenv("EXTRA_ARGS"); envArgs != "" { extraArgs = strings.Fields(envArgs) } clusterArgs := func(args ...string) []string { return slices.Concat(commonArgs, extraArgs, args) } oneInitialReplica := 1 var kmsKeyARNArgs []string if kmsKeyARN := os.Getenv("HYPERSHIFT_ETCD_KMS_KEY_ARN"); kmsKeyARN != "" { // If the value is an alias (alias/), resolve it to a full ARN. // IAM policy Resources require ARN format; aliases are rejected. if strings.HasPrefix(kmsKeyARN, "alias/") { resolved, err := a.resolveKMSKeyARN(ctx, kmsKeyARN) if err != nil { panic(fmt.Sprintf("HYPERSHIFT_ETCD_KMS_KEY_ARN is set to alias %q but could not be resolved to an ARN: %v", kmsKeyARN, err)) } log.Printf("Resolved etcd KMS key alias %q to ARN %q", kmsKeyARN, resolved) kmsKeyARN = resolved } kmsKeyARNArgs = append(kmsKeyARNArgs, "--kms-key-arn="+kmsKeyARN) } // The storage-kms test runs on the public cluster and requires the cluster // to be created with a KMS key. HYPERSHIFT_STORAGE_KMS_KEY_ALIAS carries a // pre-provisioned CI KMS key alias (e.g. alias/hypershift-ci), resolved here // to its ARN and passed to the create command. The variable is only wired on // jobs whose release supports the --initial-storage-volumes-kms-key flag; when // unset the flag is omitted and the storage-kms test skips itself (it gates on // the running HostedCluster version via SkipIfVersionBelow). publicArgs := []string{ "--public-only", "--feature-set=TechPreviewNoUpgrade", } if alias := os.Getenv("HYPERSHIFT_STORAGE_KMS_KEY_ALIAS"); alias != "" { arn, err := a.resolveKMSKeyARN(ctx, alias) if err != nil { // The variable is set, so this job expects a KMS-encrypted cluster. // Failing to resolve it would silently drop coverage (the cluster // would be created without a key and the storage-kms test would // skip), so die early. This runs before create-guests provisions // anything, so nothing is left behind. panic(fmt.Sprintf("HYPERSHIFT_STORAGE_KMS_KEY_ALIAS is set but alias %q could not be resolved to an ARN: %v", alias, err)) } log.Printf("Resolved storage KMS key alias %q to ARN %q", alias, arn) publicArgs = append(publicArgs, "--initial-storage-volumes-kms-key="+arn) } deterministicLBArgs := []string{ "--endpoint-access=PublicAndPrivate", "--feature-set=TechPreviewNoUpgrade", } if eip := os.Getenv("HYPERSHIFT_DETERMINISTIC_LB_EIP"); eip != "" { if subnet := os.Getenv("HYPERSHIFT_DETERMINISTIC_LB_SUBNET"); subnet != "" { deterministicLBArgs = append(deterministicLBArgs, "--kas-service-annotations=service.beta.kubernetes.io/aws-load-balancer-eip-allocations="+eip, "--kas-service-annotations=service.beta.kubernetes.io/aws-load-balancer-subnets="+subnet, ) } } specs := []ClusterSpec{ { Variant: "public", DestroyArgs: destroyArgs, CreateArgs: clusterArgs(publicArgs...), }, { Variant: "public-rollouts", DestroyArgs: destroyArgs, CreateArgs: clusterArgs(publicArgs...), }, { Variant: "upgrade", DestroyArgs: destroyArgs, ReleaseImage: n1Image, CreateArgs: clusterArgs( "--public-only", "--control-plane-availability-policy=HighlyAvailable", ), }, // The KarpenterBillingConsolidationTest actually tests hostedcluster teardown // behavior and so needs its own dedicated cluster. This seems somewhat leaky // in terms of test isolation because a downstream teardown of a cluster the // test doesn't actually own is only indirectly related to the test and the // test can't actually make any assertions. This is a fundamental difference // from v1 where tests own the lifecycle of the hostedcluster. In v2 tests are // explicitly decoupled from the hostedcluster lifecycle and so have no inherent // ability to make any such lifecycle assertions. This could mean that either // the assertion itself needs to change to decouple it from lifecycle somehow, // or there's a gap in the v2 framework for this sort of use case... { // Dedicated cluster for the managed ingress DNS test. managedDNS is gated // by the AWSManagedDNS feature gate, which only exists in TechPreviewNoUpgrade. Variant: "managed-dns", DestroyArgs: destroyArgs, CreateArgs: clusterArgs( "--public-only", "--feature-set=TechPreviewNoUpgrade", "--managed-dns", ), }, { Variant: "karpenter", DestroyArgs: destroyArgs, CreateArgs: clusterArgs( // Enables Karpenter-based node provisioning (AutoNode) "--auto-node", // Required for karpenter to reach the hosted cluster API server from the mgmt cluster "--endpoint-access=PublicAndPrivate", "--feature-set=TechPreviewNoUpgrade", ), }, { Variant: "karpenter-upgrade", DestroyArgs: destroyArgs, ReleaseImage: n1Image, CreateArgs: clusterArgs( // Enables Karpenter-based node provisioning (AutoNode) "--auto-node", // Required for karpenter to reach the hosted cluster API server from the mgmt cluster "--endpoint-access=PublicAndPrivate", "--control-plane-availability-policy=HighlyAvailable", ), }, { Variant: "autoscaling", DestroyArgs: destroyArgs, InitialNodePoolReplicas: &oneInitialReplica, CreateArgs: clusterArgs("--public-only"), }, { Variant: "external-oidc", DestroyArgs: destroyArgs, InitialNodePoolReplicas: &oneInitialReplica, CreateArgs: clusterArgs("--public-only"), }, { // Consumes the trust bundle and imageContentSources written to // SHARED_DIR by the mirror registry fixture in PreCreate. Variant: "image-mirroring", DestroyArgs: destroyArgs, InitialNodePoolReplicas: &oneInitialReplica, CreateArgs: clusterArgs( "--public-only", "--additional-trust-bundle="+filepath.Join(a.sharedDir, "mirror-registry-ca.pem"), "--image-content-sources="+filepath.Join(a.sharedDir, "mirror-registry-ics.yaml"), ), }, // Etcd sharding can only be configured at creation time, so it needs a // dedicated cluster rather than being layered onto an existing variant. // The EtcdSharding feature gate is TechPreviewNoUpgrade and gates the // spec.etcd.managed.shards field itself, so unless the HyperShift // Operator is installed with --tech-preview-no-upgrade the field is // absent from the CRD, the shards are dropped at admission, and the // specs skip via etcdShardsOrSkip. { Variant: "etcd-sharded", DestroyArgs: destroyArgs, CreateArgs: clusterArgs(append([]string{ "--public-only", // Shards run 3 replicas, which needs an HA control plane. "--control-plane-availability-policy=HighlyAvailable", }, v2util.EtcdShardingCreateArgs("")...)...), }, { // Dedicated cluster for the deterministic LoadBalancer IP test. // The EIP and public subnet belong to the management cluster and // are reserved by the CI pre-step before create-guests runs. Variant: "deterministic-lb", DestroyArgs: destroyArgs, InitialNodePoolReplicas: &oneInitialReplica, CreateArgs: clusterArgs(deterministicLBArgs...), }, { // Dedicated cluster for etcd KMS re-encryption tests. When // HYPERSHIFT_ETCD_KMS_KEY_ARN is set, the cluster is created with // --kms-key-arn so AWSKMSKeyRotationTest can rotate keys. When the // variable is unset the cluster is created without KMS encryption and // the rotation tests skip themselves via applies(). Variant: "kms-reencryption", DestroyArgs: destroyArgs, CreateArgs: clusterArgs(slices.Concat([]string{"--public-only"}, kmsKeyARNArgs)...), }, } return specs } // PreCreate deploys the external OIDC provider and the mirror registry fixture // before any cluster is created. The image-mirroring variant reads the fixture // artifacts from SHARED_DIR at creation time. func (a *AWSPlatformConfig) PreCreate(ctx context.Context, cl crclient.WithWatch, namespace string) error { if err := a.externalOIDC.preCreate(ctx, cl); err != nil { return err } return a.setupImageMirroring(ctx, cl) } func (a *AWSPlatformConfig) setupImageMirroring(ctx context.Context, cl crclient.Client) error { if a.sharedDir == "" { return fmt.Errorf("image mirroring requires a shared directory") } // This lane copies only two small public-image mappings, not full release // payloads. Twenty minutes bounds the entire setup, including readiness // waits, even though the generic copier allows thirty minutes per Job. ctx, cancel := context.WithTimeout(ctx, 20*time.Minute) defer cancel() copierImage, err := cliImage(ctx, cl) if err != nil { return err } registry, err := a.newMirrorRegistry(cl, mirrorregistry.Options{CopierImage: copierImage}) if err != nil { return fmt.Errorf("initialize mirror registry: %w", err) } // The public image needs no source pull secret. Mirror both its real // repository and a virtual source that cannot fall back to upstream. result, err := registry.Setup(ctx, mirrorregistry.MirrorRequest{Images: []mirrorregistry.ImageMapping{ {From: mirrorregistry.DefaultRegistryImage}, {From: mirrorregistry.DefaultRegistryImage, SourceRepository: "mirror-source.hypershift-e2e.invalid/libpod/registry"}, }}) if err != nil { return fmt.Errorf("set up mirror registry: %w", err) } return writeMirrorRegistryResult(a.sharedDir, result) } func (a *AWSPlatformConfig) PostCreate(ctx context.Context, cl crclient.WithWatch, namespace string, clusterNames map[string]string) error { return nil } func (a *AWSPlatformConfig) PostAvailable(ctx context.Context, cl crclient.WithWatch, namespace string, clusterNames map[string]string) error { return nil } func (a *AWSPlatformConfig) PostVersionRollout(ctx context.Context, cl crclient.WithWatch, namespace string, clusterNames map[string]string) error { return a.externalOIDC.postVersionRollout(ctx, cl, namespace, clusterNames) } func (a *AWSPlatformConfig) DefaultTestPlan() TestPlan { return TestPlan{ Name: "aws-full", Platform: "aws", TestMatrix: a.TestMatrix(), } } func (a *AWSPlatformConfig) TestMatrix() TestMatrix { parallel := []TestGroup{ { Name: "managed-dns", Variant: "managed-dns", LabelFilter: "hosted-cluster-managed-dns", }, { Name: "karpenter", Variant: "karpenter", LabelFilter: "karpenter", }, // TODO: It might be possible to decompose the karpenter upgrade test // into pre and post upgrade specs which communicate with a well defined // IPC protocol, like the pre step serializing observations which the // post step can use. Then we can compose the regular upgrade test here // instead of duplicating upgrade logic inside the karpenter test. { Name: "karpenter-upgrade", Variant: "karpenter-upgrade", LabelFilter: "karpenter-upgrade", }, } // When HYPERSHIFT_ETCD_KMS_KEY_ARN is unset (presubmit default) the // kms-reencryption tests skip immediately, so omitting the variant from the // matrix lets FilterClusterSpecs avoid provisioning an unused guest cluster. if os.Getenv("HYPERSHIFT_ETCD_KMS_KEY_ARN") != "" { parallel = append(parallel, TestGroup{ Name: "kms-reencryption", Variant: "kms-reencryption", LabelFilter: "etcd-encryption-reencryption", }) } rolloutLabelFilter := "nodepool-vm-size-rollout || nodepool-replace-version-upgrade || nodepool-inplace-version-upgrade || " + "nodepool-n1-release || nodepool-n2-release || nodepool-auto-repair || nodepool-osimagestream-upgrade || " + "nodepool-nto-replace-rollout || nodepool-nto-inplace-rollout || nodepool-performance-profile || nodepool-mirror-config" publicSteps := []TestGroup{ { Name: "public", Variant: "public", LabelFilter: "hosted-cluster-aws || hosted-cluster-compliance || hosted-cluster-node-communication || " + "hosted-cluster-cpo || nodepool-arm64 || secret-encryption || control-plane-workloads || " + "hosted-cluster-security || nodepool-osimagestream || hosted-cluster-ingress || hosted-cluster-dns || " + "hosted-cluster-health || hosted-cluster-metrics || hosted-cluster-image-registry || storage-kms || " + "hosted-cluster-event-ttl || installation-diagnostics", }, } // When E2E_MERGE_PUBLIC_ROLLOUTS is set (presubmit via openshift/release), // rollout tests share the "public" cluster as step 2, saving one guest. // When unset (default, safe for periodic) rollout tests run on a dedicated // "public-rollouts" cluster to stay within the 150-min step timeout. if os.Getenv("E2E_MERGE_PUBLIC_ROLLOUTS") != "" { publicSteps = append(publicSteps, TestGroup{ Name: "public-nodepool-rollouts", Variant: "public", LabelFilter: rolloutLabelFilter, }) } sequential := []SequentialGroup{ // The removal step is a day-2 mutation of imageContentSources that is // not restored, so it must stay the last step on the variant. { Name: "image-mirroring", Steps: []TestGroup{ { Name: "image-mirroring", Variant: "image-mirroring", LabelFilter: "image-mirroring", }, { Name: "image-mirroring-removal", Variant: "image-mirroring", LabelFilter: "image-mirroring-removal", }, }, }, { Name: "public", Steps: publicSteps, }, } if os.Getenv("E2E_MERGE_PUBLIC_ROLLOUTS") == "" { sequential = append(sequential, SequentialGroup{ Name: "public-rollouts", Steps: []TestGroup{ { Name: "public-nodepool-rollouts", Variant: "public-rollouts", LabelFilter: rolloutLabelFilter, }, }, }) } sequential = append(sequential, SequentialGroup{ Name: "autoscaling", Steps: []TestGroup{ { Name: "autoscaling-nodepool-machineconfig", Variant: "autoscaling", LabelFilter: "nodepool-machineconfig-rollout", }, { Name: "autoscaling-balancing", Variant: "autoscaling", LabelFilter: "nodepool-autoscaling-balancing", }, }, }, SequentialGroup{ Name: "external-oidc", Steps: []TestGroup{ { Name: "external-oidc", Variant: "external-oidc", LabelFilter: "external-oidc || global-pull-secret", }, { Name: "external-oidc-autoscaling", Variant: "external-oidc", LabelFilter: "nodepool-autoscaling-scale-up-down", }, { Name: "external-oidc-trust-bundle", Variant: "external-oidc", LabelFilter: "nodepool-trust-bundle", }, }, }, SequentialGroup{ Name: "upgrade-and-chaos", Steps: []TestGroup{ { Name: "upgrade", Variant: "upgrade", LabelFilter: "control-plane-upgrade", }, { Name: "post-upgrade-health", Variant: "upgrade", LabelFilter: "hosted-cluster-health || control-plane-workloads", }, { Name: "control-plane-tls", Variant: "upgrade", LabelFilter: "control-plane-pki-operator", }, { Name: "etcd-chaos", Variant: "upgrade", LabelFilter: "etcd-chaos", }, }, }, SequentialGroup{ Name: "etcd-sharding", Steps: []TestGroup{ // Shard deployment, kube-apiserver routing, per-kind key // placement, shard member restart and day-2 immutability. { Name: "etcd-sharding", Variant: "etcd-sharded", LabelFilter: "etcd-sharding", }, // Cluster operations with sharding enabled: runs after the // sharding specs so it also covers recovery from the shard // member restart. { Name: "post-sharding-health", Variant: "etcd-sharded", LabelFilter: "hosted-cluster-health || control-plane-workloads", }, }, }, ) return TestMatrix{ Parallel: parallel, Sequential: sequential, } } func (a *AWSPlatformConfig) SetupTestEnv(sharedDir string) { setupExternalOIDCTestEnv(sharedDir) // E2E_AWS_KMS_KEY_ARN_ALTERNATE may be set to a KMS alias by CI. The rotation // test writes it verbatim into spec.secretEncryption.kms.aws.activeKey.arn, which // the HostedCluster CRD requires to be a full ARN. Resolve the alias here, before // any test reads the env var, so the patch is never rejected by API validation. if alt := os.Getenv("E2E_AWS_KMS_KEY_ARN_ALTERNATE"); strings.HasPrefix(alt, "alias/") { resolved, err := a.resolveKMSKeyARN(context.Background(), alt) if err != nil { panic(fmt.Sprintf("E2E_AWS_KMS_KEY_ARN_ALTERNATE is set to alias %q but could not be resolved to an ARN: %v", alt, err)) } log.Printf("Resolved alternate KMS key alias %q to ARN %q", alt, resolved) if err := os.Setenv("E2E_AWS_KMS_KEY_ARN_ALTERNATE", resolved); err != nil { panic(fmt.Sprintf("failed to update E2E_AWS_KMS_KEY_ARN_ALTERNATE: %v", err)) } } } // resolveKMSKeyARN resolves a KMS key alias (e.g. "alias/hypershift-ci") to its // full key ARN using DescribeKey. AWS credentials are sourced from the SDK default // credential chain (shared config/credentials files, environment variables, and // instance/role providers). resolveKMSKeyARNFn can be set on the struct to override // this behavior in tests without making real AWS API calls. func (a *AWSPlatformConfig) resolveKMSKeyARN(ctx context.Context, alias string) (string, error) { if a.resolveKMSKeyARNFn != nil { return a.resolveKMSKeyARNFn(ctx, alias) } ctx, cancel := context.WithTimeout(ctx, 30*time.Second) defer cancel() awsConfig := awsutil.NewSession(ctx, "e2e-storage-kms", "", "", "", a.region) retryer := awsutil.NewConfig() kmsClient := kms.NewFromConfig(*awsConfig, func(o *kms.Options) { o.Retryer = retryer() }) out, err := kmsClient.DescribeKey(ctx, &kms.DescribeKeyInput{KeyId: aws.String(alias)}) if err != nil { return "", fmt.Errorf("describing KMS key %q: %w", alias, err) } if out.KeyMetadata == nil || out.KeyMetadata.Arn == nil { return "", fmt.Errorf("KMS key with alias %q has no ARN", alias) } return *out.KeyMetadata.Arn, nil }