Skip to content

Commit 8b30658

Browse files
CR-9252 skip-ingress flag (#252)
* added --skip-ingress flag * skip ingress class check * - added Host when creating ingress - added info message for installing with no ingress * modify info message * validate ingress host and parse host name for ingress manifest * bump * cyclomtic complexity fix * set runtime version when in dev mode inside Download func * refactor RunRuntimeInstall * handle err * generate docs * improvements after review * add to info msg * improvements * indentations * bump * gen docs * added condition to getting ingress from controller
1 parent 8f1186c commit 8b30658

File tree

10 files changed

+171
-57
lines changed

10 files changed

+171
-57
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION=v0.0.231
1+
VERSION=v0.0.232
22

33
OUT_DIR=dist
44
YEAR?=$(shell date +"%Y")

cmd/commands/common.go

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ func IsValidName(s string) (bool, error) {
8484
return regexp.MatchString(`^[a-z]([-a-z0-9]{0,61}[a-z0-9])?$`, s)
8585
}
8686

87+
func isValidIngressHost(ingressHost string) (bool, error) {
88+
return regexp.MatchString(`^(http|https)://`, ingressHost)
89+
}
90+
8791
func getControllerName(s string) string {
8892
split := strings.Split(s, "/")
8993
return split[1]
@@ -290,7 +294,7 @@ func ensureGitPAT(cmd *cobra.Command, opts *RuntimeInstallOptions) error {
290294
var err error
291295
tokenFromFlag := opts.GitIntegrationRegistrationOpts.Token
292296

293-
if tokenFromFlag == "" {
297+
if tokenFromFlag == "" {
294298
if !store.Get().Silent {
295299
err = getGitPATFromUserInput(cmd, opts)
296300
if err != nil {
@@ -442,7 +446,7 @@ func getKubeContextNameFromUserSelect(cmd *cobra.Command, kubeContextName *strin
442446

443447
func getIngressHostFromUserInput(ctx context.Context, opts *RuntimeInstallOptions, foundIngressHost string) error {
444448
ingressHostPrompt := promptui.Prompt{
445-
Label: "Ingress host",
449+
Label: "Ingress host",
446450
Default: foundIngressHost,
447451
Pointer: promptui.PipeCursor,
448452
}
@@ -452,11 +456,28 @@ func getIngressHostFromUserInput(ctx context.Context, opts *RuntimeInstallOption
452456
return err
453457
}
454458

459+
err = validateIngressHost(ingressHostInput)
460+
if err != nil {
461+
return err
462+
}
463+
455464
opts.IngressHost = ingressHostInput
465+
opts.HostName = util.ParseHostNameFromIngressHost(ingressHostInput)
456466

457467
return nil
458468
}
459469

470+
func validateIngressHost(ingressHost string) error {
471+
isValid, err := isValidIngressHost(ingressHost)
472+
if err != nil {
473+
err = fmt.Errorf("could not verify ingress host: %w", err)
474+
} else if !isValid {
475+
err = fmt.Errorf("ingress host must begin with protocol 'http://' or 'https://'")
476+
}
477+
478+
return err
479+
}
480+
460481
func setIngressHost(ctx context.Context, opts *RuntimeInstallOptions) error {
461482
log.G(ctx).Info("Retrieving ingress controller info from your cluster...\n")
462483

@@ -467,22 +488,30 @@ func setIngressHost(ctx context.Context, opts *RuntimeInstallOptions) error {
467488
}
468489

469490
var foundIngressHost string
491+
var foundHostName string
470492

471493
for _, s := range ServicesList.Items {
472494
if s.ObjectMeta.Name == opts.IngressController && s.Spec.Type == "LoadBalancer" {
473-
ingress := s.Status.LoadBalancer.Ingress[0]
474-
if ingress.Hostname != "" {
475-
foundIngressHost = fmt.Sprintf("https://%s", ingress.Hostname)
476-
break
477-
} else {
478-
foundIngressHost = fmt.Sprintf("https://%s", ingress.IP)
479-
break
495+
if len(s.Status.LoadBalancer.Ingress) > 0 {
496+
ingress := s.Status.LoadBalancer.Ingress[0]
497+
if ingress.Hostname != "" {
498+
foundHostName = ingress.Hostname
499+
break
500+
} else {
501+
foundHostName = ingress.IP
502+
break
503+
}
480504
}
481505
}
482506
}
483507

508+
if foundHostName != "" {
509+
foundIngressHost = fmt.Sprintf("https://%s", foundHostName)
510+
}
511+
484512
if store.Get().Silent {
485513
log.G(ctx).Warnf("Using ingress host %s", foundIngressHost)
514+
opts.HostName = foundHostName
486515
opts.IngressHost = foundIngressHost
487516
} else {
488517
err = getIngressHostFromUserInput(ctx, opts, foundIngressHost)

cmd/commands/git-source.go

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ type (
5959
CreateDemoResources bool
6060
Exclude string
6161
Include string
62+
HostName string
6263
IngressHost string
6364
IngressClass string
6465
}
@@ -87,6 +88,7 @@ type (
8788
runtimeName string
8889
gsCloneOpts *git.CloneOptions
8990
gsFs fs.FS
91+
hostName string
9092
ingressHost string
9193
ingressClass string
9294
}
@@ -245,6 +247,7 @@ func createDemoResources(ctx context.Context, opts *GitSourceCreateOptions, gsRe
245247
runtimeName: opts.RuntimeName,
246248
gsCloneOpts: opts.GsCloneOpts,
247249
gsFs: gsFs,
250+
hostName: opts.HostName,
248251
ingressHost: opts.IngressHost,
249252
ingressClass: opts.IngressClass,
250253
})
@@ -686,22 +689,23 @@ func createDemoWorkflowTemplate(gsFs fs.FS) error {
686689
}
687690

688691
func createGithubExamplePipeline(opts *gitSourceGithubExampleOptions) error {
689-
// Create an ingress that will manage external access to the github eventsource service
690-
var err error
691-
ingress := createGithubExampleIngress(opts.ingressClass)
692-
ingressFilePath := opts.gsFs.Join(opts.gsCloneOpts.Path(), store.Get().GithubExampleIngressFileName)
692+
if !store.Get().SkipIngress {
693+
// Create an ingress that will manage external access to the github eventsource service
694+
ingress := createGithubExampleIngress(opts.ingressClass, opts.ingressHost, opts.hostName)
695+
ingressFilePath := opts.gsFs.Join(opts.gsCloneOpts.Path(), store.Get().GithubExampleIngressFileName)
693696

694-
ingressRedundanded, err := cleanUpFieldsIngressGithub(&ingress)
697+
ingressRedundanded, err := cleanUpFieldsIngressGithub(&ingress)
695698

696-
if err != nil {
697-
err = opts.gsCloneOpts.FS.WriteYamls(ingressFilePath, ingress)
699+
if err != nil {
700+
err = opts.gsCloneOpts.FS.WriteYamls(ingressFilePath, ingress)
698701

699-
} else {
700-
err = opts.gsCloneOpts.FS.WriteYamls(ingressFilePath, ingressRedundanded)
701-
}
702+
} else {
703+
err = opts.gsCloneOpts.FS.WriteYamls(ingressFilePath, ingressRedundanded)
704+
}
702705

703-
if err != nil {
704-
return fmt.Errorf("failed to write yaml of github example ingress. Error: %w", err)
706+
if err != nil {
707+
return fmt.Errorf("failed to write yaml of github example ingress. Error: %w", err)
708+
}
705709
}
706710

707711
// Create a github eventsource that will listen to push events in the git source repo
@@ -740,10 +744,11 @@ func createGithubExamplePipeline(opts *gitSourceGithubExampleOptions) error {
740744
return nil
741745
}
742746

743-
func createGithubExampleIngress(ingressClass string) *netv1.Ingress {
747+
func createGithubExampleIngress(ingressClass string, ingressHost string, hostName string) *netv1.Ingress {
744748
return ingressutil.CreateIngress(&ingressutil.CreateIngressOptions{
745749
Name: store.Get().CodefreshDeliveryPipelines,
746750
IngressClassName: ingressClass,
751+
Host: hostName,
747752
Paths: []ingressutil.IngressPath{
748753
{
749754
Path: store.Get().GithubExampleEventSourceEndpointPath,

cmd/commands/runtime.go

Lines changed: 93 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ type (
7272
RuntimeName string
7373
RuntimeToken string
7474
RuntimeStoreIV string
75+
HostName string
7576
IngressHost string
7677
IngressClass string
7778
IngressController string
@@ -222,6 +223,7 @@ func NewRuntimeInstallCommand() *cobra.Command {
222223
cmd.Flags().BoolVar(&installationOpts.SkipClusterChecks, "skip-cluster-checks", false, "Skips the cluster's checks")
223224
cmd.Flags().DurationVar(&store.Get().WaitTimeout, "wait-timeout", store.Get().WaitTimeout, "How long to wait for the runtime components to be ready")
224225
cmd.Flags().StringVar(&gitIntegrationCreationOpts.APIURL, "provider-api-url", "", "Git provider API url")
226+
cmd.Flags().BoolVar(&store.Get().SkipIngress, "skip-ingress", false, "Skips the creation of ingress resources")
225227
cmd.Flags().BoolVar(&store.Get().BypassIngressClassCheck, "bypass-ingress-class-check", false, "Disables the ingress class check during pre-installation")
226228

227229
installationOpts.InsCloneOpts = apu.AddCloneFlags(cmd, &apu.CloneFlagsOptions{
@@ -406,7 +408,7 @@ func ensureIngressHost(cmd *cobra.Command, opts *RuntimeInstallOptions) error {
406408
}
407409

408410
func ensureIngressClass(ctx context.Context, opts *RuntimeInstallOptions) error {
409-
if store.Get().BypassIngressClassCheck {
411+
if store.Get().BypassIngressClassCheck || store.Get().SkipIngress {
410412
return nil
411413
}
412414

@@ -507,22 +509,12 @@ func RunRuntimeInstall(ctx context.Context, opts *RuntimeInstallOptions) error {
507509

508510
handleCliStep(reporter.InstallPhaseStart, "Runtime installation phase started", nil, true)
509511

510-
rt, err := runtime.Download(opts.Version, opts.RuntimeName)
511-
handleCliStep(reporter.InstallStepDownloadRuntimeDefinition, "Downloading runtime definition", err, true)
512+
rt, server, err := runtimeInstallPreparations(opts)
512513
if err != nil {
513-
return fmt.Errorf("failed to download runtime definition: %w", err)
514-
}
515-
516-
runtimeVersion := "v99.99.99"
517-
if rt.Spec.Version != nil { // in dev mode
518-
runtimeVersion = rt.Spec.Version.String()
514+
return err
519515
}
520516

521-
server, err := util.CurrentServer()
522-
handleCliStep(reporter.InstallStepGetServerAddress, "Getting current server address", err, true)
523-
if err != nil {
524-
return fmt.Errorf("failed to get current server address: %w", err)
525-
}
517+
runtimeVersion := rt.Spec.Version.String()
526518

527519
componentNames := getComponents(rt, opts)
528520

@@ -585,6 +577,68 @@ func RunRuntimeInstall(ctx context.Context, opts *RuntimeInstallOptions) error {
585577
return util.DecorateErrorWithDocsLink(fmt.Errorf("failed to create codefresh-cm: %w", err))
586578
}
587579

580+
err = createRuntimeComponents(ctx, opts, rt)
581+
if err != nil {
582+
return err
583+
}
584+
585+
err = createGitSources(ctx, opts)
586+
if err != nil {
587+
return err
588+
}
589+
590+
timeoutErr := intervalCheckIsRuntimePersisted(ctx, opts.RuntimeName)
591+
handleCliStep(reporter.InstallStepCompleteRuntimeInstallation, "Completing runtime installation", timeoutErr, true)
592+
if timeoutErr != nil {
593+
return util.DecorateErrorWithDocsLink(fmt.Errorf("failed to complete installation: %w", timeoutErr))
594+
}
595+
596+
err = createGitIntegration(ctx, opts)
597+
if err != nil {
598+
return err
599+
}
600+
601+
installationSuccessMsg := fmt.Sprintf("Runtime '%s' installed successfully", opts.RuntimeName)
602+
skipIngressInfoMsg := fmt.Sprintf(
603+
`To complete the installation:
604+
1. Configure your cluster's routing service with path to '/%s' and '%s'
605+
2. Create and register Git integration using the commands:
606+
cf integration git add default --runtime %s --api-url %s
607+
cf integration git register default --runtime %s --token <AUTHENTICATION_TOKEN>`,
608+
store.Get().AppProxyIngressPath,
609+
store.Get().GithubExampleEventSourceEndpointPath,
610+
opts.RuntimeName,
611+
opts.GitIntegrationCreationOpts.APIURL,
612+
opts.RuntimeName)
613+
614+
summaryArr = append(summaryArr, summaryLog{installationSuccessMsg, Info})
615+
if store.Get().SkipIngress {
616+
summaryArr = append(summaryArr, summaryLog{skipIngressInfoMsg, Info})
617+
}
618+
619+
log.G(ctx).Infof(installationSuccessMsg)
620+
621+
return nil
622+
}
623+
624+
func runtimeInstallPreparations(opts *RuntimeInstallOptions) (*runtime.Runtime, string, error) {
625+
rt, err := runtime.Download(opts.Version, opts.RuntimeName)
626+
handleCliStep(reporter.InstallStepDownloadRuntimeDefinition, "Downloading runtime definition", err, true)
627+
if err != nil {
628+
return nil, "", fmt.Errorf("failed to download runtime definition: %w", err)
629+
}
630+
631+
server, err := util.CurrentServer()
632+
handleCliStep(reporter.InstallStepGetServerAddress, "Getting current server address", err, true)
633+
if err != nil {
634+
return nil, "", fmt.Errorf("failed to get current server address: %w", err)
635+
}
636+
637+
return rt, server, nil
638+
}
639+
640+
func createRuntimeComponents(ctx context.Context, opts *RuntimeInstallOptions, rt *runtime.Runtime) error {
641+
var err error
588642
for _, component := range rt.Spec.Components {
589643
infoStr := fmt.Sprintf("Creating component '%s'", component.Name)
590644
log.G(ctx).Infof(infoStr)
@@ -606,13 +660,18 @@ func RunRuntimeInstall(ctx context.Context, opts *RuntimeInstallOptions) error {
606660
return util.DecorateErrorWithDocsLink(fmt.Errorf("failed to install components: %s", err))
607661
}
608662

663+
return nil
664+
}
665+
666+
func createGitSources(ctx context.Context, opts *RuntimeInstallOptions) error {
609667
gitSrcMessage := fmt.Sprintf("Creating git source `%s`", store.Get().GitSourceName)
610-
err = RunGitSourceCreate(ctx, &GitSourceCreateOptions{
668+
err := RunGitSourceCreate(ctx, &GitSourceCreateOptions{
611669
InsCloneOpts: opts.InsCloneOpts,
612670
GsCloneOpts: opts.GsCloneOpts,
613671
GsName: store.Get().GitSourceName,
614672
RuntimeName: opts.RuntimeName,
615673
CreateDemoResources: opts.InstallDemoResources,
674+
HostName: opts.HostName,
616675
IngressHost: opts.IngressHost,
617676
IngressClass: opts.IngressClass,
618677
})
@@ -642,28 +701,30 @@ func RunRuntimeInstall(ctx context.Context, opts *RuntimeInstallOptions) error {
642701
return util.DecorateErrorWithDocsLink(fmt.Errorf("failed to create `%s`: %w", store.Get().MarketplaceGitSourceName, err))
643702
}
644703

645-
timeoutErr := intervalCheckIsRuntimePersisted(ctx, opts.RuntimeName)
646-
handleCliStep(reporter.InstallStepCompleteRuntimeInstallation, "Completing runtime installation", timeoutErr, true)
647-
if timeoutErr != nil {
648-
return util.DecorateErrorWithDocsLink(fmt.Errorf("failed to complete installation: %w", timeoutErr))
649-
}
704+
return nil
705+
}
706+
707+
func createGitIntegration(ctx context.Context, opts *RuntimeInstallOptions) error {
708+
var gitIntgErr error
709+
var appendToLog bool
650710

651-
gitIntgErr := addDefaultGitIntegration(ctx, opts.RuntimeName, opts.GitIntegrationCreationOpts)
652-
handleCliStep(reporter.InstallStepCreateDefaultGitIntegration, "Creating a default git integration", gitIntgErr, true)
711+
if !store.Get().SkipIngress {
712+
gitIntgErr = addDefaultGitIntegration(ctx, opts.RuntimeName, opts.GitIntegrationCreationOpts)
713+
appendToLog = true
714+
}
715+
handleCliStep(reporter.InstallStepCreateDefaultGitIntegration, "Creating a default git integration", gitIntgErr, appendToLog)
653716
if gitIntgErr != nil {
654717
return util.DecorateErrorWithDocsLink(fmt.Errorf("failed to create default git integration: %w", gitIntgErr))
655718
}
656719

657-
gitIntgErr = registerUserToGitIntegration(ctx, opts.RuntimeName, opts.GitIntegrationRegistrationOpts)
658-
handleCliStep(reporter.InstallStepRegisterToDefaultGitIntegration, "Registering user to the default git integration", gitIntgErr, true)
720+
if !store.Get().SkipIngress {
721+
gitIntgErr = registerUserToGitIntegration(ctx, opts.RuntimeName, opts.GitIntegrationRegistrationOpts)
722+
}
723+
handleCliStep(reporter.InstallStepRegisterToDefaultGitIntegration, "Registering user to the default git integration", gitIntgErr, appendToLog)
659724
if gitIntgErr != nil {
660725
return util.DecorateErrorWithDocsLink(fmt.Errorf("failed to register user to the default git integration: %w", gitIntgErr))
661726
}
662727

663-
installationSuccessMsg := fmt.Sprintf("Runtime '%s' installed successfully", opts.RuntimeName)
664-
summaryArr = append(summaryArr, summaryLog{installationSuccessMsg, Info})
665-
log.G(ctx).Infof(installationSuccessMsg)
666-
667728
return nil
668729
}
669730

@@ -702,7 +763,7 @@ func registerUserToGitIntegration(ctx context.Context, runtime string, opts *apm
702763

703764
func installComponents(ctx context.Context, opts *RuntimeInstallOptions, rt *runtime.Runtime) error {
704765
var err error
705-
if opts.IngressHost != "" {
766+
if opts.IngressHost != "" && !store.Get().SkipIngress {
706767
if err = createWorkflowsIngress(ctx, opts, rt); err != nil {
707768
return fmt.Errorf("failed to patch Argo-Workflows ingress: %w", err)
708769
}
@@ -1285,6 +1346,7 @@ func createWorkflowsIngress(ctx context.Context, opts *RuntimeInstallOptions, rt
12851346
Name: rt.Name + store.Get().WorkflowsIngressName,
12861347
Namespace: rt.Namespace,
12871348
IngressClassName: opts.IngressClass,
1349+
Host: opts.HostName,
12881350
Annotations: map[string]string{
12891351
"ingress.kubernetes.io/protocol": "https",
12901352
"ingress.kubernetes.io/rewrite-target": "/$2",
@@ -1367,11 +1429,12 @@ func configureAppProxy(ctx context.Context, opts *RuntimeInstallOptions, rt *run
13671429
},
13681430
})
13691431

1370-
if opts.IngressHost != "" {
1432+
if opts.IngressHost != "" && !store.Get().SkipIngress {
13711433
ingress := ingressutil.CreateIngress(&ingressutil.CreateIngressOptions{
13721434
Name: rt.Name + store.Get().AppProxyIngressName,
13731435
Namespace: rt.Namespace,
13741436
IngressClassName: opts.IngressClass,
1437+
Host: opts.HostName,
13751438
Paths: []ingressutil.IngressPath{
13761439
{
13771440
Path: fmt.Sprintf("/%s", store.Get().AppProxyIngressPath),

docs/commands/cli-v2_runtime_install.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ cli-v2 runtime install [runtime_name] [flags]
4242
--provider-api-url string Git provider API url
4343
--repo string Repository URL [GIT_REPO]
4444
--skip-cluster-checks Skips the cluster's checks
45+
--skip-ingress Skips the creation of ingress resources
4546
--version string The runtime version to install (default: latest)
4647
--wait-timeout duration How long to wait for the runtime components to be ready (default 8m0s)
4748
```

0 commit comments

Comments
 (0)