Skip to content

Commit

Permalink
[#1041] Restructure cli for building providers (#1159)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrismatix committed Mar 5, 2021
1 parent 7669181 commit c196e5f
Show file tree
Hide file tree
Showing 12 changed files with 143 additions and 62 deletions.
5 changes: 3 additions & 2 deletions docs/docs/cli/reference.md
Expand Up @@ -94,8 +94,9 @@ airy create [flags]
#### Options

```
-h, --help help for create
--provider string One of the supported providers (aws|local|minikube). Default is aws (default "local")
-h, --help help for create
--namespace string (optional) Kubernetes namespace that Airy should be installed to. (default "default")
--provider string One of the supported providers (aws|local|minikube). (default "local")
```

#### Options inherited from parent commands
Expand Down
3 changes: 3 additions & 0 deletions infrastructure/cli/pkg/cmd/create/BUILD
Expand Up @@ -18,6 +18,9 @@ go_library(
],
importpath = "cli/pkg/cmd/create",
visibility = ["//visibility:public"],
x_defs = {
"version": "{STABLE_VERSION}",
},
deps = [
"//infrastructure/cli/pkg/providers",
"@com_github_spf13_cobra//:cobra",
Expand Down
24 changes: 17 additions & 7 deletions infrastructure/cli/pkg/cmd/create/create.go
Expand Up @@ -8,9 +8,10 @@ import (
)

var (
provider string
version string
CreateCmd = &cobra.Command{
provider string
namespace string
version string
CreateCmd = &cobra.Command{
Use: "create",
Short: "Creates an instance of Airy Core",
Long: ``,
Expand All @@ -19,21 +20,30 @@ var (
)

func init() {
CreateCmd.Flags().StringVar(&provider, "provider", "local", "One of the supported providers (aws|local|minikube). Default is aws")
CreateCmd.Flags().StringVar(&provider, "provider", "local", "One of the supported providers (aws|local|minikube).")
CreateCmd.Flags().StringVar(&namespace, "namespace", "default", "(optional) Kubernetes namespace that Airy should be installed to.")
CreateCmd.MarkFlagRequired("provider")

}

func create(cmd *cobra.Command, args []string) {
fmt.Println("⚙️ Creating core with provider", provider)

clientset, err := providers.GetProvider(providers.Provider(provider))
provider := providers.MustGet(providers.ProviderName(provider))

context, err := provider.Provision()
if err != nil {
fmt.Println("could not provision cluster: ", err)
os.Exit(1)
}

clientset, err := context.GetClientSet()
if err != nil {
fmt.Println("provisioning cluster failed: ", err)
fmt.Println("could not get clientset: ", err)
os.Exit(1)
}

helm := New(clientset, "develop", "default")
helm := New(clientset, version, namespace)
if err := helm.Setup(); err != nil {
fmt.Println("setting up Helm failed with err: ", err)
os.Exit(1)
Expand Down
12 changes: 12 additions & 0 deletions infrastructure/cli/pkg/kube/BUILD
@@ -0,0 +1,12 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "kube",
srcs = ["context.go"],
importpath = "cli/pkg/kube",
visibility = ["//visibility:public"],
deps = [
"@io_k8s_client_go//kubernetes:go_default_library",
"@io_k8s_client_go//tools/clientcmd:go_default_library",
],
)
41 changes: 41 additions & 0 deletions infrastructure/cli/pkg/kube/context.go
@@ -0,0 +1,41 @@
package kube

import (
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)

type KubeCtx struct {
kubeConfigPath string
contextName string
}

func New(kubeConfigPath, contextName string) KubeCtx {
return KubeCtx{
kubeConfigPath: kubeConfigPath,
contextName: contextName,
}
}

func (c *KubeCtx) GetClientSet() (*kubernetes.Clientset, error) {
if c.contextName == "" {
config, err := clientcmd.BuildConfigFromFlags("", c.kubeConfigPath)
if err != nil {
return nil, err
}

return kubernetes.NewForConfig(config)
}

file, err := clientcmd.LoadFromFile(c.kubeConfigPath)
if err != nil {
return nil, err
}

config, err := clientcmd.NewNonInteractiveClientConfig(*file, c.contextName, nil, nil).ClientConfig()
if err != nil {
return nil, err
}

return kubernetes.NewForConfig(config)
}
5 changes: 2 additions & 3 deletions infrastructure/cli/pkg/providers/BUILD
Expand Up @@ -2,13 +2,12 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "providers",
srcs = ["providers.go"],
srcs = ["provider.go"],
importpath = "cli/pkg/providers",
visibility = ["//visibility:public"],
deps = [
"//infrastructure/cli/pkg/kube",
"//infrastructure/cli/pkg/providers/aws",
"//infrastructure/cli/pkg/providers/minikube",
"@io_k8s_client_go//kubernetes:go_default_library",
"@io_k8s_client_go//tools/clientcmd:go_default_library",
],
)
2 changes: 1 addition & 1 deletion infrastructure/cli/pkg/providers/aws/BUILD
Expand Up @@ -5,5 +5,5 @@ go_library(
srcs = ["aws.go"],
importpath = "cli/pkg/providers/aws",
visibility = ["//visibility:public"],
deps = ["@io_k8s_client_go//kubernetes:go_default_library"],
deps = ["//infrastructure/cli/pkg/kube"],
)
12 changes: 8 additions & 4 deletions infrastructure/cli/pkg/providers/aws/aws.go
@@ -1,14 +1,18 @@
package aws

import (
"cli/pkg/kube"
"fmt"
"k8s.io/client-go/kubernetes"
"os"
)

type Aws struct {
}

func Create() (*kubernetes.Clientset, error) {
fmt.Println("aws provider not yet implemented")
func (a *Aws) Provision() (kube.KubeCtx, error) {
// Use this to
//clientcmd.NewNonInteractiveClientConfig()
fmt.Println("minikube provider not yet implemented")
os.Exit(1)
return nil, nil
return kube.KubeCtx{}, nil
}
2 changes: 1 addition & 1 deletion infrastructure/cli/pkg/providers/minikube/BUILD
Expand Up @@ -5,5 +5,5 @@ go_library(
srcs = ["minikube.go"],
importpath = "cli/pkg/providers/minikube",
visibility = ["//visibility:public"],
deps = ["@io_k8s_client_go//kubernetes:go_default_library"],
deps = ["//infrastructure/cli/pkg/kube"],
)
12 changes: 9 additions & 3 deletions infrastructure/cli/pkg/providers/minikube/minikube.go
@@ -1,14 +1,20 @@
package minikube

import (
"cli/pkg/kube"
"fmt"
"k8s.io/client-go/kubernetes"
"os"
)

func Create() (*kubernetes.Clientset, error) {
type Minikube struct {
}

func (m *Minikube) Provision() (kube.KubeCtx, error) {
// Use this to
//clientcmd.NewNonInteractiveClientConfig()
fmt.Println("minikube provider not yet implemented")
os.Exit(1)
return nil, nil
return kube.KubeCtx{}, nil
}


46 changes: 46 additions & 0 deletions infrastructure/cli/pkg/providers/provider.go
@@ -0,0 +1,46 @@
package providers

import (
"cli/pkg/kube"
"cli/pkg/providers/aws"
"cli/pkg/providers/minikube"
"fmt"
"os"
)

type ProviderName string

const (
Local ProviderName = "local"
Minikube ProviderName = "minikube"
Aws ProviderName = "aws"
)

type Provider interface {
Provision() (kube.KubeCtx, error)
}

func MustGet(providerName ProviderName) Provider {
if providerName == Minikube {
return &minikube.Minikube{}
}

if providerName == Aws {
return &aws.Aws{}
}

// TODO remove this provider in #1041
if providerName == Local {
return &LocalProvider{}
}

panic(fmt.Sprintf("unknown provider \"%v\"", providerName))
}

// TODO remove this provider in #1041
type LocalProvider struct {
}

func (l *LocalProvider) Provision() (kube.KubeCtx, error) {
return kube.New(os.Getenv("KUBE_CONFIG_PATH"), ""), nil
}
41 changes: 0 additions & 41 deletions infrastructure/cli/pkg/providers/providers.go

This file was deleted.

0 comments on commit c196e5f

Please sign in to comment.