Skip to content

Commit 2534aaa

Browse files
CR-9052 ingress host from cluster (#228)
* getting ingress host from cluster * bump * small fix * add info log * docs update * take ingress controller from class spec * take services from all ns * improved logs
1 parent 567571e commit 2534aaa

File tree

6 files changed

+53
-53
lines changed

6 files changed

+53
-53
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.209
1+
VERSION=v0.0.210
22

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

cmd/commands/common.go

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"github.com/codefresh-io/cli-v2/pkg/store"
3434
"github.com/codefresh-io/cli-v2/pkg/util"
3535
"github.com/manifoldco/promptui"
36+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3637
"k8s.io/client-go/tools/clientcmd"
3738

3839
"github.com/spf13/cobra"
@@ -81,8 +82,9 @@ func IsValidName(s string) (bool, error) {
8182
return regexp.MatchString(`^[a-z]([-a-z0-9]{0,61}[a-z0-9])?$`, s)
8283
}
8384

84-
func IsValidIngressHost(ingress string) (bool, error) {
85-
return regexp.MatchString(`^(http|https)://`, ingress)
85+
func getControllerName(s string) string {
86+
split := strings.Split(s, "/")
87+
return split[1]
8688
}
8789

8890
func askUserIfToInstallDemoResources(cmd *cobra.Command, sampleInstall *bool) error {
@@ -360,44 +362,31 @@ func getKubeContextNameFromUserSelect(cmd *cobra.Command, kubeContextName *strin
360362
return nil
361363
}
362364

363-
func getIngressHostFromUserInput(cmd *cobra.Command, ingressHost *string) error {
364-
if ingressHost != nil && *ingressHost != "" {
365-
*ingressHost = strings.TrimSpace(*ingressHost)
366-
return nil
367-
}
368-
369-
if store.Get().Silent {
370-
return fmt.Errorf("missing ingress host")
371-
}
372-
373-
ingressHostPrompt := promptui.Prompt{
374-
Label: "Ingress host (required)",
375-
}
365+
func getIngressHostFromCluster(ctx context.Context, opts *RuntimeInstallOptions) error {
366+
log.G(ctx).Info("Retrieving ingress controller info from your cluster...\n")
376367

377-
ingressHostInput, err := ingressHostPrompt.Run()
368+
cs := opts.KubeFactory.KubernetesClientSetOrDie()
369+
ServicesList, err := cs.CoreV1().Services("").List(ctx, metav1.ListOptions{})
378370
if err != nil {
379-
return fmt.Errorf("Prompt error: %w", err)
371+
return fmt.Errorf("failed to get ingress controller info from your cluster: %w", err)
380372
}
381373

382-
ingressHostInput = strings.TrimSpace(ingressHostInput)
383-
if ingressHostInput == "" {
384-
return fmt.Errorf("missing ingress host")
374+
for _, s := range ServicesList.Items {
375+
if s.ObjectMeta.Name == opts.IngressController {
376+
opts.IngressHost = fmt.Sprintf("https://%s", s.Status.LoadBalancer.Ingress[0].Hostname)
377+
break
378+
}
385379
}
386380

387-
die(cmd.Flags().Set("ingress-host", ingressHostInput))
388-
*ingressHost = ingressHostInput
381+
if opts.IngressController == "" {
382+
return fmt.Errorf("failed to fetch ingress host from the cluster. please make sure you have a nginx ingress controller installed properly")
383+
}
389384

390385
return nil
391386
}
392387

393388
func checkIngressHostCertificate(ctx context.Context, ingress string) (bool, error) {
394389
var err error
395-
match, _ := regexp.MatchString(`^http://`, ingress)
396-
if match { //if user provided http ingress
397-
log.G(ctx).Warn("The ingress host uses an insecure protocol. The browser may block subsequent runtime requests from the UI unless explicitly approved.")
398-
399-
return true, nil
400-
}
401390

402391
res, err := http.Get(ingress)
403392

@@ -459,7 +448,7 @@ func askUserIfToProceedWithInsecure(ctx context.Context) error {
459448
Selected: "{{ . | yellow }} ",
460449
}
461450

462-
log.G(ctx).Warnf("The provided ingressHost does not have a valid certificate.")
451+
log.G(ctx).Warnf("The ingress host does not have a valid certificate.")
463452
labelStr := fmt.Sprintf("%vDo you wish to continue the installation with insecure ingress host mode?%v", CYAN, COLOR_RESET)
464453

465454
prompt := promptui.Select{

cmd/commands/runtime.go

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ type (
7474
RuntimeStoreIV string
7575
IngressHost string
7676
IngressClass string
77+
IngressController string
7778
Insecure bool
7879
InstallDemoResources bool
7980
Version *semver.Version
@@ -190,7 +191,7 @@ func NewRuntimeInstallCommand() *cobra.Command {
190191
"Runtime name": installationOpts.RuntimeName,
191192
"Repository URL": installationOpts.InsCloneOpts.Repo,
192193
"Ingress host": installationOpts.IngressHost,
193-
"IngressClass": installationOpts.IngressClass,
194+
"Ingress class": installationOpts.IngressClass,
194195
"Installing demo resources": strconv.FormatBool(installationOpts.InstallDemoResources),
195196
}
196197

@@ -225,8 +226,8 @@ func NewRuntimeInstallCommand() *cobra.Command {
225226
})
226227
installationOpts.KubeFactory = kube.AddFlags(cmd.Flags())
227228

228-
util.Die(cmd.MarkFlagRequired("ingress-host"))
229229
util.Die(cmd.Flags().MarkHidden("bypass-ingress-class-check"))
230+
util.Die(cmd.Flags().MarkHidden("ingress-host"))
230231

231232
return cmd
232233
}
@@ -282,20 +283,20 @@ func runtimeInstallCommandPreRunHandler(cmd *cobra.Command, opts *RuntimeInstall
282283
return err
283284
}
284285

285-
err = ensureRepo(cmd, opts.RuntimeName, opts.InsCloneOpts, false)
286-
handleCliStep(reporter.InstallStepPreCheckEnsureRuntimeRepo, "Getting runtime repo", err, false)
286+
err = ensureIngressHost(cmd, opts)
287+
handleCliStep(reporter.InstallStepPreCheckEnsureIngressHost, "Getting ingressHost", err, false)
287288
if err != nil {
288289
return err
289290
}
290291

291-
err = ensureGitToken(cmd, opts.InsCloneOpts)
292-
handleCliStep(reporter.InstallStepPreCheckEnsureGitToken, "Getting git token", err, false)
292+
err = ensureRepo(cmd, opts.RuntimeName, opts.InsCloneOpts, false)
293+
handleCliStep(reporter.InstallStepPreCheckEnsureRuntimeRepo, "Getting runtime repo", err, false)
293294
if err != nil {
294295
return err
295296
}
296297

297-
err = ensureIngressHost(cmd, opts)
298-
handleCliStep(reporter.InstallStepPreCheckEnsureIngressHost, "Getting ingressHost", err, false)
298+
err = ensureGitToken(cmd, opts.InsCloneOpts)
299+
handleCliStep(reporter.InstallStepPreCheckEnsureGitToken, "Getting git token", err, false)
299300
if err != nil {
300301
return err
301302
}
@@ -381,17 +382,18 @@ func runtimeUpgradeCommandPreRunHandler(cmd *cobra.Command, args []string, clone
381382
}
382383

383384
func ensureIngressHost(cmd *cobra.Command, opts *RuntimeInstallOptions) error {
384-
if err := getIngressHostFromUserInput(cmd, &opts.IngressHost); err != nil {
385-
return err
385+
if opts.IngressHost != "" { // ingress host provided by hidden flag (for our tests)
386+
return nil
386387
}
387388

388-
isValid, err := IsValidIngressHost(opts.IngressHost)
389-
if err != nil {
390-
log.G(cmd.Context()).Fatal("failed to check the validity of the ingress host")
391-
} else if !isValid {
392-
log.G(cmd.Context()).Fatal("ingress host must begin with a protocol http:// or https://")
389+
if err := getIngressHostFromCluster(cmd.Context(), opts); err != nil {
390+
return err
393391
}
394392

393+
log.G(cmd.Context()).Infof("Using ingress host: %s", opts.IngressHost)
394+
395+
log.G(cmd.Context()).Info("Validating ingress host")
396+
395397
certValid, err := checkIngressHostCertificate(cmd.Context(), opts.IngressHost)
396398
if err != nil {
397399
log.G(cmd.Context()).Fatalf("failed to check ingress host: %v", err)
@@ -410,8 +412,8 @@ func ensureIngressClass(ctx context.Context, opts *RuntimeInstallOptions) error
410412
if store.Get().BypassIngressClassCheck {
411413
return nil
412414
}
413-
414-
fmt.Print("Retrieving ingress class info from your cluster...\n")
415+
416+
log.G(ctx).Info("Retrieving ingress class info from your cluster...\n")
415417

416418
cs := opts.KubeFactory.KubernetesClientSetOrDie()
417419
ingressClassList, err := cs.NetworkingV1().IngressClasses().List(ctx, metav1.ListOptions{})
@@ -420,18 +422,21 @@ func ensureIngressClass(ctx context.Context, opts *RuntimeInstallOptions) error
420422
}
421423

422424
var ingressClassNames []string
425+
ingressClassNameToController := make(map[string]string)
423426
var isValidClass bool
424427
for _, ic := range ingressClassList.Items {
425428
if ic.ObjectMeta.Labels["app.kubernetes.io/name"] == "ingress-nginx" {
426429
ingressClassNames = append(ingressClassNames, ic.Name)
430+
ingressClassNameToController[ic.Name] = fmt.Sprintf("%s-controller", getControllerName(ic.Spec.Controller))
427431
if opts.IngressClass == ic.Name {
428432
isValidClass = true
429433
}
430434
}
431435
}
432436

433437
if opts.IngressClass != "" { //if user provided ingress class by flag
434-
if isValidClass {
438+
if isValidClass {
439+
opts.IngressController = ingressClassNameToController[opts.IngressClass]
435440
return nil
436441
}
437442
return fmt.Errorf("Ingress Class '%s' is not supported. Only the ingress class of type NGINX is supported. for more information: %s", opts.IngressClass, store.Get().RequirementsLink)
@@ -444,11 +449,18 @@ func ensureIngressClass(ctx context.Context, opts *RuntimeInstallOptions) error
444449
if len(ingressClassNames) == 1 {
445450
log.G(ctx).Info("Using ingress class: ", ingressClassNames[0])
446451
opts.IngressClass = ingressClassNames[0]
452+
opts.IngressController = ingressClassNameToController[opts.IngressClass]
447453
return nil
448454
}
449455

450456
if !store.Get().Silent {
451-
return getIngressClassFromUserSelect(ctx, ingressClassNames, &opts.IngressClass)
457+
err = getIngressClassFromUserSelect(ctx, ingressClassNames, &opts.IngressClass)
458+
if err != nil {
459+
return err
460+
}
461+
462+
opts.IngressController = ingressClassNameToController[opts.IngressClass]
463+
return nil
452464
}
453465

454466
return fmt.Errorf("Please add the --ingress-class flag and define its value")

docs/commands/cli-v2_runtime_install.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ cli-v2 runtime install [runtime_name] [flags]
3838
-u, --git-user string Your git provider user name [GIT_USER] (not required in GitHub)
3939
-h, --help help for install
4040
--ingress-class string The ingress class name
41-
--ingress-host string The ingress host
4241
--kubeconfig string Path to the kubeconfig file to use for CLI requests.
4342
-n, --namespace string If present, the namespace scope for this CLI request
4443
--provider string The git provider, one of: gitea|github|gitlab

docs/releases/release_notes.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ cf version
2323

2424
```bash
2525
# download and extract the binary
26-
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.209/cf-linux-amd64.tar.gz | tar zx
26+
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.210/cf-linux-amd64.tar.gz | tar zx
2727

2828
# move the binary to your $PATH
2929
mv ./cf-linux-amd64 /usr/local/bin/cf
@@ -36,7 +36,7 @@ cf version
3636

3737
```bash
3838
# download and extract the binary
39-
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.209/cf-darwin-amd64.tar.gz | tar zx
39+
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.210/cf-darwin-amd64.tar.gz | tar zx
4040

4141
# move the binary to your $PATH
4242
mv ./cf-darwin-amd64 /usr/local/bin/cf

manifests/runtime.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ metadata:
55
namespace: '{{ namespace }}'
66
spec:
77
defVersion: 1.0.0
8-
version: 0.0.209
8+
version: 0.0.210
99
bootstrapSpecifier: github.com/codefresh-io/cli-v2/manifests/argo-cd
1010
components:
1111
- name: events

0 commit comments

Comments
 (0)