-
Notifications
You must be signed in to change notification settings - Fork 62
/
root.go
250 lines (211 loc) · 7.83 KB
/
root.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package create
import (
"context"
"fmt"
"net/url"
"os"
"path/filepath"
"strings"
"github.com/cnoe-io/idpbuilder/api/v1alpha1"
"github.com/cnoe-io/idpbuilder/globals"
"github.com/cnoe-io/idpbuilder/pkg/build"
"github.com/cnoe-io/idpbuilder/pkg/cmd/helpers"
"github.com/cnoe-io/idpbuilder/pkg/k8s"
"github.com/spf13/cobra"
"k8s.io/client-go/util/homedir"
)
const (
recreateClusterUsage = "Delete cluster first if it already exists."
buildNameUsage = "Name for build (Prefix for kind cluster name, pod names, etc)."
kubeVersionUsage = "Version of the kind kubernetes cluster to create."
extraPortsMappingUsage = "List of extra ports to expose on the docker container and kubernetes cluster as nodePort " +
"(e.g. \"22:32222,9090:39090,etc\")."
kindConfigPathUsage = "Path of the kind config file to be used instead of the default."
hostUsage = "Host name to access resources in this cluster."
ingressHostUsage = "Host name used by ingresses. Useful when you have another proxy in front of ingress-nginx that idpbuilder provisions."
protocolUsage = "Protocol to use to access web UIs. http or https."
portUsage = "Port number to use to access web UIs."
pathRoutingUsage = "When set to true, web UIs are exposed under single domain name. " +
"e.g. \"https://cnoe.localtest.me/argocd\" instead of \"https://argocd.cnoe.localtest.me\""
extraPackagesUsage = "Paths to locations containing custom packages"
packageCustomizationFilesUsage = "Name of the package and the path to file to customize the core packages with. " +
"valid package names are: argocd, nginx, and gitea. e.g. argocd:/tmp/argocd.yaml"
noExitUsage = "When set, idpbuilder will not exit after all packages are synced. Useful for continuously syncing local directories."
)
var (
// Flags
recreateCluster bool
buildName string
kubeVersion string
extraPortsMapping string
kindConfigPath string
extraPackages []string
packageCustomizationFiles []string
noExit bool
protocol string
host string
ingressHost string
port string
pathRouting bool
)
var CreateCmd = &cobra.Command{
Use: "create",
Short: "(Re)Create an IDP cluster",
Long: ``,
RunE: create,
PreRunE: preCreateE,
SilenceUsage: true,
}
func init() {
// cluster related flags
CreateCmd.PersistentFlags().BoolVar(&recreateCluster, "recreate", false, recreateClusterUsage)
CreateCmd.PersistentFlags().StringVar(&buildName, "build-name", "localdev", buildNameUsage)
CreateCmd.PersistentFlags().MarkDeprecated("build-name", "use --name instead.")
CreateCmd.PersistentFlags().StringVar(&buildName, "name", "localdev", buildNameUsage)
CreateCmd.PersistentFlags().StringVar(&kubeVersion, "kube-version", "v1.30.3", kubeVersionUsage)
CreateCmd.PersistentFlags().StringVar(&extraPortsMapping, "extra-ports", "", extraPortsMappingUsage)
CreateCmd.PersistentFlags().StringVar(&kindConfigPath, "kind-config", "", kindConfigPathUsage)
// in-cluster resources related flags
CreateCmd.PersistentFlags().StringVar(&host, "host", globals.DefaultHostName, hostUsage)
CreateCmd.PersistentFlags().StringVar(&ingressHost, "ingress-host-name", "", ingressHostUsage)
CreateCmd.PersistentFlags().StringVar(&protocol, "protocol", "https", protocolUsage)
CreateCmd.PersistentFlags().StringVar(&port, "port", "8443", portUsage)
CreateCmd.PersistentFlags().BoolVar(&pathRouting, "use-path-routing", false, pathRoutingUsage)
CreateCmd.Flags().StringSliceVarP(&extraPackages, "package", "p", []string{}, extraPackagesUsage)
CreateCmd.Flags().StringSliceVarP(&packageCustomizationFiles, "package-custom-file", "c", []string{}, packageCustomizationFilesUsage)
// idpbuilder related flags
CreateCmd.Flags().BoolVarP(&noExit, "no-exit", "n", true, noExitUsage)
}
func preCreateE(cmd *cobra.Command, args []string) error {
return helpers.SetLogger()
}
func create(cmd *cobra.Command, args []string) error {
ctx, ctxCancel := context.WithCancel(cmd.Context())
defer ctxCancel()
kubeConfigPath := filepath.Join(homedir.HomeDir(), ".kube", "config")
protocol = strings.ToLower(protocol)
host = strings.ToLower(host)
if ingressHost == "" {
ingressHost = host
}
err := validate()
if err != nil {
return err
}
var absDirPaths []string
var remotePaths []string
if len(extraPackages) > 0 {
r, l, pErr := helpers.ParsePackageStrings(extraPackages)
if pErr != nil {
return pErr
}
absDirPaths = l
remotePaths = r
}
o := make(map[string]v1alpha1.PackageCustomization)
for i := range packageCustomizationFiles {
c, pErr := getPackageCustomFile(packageCustomizationFiles[i])
if pErr != nil {
return pErr
}
o[c.Name] = c
}
exitOnSync := true
if cmd.Flags().Changed("no-exit") {
exitOnSync = !noExit
}
opts := build.NewBuildOptions{
Name: buildName,
KubeVersion: kubeVersion,
KubeConfigPath: kubeConfigPath,
KindConfigPath: kindConfigPath,
ExtraPortsMapping: extraPortsMapping,
TemplateData: v1alpha1.BuildCustomizationSpec{
Protocol: protocol,
Host: host,
IngressHost: ingressHost,
Port: port,
UsePathRouting: pathRouting,
},
CustomPackageDirs: absDirPaths,
CustomPackageUrls: remotePaths,
ExitOnSync: exitOnSync,
PackageCustomization: o,
Scheme: k8s.GetScheme(),
CancelFunc: ctxCancel,
}
b := build.NewBuild(opts)
if err := b.Run(ctx, recreateCluster); err != nil {
return err
}
if cmd.Context().Err() != nil {
return context.Cause(cmd.Context())
}
printSuccessMsg()
return nil
}
func validate() error {
if buildName == "" {
return fmt.Errorf("must specify build-name")
}
_, err := url.Parse(fmt.Sprintf("%s://%s:%s", protocol, host, port))
if err != nil {
return fmt.Errorf("invalid url: %w", err)
}
for i := range packageCustomizationFiles {
_, pErr := getPackageCustomFile(packageCustomizationFiles[i])
if pErr != nil {
return pErr
}
}
_, _, err = helpers.ParsePackageStrings(extraPackages)
return err
}
func getPackageCustomFile(input string) (v1alpha1.PackageCustomization, error) {
// the format should be `<package-name>:<path-to-file>`
s := strings.Split(input, ":")
if len(s) != 2 {
return v1alpha1.PackageCustomization{}, fmt.Errorf("ensure %s is formatted as <package-name>:<path-to-file>", input)
}
paths, err := helpers.GetAbsFilePaths([]string{s[1]}, false)
if err != nil {
return v1alpha1.PackageCustomization{}, err
}
err = helpers.ValidateKubernetesYamlFile(paths[0])
if err != nil {
return v1alpha1.PackageCustomization{}, err
}
corePkgs := map[string]struct{}{v1alpha1.ArgoCDPackageName: {}, v1alpha1.GiteaPackageName: {}, v1alpha1.IngressNginxPackageName: {}}
name := s[0]
_, ok := corePkgs[name]
if !ok {
return v1alpha1.PackageCustomization{}, fmt.Errorf("customization for %s not supported", name)
}
return v1alpha1.PackageCustomization{
Name: name,
FilePath: paths[0],
}, nil
}
func printSuccessMsg() {
subDomain := "argocd."
subPath := ""
if pathRouting == true {
subDomain = ""
subPath = "argocd"
}
var argoURL string
proxy := behindProxy()
if proxy {
argoURL = fmt.Sprintf("https://%s/argocd", host)
} else {
argoURL = fmt.Sprintf("%s://%s%s:%s/%s", protocol, subDomain, host, port, subPath)
}
fmt.Print("\n\n########################### Finished Creating IDP Successfully! ############################\n\n\n")
fmt.Printf("Can Access ArgoCD at %s\nUsername: admin\n", argoURL)
fmt.Print(`Password can be retrieved by running: idpbuilder get secrets -p argocd`, "\n")
}
func behindProxy() bool {
// check if we are in codespaces: https://docs.github.com/en/codespaces/developing-in-a-codespace/default-environment-variables-for-your-codespace
_, ok := os.LookupEnv("CODESPACES")
return ok
}