|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + ctx "context" |
| 5 | + "encoding/base64" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + |
| 9 | + "github.com/flux-subsystem-argo/flamingo/pkg/utils" |
| 10 | + "github.com/spf13/cobra" |
| 11 | + "k8s.io/client-go/tools/clientcmd" |
| 12 | +) |
| 13 | + |
| 14 | +var addClusterCmd = &cobra.Command{ |
| 15 | + Use: "add-cluster CONTEXT_NAME", |
| 16 | + Short: "Add a cluster to Flamingo", |
| 17 | + Long: ` |
| 18 | +# Add a cluster to Flamingo |
| 19 | +flamingo add-cluster my-cluster |
| 20 | +
|
| 21 | +# Add cluster dev-1, override the name and address, and skip TLS verification |
| 22 | +flamingo add-cluster dev-1 \ |
| 23 | + --server-name=dev-1.example.com \ |
| 24 | + --server-addr=https://dev-1.example.com:6443 \ |
| 25 | + --insecure |
| 26 | +`, |
| 27 | + Args: cobra.ExactArgs(1), |
| 28 | + RunE: addClusterCmdRun, |
| 29 | +} |
| 30 | + |
| 31 | +var addClusterFlags struct { |
| 32 | + insecureSkipTLSVerify bool |
| 33 | + serverName string |
| 34 | + serverAddress string |
| 35 | + export bool |
| 36 | +} |
| 37 | + |
| 38 | +func init() { |
| 39 | + addClusterCmd.Flags().BoolVar(&addClusterFlags.insecureSkipTLSVerify, "insecure", false, "If true, the server's certificate will not be checked for validity. This will make your HTTPS connections insecure") |
| 40 | + addClusterCmd.Flags().StringVar(&addClusterFlags.serverName, "server-name", "", "If set, this overrides the hostname used to validate the server certificate") |
| 41 | + addClusterCmd.Flags().StringVar(&addClusterFlags.serverAddress, "server-addr", "", "If set, this overrides the server address used to connect to the cluster") |
| 42 | + addClusterCmd.Flags().BoolVar(&addClusterFlags.export, "export", false, "export manifests instead of installing") |
| 43 | + |
| 44 | + rootCmd.AddCommand(addClusterCmd) |
| 45 | +} |
| 46 | + |
| 47 | +func addClusterCmdRun(cmd *cobra.Command, args []string) error { |
| 48 | + leafClusterContext := args[0] |
| 49 | + |
| 50 | + kubeconfig := "" |
| 51 | + if *kubeconfigArgs.KubeConfig == "" { |
| 52 | + kubeconfig = clientcmd.RecommendedHomeFile |
| 53 | + } else { |
| 54 | + kubeconfig = *kubeconfigArgs.KubeConfig |
| 55 | + } |
| 56 | + |
| 57 | + // Load the kubeconfig file |
| 58 | + config, err := clientcmd.LoadFromFile(kubeconfig) |
| 59 | + if err != nil { |
| 60 | + return err |
| 61 | + } |
| 62 | + |
| 63 | + // Specify the context name |
| 64 | + contextName := leafClusterContext |
| 65 | + |
| 66 | + // Get the context |
| 67 | + context, exists := config.Contexts[contextName] |
| 68 | + if !exists { |
| 69 | + return fmt.Errorf("context not found") |
| 70 | + } |
| 71 | + |
| 72 | + // Get the cluster info |
| 73 | + cluster, exists := config.Clusters[context.Cluster] |
| 74 | + if !exists { |
| 75 | + return fmt.Errorf("cluster not found") |
| 76 | + } |
| 77 | + |
| 78 | + // Get the user info |
| 79 | + user, exists := config.AuthInfos[context.AuthInfo] |
| 80 | + if !exists { |
| 81 | + return fmt.Errorf("user not found") |
| 82 | + } |
| 83 | + |
| 84 | + template := `apiVersion: v1 |
| 85 | +kind: Secret |
| 86 | +metadata: |
| 87 | + name: %s-cluster |
| 88 | + namespace: argocd |
| 89 | + labels: |
| 90 | + argocd.argoproj.io/secret-type: cluster |
| 91 | + flamingo/cluster: "true" |
| 92 | + annotations: |
| 93 | + flamingo/external-address: "%s" |
| 94 | + flamingo/internal-address: "%s" |
| 95 | +type: Opaque |
| 96 | +stringData: |
| 97 | + name: %s |
| 98 | + server: %s |
| 99 | + config: | |
| 100 | + { |
| 101 | + "tlsClientConfig": { |
| 102 | + "insecure": %v, |
| 103 | + "certData": "%s", |
| 104 | + "keyData": "%s", |
| 105 | + "serverName": "%s" |
| 106 | + } |
| 107 | + } |
| 108 | +` |
| 109 | + serverAddress := addClusterFlags.serverAddress |
| 110 | + if serverAddress == "" { |
| 111 | + serverAddress = cluster.Server |
| 112 | + } |
| 113 | + |
| 114 | + result := fmt.Sprintf(template, |
| 115 | + contextName, |
| 116 | + cluster.Server, // external address (known to the user via kubectl config view) |
| 117 | + serverAddress, // internal address |
| 118 | + contextName, |
| 119 | + serverAddress, |
| 120 | + addClusterFlags.insecureSkipTLSVerify, |
| 121 | + base64.StdEncoding.EncodeToString(user.ClientCertificateData), |
| 122 | + base64.StdEncoding.EncodeToString(user.ClientKeyData), |
| 123 | + addClusterFlags.serverName, |
| 124 | + ) |
| 125 | + |
| 126 | + if addClusterFlags.export { |
| 127 | + fmt.Print(result) |
| 128 | + return nil |
| 129 | + } else { |
| 130 | + logger.Actionf("applying generated cluster secret %s in %s namespace", contextName+"-cluster", rootArgs.applicationNamespace) |
| 131 | + applyOutput, err := utils.Apply(ctx.Background(), kubeconfigArgs, kubeclientOptions, []byte(result)) |
| 132 | + if err != nil { |
| 133 | + return fmt.Errorf("apply failed: %w", err) |
| 134 | + } |
| 135 | + fmt.Fprintln(os.Stderr, applyOutput) |
| 136 | + } |
| 137 | + |
| 138 | + return nil |
| 139 | +} |
0 commit comments