Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support zone autocompletion #12366

Merged
merged 2 commits into from
Sep 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/kops/BUILD.bazel

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 39 additions & 7 deletions cmd/kops/create_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"io"
"io/ioutil"
"os"
"sort"
"strings"

"github.com/aws/aws-sdk-go/service/ec2"
Expand All @@ -42,12 +43,14 @@ import (
kopsutil "k8s.io/kops/pkg/apis/kops/util"
"k8s.io/kops/pkg/apis/kops/validation"
"k8s.io/kops/pkg/assets"
"k8s.io/kops/pkg/clouds"
"k8s.io/kops/pkg/clusteraddons"
"k8s.io/kops/pkg/commands"
"k8s.io/kops/pkg/commands/commandutils"
"k8s.io/kops/pkg/featureflag"
"k8s.io/kops/pkg/kubeconfig"
"k8s.io/kops/pkg/kubemanifest"
"k8s.io/kops/pkg/zones"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup"
"k8s.io/kops/upup/pkg/fi/utils"
Expand Down Expand Up @@ -218,15 +221,24 @@ func NewCmdCreateCluster(f *util.Factory, out io.Writer) *cobra.Command {
})
}

cmd.Flags().StringVar(&options.CloudProvider, "cloud", options.CloudProvider, fmt.Sprintf("Cloud provider to use - %s", strings.Join(cloudup.SupportedClouds(), ", ")))
var validClouds []string
{
allClouds := clouds.SupportedClouds()
var validClouds []string
for _, c := range allClouds {
validClouds = append(validClouds, string(c))
}
sort.Strings(validClouds)
}
cmd.Flags().StringVar(&options.CloudProvider, "cloud", options.CloudProvider, fmt.Sprintf("Cloud provider to use - %s", strings.Join(validClouds, ", ")))
cmd.RegisterFlagCompletionFunc("cloud", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return cloudup.SupportedClouds(), cobra.ShellCompDirectiveNoFileComp
return validClouds, cobra.ShellCompDirectiveNoFileComp
})

cmd.Flags().StringSliceVar(&options.Zones, "zones", options.Zones, "Zones in which to run the cluster")
cmd.RegisterFlagCompletionFunc("zones", completeZone)
cmd.RegisterFlagCompletionFunc("zones", completeZone(options))
cmd.Flags().StringSliceVar(&options.MasterZones, "master-zones", options.MasterZones, "Zones in which to run masters (must be an odd number)")
cmd.RegisterFlagCompletionFunc("master-zones", completeZone)
cmd.RegisterFlagCompletionFunc("master-zones", completeZone(options))

if featureflag.ClusterAddons.Enabled() {
cmd.Flags().StringSliceVar(&options.AddonPaths, "add", options.AddonPaths, "Paths to addons we should add to the cluster")
Expand Down Expand Up @@ -829,9 +841,29 @@ func loadSSHPublicKeys(sshPublicKey string) (map[string][]byte, error) {
return sshPublicKeys, nil
}

func completeZone(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
// TODO call into cloud provider(s) to get list of valid zones
return nil, cobra.ShellCompDirectiveNoFileComp
func completeZone(options *CreateClusterOptions) func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
var allClouds []api.CloudProviderID
if options.CloudProvider != "" {
allClouds = []api.CloudProviderID{api.CloudProviderID(options.CloudProvider)}
} else {
allClouds = clouds.SupportedClouds()
}

var allZones []string
for _, c := range allClouds {
zones := zones.WellKnownZonesForCloud(c)
for _, z := range zones {
if options.CloudProvider == "" {
allZones = append(allZones, z+"\t"+string(c))
} else {
allZones = append(allZones, z)
}
}
}
sort.Strings(allZones)
return allZones, cobra.ShellCompDirectiveNoFileComp
}
}

func completeKubernetesVersion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
Expand Down
2 changes: 1 addition & 1 deletion docs/cli/kops_create_cluster.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions pkg/clouds/BUILD.bazel

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions pkg/clouds/supported.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
Copyright 2021 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package clouds

import (
"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/featureflag"
)

func SupportedClouds() []kops.CloudProviderID {
clouds := []kops.CloudProviderID{
kops.CloudProviderAWS,
kops.CloudProviderDO,
kops.CloudProviderOpenstack,
}
if featureflag.AlphaAllowALI.Enabled() {
clouds = append(clouds, kops.CloudProviderALI)
}
if featureflag.Azure.Enabled() {
clouds = append(clouds, kops.CloudProviderAzure)
}
if featureflag.AlphaAllowGCE.Enabled() {
clouds = append(clouds, kops.CloudProviderGCE)
}

return clouds
}
13 changes: 13 additions & 0 deletions pkg/zones/BUILD.bazel

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading