|
| 1 | +package nodepool |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strconv" |
| 6 | + "strings" |
| 7 | + |
| 8 | + cfg "github.com/coreos/kube-aws/config" |
| 9 | + "github.com/coreos/kube-aws/filegen" |
| 10 | + "github.com/coreos/kube-aws/nodepool/config" |
| 11 | + "github.com/spf13/cobra" |
| 12 | +) |
| 13 | + |
| 14 | +var ( |
| 15 | + cmdInit = &cobra.Command{ |
| 16 | + Use: "init", |
| 17 | + Short: "Initialize default node pool configuration", |
| 18 | + Long: ``, |
| 19 | + RunE: runCmdInit, |
| 20 | + SilenceUsage: true, |
| 21 | + } |
| 22 | + |
| 23 | + initOpts = config.ComputedConfig{} |
| 24 | +) |
| 25 | + |
| 26 | +func init() { |
| 27 | + NodePoolCmd.AddCommand(cmdInit) |
| 28 | + cmdInit.Flags().StringVar(&initOpts.AvailabilityZone, "availability-zone", "", "The AWS availability-zone to deploy to") |
| 29 | + cmdInit.Flags().StringVar(&initOpts.KeyName, "key-name", "", "The AWS key-pair for ssh access to nodes") |
| 30 | + cmdInit.Flags().StringVar(&initOpts.KMSKeyARN, "kms-key-arn", "", "The ARN of the AWS KMS key for encrypting TLS assets") |
| 31 | + cmdInit.Flags().StringVar(&initOpts.AmiId, "ami-id", "", "The AMI ID of CoreOS") |
| 32 | +} |
| 33 | + |
| 34 | +func runCmdInit(cmd *cobra.Command, args []string) error { |
| 35 | + initOpts.NodePoolName = nodePoolOpts.PoolName |
| 36 | + |
| 37 | + // Validate flags. |
| 38 | + required := []struct { |
| 39 | + name, val string |
| 40 | + }{ |
| 41 | + {"--node-pool-name", initOpts.NodePoolName}, |
| 42 | + {"--availability-zone", initOpts.AvailabilityZone}, |
| 43 | + } |
| 44 | + var missing []string |
| 45 | + for _, req := range required { |
| 46 | + if req.val == "" { |
| 47 | + missing = append(missing, strconv.Quote(req.name)) |
| 48 | + } |
| 49 | + } |
| 50 | + if len(missing) != 0 { |
| 51 | + return fmt.Errorf("Missing required flag(s): %s", strings.Join(missing, ", ")) |
| 52 | + } |
| 53 | + |
| 54 | + // Read the config from file. |
| 55 | + mainConfig, err := cfg.ClusterFromFile(clusterConfigFilePath()) |
| 56 | + if err != nil { |
| 57 | + return fmt.Errorf("Failed to read cluster config: %v", err) |
| 58 | + } |
| 59 | + |
| 60 | + main, err := mainConfig.Config() |
| 61 | + if err != nil { |
| 62 | + return fmt.Errorf("Failed to create config: %v", err) |
| 63 | + } |
| 64 | + |
| 65 | + // Required and inheritable settings for the node pool. |
| 66 | + // |
| 67 | + // These can be set via command-line options to kube-aws nodepool init. |
| 68 | + // If omitted, these can be inherited from the main cluster's cluster.yaml. |
| 69 | + |
| 70 | + if initOpts.Region == "" { |
| 71 | + initOpts.Region = main.Region |
| 72 | + } |
| 73 | + |
| 74 | + if initOpts.KeyName == "" { |
| 75 | + initOpts.KeyName = main.KeyName |
| 76 | + } |
| 77 | + |
| 78 | + if initOpts.KMSKeyARN == "" { |
| 79 | + initOpts.KMSKeyARN = main.KMSKeyARN |
| 80 | + } |
| 81 | + |
| 82 | + if initOpts.AmiId == "" { |
| 83 | + initOpts.AmiId = main.AmiId |
| 84 | + } |
| 85 | + |
| 86 | + if initOpts.ReleaseChannel == "" { |
| 87 | + initOpts.ReleaseChannel = main.ReleaseChannel |
| 88 | + } |
| 89 | + |
| 90 | + if initOpts.VPCCIDR == "" { |
| 91 | + initOpts.VPCCIDR = main.VPCCIDR |
| 92 | + } |
| 93 | + |
| 94 | + // Required, inheritable and importable settings for the node pool. |
| 95 | + // |
| 96 | + // These can be customized in the node pool's cluster.yaml |
| 97 | + // If omitted from it, these can also can be exported from the main cluster |
| 98 | + // and then imported to the node pool in the cloudformation layer. |
| 99 | + |
| 100 | + if initOpts.VPCID == "" { |
| 101 | + initOpts.VPCID = main.VPCID |
| 102 | + } |
| 103 | + |
| 104 | + if initOpts.RouteTableID == "" { |
| 105 | + initOpts.RouteTableID = main.RouteTableID |
| 106 | + } |
| 107 | + |
| 108 | + if initOpts.EtcdEndpoints == "" { |
| 109 | + initOpts.EtcdEndpoints = main.EtcdEndpoints |
| 110 | + } |
| 111 | + |
| 112 | + initOpts.ClusterName = main.ClusterName |
| 113 | + |
| 114 | + // Required and shared settings for the node pool. |
| 115 | + // |
| 116 | + // These can not be customized in the node pool's cluster yaml. |
| 117 | + // Customizing these values in each node pool doesn't make sense |
| 118 | + // because inconsistency between the main cluster and node pools result in |
| 119 | + // unusable worker nodes that can't communicate with k8s apiserver, kube-dns, calico, etc. |
| 120 | + |
| 121 | + initOpts.KubeClusterSettings = main.KubeClusterSettings |
| 122 | + |
| 123 | + if err := filegen.CreateFileFromTemplate(nodePoolClusterConfigFilePath(), initOpts, config.DefaultClusterConfig); err != nil { |
| 124 | + return fmt.Errorf("Error exec-ing default config template: %v", err) |
| 125 | + } |
| 126 | + |
| 127 | + successMsg := |
| 128 | + `Success! Created %s |
| 129 | +
|
| 130 | +Next steps: |
| 131 | +1. (Optional) Edit %s to parameterize the cluster. |
| 132 | +2. Use the "kube-aws nodepool render" command to render the stack template. |
| 133 | +` |
| 134 | + |
| 135 | + fmt.Printf(successMsg, nodePoolClusterConfigFilePath, nodePoolClusterConfigFilePath) |
| 136 | + return nil |
| 137 | +} |
0 commit comments