*: add peer VPN support

This commit adds support for defining arbitrary peers that should have
access to the VPN. In k8s, this is accomplished using the new Peer CRD.
This commit is contained in:
Lucas Servén Marín
2019-05-03 12:53:40 +02:00
parent 46f55c337b
commit 2425a06cd8
47 changed files with 15812 additions and 505 deletions

View File

@@ -21,7 +21,7 @@ import (
"github.com/squat/kilo/pkg/mesh"
)
func newGraph() *cobra.Command {
func graph() *cobra.Command {
return &cobra.Command{
Use: "graph",
Short: "Generates a graph of the Kilo network",
@@ -31,7 +31,7 @@ func newGraph() *cobra.Command {
}
func runGraph(_ *cobra.Command, _ []string) error {
ns, err := opts.backend.List()
ns, err := opts.backend.Nodes().List()
if err != nil {
return fmt.Errorf("failed to list nodes: %v", err)
}
@@ -46,7 +46,7 @@ func runGraph(_ *cobra.Command, _ []string) error {
if len(nodes) == 0 {
return fmt.Errorf("did not find any valid Kilo nodes in the cluster")
}
t, err := mesh.NewTopology(nodes, opts.granularity, hostname, 0, []byte{}, opts.subnet)
t, err := mesh.NewTopology(nodes, nil, opts.granularity, hostname, 0, []byte{}, opts.subnet)
if err != nil {
return fmt.Errorf("failed to create topology: %v", err)
}

View File

@@ -20,11 +20,13 @@ import (
"os"
"strings"
"github.com/spf13/cobra"
apiextensions "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"github.com/spf13/cobra"
"github.com/squat/kilo/pkg/k8s"
kiloclient "github.com/squat/kilo/pkg/k8s/clientset/versioned"
"github.com/squat/kilo/pkg/mesh"
"github.com/squat/kilo/pkg/version"
)
@@ -86,14 +88,20 @@ func runRoot(_ *cobra.Command, _ []string) error {
if err != nil {
return fmt.Errorf("failed to create Kubernetes config: %v", err)
}
client := kubernetes.NewForConfigOrDie(config)
opts.backend = k8s.New(client)
c := kubernetes.NewForConfigOrDie(config)
kc := kiloclient.NewForConfigOrDie(config)
ec := apiextensions.NewForConfigOrDie(config)
opts.backend = k8s.New(c, kc, ec)
default:
return fmt.Errorf("backend %v unknown; posible values are: %s", backend, availableBackends)
}
if err := opts.backend.Init(make(chan struct{})); err != nil {
return fmt.Errorf("failed to initialize backend: %v", err)
if err := opts.backend.Nodes().Init(make(chan struct{})); err != nil {
return fmt.Errorf("failed to initialize node backend: %v", err)
}
if err := opts.backend.Peers().Init(make(chan struct{})); err != nil {
return fmt.Errorf("failed to initialize peer backend: %v", err)
}
return nil
}
@@ -112,7 +120,8 @@ func main() {
cmd.PersistentFlags().StringVar(&subnet, "subnet", "10.4.0.0/16", "CIDR from which to allocate addressees to WireGuard interfaces.")
for _, subCmd := range []*cobra.Command{
newGraph(),
graph(),
showConf(),
} {
cmd.AddCommand(subCmd)
}

146
cmd/kgctl/showconf.go Normal file
View File

@@ -0,0 +1,146 @@
// Copyright 2019 the Kilo authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"errors"
"fmt"
"github.com/spf13/cobra"
"github.com/squat/kilo/pkg/mesh"
)
func showConf() *cobra.Command {
cmd := &cobra.Command{
Use: "showconf",
Short: "Show the WireGuard configuration for a node or peer in the Kilo network",
Long: "",
}
for _, subCmd := range []*cobra.Command{
showConfNode(),
showConfPeer(),
} {
cmd.AddCommand(subCmd)
}
return cmd
}
func showConfNode() *cobra.Command {
return &cobra.Command{
Use: "node",
Short: "Show the WireGuard configuration for a node in the Kilo network",
Long: "",
RunE: runShowConfNode,
Args: cobra.ExactArgs(1),
}
}
func showConfPeer() *cobra.Command {
return &cobra.Command{
Use: "peer",
Short: "Show the WireGuard configuration for a peer in the Kilo network",
Long: "",
RunE: runShowConfPeer,
Args: cobra.ExactArgs(1),
}
}
func runShowConfNode(_ *cobra.Command, args []string) error {
ns, err := opts.backend.Nodes().List()
if err != nil {
return fmt.Errorf("failed to list nodes: %v", err)
}
ps, err := opts.backend.Peers().List()
if err != nil {
return fmt.Errorf("failed to list peers: %v", err)
}
hostname := args[0]
nodes := make(map[string]*mesh.Node)
for _, n := range ns {
if n.Ready() {
nodes[n.Name] = n
}
}
if len(nodes) == 0 {
return errors.New("did not find any valid Kilo nodes in the cluster")
}
if _, ok := nodes[hostname]; !ok {
return fmt.Errorf("did not find any node named %q in the cluster", hostname)
}
peers := make(map[string]*mesh.Peer)
for _, p := range ps {
if p.Ready() {
peers[p.Name] = p
}
}
t, err := mesh.NewTopology(nodes, peers, opts.granularity, hostname, mesh.DefaultKiloPort, []byte{}, opts.subnet)
if err != nil {
return fmt.Errorf("failed to create topology: %v", err)
}
c, err := t.Conf().Bytes()
if err != nil {
return fmt.Errorf("failed to generate configuration: %v", err)
}
fmt.Printf(string(c))
return nil
}
func runShowConfPeer(_ *cobra.Command, args []string) error {
ns, err := opts.backend.Nodes().List()
if err != nil {
return fmt.Errorf("failed to list nodes: %v", err)
}
ps, err := opts.backend.Peers().List()
if err != nil {
return fmt.Errorf("failed to list peers: %v", err)
}
var hostname string
nodes := make(map[string]*mesh.Node)
for _, n := range ns {
if n.Ready() {
nodes[n.Name] = n
hostname = n.Name
}
}
if len(nodes) == 0 {
return errors.New("did not find any valid Kilo nodes in the cluster")
}
peer := args[0]
peers := make(map[string]*mesh.Peer)
for _, p := range ps {
if p.Ready() {
peers[p.Name] = p
}
}
if _, ok := peers[peer]; !ok {
return fmt.Errorf("did not find any peer named %q in the cluster", peer)
}
t, err := mesh.NewTopology(nodes, peers, opts.granularity, hostname, mesh.DefaultKiloPort, []byte{}, opts.subnet)
if err != nil {
return fmt.Errorf("failed to create topology: %v", err)
}
c, err := t.PeerConf(peer).Bytes()
if err != nil {
return fmt.Errorf("failed to generate configuration: %v", err)
}
fmt.Printf(string(c))
return nil
}