-
Notifications
You must be signed in to change notification settings - Fork 787
/
create_addon_ingress.go
149 lines (129 loc) · 4.41 KB
/
create_addon_ingress.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package create
import (
"fmt"
"io/ioutil"
"path/filepath"
"github.com/jenkins-x/jx-logging/pkg/log"
"github.com/jenkins-x/jx/v2/pkg/cmd/create/options"
"github.com/jenkins-x/jx/v2/pkg/cmd/helper"
"github.com/jenkins-x/jx/v2/pkg/cmd/initcmd"
"github.com/jenkins-x/jx/v2/pkg/cmd/opts"
"github.com/jenkins-x/jx/v2/pkg/cmd/templates"
"github.com/jenkins-x/jx/v2/pkg/helm"
"github.com/jenkins-x/jx/v2/pkg/kube"
"github.com/jenkins-x/jx/v2/pkg/util"
"github.com/pkg/errors"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
var (
createAddonIngressControllerLong = templates.LongDesc(`
Create an Ingress Controller to expose services outside of your remote Staging/Production cluster
`)
createAddonIngressControllerExample = templates.Examples(`
# Creates the ingress controller
jx create addon ingctl
`)
)
// CreateAddonIngressControllerOptions the options for the create spring command
type CreateAddonIngressControllerOptions struct {
CreateAddonOptions
InitOptions initcmd.InitOptions
}
// NewCmdCreateAddonIngressController creates a command object for the "create" command
func NewCmdCreateAddonIngressController(commonOpts *opts.CommonOptions) *cobra.Command {
options := &CreateAddonIngressControllerOptions{
CreateAddonOptions: CreateAddonOptions{
CreateOptions: options.CreateOptions{
CommonOptions: commonOpts,
},
},
}
cmd := &cobra.Command{
Use: "ingress controller",
Short: "Create an Ingress Controller to expose services outside of your remote Staging/Production cluster",
Aliases: []string{"ingctl"},
Long: createAddonIngressControllerLong,
Example: createAddonIngressControllerExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
helper.CheckErr(err)
},
}
options.InitOptions.AddIngressFlags(cmd)
return cmd
}
// Run implements the command
func (o *CreateAddonIngressControllerOptions) Run() error {
jxClient, ns, err := o.JXClient()
if err != nil {
return err
}
envName := kube.LabelValueThisEnvironment
env, err := jxClient.JenkinsV1().Environments(ns).Get(envName, metav1.GetOptions{})
if err != nil {
return errors.Wrapf(err, "could not find the Environment CRD %s in namespace %s", envName, ns)
}
gitRepo := env.Spec.Source.URL
if gitRepo == "" {
return fmt.Errorf("could not find the Git Source URL in the Environment CRD %s in namespace %s", envName, ns)
}
o.InitOptions.CommonOptions = o.CommonOptions
err = o.InitOptions.InitIngress()
if err != nil {
return err
}
// now lets try update the domain
domain := o.InitOptions.Domain
if domain == "" {
log.Logger().Error("No domain could be discovered so we cannot update the domain entry in your GitOps repository")
}
log.Logger().Infof("domain is %s", util.ColorInfo(domain))
log.Logger().Infof("\n\nLets create a Pull Request against %s to modify the domain...\n", util.ColorInfo(gitRepo))
// now lets make sure we have the latest domain in the git repository
return o.createPullRequestForDomain(gitRepo, domain)
}
func (o *CreateAddonIngressControllerOptions) createPullRequestForDomain(gitRepoURL string, domain string) error {
po := &opts.PullRequestDetails{
RepositoryGitURL: gitRepoURL,
RepositoryBranch: "master",
}
dir, err := ioutil.TempDir("", "create-version-pr")
if err != nil {
return err
}
po.Dir = dir
po.BranchNameText = "set-ingress-domain"
po.Title = "set ingress domain"
po.RepositoryMessage = "environment repository"
po.Message = "update the ingress domain"
return o.CreatePullRequest(po, func() error {
return o.modifyDomainInValuesFiles(dir, domain)
})
}
func (o *CreateAddonIngressControllerOptions) modifyDomainInValuesFiles(dir string, domain string) error {
fileName := filepath.Join(dir, "env", "values.yaml")
exists, err := util.FileExists(fileName)
if err != nil {
return err
}
if !exists {
return fmt.Errorf("Could not find helm file in cloned environment git repository: %s", fileName)
}
data, err := ioutil.ReadFile(fileName)
if err != nil {
return errors.Wrapf(err, "failed to load helm values file %s", fileName)
}
values, err := helm.LoadValues(data)
if err != nil {
return errors.Wrapf(err, "failed to parse helm values file %s", fileName)
}
util.SetMapValueViaPath(values, "expose.config.domain", domain)
err = helm.SaveFile(fileName, values)
if err != nil {
return errors.Wrapf(err, "failed to save helm values file %s", fileName)
}
return nil
}