This repository has been archived by the owner on Jun 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 308
/
create.go
152 lines (124 loc) · 4.81 KB
/
create.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
// Copyright 2020-2021 VMware Tanzu Community Edition contributors. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/spf13/cobra"
"github.com/vmware-tanzu/tanzu-framework/pkg/v1/config"
"github.com/vmware-tanzu/tanzu-framework/pkg/v1/tkg/constants"
"github.com/vmware-tanzu/tanzu-framework/pkg/v1/tkg/log"
"github.com/vmware-tanzu/tanzu-framework/pkg/v1/tkg/region"
"github.com/vmware-tanzu/tanzu-framework/pkg/v1/tkg/tkgctl"
"github.com/vmware-tanzu/tanzu-framework/pkg/v1/tkg/types"
)
type initStandaloneOptions struct {
clusterConfigFile string
infrastructureProvider string
ui bool
bind string
browser string
}
// CreateCmd creates a standalone workload cluster.
var CreateCmd = &cobra.Command{
Use: "create <cluster name> -f <configuration location>",
Short: "create a standalone workload cluster",
RunE: create,
}
var iso = initStandaloneOptions{}
const defaultPlan = "dev"
func init() {
CreateCmd.Flags().StringVarP(&iso.clusterConfigFile, "file", "f", "", "Configuration file from which to create a standalone cluster")
CreateCmd.Flags().BoolVarP(&iso.ui, "ui", "u", false, "Launch interactive standalone cluster provisioning UI")
CreateCmd.Flags().StringVarP(&iso.infrastructureProvider, "infrastructure", "i", "", "Infrastructure to deploy the standalone cluster on. Only needed when using -i docker.")
CreateCmd.Flags().StringVarP(&iso.bind, "bind", "b", "127.0.0.1:8080", "Specify the IP and port to bind the Kickstart UI against (e.g. 127.0.0.1:8080).")
CreateCmd.Flags().StringVarP(&iso.browser, "browser", "", "", "Specify the browser to open the Kickstart UI on. Use 'none' for no browser. Defaults to OS default browser. Supported: ['chrome', 'firefox', 'safari', 'ie', 'edge', 'none']")
}
func create(cmd *cobra.Command, args []string) error {
var clusterName string
// validate a cluster name was passed when not using the kickstart UI
if len(args) < 1 && !iso.ui {
return fmt.Errorf("no cluster name specified")
} else if len(args) == 1 {
clusterName = args[0]
}
// create new client
c, err := newTKGCtlClient(false)
if err != nil {
return NonUsageError(cmd, err, "unable to create Tanzu Standalone Cluster client")
}
// create a new standlone cluster
initRegionOpts := tkgctl.InitRegionOptions{
ClusterConfigFile: iso.clusterConfigFile,
ClusterName: clusterName,
Plan: defaultPlan,
UI: iso.ui,
Bind: iso.bind,
Browser: iso.browser,
Edition: BuildEdition,
}
if iso.infrastructureProvider != "" {
initRegionOpts.InfrastructureProvider = iso.infrastructureProvider
}
err = c.InitStandalone(initRegionOpts)
if err != nil {
return Error(err, "failed to initialize standalone cluster.")
}
err = saveStandaloneClusterConfig(clusterName, iso.clusterConfigFile)
if err != nil {
return Error(err, "failed to store standalone bootstrap cluster config")
}
return nil
}
func newTKGCtlClient(forceUpdateTKGCompatibilityImage bool) (tkgctl.TKGClient, error) {
configDir, err := getTKGConfigDir()
if err != nil {
return nil, Error(err, "unable to determine Tanzu configuration directory.")
}
return tkgctl.New(tkgctl.Options{
ConfigDir: configDir,
CustomizerOptions: types.CustomizerOptions{
RegionManagerFactory: region.NewFactory(),
},
LogOptions: tkgctl.LoggingOptions{Verbosity: logLevel, File: logFile},
ForceUpdateTKGCompatibilityImage: forceUpdateTKGCompatibilityImage,
})
}
func getTKGConfigDir() (string, error) {
tanzuConfigDir, err := config.LocalDir()
if err != nil {
return "", Error(err, "unable to get home directory")
}
return filepath.Join(tanzuConfigDir, "tkg"), nil
}
func saveStandaloneClusterConfig(clusterName, clusterConfigPath string) error {
// If there is no cluster config provided
// assume CAPD and don't try to save anything
if clusterConfigPath == "" {
return nil
}
// Get config contents
clusterConfigBytes, err := os.ReadFile(clusterConfigPath)
if err != nil {
return fmt.Errorf("cannot read cluster config file: %v", err)
}
// Save the cluster configuration for future restore cycle
configDir, err := getTKGConfigDir()
if err != nil {
return err
}
clusterConfigDir := filepath.Join(configDir, "clusterconfigs")
err = os.MkdirAll(clusterConfigDir, 0755)
if err != nil {
return err
}
clusterConfigFile := clusterName + ".yaml"
writeConfigPath := filepath.Join(clusterConfigDir, clusterConfigFile)
log.Infof("Saving bootstrap cluster config for standalone cluster at '%v'", writeConfigPath)
err = os.WriteFile(writeConfigPath, clusterConfigBytes, constants.ConfigFilePermissions)
if err != nil {
return fmt.Errorf("cannot write cluster config file for standalone bootstrap cluster: %v", err)
}
return nil
}