Skip to content

Commit

Permalink
Add k3s support.
Browse files Browse the repository at this point in the history
  • Loading branch information
ColinChartier committed Aug 21, 2019
1 parent 85a9ba2 commit d2e0b88
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 6 deletions.
12 changes: 11 additions & 1 deletion provisioners/common.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package provisioners
package provisioners

import (
"fmt"
"github.com/distributed-containers-inc/sanic/provisioners/external"
"github.com/distributed-containers-inc/sanic/provisioners/k3s"
"github.com/distributed-containers-inc/sanic/provisioners/localdev"
"github.com/distributed-containers-inc/sanic/provisioners/provisioner"
)
Expand All @@ -13,6 +14,9 @@ var provisionerBuilders = map[string]provisionerBuilder{
"external": func(args map[string]string) provisioner.Provisioner {
return external.Create(args)
},
"k3s": func(args map[string]string) provisioner.Provisioner {
return &k3s.ProvisionerK3s{}
},
"localdev": func(args map[string]string) provisioner.Provisioner {
return &localdev.ProvisionerLocalDev{}
},
Expand All @@ -24,6 +28,12 @@ var provisionerValidators = map[string]provisionerConfigValidator{
"external": func(args map[string]string) error {
return external.ValidateConfig(args)
},
"k3s": func(args map[string]string) error {
for k := range args {
return fmt.Errorf("k3s takes no arguments, got %s", k)
}
return nil
},
"localdev": func(args map[string]string) error {
for k := range args {
return fmt.Errorf("localdev takes no arguments, got %s", k)
Expand Down
4 changes: 4 additions & 0 deletions provisioners/external/external.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ func (provisioner *ProvisionerExternal) EnsureCluster() error {

//KubectlCommand for external just returns a provisioner that has KUBECONFIG pointing to the configured directory
func (provisioner *ProvisionerExternal) KubectlCommand(args ...string) (*exec.Cmd, error) {
if _, err := exec.LookPath("kubectl"); err != nil {
return nil, errors.Wrap(err, "could not find kubectl executable in path - is it installed?")
}

cmd := exec.Command("kubectl", args...)

kubeconfig := provisioner.kubeConfigLocation
Expand Down
80 changes: 80 additions & 0 deletions provisioners/k3s/k3s.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package k3s

import (
"context"
"fmt"
provisionerutil "github.com/distributed-containers-inc/sanic/provisioners/util"
"github.com/pkg/errors"
"os"
"os/exec"
"strings"
)

//ProvisionerK3s starts k3s server
type ProvisionerK3s struct {
}

//EnsureCluster just ensures the registry is running
func (provisioner *ProvisionerK3s) EnsureCluster() error {
return provisionerutil.StartRegistry(provisioner, context.Background(), map[string]string{"node-role.kubernetes.io/master": "true"})
}

//KubectlCommand for k3s is just a wrapper around "k3s kubectl"
func (provisioner *ProvisionerK3s) KubectlCommand(args ...string) (*exec.Cmd, error) {
if _, err := exec.LookPath("k3s"); err != nil {
return nil, errors.Wrap(err, "could not find k3s executable in path - is it installed?")
}
cmd := exec.Command("k3s", append([]string{"kubectl"}, args...)...)
cmd.Env = os.Environ()

return cmd, nil
}

func (provisioner *ProvisionerK3s) Registry() (registryAddr string, registryInsecure bool, err error) {
cmd, err := provisioner.KubectlCommand(
"get", "service",
"--namespace", "kube-system",
"sanic-registry",
"--output", "jsonpath={.spec.clusterIP}",
)
if err != nil {
return
}
out, err := cmd.Output()
if err != nil {
return
}
ip := strings.TrimSpace(string(out))
if ip == "" {
err = fmt.Errorf("could not connect to registry - try 'sanic deploy' and waiting 90 seconds")
return
}
registryAddr = fmt.Sprintf("%s:5000", ip)
registryInsecure = true
return
}

func (provisioner *ProvisionerK3s) EdgeNodes() ([]string, error) {
cmd, err := provisioner.KubectlCommand(
"get", "services",
"-n", "kube-system",
"-o", "jsonpath={.spec.clusterIP}",
"traefik",
)
if err != nil {
return nil, errors.Wrap(err, "could not get the traefik service")
}
out, err := cmd.Output()
if err != nil {
return nil, errors.Wrap(err, "could not get the traefik service")
}
ip := strings.TrimSpace(string(out))
if ip == "" {
return nil, fmt.Errorf("could not get the IP of the traefik service")
}
return []string{ip}, nil
}

func (provisioner *ProvisionerK3s) InClusterDir(hostDir string) string {
return hostDir //k3s runs the server on the computer itself
}
5 changes: 4 additions & 1 deletion provisioners/localdev/localdev.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ type ProvisionerLocalDev struct{}

//KubectlCommand : In ProvisionerLocalDev, returns kubectl pointing to kind's own generated configuration
func (provisioner *ProvisionerLocalDev) KubectlCommand(args ...string) (*exec.Cmd, error) {
if _, err := exec.LookPath("kubectl"); err != nil {
return nil, errors.Wrap(err, "could not find kubectl executable in path - is it installed?")
}
cmd := exec.Command("kubectl", args...)
configPath := kindContext.KubeConfigPath()
if _, err := os.Stat(configPath); err != nil {
Expand All @@ -55,7 +58,7 @@ func (provisioner *ProvisionerLocalDev) EnsureCluster() error {
return err
}
err = util.RunContextuallyInParallel(context.Background(),
func(ctx context.Context) error { return provisionerutil.StartRegistry(provisioner, ctx) },
func(ctx context.Context) error { return provisionerutil.StartRegistry(provisioner, ctx, map[string]string{"node-role.kubernetes.io/master": ""}) },
provisioner.patchRegistryContainers,
provisioner.startIngressController,
)
Expand Down
9 changes: 5 additions & 4 deletions provisioners/util/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ spec:
name: sanic-registry
spec:
terminationGracePeriodSeconds: 10
hostNetwork: true
nodeSelector:
node-role.kubernetes.io/master: ""
{{range $key, $value := .NodeSelectors}}{{$key}}: "{{$value}}"
{{end}}
tolerations:
- key: node-role.kubernetes.io/master
operator: Exists
Expand Down Expand Up @@ -67,7 +67,7 @@ spec:
`

//StartRegistry : makes a pod definition using registry:2
func StartRegistry(provisioner provisioner.Provisioner, ctx context.Context) error {
func StartRegistry(provisioner provisioner.Provisioner, ctx context.Context, nodeSelectors map[string]string) error {
cmd, err := provisioner.KubectlCommand("apply", "-f", "-")

if err != nil {
Expand All @@ -76,14 +76,15 @@ func StartRegistry(provisioner provisioner.Provisioner, ctx context.Context) err

type yamlConfig struct {
RegistryNodePort int
NodeSelectors map[string]string
}
t, err := template.New("").Parse(registryYaml)
if err != nil {
panic(err)
}

stdinBuffer := &bytes.Buffer{}
err = t.Execute(stdinBuffer, &yamlConfig{RegistryNodePort: RegistryNodePort})
err = t.Execute(stdinBuffer, &yamlConfig{RegistryNodePort: RegistryNodePort, NodeSelectors: nodeSelectors})
if err != nil {
panic(err)
}
Expand Down

0 comments on commit d2e0b88

Please sign in to comment.