forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_addon_prow.go
152 lines (129 loc) · 4.15 KB
/
create_addon_prow.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
150
151
152
package cmd
import (
"io"
"strings"
"github.com/spf13/cobra"
"fmt"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/kube"
"github.com/jenkins-x/jx/pkg/util"
"github.com/pkg/errors"
)
const (
defaultProwReleaseName = "prow"
prowVersion = "0.0.9"
)
var (
createAddonProwLong = templates.LongDesc(`
Creates the prow addon for handling webhook events
`)
createAddonProwExample = templates.Examples(`
# Create the prow addon
jx create addon prow
# Create the prow addon in a custom namespace
jx create addon prow -n mynamespace
`)
)
// CreateAddonProwOptions the options for the create spring command
type CreateAddonProwOptions struct {
CreateAddonOptions
Chart string
HMACToken string
OAUTHToken string
Username string
Password string
}
// NewCmdCreateAddonProw creates a command object for the "create" command
func NewCmdCreateAddonProw(f Factory, out io.Writer, errOut io.Writer) *cobra.Command {
options := &CreateAddonProwOptions{
CreateAddonOptions: CreateAddonOptions{
CreateOptions: CreateOptions{
CommonOptions: CommonOptions{
Factory: f,
Out: out,
Err: errOut,
},
},
},
}
cmd := &cobra.Command{
Use: "prow",
Short: "Create a prow addon",
Long: createAddonProwLong,
Example: createAddonProwExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
options.addCommonFlags(cmd)
options.addFlags(cmd, "", defaultProwReleaseName)
cmd.Flags().StringVarP(&options.Version, "version", "v", prowVersion, "The version of the prow addon to use")
cmd.Flags().StringVarP(&options.Chart, optionChart, "c", kube.ChartProw, "The name of the chart to use")
cmd.Flags().StringVarP(&options.HMACToken, "hmac-token", "", "", "OPTIONAL: The hmac-token is the token that you give to GitHub for validating webhooks. Generate it using any reasonable randomness-generator, eg openssl rand -hex 20")
cmd.Flags().StringVarP(&options.OAUTHToken, "oauth-token", "", "", "OPTIONAL: The oauth-token is an OAuth2 token that has read and write access to the bot account. Generate it from the account's settings -> Personal access tokens -> Generate new token.")
cmd.Flags().StringVarP(&options.Username, "username", "", "", "Overwrite cluster admin username")
cmd.Flags().StringVarP(&options.Password, "password", "", "", "Overwrite the default admin password used to login to the Deck UI")
return cmd
}
// Run implements the command
func (o *CreateAddonProwOptions) Run() error {
if o.ReleaseName == "" {
return util.MissingOption(optionRelease)
}
if o.Chart == "" {
return util.MissingOption(optionChart)
}
err := o.ensureHelm()
if err != nil {
return errors.Wrap(err, "failed to ensure that helm is present")
}
_, _, err = o.KubeClient()
if err != nil {
return err
}
if o.HMACToken == "" {
o.HMACToken, err = util.RandStringBytesMaskImprSrc(41)
if err != nil {
return fmt.Errorf("cannot create a random hmac token for Prow")
}
}
if o.OAUTHToken == "" {
authConfigSvc, err := o.CreateGitAuthConfigService()
if err != nil {
return err
}
config := authConfigSvc.Config()
server := config.GetOrCreateServer(config.CurrentServer)
userAuth, err := config.PickServerUserAuth(server, "Git account to be used to send webhook events", o.BatchMode)
if err != nil {
return err
}
o.OAUTHToken = userAuth.ApiToken
}
if o.Username == "" {
o.Username, err = o.GetClusterUserName()
if err != nil {
return err
}
}
devNamespace, _, err := kube.GetDevNamespace(o.kubeClient, o.currentNamespace)
if err != nil {
return fmt.Errorf("cannot find a dev team namespace to get existing exposecontroller config from. %v", err)
}
values := []string{"user=" + o.Username, "oauthToken=" + o.OAUTHToken, "hmacToken=" + o.HMACToken}
setValues := strings.Split(o.SetValues, ",")
values = append(values, setValues...)
err = o.installChart(o.ReleaseName, o.Chart, o.Version, devNamespace, true, values)
if err != nil {
return err
}
// create the ingress rule
err = o.expose(devNamespace, devNamespace, o.Password)
if err != nil {
return err
}
return nil
}