diff --git a/cmd/mapt/cmd/aws/hosts/rhelai.go b/cmd/mapt/cmd/aws/hosts/rhelai.go index 26df35c88..09369bbe7 100644 --- a/cmd/mapt/cmd/aws/hosts/rhelai.go +++ b/cmd/mapt/cmd/aws/hosts/rhelai.go @@ -4,7 +4,7 @@ import ( "github.com/redhat-developer/mapt/cmd/mapt/cmd/params" maptContext "github.com/redhat-developer/mapt/pkg/manager/context" rhelai "github.com/redhat-developer/mapt/pkg/provider/aws/action/rhel-ai" - apiRHELAI "github.com/redhat-developer/mapt/pkg/targets/host/rhelai" + apiRHELAI "github.com/redhat-developer/mapt/pkg/target/host/rhelai" "github.com/spf13/cobra" "github.com/spf13/pflag" "github.com/spf13/viper" diff --git a/cmd/mapt/cmd/aws/services/kind.go b/cmd/mapt/cmd/aws/services/kind.go index ca215529e..fa3f79189 100644 --- a/cmd/mapt/cmd/aws/services/kind.go +++ b/cmd/mapt/cmd/aws/services/kind.go @@ -6,7 +6,7 @@ import ( "github.com/redhat-developer/mapt/cmd/mapt/cmd/params" maptContext "github.com/redhat-developer/mapt/pkg/manager/context" "github.com/redhat-developer/mapt/pkg/provider/aws/action/kind" - kindApi "github.com/redhat-developer/mapt/pkg/targets/service/kind" + kindApi "github.com/redhat-developer/mapt/pkg/target/service/kind" "github.com/spf13/cobra" "github.com/spf13/pflag" "github.com/spf13/viper" diff --git a/cmd/mapt/cmd/aws/services/openshift-snc.go b/cmd/mapt/cmd/aws/services/snc.go similarity index 93% rename from cmd/mapt/cmd/aws/services/openshift-snc.go rename to cmd/mapt/cmd/aws/services/snc.go index 7d17dc0a1..ecf9ac1dd 100644 --- a/cmd/mapt/cmd/aws/services/openshift-snc.go +++ b/cmd/mapt/cmd/aws/services/snc.go @@ -3,7 +3,8 @@ package services import ( params "github.com/redhat-developer/mapt/cmd/mapt/cmd/params" maptContext "github.com/redhat-developer/mapt/pkg/manager/context" - openshiftsnc "github.com/redhat-developer/mapt/pkg/provider/aws/action/openshift-snc" + openshiftsnc "github.com/redhat-developer/mapt/pkg/provider/aws/action/snc" + sncApi "github.com/redhat-developer/mapt/pkg/target/service/snc" "github.com/spf13/cobra" "github.com/spf13/pflag" "github.com/spf13/viper" @@ -13,8 +14,10 @@ const ( cmdOpenshiftSNC = "openshift-snc" cmdOpenshiftSNCDesc = "Manage an OpenShift Single Node Cluster based on OpenShift Local. This is not intended for production use" - ocpVersion = "version" - ocpVersionDesc = "version for Openshift. If not set it will pick latest available version" + ocpVersion = "version" + ocpDefaultVersion = "4.21.0" + ocpVersionDesc = "version for Openshift." + pullSecretFile = "pull-secret-file" pullSecretFileDesc = "file path of image pull secret (download from https://console.redhat.com/openshift/create/local)" disableClusterReadiness = "disable-cluster-readiness" @@ -58,7 +61,7 @@ func createSNC() *cobra.Command { DebugLevel: viper.GetUint(params.DebugLevel), Tags: viper.GetStringMapString(params.Tags), }, - &openshiftsnc.OpenshiftSNCArgs{ + &sncApi.SNCArgs{ ComputeRequest: params.ComputeRequestArgs(), Spot: params.SpotArgs(), Version: viper.GetString(ocpVersion), @@ -73,7 +76,7 @@ func createSNC() *cobra.Command { } flagSet := pflag.NewFlagSet(params.CreateCmdName, pflag.ExitOnError) flagSet.StringP(params.ConnectionDetailsOutput, "", "", params.ConnectionDetailsOutputDesc) - flagSet.StringP(ocpVersion, "", "", ocpVersionDesc) + flagSet.StringP(ocpVersion, "", ocpDefaultVersion, ocpVersionDesc) flagSet.Bool(disableClusterReadiness, false, disableClusterReadinessDesc) flagSet.StringP(params.LinuxArch, "", params.LinuxArchDefault, params.LinuxArchDesc) flagSet.StringP(pullSecretFile, "", "", pullSecretFileDesc) diff --git a/cmd/mapt/cmd/azure/hosts/rhelai.go b/cmd/mapt/cmd/azure/hosts/rhelai.go index 1edf6d755..16c176a38 100644 --- a/cmd/mapt/cmd/azure/hosts/rhelai.go +++ b/cmd/mapt/cmd/azure/hosts/rhelai.go @@ -4,7 +4,7 @@ import ( "github.com/redhat-developer/mapt/cmd/mapt/cmd/params" maptContext "github.com/redhat-developer/mapt/pkg/manager/context" rhelai "github.com/redhat-developer/mapt/pkg/provider/azure/action/rhel-ai" - apiRHELAI "github.com/redhat-developer/mapt/pkg/targets/host/rhelai" + apiRHELAI "github.com/redhat-developer/mapt/pkg/target/host/rhelai" "github.com/spf13/cobra" "github.com/spf13/pflag" "github.com/spf13/viper" diff --git a/cmd/mapt/cmd/azure/services/kind.go b/cmd/mapt/cmd/azure/services/kind.go index 580e59e41..fe6291c99 100644 --- a/cmd/mapt/cmd/azure/services/kind.go +++ b/cmd/mapt/cmd/azure/services/kind.go @@ -7,7 +7,7 @@ import ( "github.com/redhat-developer/mapt/cmd/mapt/cmd/params" maptContext "github.com/redhat-developer/mapt/pkg/manager/context" "github.com/redhat-developer/mapt/pkg/provider/azure/action/kind" - kindApi "github.com/redhat-developer/mapt/pkg/targets/service/kind" + kindApi "github.com/redhat-developer/mapt/pkg/target/service/kind" "github.com/spf13/cobra" "github.com/spf13/pflag" "github.com/spf13/viper" diff --git a/pkg/provider/aws/action/fedora/fedora.go b/pkg/provider/aws/action/fedora/fedora.go index 0866e81c1..62c9ac4cc 100644 --- a/pkg/provider/aws/action/fedora/fedora.go +++ b/pkg/provider/aws/action/fedora/fedora.go @@ -24,9 +24,9 @@ import ( amiSVC "github.com/redhat-developer/mapt/pkg/provider/aws/services/ec2/ami" "github.com/redhat-developer/mapt/pkg/provider/aws/services/ec2/keypair" securityGroup "github.com/redhat-developer/mapt/pkg/provider/aws/services/ec2/security-group" - fedoraCloudConfig "github.com/redhat-developer/mapt/pkg/provider/util/cloud-config/fedora" "github.com/redhat-developer/mapt/pkg/provider/util/command" "github.com/redhat-developer/mapt/pkg/provider/util/output" + fedoraCloudConfig "github.com/redhat-developer/mapt/pkg/target/host/fedora" "github.com/redhat-developer/mapt/pkg/util" "github.com/redhat-developer/mapt/pkg/util/logging" resourcesUtil "github.com/redhat-developer/mapt/pkg/util/resources" diff --git a/pkg/provider/aws/action/kind/kind.go b/pkg/provider/aws/action/kind/kind.go index 1fe849a18..7dd560a9b 100644 --- a/pkg/provider/aws/action/kind/kind.go +++ b/pkg/provider/aws/action/kind/kind.go @@ -21,7 +21,7 @@ import ( "github.com/redhat-developer/mapt/pkg/provider/aws/services/ec2/keypair" securityGroup "github.com/redhat-developer/mapt/pkg/provider/aws/services/ec2/security-group" "github.com/redhat-developer/mapt/pkg/provider/util/command" - utilKind "github.com/redhat-developer/mapt/pkg/targets/service/kind" + utilKind "github.com/redhat-developer/mapt/pkg/target/service/kind" "github.com/redhat-developer/mapt/pkg/util" "github.com/redhat-developer/mapt/pkg/util/logging" resourcesUtil "github.com/redhat-developer/mapt/pkg/util/resources" diff --git a/pkg/provider/aws/action/openshift-snc/constants.go b/pkg/provider/aws/action/openshift-snc/constants.go deleted file mode 100644 index efce5abc7..000000000 --- a/pkg/provider/aws/action/openshift-snc/constants.go +++ /dev/null @@ -1,41 +0,0 @@ -package openshiftsnc - -import "fmt" - -var ( - stackName = "stackOpenshiftSNC" - awsOCPSNCID = "aos" - - diskSize int = 200 - - // This is managed by https://github.com/devtools-qe-incubator/cloud-importer - amiProduct = "Linux/UNIX" - // amiProductDescription = "Red Hat Enterprise Linux" - amiRegex = "openshift-local-%s-%s-*" - amiUserDefault = "core" - amiOwner = "391597328979" - // amiOriginRegion = "us-east-1" - - consoleURLRegex = "https://console-openshift-console.apps.%s.nip.io" - - outputHost = "aosHost" - outputUsername = "aosUsername" - outputUserPrivateKey = "aosPrivatekey" - outputKubeconfig = "aosKubeconfig" - outputKubeAdminPass = "aosKubeAdminPasss" - outputDeveloperPass = "aosDeveloperPass" - - commandCrcReadiness = "while [ ! -f /tmp/.crc-cluster-ready ]; do sleep 5; done" - commandCaServiceRan = "sudo bash -c 'until oc get node --kubeconfig /opt/kubeconfig --context system:admin || oc get node --kubeconfig /opt/crc/kubeconfig --context system:admin; do sleep 5; done'" - - // portHTTP = 80 - portHTTPS = 443 - portAPI = 6443 - - // SSM - ocpPullSecretID = "ocppullsecretid" - kapass = "kapass" - devpass = "devpass" -) - -func amiName(version, arch *string) string { return fmt.Sprintf(amiRegex, *version, *arch) } diff --git a/pkg/provider/aws/action/rhel-ai/rhelai.go b/pkg/provider/aws/action/rhel-ai/rhelai.go index b345d3faa..9c3dde761 100644 --- a/pkg/provider/aws/action/rhel-ai/rhelai.go +++ b/pkg/provider/aws/action/rhel-ai/rhelai.go @@ -24,7 +24,7 @@ import ( securityGroup "github.com/redhat-developer/mapt/pkg/provider/aws/services/ec2/security-group" "github.com/redhat-developer/mapt/pkg/provider/util/command" "github.com/redhat-developer/mapt/pkg/provider/util/output" - apiRHELAI "github.com/redhat-developer/mapt/pkg/targets/host/rhelai" + apiRHELAI "github.com/redhat-developer/mapt/pkg/target/host/rhelai" "github.com/redhat-developer/mapt/pkg/util" "github.com/redhat-developer/mapt/pkg/util/logging" resourcesUtil "github.com/redhat-developer/mapt/pkg/util/resources" diff --git a/pkg/provider/aws/action/rhel/rhel.go b/pkg/provider/aws/action/rhel/rhel.go index 0fb455aa9..d351a52a5 100644 --- a/pkg/provider/aws/action/rhel/rhel.go +++ b/pkg/provider/aws/action/rhel/rhel.go @@ -25,7 +25,7 @@ import ( securityGroup "github.com/redhat-developer/mapt/pkg/provider/aws/services/ec2/security-group" "github.com/redhat-developer/mapt/pkg/provider/util/command" "github.com/redhat-developer/mapt/pkg/provider/util/output" - rhelApi "github.com/redhat-developer/mapt/pkg/targets/host/rhel" + rhelApi "github.com/redhat-developer/mapt/pkg/target/host/rhel" "github.com/redhat-developer/mapt/pkg/util" "github.com/redhat-developer/mapt/pkg/util/logging" resourcesUtil "github.com/redhat-developer/mapt/pkg/util/resources" diff --git a/pkg/provider/aws/action/snc/constants.go b/pkg/provider/aws/action/snc/constants.go new file mode 100644 index 000000000..9dce012d7 --- /dev/null +++ b/pkg/provider/aws/action/snc/constants.go @@ -0,0 +1,22 @@ +package openshiftsnc + +import "fmt" + +var ( + diskSize int = 200 + + // This is managed by https://github.com/devtools-qe-incubator/cloud-importer + amiProduct = "Linux/UNIX" + // amiProductDescription = "Red Hat Enterprise Linux" + amiRegex = "openshift-local-%s-%s-*" + amiUserDefault = "core" + amiOwner = "391597328979" + // amiOriginRegion = "us-east-1" + + // SSM + ocpPullSecretID = "ocppullsecretid" + kapass = "kapass" + devpass = "devpass" +) + +func amiName(version, arch *string) string { return fmt.Sprintf(amiRegex, *version, *arch) } diff --git a/pkg/provider/aws/action/snc/secrets.go b/pkg/provider/aws/action/snc/secrets.go new file mode 100644 index 000000000..86db170cc --- /dev/null +++ b/pkg/provider/aws/action/snc/secrets.go @@ -0,0 +1,5 @@ +package openshiftsnc + +// This is the AWS policy required to use SSM service in order to set the values +// within userdata +var requiredPolicies = []string{"arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"} diff --git a/pkg/provider/aws/action/openshift-snc/openshift-snc.go b/pkg/provider/aws/action/snc/snc.go similarity index 67% rename from pkg/provider/aws/action/openshift-snc/openshift-snc.go rename to pkg/provider/aws/action/snc/snc.go index eb51b6b71..5821990d7 100644 --- a/pkg/provider/aws/action/openshift-snc/openshift-snc.go +++ b/pkg/provider/aws/action/snc/snc.go @@ -9,12 +9,9 @@ import ( "github.com/go-playground/validator/v10" "github.com/pulumi/pulumi-aws/sdk/v7/go/aws/ec2" "github.com/pulumi/pulumi-tls/sdk/v5/go/tls" - "github.com/pulumi/pulumi/sdk/v3/go/auto" "github.com/pulumi/pulumi/sdk/v3/go/pulumi" "github.com/redhat-developer/mapt/pkg/manager" mc "github.com/redhat-developer/mapt/pkg/manager/context" - cr "github.com/redhat-developer/mapt/pkg/provider/api/compute-request" - spotTypes "github.com/redhat-developer/mapt/pkg/provider/api/spot" "github.com/redhat-developer/mapt/pkg/provider/aws" awsConstants "github.com/redhat-developer/mapt/pkg/provider/aws/constants" "github.com/redhat-developer/mapt/pkg/provider/aws/data" @@ -29,24 +26,13 @@ import ( securityGroup "github.com/redhat-developer/mapt/pkg/provider/aws/services/ec2/security-group" "github.com/redhat-developer/mapt/pkg/provider/aws/services/ssm" "github.com/redhat-developer/mapt/pkg/provider/util/command" - "github.com/redhat-developer/mapt/pkg/provider/util/output" "github.com/redhat-developer/mapt/pkg/provider/util/security" + apiSNC "github.com/redhat-developer/mapt/pkg/target/service/snc" "github.com/redhat-developer/mapt/pkg/util" "github.com/redhat-developer/mapt/pkg/util/logging" resourcesUtil "github.com/redhat-developer/mapt/pkg/util/resources" ) -type OpenshiftSNCArgs struct { - Prefix string - ComputeRequest *cr.ComputeRequestArgs - Version string - DisableClusterReadiness bool - Arch string - PullSecretFile string - Spot *spotTypes.SpotArgs - Timeout string -} - type openshiftSNCRequest struct { mCtx *mc.Context prefix *string @@ -68,20 +54,10 @@ func (r *openshiftSNCRequest) validate() error { return v.Struct(r) } -type OpenshiftSncResultsMetadata struct { - Username string `json:"username"` - PrivateKey string `json:"private_key"` - Host string `json:"host"` - Kubeconfig string `json:"kubeconfig"` - KubeadminPass string `json:"kubeadmin_pass"` - SpotPrice *float64 `json:"spot_price,omitempty"` - ConsoleUrl string `json:"console_url,omitempty"` -} - // Create orchestrate 3 stacks: // If spot is enable it will run best spot option to get the best option to spin the machine // Then it will run the stack for windows dedicated host -func Create(mCtxArgs *mc.ContextArgs, args *OpenshiftSNCArgs) (_ *OpenshiftSncResultsMetadata, err error) { +func Create(mCtxArgs *mc.ContextArgs, args *apiSNC.SNCArgs) (_ *apiSNC.SNCResults, err error) { // Create mapt Context mCtx, err := mc.Init(mCtxArgs, aws.Provider()) if err != nil { @@ -130,7 +106,7 @@ func Destroy(mCtxArgs *mc.ContextArgs) (err error) { if err = aws.DestroyStack( mCtx, aws.DestroyStackRequest{ - Stackname: stackName, + Stackname: apiSNC.StackName, }); err != nil { return err } @@ -145,9 +121,12 @@ func Destroy(mCtxArgs *mc.ContextArgs) (err error) { return aws.CleanupState(mCtx) } -func (r *openshiftSNCRequest) createCluster() (*OpenshiftSncResultsMetadata, error) { +func (r *openshiftSNCRequest) createCluster() (*apiSNC.SNCResults, error) { + if err := r.validate(); err != nil { + return nil, err + } cs := manager.Stack{ - StackName: r.mCtx.StackNameByProject(stackName), + StackName: r.mCtx.StackNameByProject(apiSNC.StackName), ProjectName: r.mCtx.ProjectName(), BackedURL: r.mCtx.BackedURL(), ProviderCredentials: aws.GetClouProviderCredentials( @@ -161,7 +140,10 @@ func (r *openshiftSNCRequest) createCluster() (*OpenshiftSncResultsMetadata, err return nil, fmt.Errorf("stack creation failed: %w", err) } - return r.manageResults(sr, r.prefix) + return apiSNC.Results(sr, r.prefix, + r.mCtx.GetResultsOutputPath(), + r.allocationData.SpotPrice, + r.disableClusterReadiness) } func (r *openshiftSNCRequest) deploy(ctx *pulumi.Context) error { @@ -180,7 +162,7 @@ func (r *openshiftSNCRequest) deploy(ctx *pulumi.Context) error { nw, err := network.Create(ctx, r.mCtx, &network.NetworkArgs{ Prefix: *r.prefix, - ID: awsOCPSNCID, + ID: apiSNC.OCPSNCID, Region: *r.allocationData.Region, AZ: *r.allocationData.AZ, CreateLoadBalancer: r.allocationData.SpotPrice != nil, @@ -192,12 +174,12 @@ func (r *openshiftSNCRequest) deploy(ctx *pulumi.Context) error { // Create Keypair kpr := keypair.KeyPairRequest{ Name: resourcesUtil.GetResourceName( - *r.prefix, awsOCPSNCID, "pk")} + *r.prefix, apiSNC.OCPSNCID, "pk")} keyResources, err := kpr.Create(ctx, r.mCtx) if err != nil { return err } - ctx.Export(fmt.Sprintf("%s-%s", *r.prefix, outputUserPrivateKey), + ctx.Export(fmt.Sprintf("%s-%s", *r.prefix, apiSNC.OutputUserPrivateKey), keyResources.PrivateKey.PrivateKeyPem) if r.mCtx.Debug() { keyResources.PrivateKey.PrivateKeyPem.ApplyT( @@ -212,7 +194,7 @@ func (r *openshiftSNCRequest) deploy(ctx *pulumi.Context) error { return err } // Instance profile required by logic within userdata - iProfile, err := iam.InstanceProfile(ctx, r.prefix, &awsOCPSNCID, cloudConfigRequiredProfiles) + iProfile, err := iam.InstanceProfile(ctx, r.prefix, &apiSNC.OCPSNCID, requiredPolicies) if err != nil { return err } @@ -221,15 +203,15 @@ func (r *openshiftSNCRequest) deploy(ctx *pulumi.Context) error { if err != nil { return err } - ctx.Export(fmt.Sprintf("%s-%s", *r.prefix, outputKubeAdminPass), + ctx.Export(fmt.Sprintf("%s-%s", *r.prefix, apiSNC.OutputKubeAdminPass), kaPass) - ctx.Export(fmt.Sprintf("%s-%s", *r.prefix, outputDeveloperPass), + ctx.Export(fmt.Sprintf("%s-%s", *r.prefix, apiSNC.OutputDeveloperPass), devPass) // Create instance cr := compute.ComputeRequest{ MCtx: r.mCtx, Prefix: *r.prefix, - ID: awsOCPSNCID, + ID: apiSNC.OCPSNCID, VPC: nw.Vpc, Subnet: nw.Subnet, AMI: ami, @@ -239,7 +221,7 @@ func (r *openshiftSNCRequest) deploy(ctx *pulumi.Context) error { DiskSize: &diskSize, LB: nw.LoadBalancer, Eip: nw.Eip, - LBTargetGroups: []int{securityGroup.SSH_PORT, portHTTPS, portAPI}, + LBTargetGroups: []int{securityGroup.SSH_PORT, apiSNC.PortHTTPS, apiSNC.PortAPI}, InstanceProfile: iProfile, UserDataAsBase64: udB64, DependsOn: udDependecies, @@ -253,14 +235,14 @@ func (r *openshiftSNCRequest) deploy(ctx *pulumi.Context) error { if err != nil { return err } - ctx.Export(fmt.Sprintf("%s-%s", *r.prefix, outputUsername), + ctx.Export(fmt.Sprintf("%s-%s", *r.prefix, apiSNC.OutputUsername), pulumi.String(amiUserDefault)) - ctx.Export(fmt.Sprintf("%s-%s", *r.prefix, outputHost), + ctx.Export(fmt.Sprintf("%s-%s", *r.prefix, apiSNC.OutputHost), c.GetHostIP(true)) if len(*r.timeout) > 0 { if err = serverless.OneTimeDelayedTask(ctx, r.mCtx, *r.allocationData.Region, *r.prefix, - awsOCPSNCID, + apiSNC.OCPSNCID, fmt.Sprintf("aws %s destroy --project-name %s --backed-url %s --serverless", "openshift-snc", r.mCtx.ProjectName(), @@ -269,79 +251,14 @@ func (r *openshiftSNCRequest) deploy(ctx *pulumi.Context) error { return err } } - if !r.disableClusterReadiness { - // Use kubeconfig as the readiness for the cluster - kubeconfig, err := kubeconfig(ctx, r.prefix, c, keyResources.PrivateKey) - if err != nil { - return err - } - ctx.Export(fmt.Sprintf("%s-%s", *r.prefix, outputKubeconfig), - pulumi.ToSecret(kubeconfig)) - return nil - } - return c.Readiness(ctx, command.CommandPing, *r.prefix, awsOCPSNCID, - keyResources.PrivateKey, amiUserDefault, nil, c.Dependencies) -} - -// Write exported values in context to files o a selected target folder -func (r *openshiftSNCRequest) manageResults(stackResult auto.UpResult, prefix *string) (*OpenshiftSncResultsMetadata, error) { - username, err := getResultOutput(outputUsername, stackResult, prefix) - if err != nil { - return nil, err - } - privateKey, err := getResultOutput(outputUserPrivateKey, stackResult, prefix) - if err != nil { - return nil, err - } - host, err := getResultOutput(outputHost, stackResult, prefix) - if err != nil { - return nil, err - } - kubeAdminPass, err := getResultOutput(outputKubeAdminPass, stackResult, prefix) + // Use kubeconfig as the readiness for the cluster + kubeconfig, err := kubeconfig(ctx, r.prefix, c, keyResources.PrivateKey, *r.version, r.disableClusterReadiness) if err != nil { - return nil, err - } - kubeconfig := "" - if !r.disableClusterReadiness { - kubeconfig, err = getResultOutput(outputKubeconfig, stackResult, prefix) - if err != nil { - return nil, err - } - } - - hostIPKey := fmt.Sprintf("%s-%s", *prefix, outputHost) - results := map[string]string{ - fmt.Sprintf("%s-%s", *prefix, outputUsername): "username", - fmt.Sprintf("%s-%s", *prefix, outputUserPrivateKey): "id_rsa", - hostIPKey: "host", - fmt.Sprintf("%s-%s", *prefix, outputKubeconfig): "kubeconfig", - fmt.Sprintf("%s-%s", *prefix, outputKubeAdminPass): "kubeadmin_pass", - fmt.Sprintf("%s-%s", *prefix, outputDeveloperPass): "developer_pass", - } - - outputPath := r.mCtx.GetResultsOutputPath() - if len(outputPath) == 0 { - logging.Warn("conn-details-output flag not set; skipping writing output files.") - } else { - if err := output.Write(stackResult, outputPath, results); err != nil { - return nil, fmt.Errorf("failed to write results: %w", err) - } - } - - consoleURL := fmt.Sprintf(consoleURLRegex, host) - if eip, ok := stackResult.Outputs[hostIPKey].Value.(string); ok { - fmt.Printf("Cluster has been started you can access console at: %s.\n", fmt.Sprintf(consoleURLRegex, eip)) + return err } - - return &OpenshiftSncResultsMetadata{ - Username: username, - PrivateKey: privateKey, - Host: host, - Kubeconfig: kubeconfig, - KubeadminPass: kubeAdminPass, - SpotPrice: r.allocationData.SpotPrice, - ConsoleUrl: consoleURL, - }, nil + ctx.Export(fmt.Sprintf("%s-%s", *r.prefix, apiSNC.OutputKubeconfig), + pulumi.ToSecret(kubeconfig)) + return nil } // security group for Openshift @@ -349,12 +266,12 @@ func securityGroups(ctx *pulumi.Context, mCtx *mc.Context, prefix *string, vpc *ec2.Vpc) (pulumi.StringArray, error) { // Create SG with ingress rules sg, err := securityGroup.SGRequest{ - Name: resourcesUtil.GetResourceName(*prefix, awsOCPSNCID, "sg"), + Name: resourcesUtil.GetResourceName(*prefix, apiSNC.OCPSNCID, "sg"), VPC: vpc, - Description: fmt.Sprintf("sg for %s", awsOCPSNCID), + Description: fmt.Sprintf("sg for %s", apiSNC.OCPSNCID), IngressRules: []securityGroup.IngressRules{securityGroup.SSH_TCP, - {Description: "Console", FromPort: portHTTPS, ToPort: portHTTPS, Protocol: "tcp"}, - {Description: "API", FromPort: portAPI, ToPort: portAPI, Protocol: "tcp"}}, + {Description: "Console", FromPort: apiSNC.PortHTTPS, ToPort: apiSNC.PortHTTPS, Protocol: "tcp"}, + {Description: "API", FromPort: apiSNC.PortAPI, ToPort: apiSNC.PortAPI, Protocol: "tcp"}}, }.Create(ctx, mCtx) if err != nil { return nil, err @@ -403,7 +320,7 @@ func (r *openshiftSNCRequest) userData(ctx *pulumi.Context, // KubeAdmin pass kaPassword, err := security.CreatePassword(ctx, resourcesUtil.GetResourceName( - *r.prefix, awsOCPSNCID, "kubeadminpassword")) + *r.prefix, apiSNC.OCPSNCID, "kubeadminpassword")) if err != nil { return nil, nil, nil, nil, err } @@ -415,7 +332,7 @@ func (r *openshiftSNCRequest) userData(ctx *pulumi.Context, // Developer pass devPassword, err := security.CreatePassword(ctx, resourcesUtil.GetResourceName( - *r.prefix, awsOCPSNCID, "devpassword")) + *r.prefix, apiSNC.OCPSNCID, "devpassword")) if err != nil { return nil, nil, nil, nil, err } @@ -426,7 +343,7 @@ func (r *openshiftSNCRequest) userData(ctx *pulumi.Context, dependecies = append(dependecies, devPassParam) ccB64 := pulumi.All(newPublicKey, lbEIP).ApplyT( func(args []interface{}) (string, error) { - ccB64, err := cloudConfig(dataValues{ + ccB64, err := apiSNC.CloudConfig(apiSNC.DataValues{ Username: amiUserDefault, PubKey: args[0].(string), PublicIP: args[1].(string), @@ -442,6 +359,8 @@ func (r *openshiftSNCRequest) userData(ctx *pulumi.Context, func kubeconfig(ctx *pulumi.Context, prefix *string, c *compute.Compute, mk *tls.PrivateKey, + ocpVersion string, + disableClusterReadiness bool, ) (pulumi.StringOutput, error) { // Once the cluster setup is comleted we // get the kubeconfig file from the host running the cluster @@ -452,7 +371,7 @@ func kubeconfig(ctx *pulumi.Context, sshReadyCmd, err := c.RunCommand(ctx, command.CommandPing, compute.LoggingCmdStd, - fmt.Sprintf("%s-ssh-readiness", *prefix), awsOCPSNCID, + fmt.Sprintf("%s-ssh-readiness", *prefix), apiSNC.OCPSNCID, mk, amiUserDefault, nil, c.Dependencies) if err != nil { return pulumi.StringOutput{}, err @@ -460,18 +379,18 @@ func kubeconfig(ctx *pulumi.Context, // Check cluster is ready ocpReadyCmd, err := c.RunCommand(ctx, - commandCrcReadiness, + util.If(disableClusterReadiness, apiSNC.CommandKubeconfigExists, apiSNC.CommandCrcReadiness), compute.LoggingCmdStd, - fmt.Sprintf("%s-ocp-readiness", *prefix), awsOCPSNCID, + fmt.Sprintf("%s-ocp-readiness", *prefix), apiSNC.OCPSNCID, mk, amiUserDefault, nil, []pulumi.Resource{sshReadyCmd}) if err != nil { return pulumi.StringOutput{}, err } // Check ocp-cluster-ca.service succeeds ocpCaRotatedCmd, err := c.RunCommand(ctx, - commandCaServiceRan, + apiSNC.CommandCaServiceRan(ocpVersion), compute.LoggingCmdStd, - fmt.Sprintf("%s-ocp-ca-rotated", *prefix), awsOCPSNCID, + fmt.Sprintf("%s-ocp-ca-rotated", *prefix), apiSNC.OCPSNCID, mk, amiUserDefault, nil, []pulumi.Resource{ocpReadyCmd}) if err != nil { return pulumi.StringOutput{}, err @@ -482,7 +401,7 @@ func kubeconfig(ctx *pulumi.Context, getKC, err := c.RunCommand(ctx, getKCCmd, compute.NoLoggingCmdStd, - fmt.Sprintf("%s-kubeconfig", *prefix), awsOCPSNCID, mk, amiUserDefault, + fmt.Sprintf("%s-kubeconfig", *prefix), apiSNC.OCPSNCID, mk, amiUserDefault, nil, []pulumi.Resource{ocpCaRotatedCmd}) if err != nil { return pulumi.StringOutput{}, err @@ -495,16 +414,3 @@ func kubeconfig(ctx *pulumi.Context, }).(pulumi.StringOutput) return kubeconfig, nil } - -func getResultOutput(name string, sr auto.UpResult, prefix *string) (string, error) { - key := fmt.Sprintf("%s-%s", *prefix, name) - output, ok := sr.Outputs[key] - if !ok { - return "", fmt.Errorf("output not found: %s", key) - } - value, ok := output.Value.(string) - if !ok { - return "", fmt.Errorf("output for %s is not a string", key) - } - return value, nil -} diff --git a/pkg/provider/aws/action/windows/windows.go b/pkg/provider/aws/action/windows/windows.go index bd67046c6..6be9c264f 100644 --- a/pkg/provider/aws/action/windows/windows.go +++ b/pkg/provider/aws/action/windows/windows.go @@ -26,10 +26,10 @@ import ( amiSVC "github.com/redhat-developer/mapt/pkg/provider/aws/services/ec2/ami" "github.com/redhat-developer/mapt/pkg/provider/aws/services/ec2/keypair" securityGroup "github.com/redhat-developer/mapt/pkg/provider/aws/services/ec2/security-group" - cloudConfigWindowsServer "github.com/redhat-developer/mapt/pkg/provider/util/cloud-config/windows-server" "github.com/redhat-developer/mapt/pkg/provider/util/command" "github.com/redhat-developer/mapt/pkg/provider/util/output" "github.com/redhat-developer/mapt/pkg/provider/util/security" + cloudConfigWindowsServer "github.com/redhat-developer/mapt/pkg/target/host/windows-server" "github.com/redhat-developer/mapt/pkg/util" "github.com/redhat-developer/mapt/pkg/util/logging" resourcesUtil "github.com/redhat-developer/mapt/pkg/util/resources" diff --git a/pkg/provider/azure/action/kind/kind.go b/pkg/provider/azure/action/kind/kind.go index dac0ebf55..2e6931bff 100644 --- a/pkg/provider/azure/action/kind/kind.go +++ b/pkg/provider/azure/action/kind/kind.go @@ -19,7 +19,7 @@ import ( virtualmachine "github.com/redhat-developer/mapt/pkg/provider/azure/modules/virtual-machine" securityGroup "github.com/redhat-developer/mapt/pkg/provider/azure/services/network/security-group" "github.com/redhat-developer/mapt/pkg/provider/util/command" - utilKind "github.com/redhat-developer/mapt/pkg/targets/service/kind" + utilKind "github.com/redhat-developer/mapt/pkg/target/service/kind" "github.com/redhat-developer/mapt/pkg/util" resourcesUtil "github.com/redhat-developer/mapt/pkg/util/resources" ) diff --git a/pkg/provider/azure/action/rhel-ai/rhelai.go b/pkg/provider/azure/action/rhel-ai/rhelai.go index 6b346bd37..13ae7060e 100644 --- a/pkg/provider/azure/action/rhel-ai/rhelai.go +++ b/pkg/provider/azure/action/rhel-ai/rhelai.go @@ -8,7 +8,7 @@ import ( azureLinux "github.com/redhat-developer/mapt/pkg/provider/azure/action/linux" "github.com/redhat-developer/mapt/pkg/provider/azure/data" "github.com/redhat-developer/mapt/pkg/provider/util/command" - apiRHELAI "github.com/redhat-developer/mapt/pkg/targets/host/rhelai" + apiRHELAI "github.com/redhat-developer/mapt/pkg/target/host/rhelai" "github.com/redhat-developer/mapt/pkg/util/logging" ) diff --git a/pkg/provider/azure/action/rhel/rhel.go b/pkg/provider/azure/action/rhel/rhel.go index 0b3f58ff9..88512ed3b 100644 --- a/pkg/provider/azure/action/rhel/rhel.go +++ b/pkg/provider/azure/action/rhel/rhel.go @@ -7,7 +7,7 @@ import ( azureLinux "github.com/redhat-developer/mapt/pkg/provider/azure/action/linux" "github.com/redhat-developer/mapt/pkg/provider/azure/data" "github.com/redhat-developer/mapt/pkg/provider/util/command" - rhelApi "github.com/redhat-developer/mapt/pkg/targets/host/rhel" + rhelApi "github.com/redhat-developer/mapt/pkg/target/host/rhel" "github.com/redhat-developer/mapt/pkg/util/logging" ) diff --git a/pkg/provider/util/cloud-config/fedora/cloud-config b/pkg/target/host/fedora/cloud-config similarity index 100% rename from pkg/provider/util/cloud-config/fedora/cloud-config rename to pkg/target/host/fedora/cloud-config diff --git a/pkg/provider/util/cloud-config/fedora/fedora.go b/pkg/target/host/fedora/fedora.go similarity index 100% rename from pkg/provider/util/cloud-config/fedora/fedora.go rename to pkg/target/host/fedora/fedora.go diff --git a/pkg/targets/host/rhel/cloud-config-base b/pkg/target/host/rhel/cloud-config-base similarity index 100% rename from pkg/targets/host/rhel/cloud-config-base rename to pkg/target/host/rhel/cloud-config-base diff --git a/pkg/targets/host/rhel/cloud-config-snc b/pkg/target/host/rhel/cloud-config-snc similarity index 100% rename from pkg/targets/host/rhel/cloud-config-snc rename to pkg/target/host/rhel/cloud-config-snc diff --git a/pkg/targets/host/rhel/cloud-config.go b/pkg/target/host/rhel/cloud-config.go similarity index 100% rename from pkg/targets/host/rhel/cloud-config.go rename to pkg/target/host/rhel/cloud-config.go diff --git a/pkg/targets/host/rhelai/api.go b/pkg/target/host/rhelai/api.go similarity index 100% rename from pkg/targets/host/rhelai/api.go rename to pkg/target/host/rhelai/api.go diff --git a/pkg/provider/util/cloud-config/windows-server/bootstrap.ps1 b/pkg/target/host/windows-server/bootstrap.ps1 similarity index 100% rename from pkg/provider/util/cloud-config/windows-server/bootstrap.ps1 rename to pkg/target/host/windows-server/bootstrap.ps1 diff --git a/pkg/provider/util/cloud-config/windows-server/windows-server.go b/pkg/target/host/windows-server/windows-server.go similarity index 100% rename from pkg/provider/util/cloud-config/windows-server/windows-server.go rename to pkg/target/host/windows-server/windows-server.go diff --git a/pkg/targets/service/kind/api.go b/pkg/target/service/kind/api.go similarity index 100% rename from pkg/targets/service/kind/api.go rename to pkg/target/service/kind/api.go diff --git a/pkg/targets/service/kind/cloud-config b/pkg/target/service/kind/cloud-config similarity index 100% rename from pkg/targets/service/kind/cloud-config rename to pkg/target/service/kind/cloud-config diff --git a/pkg/targets/service/kind/cloud-config.go b/pkg/target/service/kind/cloud-config.go similarity index 100% rename from pkg/targets/service/kind/cloud-config.go rename to pkg/target/service/kind/cloud-config.go diff --git a/pkg/targets/service/kind/cloud-config_test.go b/pkg/target/service/kind/cloud-config_test.go similarity index 100% rename from pkg/targets/service/kind/cloud-config_test.go rename to pkg/target/service/kind/cloud-config_test.go diff --git a/pkg/targets/service/kind/util.go b/pkg/target/service/kind/util.go similarity index 100% rename from pkg/targets/service/kind/util.go rename to pkg/target/service/kind/util.go diff --git a/pkg/target/service/snc/api.go b/pkg/target/service/snc/api.go new file mode 100644 index 000000000..aab097e3f --- /dev/null +++ b/pkg/target/service/snc/api.go @@ -0,0 +1,62 @@ +package snc + +import ( + "fmt" + + cr "github.com/redhat-developer/mapt/pkg/provider/api/compute-request" + spotTypes "github.com/redhat-developer/mapt/pkg/provider/api/spot" + "github.com/redhat-developer/mapt/pkg/util" + "golang.org/x/mod/semver" +) + +var ( + StackName = "stackOpenshiftSNC" + OCPSNCID = "snc" + + consoleURLRegex = "https://console-openshift-console.apps.%s.nip.io" + + OutputHost = "aosHost" + OutputUsername = "aosUsername" + OutputUserPrivateKey = "aosPrivatekey" + OutputKubeconfig = "aosKubeconfig" + OutputKubeAdminPass = "aosKubeAdminPasss" + OutputDeveloperPass = "aosDeveloperPass" + + PortHTTPS = 443 + PortAPI = 6443 +) + +var ( + ClientKubeconfigPath = "/opt/crc/kubeconfig" + ContextAdminStarterVersion = "4.20.8" + contextAdmin = "admin" + contextSystemAdmin = "system:admin" + CommandKubeconfigExists = fmt.Sprintf("while [ ! -f %s ]; do sleep 5; done", ClientKubeconfigPath) + CommandCrcReadiness = "while [ ! -f /tmp/.crc-cluster-ready ]; do sleep 5; done" + commandCaServiceRan = "sudo bash -c 'until oc get node --kubeconfig /opt/kubeconfig --context %s || oc get node --kubeconfig /opt/crc/kubeconfig --context system:admin; do sleep 5; done'" +) + +func CommandCaServiceRan(version string) string { + return fmt.Sprintf(commandCaServiceRan, util.If(semver.Compare(version, ContextAdminStarterVersion) < 0, contextSystemAdmin, contextAdmin)) +} + +type SNCArgs struct { + Prefix string + ComputeRequest *cr.ComputeRequestArgs + Version string + DisableClusterReadiness bool + Arch string + PullSecretFile string + Spot *spotTypes.SpotArgs + Timeout string +} + +type SNCResults struct { + Username string `json:"username"` + PrivateKey string `json:"private_key"` + Host string `json:"host"` + Kubeconfig string `json:"kubeconfig"` + KubeadminPass string `json:"kubeadmin_pass"` + SpotPrice *float64 `json:"spot_price,omitempty"` + ConsoleUrl string `json:"console_url,omitempty"` +} diff --git a/pkg/provider/aws/action/openshift-snc/cloud-config b/pkg/target/service/snc/cloud-config similarity index 100% rename from pkg/provider/aws/action/openshift-snc/cloud-config rename to pkg/target/service/snc/cloud-config diff --git a/pkg/provider/aws/action/openshift-snc/cloudconfig.go b/pkg/target/service/snc/cloudconfig.go similarity index 66% rename from pkg/provider/aws/action/openshift-snc/cloudconfig.go rename to pkg/target/service/snc/cloudconfig.go index ed6f5f42f..e7438b4bf 100644 --- a/pkg/provider/aws/action/openshift-snc/cloudconfig.go +++ b/pkg/target/service/snc/cloudconfig.go @@ -1,4 +1,4 @@ -package openshiftsnc +package snc import ( _ "embed" @@ -7,7 +7,7 @@ import ( "github.com/redhat-developer/mapt/pkg/util/file" ) -type dataValues struct { +type DataValues struct { // user auth information Username string PubKey string @@ -20,12 +20,10 @@ type dataValues struct { } //go:embed cloud-config -var CloudConfig []byte +var CloudConfigFile []byte -var cloudConfigRequiredProfiles = []string{"arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"} - -func cloudConfig(data dataValues) (*string, error) { - templateConfig := string(CloudConfig[:]) +func CloudConfig(data DataValues) (*string, error) { + templateConfig := string(CloudConfigFile[:]) cc, err := file.Template(data, templateConfig) if err != nil { return nil, err diff --git a/pkg/target/service/snc/snc.go b/pkg/target/service/snc/snc.go new file mode 100644 index 000000000..c9bab681c --- /dev/null +++ b/pkg/target/service/snc/snc.go @@ -0,0 +1,83 @@ +package snc + +import ( + "fmt" + + "github.com/pulumi/pulumi/sdk/v3/go/auto" + "github.com/redhat-developer/mapt/pkg/provider/util/output" + "github.com/redhat-developer/mapt/pkg/util/logging" +) + +func Results(stackResult auto.UpResult, prefix *string, + resultOutputPath string, spotPrice *float64, + disableClusterReadiness bool) (*SNCResults, error) { + username, err := getResultOutput(OutputUsername, stackResult, prefix) + if err != nil { + return nil, err + } + privateKey, err := getResultOutput(OutputUserPrivateKey, stackResult, prefix) + if err != nil { + return nil, err + } + host, err := getResultOutput(OutputHost, stackResult, prefix) + if err != nil { + return nil, err + } + kubeAdminPass, err := getResultOutput(OutputKubeAdminPass, stackResult, prefix) + if err != nil { + return nil, err + } + kubeconfig := "" + if !disableClusterReadiness { + kubeconfig, err = getResultOutput(OutputKubeconfig, stackResult, prefix) + if err != nil { + return nil, err + } + } + + hostIPKey := fmt.Sprintf("%s-%s", *prefix, OutputHost) + results := map[string]string{ + fmt.Sprintf("%s-%s", *prefix, OutputUsername): "username", + fmt.Sprintf("%s-%s", *prefix, OutputUserPrivateKey): "id_rsa", + hostIPKey: "host", + fmt.Sprintf("%s-%s", *prefix, OutputKubeconfig): "kubeconfig", + fmt.Sprintf("%s-%s", *prefix, OutputKubeAdminPass): "kubeadmin_pass", + fmt.Sprintf("%s-%s", *prefix, OutputDeveloperPass): "developer_pass", + } + + if len(resultOutputPath) == 0 { + logging.Warn("conn-details-output flag not set; skipping writing output files.") + } else { + if err := output.Write(stackResult, resultOutputPath, results); err != nil { + return nil, fmt.Errorf("failed to write results: %w", err) + } + } + + consoleURL := fmt.Sprintf(consoleURLRegex, host) + if eip, ok := stackResult.Outputs[hostIPKey].Value.(string); ok { + fmt.Printf("Cluster has been started you can access console at: %s.\n", fmt.Sprintf(consoleURLRegex, eip)) + } + + return &SNCResults{ + Username: username, + PrivateKey: privateKey, + Host: host, + Kubeconfig: kubeconfig, + KubeadminPass: kubeAdminPass, + SpotPrice: spotPrice, + ConsoleUrl: consoleURL, + }, nil +} + +func getResultOutput(name string, sr auto.UpResult, prefix *string) (string, error) { + key := fmt.Sprintf("%s-%s", *prefix, name) + output, ok := sr.Outputs[key] + if !ok { + return "", fmt.Errorf("output not found: %s", key) + } + value, ok := output.Value.(string) + if !ok { + return "", fmt.Errorf("output for %s is not a string", key) + } + return value, nil +}