1/*
2Copyright 2017 by the contributors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package main
18
19import (
20	"fmt"
21	"os"
22
23	"github.com/sirupsen/logrus"
24	"github.com/spf13/cobra"
25	"github.com/spf13/viper"
26)
27
28var initCmd = &cobra.Command{
29	Use:   "init",
30	Short: "Pre-generate certificate, private key, and kubeconfig files for the server.",
31	Long:  ``,
32	Run: func(cmd *cobra.Command, args []string) {
33		cfg, err := getConfig()
34		if err != nil {
35			fmt.Fprintf(os.Stderr, "could not get config: %v\n", err)
36			os.Exit(1)
37		}
38
39		localCfg := cfg
40		localCfg.GenerateKubeconfigPath = "aws-iam-authenticator.kubeconfig"
41		localCfg.StateDir = "./"
42
43		err = localCfg.GenerateFiles()
44		if err != nil {
45			fmt.Fprintf(os.Stderr, "could not initialize: %v\n", err)
46			os.Exit(1)
47		}
48
49		logrus.Infof("copy %s to %s on kubernetes master node(s)", localCfg.CertPath(), cfg.CertPath())
50		logrus.Infof("copy %s to %s on kubernetes master node(s)", localCfg.KeyPath(), cfg.KeyPath())
51		logrus.Infof("copy %s to %s on kubernetes master node(s)", localCfg.GenerateKubeconfigPath, cfg.GenerateKubeconfigPath)
52		logrus.Infof("configure your apiserver with `--authentication-token-webhook-config-file=%s` to enable authentication with aws-iam-authenticator", cfg.GenerateKubeconfigPath)
53	},
54}
55
56func init() {
57	initCmd.Flags().String(
58		"hostname",
59		"localhost",
60		"Hostname that should be used for writing the self-signed certificates")
61	viper.BindPFlag("server.hostname", initCmd.Flags().Lookup("hostname"))
62
63	initCmd.Flags().String(
64		"address",
65		"127.0.0.1",
66		"IP Address to bind the server to listen to. (should be a 127.0.0.1 or 0.0.0.0)")
67	viper.BindPFlag("server.address", initCmd.Flags().Lookup("address"))
68
69	rootCmd.AddCommand(initCmd)
70}
71