|
| 1 | +/* |
| 2 | +Copyright 2019 The kubewg Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package main |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "flag" |
| 22 | + "net" |
| 23 | + "os" |
| 24 | + "strings" |
| 25 | + "text/template" |
| 26 | + |
| 27 | + "k8s.io/apimachinery/pkg/runtime" |
| 28 | + _ "k8s.io/client-go/plugin/pkg/client/auth/gcp" |
| 29 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 30 | + "sigs.k8s.io/controller-runtime/pkg/client/config" |
| 31 | + logf "sigs.k8s.io/controller-runtime/pkg/runtime/log" |
| 32 | + |
| 33 | + "github.com/munnerz/kubewg/pkg/apis" |
| 34 | + wgv1alpha1 "github.com/munnerz/kubewg/pkg/apis/wg/v1alpha1" |
| 35 | +) |
| 36 | + |
| 37 | +var scheme = runtime.NewScheme() |
| 38 | + |
| 39 | +var ( |
| 40 | + privateKey = flag.String("private-key", "", "private key to use in the generated config") |
| 41 | + peerName = flag.String("peer-name", "", "name of the peer to get config for") |
| 42 | + peerNamespace = flag.String("peer-namespace", "", "namespace of the peer to get config for") |
| 43 | +) |
| 44 | + |
| 45 | +func main() { |
| 46 | + flag.Parse() |
| 47 | + logf.SetLogger(logf.ZapLogger(false)) |
| 48 | + log := logf.Log.WithName("mkconf") |
| 49 | + |
| 50 | + log.Info("using private key", "key", *privateKey) |
| 51 | + // Get a config to talk to the apiserver |
| 52 | + //log.Info("setting up client for manager") |
| 53 | + cfg, err := config.GetConfig() |
| 54 | + if err != nil { |
| 55 | + log.Error(err, "unable to set up client config") |
| 56 | + os.Exit(1) |
| 57 | + } |
| 58 | + |
| 59 | + // Setup Scheme for all resources |
| 60 | + //log.Info("setting up scheme") |
| 61 | + if err := apis.AddToScheme(scheme); err != nil { |
| 62 | + log.Error(err, "unable add APIs to scheme") |
| 63 | + os.Exit(1) |
| 64 | + } |
| 65 | + |
| 66 | + cl, err := client.New(cfg, client.Options{ |
| 67 | + Scheme: scheme, |
| 68 | + }) |
| 69 | + if err != nil { |
| 70 | + log.Error(err, "error creating client") |
| 71 | + os.Exit(1) |
| 72 | + } |
| 73 | + |
| 74 | + var p wgv1alpha1.Peer |
| 75 | + if err := cl.Get(context.TODO(), client.ObjectKey{ |
| 76 | + Namespace: *peerNamespace, |
| 77 | + Name: *peerName, |
| 78 | + }, &p); err != nil { |
| 79 | + log.Error(err, "error getting peer") |
| 80 | + os.Exit(1) |
| 81 | + } |
| 82 | + |
| 83 | + err = generateConf(&p) |
| 84 | + if err != nil { |
| 85 | + log.Error(err, "error generating config") |
| 86 | + os.Exit(1) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func generateConf(p *wgv1alpha1.Peer) error { |
| 91 | + _, port, err := net.SplitHostPort(p.Spec.Endpoint) |
| 92 | + if err != nil { |
| 93 | + return err |
| 94 | + } |
| 95 | + var peers []peerData |
| 96 | + for _, peer := range p.Status.Peers { |
| 97 | + peers = append(peers, peerData{ |
| 98 | + PublicKey: peer.PublicKey, |
| 99 | + AllowedIPs: strings.Join(peer.AllowedIPs, ","), |
| 100 | + Endpoint: peer.Endpoint, |
| 101 | + }) |
| 102 | + } |
| 103 | + data := gotmpldata{ |
| 104 | + Address: p.Status.Address + "/32", |
| 105 | + ListenPort: port, |
| 106 | + PrivateKey: *privateKey, |
| 107 | + Peers: peers, |
| 108 | + } |
| 109 | + t, err := template.New("peerconfig").Parse(gotmpl) |
| 110 | + if err != nil { |
| 111 | + return err |
| 112 | + } |
| 113 | + |
| 114 | + err = t.Execute(os.Stdout, data) |
| 115 | + if err != nil { |
| 116 | + return err |
| 117 | + } |
| 118 | + |
| 119 | + return nil |
| 120 | +} |
| 121 | + |
| 122 | +type gotmpldata struct { |
| 123 | + Address string |
| 124 | + ListenPort string |
| 125 | + PrivateKey string |
| 126 | + Peers []peerData |
| 127 | +} |
| 128 | + |
| 129 | +type peerData struct { |
| 130 | + PublicKey string |
| 131 | + AllowedIPs string |
| 132 | + Endpoint string |
| 133 | +} |
| 134 | + |
| 135 | +var gotmpl = `[Interface] |
| 136 | +Address = {{.Address}} |
| 137 | +ListenPort = {{.ListenPort}} |
| 138 | +PrivateKey = {{ printf "%s" .PrivateKey}} |
| 139 | +
|
| 140 | +{{ range .Peers }} |
| 141 | +[Peer] |
| 142 | +PublicKey = {{.PublicKey}} |
| 143 | +AllowedIPs = {{.AllowedIPs}} |
| 144 | +{{ if .Endpoint -}} |
| 145 | +Endpoint = {{ .Endpoint }} |
| 146 | +{{ end -}} |
| 147 | +PersistentKeepalive = 15 |
| 148 | +{{ end }} |
| 149 | +` |
0 commit comments