Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(upgrade ingress): add a flag to skip the update of jx related resources #2358

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions pkg/jx/cmd/create_vault.go
Expand Up @@ -2,11 +2,11 @@ package cmd

import (
"fmt"
"github.com/banzaicloud/bank-vaults/operator/pkg/client/clientset/versioned"
"github.com/jenkins-x/jx/pkg/kube/services"
"io"
"time"

"github.com/banzaicloud/bank-vaults/operator/pkg/client/clientset/versioned"
"github.com/jenkins-x/jx/pkg/kube/services"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1/terminal"
Expand Down Expand Up @@ -260,5 +260,6 @@ func (o *CreateVaultOptions) exposeVault(vaultService string) error {
options := &o.UpgradeIngressOptions
options.Namespaces = []string{o.Namespace}
options.Services = []string{vaultService}
options.SkipResourcesUpdate = true
return options.Run()
}
86 changes: 48 additions & 38 deletions pkg/jx/cmd/upgrade_ingress.go
Expand Up @@ -2,11 +2,12 @@ package cmd

import (
"fmt"
"io"
"strings"

"github.com/jenkins-x/jx/pkg/gits"
"github.com/jenkins-x/jx/pkg/kube/services"
"github.com/pkg/errors"
"io"
"strings"

"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/kube"
Expand Down Expand Up @@ -40,12 +41,13 @@ const (
type UpgradeIngressOptions struct {
CreateOptions

SkipCertManager bool
Cluster bool
Namespaces []string
Version string
TargetNamespaces []string
Services []string
SkipCertManager bool
Cluster bool
Namespaces []string
Version string
TargetNamespaces []string
Services []string
SkipResourcesUpdate bool

IngressConfig kube.IngressConfig
}
Expand Down Expand Up @@ -89,36 +91,38 @@ func (o *UpgradeIngressOptions) addFlags(cmd *cobra.Command) {
cmd.Flags().StringArrayVarP(&o.Namespaces, "namespaces", "", []string{}, "Namespaces to upgrade")
cmd.Flags().BoolVarP(&o.SkipCertManager, "skip-certmanager", "", false, "Skips certmanager installation")
cmd.Flags().StringArrayVarP(&o.Services, "services", "", []string{}, "Services to upgrdde")
cmd.Flags().BoolVarP(&o.SkipResourcesUpdate, "skip-resources-update", "", false, "Skips the update of jx related resources such as webhook or Jenkins URL")
}

// Run implements the command
func (o *UpgradeIngressOptions) Run() error {

_, _, err := o.KubeClient()
if err != nil {
return fmt.Errorf("cannot connect to Kubernetes cluster: %v", err)
}

o.devNamespace, _, err = kube.GetDevNamespace(o.KubeClientCached, o.currentNamespace)
if err != nil {
return err
return errors.Wrap(err, "getting the dev namesapce")
}

previousWebHookEndpoint, err := o.GetWebHookEndpoint()
if err != nil {
return err
previousWebHookEndpoint := ""
if !o.SkipResourcesUpdate {
previousWebHookEndpoint, err = o.GetWebHookEndpoint()
if err != nil {
return errors.Wrap(err, "getting the webhook endpoint")
}
}

// if existing ingress exist in the namespaces ask do you want to delete them?
ingressToDelete, err := o.getExistingIngressRules()
if err != nil {
return err
return errors.Wrap(err, "getting the existing ingress rules")
}

// wizard to ask for config values
err = o.confirmExposecontrollerConfig()
if err != nil {
return err
return errors.Wrap(err, "configure exposecontroller")
}

// confirm values
Expand All @@ -132,25 +136,25 @@ func (o *UpgradeIngressOptions) Run() error {
// save details to a configmap
_, err = kube.SaveAsConfigMap(o.KubeClientCached, kube.ConfigMapIngressConfig, o.devNamespace, o.IngressConfig)
if err != nil {
return err
return errors.Wrap(err, "saving ingress config into a configmap")
}

err = o.CleanServiceAnnotations(o.Services...)
if err != nil {
return err
return errors.Wrap(err, "cleaning service annotations")
}

// if tls create CRDs
if o.IngressConfig.TLS {
err = o.ensureCertmanagerSetup()
if err != nil {
return err
return errors.Wrap(err, "ensure cert-manager setup")
}
}
// annotate any service that has expose=true with correct certmanager staging / prod annotation
err = o.AnnotateExposedServicesWithCertManager(o.Services...)
if err != nil {
return err
return errors.Wrap(err, "annotating the exposed service with cert-manager")
}

// delete ingress
Expand All @@ -164,45 +168,52 @@ func (o *UpgradeIngressOptions) Run() error {

err = o.recreateIngressRules()
if err != nil {
return err
return errors.Wrap(err, "recreating the ingress rules")
}

_, _, err = o.JXClient()
if !o.SkipResourcesUpdate {
o.updateResources(previousWebHookEndpoint)
}

log.Success("Ingress rules recreated\n")

// todo wait for certs secrets to update ingress rules?
if o.IngressConfig.TLS {
log.Warn("It can take around 5 minutes for Cert Manager to get certificates from Lets Encrypt and update Ingress rules\n")
log.Info("Use the following commands to diagnose any issues:\n")
log.Infof("jx logs %s -n %s\n", CertManagerDeployment, CertManagerNamespace)
log.Info("kubectl describe certificates\n")
log.Info("kubectl describe issuers\n\n")
}

return nil
}

func (o *UpgradeIngressOptions) updateResources(previousWebHookEndpoint string) error {
_, _, err := o.JXClient()
if err != nil {
return errors.Wrap(err, "failed to get jxclient")
}

isProwEnabled, err := o.isProw()
if err != nil {
return err
return errors.Wrap(err, "checking if is prow")
}

if !isProwEnabled {
err = o.updateJenkinsURL(o.TargetNamespaces)
if err != nil {
return err
return errors.Wrap(err, "upgrade jenkins URL")
}
}
// todo wait for certs secrets to update ingress rules?

log.Success("Ingress rules recreated\n")

if o.IngressConfig.TLS {
log.Warn("It can take around 5 minutes for Cert Manager to get certificates from Lets Encrypt and update Ingress rules\n")
log.Info("Use the following commands to diagnose any issues:\n")
log.Infof("jx logs %s -n %s\n", CertManagerDeployment, CertManagerNamespace)
log.Info("kubectl describe certificates\n")
log.Info("kubectl describe issuers\n\n")
}

updatedWebHookEndpoint, err := o.GetWebHookEndpoint()
if err != nil {
return err
return errors.Wrap(err, "retrieving the webhook endpoint")
}

log.Infof("Previous webhook endpoint %s\n", previousWebHookEndpoint)
log.Infof("Updated webhook endpoint %s\n", updatedWebHookEndpoint)

updateWebHooks := true
if !o.BatchMode {
if !util.Confirm("Do you want to update all existing webhooks?", true, "", o.In, o.Out, o.Err) {
Expand All @@ -213,7 +224,6 @@ func (o *UpgradeIngressOptions) Run() error {
if updateWebHooks {
o.updateWebHooks(previousWebHookEndpoint, updatedWebHookEndpoint)
}

return nil
}

Expand Down