|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + |
| 7 | + "github.com/ory/viper" |
| 8 | + "github.com/spf13/cobra" |
| 9 | + |
| 10 | + "knative.dev/func/pkg/cluster" |
| 11 | +) |
| 12 | + |
| 13 | +// NewClusterCmd creates the parent command for cluster management. |
| 14 | +// |
| 15 | +// The command is marked Hidden and gated behind the FUNC_ENABLE_CLUSTER |
| 16 | +// environment variable: invoking any subcommand without it set returns a |
| 17 | +// non-zero exit and a helpful message. The command still appears in |
| 18 | +// explicit `func cluster --help`, but not in the top-level listing. |
| 19 | +func NewClusterCmd() *cobra.Command { |
| 20 | + cmd := &cobra.Command{ |
| 21 | + Use: "cluster", |
| 22 | + Aliases: []string{"clusters"}, |
| 23 | + Short: "Manage local clusters (experimental)", |
| 24 | + Hidden: true, |
| 25 | + PersistentPreRunE: func(cmd *cobra.Command, args []string) error { |
| 26 | + if os.Getenv("FUNC_ENABLE_CLUSTER") == "" { |
| 27 | + return fmt.Errorf( |
| 28 | + "'%s cluster' is an experimental feature and is not enabled by default.\n"+ |
| 29 | + "Set FUNC_ENABLE_CLUSTER to a non-empty value to enable it, e.g.:\n"+ |
| 30 | + " export FUNC_ENABLE_CLUSTER=1", |
| 31 | + cmd.Root().Name()) |
| 32 | + } |
| 33 | + return nil |
| 34 | + }, |
| 35 | + Long: ` |
| 36 | +NAME |
| 37 | + {{rootCmdUse}} cluster - Manage local development clusters (experimental) |
| 38 | +
|
| 39 | +SYNOPSIS |
| 40 | + {{rootCmdUse}} cluster <create|delete|list> [flags] |
| 41 | +
|
| 42 | +DESCRIPTION |
| 43 | + Manages local development clusters with Knative, Tekton, and other |
| 44 | + components pre-installed, alongside a local container registry for |
| 45 | + function image builds. |
| 46 | +
|
| 47 | + This is an experimental feature; set FUNC_ENABLE_CLUSTER to a |
| 48 | + non-empty value to enable it. |
| 49 | +
|
| 50 | + See '{{rootCmdUse}} cluster create --help' for the full list of |
| 51 | + configurable components and flags. |
| 52 | +
|
| 53 | +EXAMPLES |
| 54 | + o Create a default development cluster |
| 55 | + $ {{rootCmdUse}} cluster create |
| 56 | +
|
| 57 | + o Create a minimal cluster (just Kubernetes + registry) |
| 58 | + $ {{rootCmdUse}} cluster create --serving=false --eventing=false |
| 59 | +
|
| 60 | + o List existing clusters |
| 61 | + $ {{rootCmdUse}} cluster list |
| 62 | +
|
| 63 | + o Remove the default cluster |
| 64 | + $ {{rootCmdUse}} cluster delete |
| 65 | +
|
| 66 | +`, |
| 67 | + } |
| 68 | + cmd.AddCommand(NewClusterCreateCmd()) |
| 69 | + cmd.AddCommand(NewClusterDeleteCmd()) |
| 70 | + cmd.AddCommand(NewClusterListCmd()) |
| 71 | + return cmd |
| 72 | +} |
| 73 | + |
| 74 | +// NewClusterCreateCmd creates the 'func cluster create' command. |
| 75 | +func NewClusterCreateCmd() *cobra.Command { |
| 76 | + cmd := &cobra.Command{ |
| 77 | + Use: "create", |
| 78 | + Short: "Create a local development cluster", |
| 79 | + Long: ` |
| 80 | +NAME |
| 81 | + {{rootCmdUse}} cluster create - Create a local development cluster |
| 82 | +
|
| 83 | +SYNOPSIS |
| 84 | + {{rootCmdUse}} cluster create [-n|--name] [--domain] [--serving] [--eventing] |
| 85 | + [--tekton] [--keda] [--container-engine] |
| 86 | + [--namespace] [--pac-host] |
| 87 | + [--skip-binaries] [--skip-registry-config] [--no-cleanup] |
| 88 | + [--retries] |
| 89 | +
|
| 90 | +DESCRIPTION |
| 91 | + Creates a local development cluster preconfigured to run Functions, |
| 92 | + along with a local container registry and the components needed for |
| 93 | + the function runtime (Serving, Eventing, Tekton, etc). |
| 94 | +
|
| 95 | + The generated kubeconfig is written under the func config directory and |
| 96 | + does not alter the system's existing kubernetes configuration. To use the |
| 97 | + cluster, export the KUBECONFIG path shown on successful creation, e.g.: |
| 98 | +
|
| 99 | + export KUBECONFIG=~/.config/func/clusters/func.local/kubeconfig.yaml |
| 100 | +
|
| 101 | + Installed components are controlled by the --serving, --eventing, |
| 102 | + --tekton, and --keda flags. Required binaries are downloaded into the |
| 103 | + func config directory on first use unless --skip-binaries is set. |
| 104 | +
|
| 105 | +EXAMPLES |
| 106 | + o Create a default development cluster |
| 107 | + $ {{rootCmdUse}} cluster create |
| 108 | +
|
| 109 | + o Create a minimal cluster (just Kubernetes + registry) |
| 110 | + $ {{rootCmdUse}} cluster create --serving=false --eventing=false |
| 111 | +
|
| 112 | + o Create a cluster with a custom name and domain |
| 113 | + $ {{rootCmdUse}} cluster create --name myproject --domain example.local |
| 114 | +
|
| 115 | + o Preserve a failed cluster for inspection |
| 116 | + $ {{rootCmdUse}} cluster create --no-cleanup |
| 117 | +`, |
| 118 | + |
| 119 | + PreRunE: bindEnv("name", "retries", "serving", "eventing", "tekton", |
| 120 | + "keda", "domain", "container-engine", "namespace", |
| 121 | + "pac-host", "skip-binaries", |
| 122 | + "skip-registry-config", "no-cleanup"), |
| 123 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 124 | + cfg := newClusterCreateConfig() |
| 125 | + if err := cluster.Create(cmd.Context(), cfg, cmd.OutOrStderr()); err != nil { |
| 126 | + return err |
| 127 | + } |
| 128 | + // Scriptable output: on success stdout receives only the |
| 129 | + // kubeconfig path, so callers can do e.g. |
| 130 | + // export KUBECONFIG=$(func cluster create) |
| 131 | + fmt.Fprintln(cmd.OutOrStdout(), cfg.Kubeconfig()) |
| 132 | + return nil |
| 133 | + }, |
| 134 | + } |
| 135 | + |
| 136 | + cmd.Flags().StringP("name", "n", "func", |
| 137 | + "Cluster name ($FUNC_CLUSTER_NAME)") |
| 138 | + cmd.Flags().Int("retries", 1, |
| 139 | + "Max cluster allocation attempts ($FUNC_CLUSTER_RETRIES)") |
| 140 | + cmd.Flags().Bool("serving", true, |
| 141 | + "Install Knative Serving ($FUNC_CLUSTER_SERVING)") |
| 142 | + cmd.Flags().Bool("eventing", true, |
| 143 | + "Install Knative Eventing ($FUNC_CLUSTER_EVENTING)") |
| 144 | + cmd.Flags().Bool("tekton", false, |
| 145 | + "Install Tekton + Pipelines-as-Code for in-cluster (remote) builds ($FUNC_CLUSTER_TEKTON)") |
| 146 | + cmd.Flags().Bool("keda", false, |
| 147 | + "Install KEDA ($FUNC_CLUSTER_KEDA)") |
| 148 | + cmd.Flags().String("domain", "localtest.me", |
| 149 | + "DNS domain for services ($FUNC_CLUSTER_DOMAIN)") |
| 150 | + cmd.Flags().String("container-engine", "", |
| 151 | + "Container engine: docker or podman; auto-detected if unset, preferring docker when both are installed ($FUNC_CONTAINER_ENGINE)") |
| 152 | + cmd.Flags().String("namespace", "default", |
| 153 | + "Kubernetes namespace for RBAC bindings ($FUNC_NAMESPACE)") |
| 154 | + cmd.Flags().String("pac-host", "pac-ctr.localtest.me", |
| 155 | + "PAC controller hostname ($FUNC_INT_PAC_HOST)") |
| 156 | + cmd.Flags().Bool("skip-binaries", false, |
| 157 | + "Skip automatic binary downloads ($FUNC_SKIP_BINARIES)") |
| 158 | + cmd.Flags().Bool("skip-registry-config", false, |
| 159 | + "Skip host registry configuration ($FUNC_SKIP_REGISTRY_CONFIG)") |
| 160 | + cmd.Flags().Bool("no-cleanup", false, |
| 161 | + "Don't delete cluster on failure ($FUNC_NO_CLEANUP)") |
| 162 | + |
| 163 | + return cmd |
| 164 | +} |
| 165 | + |
| 166 | +func newClusterCreateConfig() cluster.ClusterConfig { |
| 167 | + return cluster.ClusterConfig{ |
| 168 | + Name: viper.GetString("name"), |
| 169 | + Domain: viper.GetString("domain"), |
| 170 | + Serving: viper.GetBool("serving"), |
| 171 | + Eventing: viper.GetBool("eventing"), |
| 172 | + Tekton: viper.GetBool("tekton"), |
| 173 | + Keda: viper.GetBool("keda"), |
| 174 | + Retries: viper.GetInt("retries"), |
| 175 | + Namespace: viper.GetString("namespace"), |
| 176 | + PacHost: viper.GetString("pac-host"), |
| 177 | + SkipBinaries: viper.GetBool("skip-binaries"), |
| 178 | + SkipRegistryConfig: viper.GetBool("skip-registry-config"), |
| 179 | + NoCleanup: viper.GetBool("no-cleanup"), |
| 180 | + ContainerEngineOverride: viper.GetString("container-engine"), |
| 181 | + KubectlOverride: os.Getenv("FUNC_TEST_KUBECTL"), // override binary path |
| 182 | + KindOverride: os.Getenv("FUNC_TEST_KIND"), // override binary path |
| 183 | + GitHubActions: os.Getenv("GITHUB_ACTIONS") == "true", // detect CI environments |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +// NewClusterDeleteCmd creates the 'func cluster delete' command. |
| 188 | +func NewClusterDeleteCmd() *cobra.Command { |
| 189 | + cmd := &cobra.Command{ |
| 190 | + Use: "delete [name]", |
| 191 | + Short: "Delete a local development cluster", |
| 192 | + Long: ` |
| 193 | +NAME |
| 194 | + {{rootCmdUse}} cluster delete - Delete a local development cluster |
| 195 | +
|
| 196 | +SYNOPSIS |
| 197 | + {{rootCmdUse}} cluster delete [name] [--container-engine] |
| 198 | + [--skip-registry-config] |
| 199 | +
|
| 200 | +DESCRIPTION |
| 201 | + Deletes a local development cluster and its associated registry |
| 202 | + container. If no name is given, the default cluster "func" is deleted. |
| 203 | +
|
| 204 | + When multiple func-managed clusters exist, specify which one by name. |
| 205 | + Use '{{rootCmdUse}} cluster list' to see existing clusters. |
| 206 | +
|
| 207 | + Pass --skip-registry-config to mirror a create invocation that |
| 208 | + used the same flag; otherwise delete attempts to remove host |
| 209 | + registry-trust entries it never added (harmless but noisy, and may |
| 210 | + prompt for sudo). |
| 211 | +
|
| 212 | +EXAMPLES |
| 213 | + o Delete the default "func" cluster |
| 214 | + $ {{rootCmdUse}} cluster delete |
| 215 | +
|
| 216 | + o Delete a named cluster |
| 217 | + $ {{rootCmdUse}} cluster delete myproject |
| 218 | +
|
| 219 | +`, |
| 220 | + |
| 221 | + PreRunE: bindEnv("container-engine", "skip-registry-config"), |
| 222 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 223 | + return runClusterDelete(cmd, args) |
| 224 | + }, |
| 225 | + } |
| 226 | + |
| 227 | + cmd.Flags().String("container-engine", "", |
| 228 | + "Container engine: docker or podman; auto-detected if unset, preferring docker when both are installed ($FUNC_CONTAINER_ENGINE)") |
| 229 | + cmd.Flags().Bool("skip-registry-config", false, |
| 230 | + "Skip host registry configuration revert ($FUNC_SKIP_REGISTRY_CONFIG)") |
| 231 | + |
| 232 | + return cmd |
| 233 | +} |
| 234 | + |
| 235 | +func newClusterDeleteConfig() cluster.ClusterConfig { |
| 236 | + return cluster.ClusterConfig{ |
| 237 | + Name: "func", |
| 238 | + ContainerEngineOverride: viper.GetString("container-engine"), |
| 239 | + SkipRegistryConfig: viper.GetBool("skip-registry-config"), |
| 240 | + KubectlOverride: os.Getenv("FUNC_TEST_KUBECTL"), |
| 241 | + KindOverride: os.Getenv("FUNC_TEST_KIND"), |
| 242 | + GitHubActions: os.Getenv("GITHUB_ACTIONS") == "true", |
| 243 | + } |
| 244 | +} |
| 245 | + |
| 246 | +func runClusterDelete(cmd *cobra.Command, args []string) error { |
| 247 | + cfg := newClusterDeleteConfig() |
| 248 | + if len(args) > 0 { |
| 249 | + cfg.Name = args[0] |
| 250 | + } |
| 251 | + |
| 252 | + clusters := cluster.List() |
| 253 | + for _, c := range clusters { |
| 254 | + if c == cfg.Name { |
| 255 | + return cluster.Delete(cmd.Context(), cfg, cmd.OutOrStderr()) |
| 256 | + } |
| 257 | + } |
| 258 | + |
| 259 | + if len(clusters) == 0 { |
| 260 | + return fmt.Errorf("no clusters exist; use 'func cluster create' to create one") |
| 261 | + } |
| 262 | + return fmt.Errorf("cluster %q not found; existing clusters: %v\nUse 'func cluster create' to create one", cfg.Name, clusters) |
| 263 | +} |
| 264 | + |
| 265 | +// NewClusterListCmd creates the 'func cluster list' command. |
| 266 | +func NewClusterListCmd() *cobra.Command { |
| 267 | + return &cobra.Command{ |
| 268 | + Use: "list", |
| 269 | + Short: "List local development clusters", |
| 270 | + Long: ` |
| 271 | +NAME |
| 272 | + {{rootCmdUse}} cluster list - List local development clusters |
| 273 | +
|
| 274 | +SYNOPSIS |
| 275 | + {{rootCmdUse}} cluster list |
| 276 | +
|
| 277 | +DESCRIPTION |
| 278 | + Lists local development clusters managed by func. Output is the bare |
| 279 | + cluster name, one per line. |
| 280 | +
|
| 281 | +EXAMPLES |
| 282 | + o List existing clusters |
| 283 | + $ {{rootCmdUse}} cluster list |
| 284 | +
|
| 285 | +`, |
| 286 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 287 | + for _, name := range cluster.List() { |
| 288 | + fmt.Fprintln(cmd.OutOrStdout(), name) |
| 289 | + } |
| 290 | + return nil |
| 291 | + }, |
| 292 | + } |
| 293 | +} |