-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcreate.go
83 lines (74 loc) · 2.17 KB
/
create.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package cmd
import (
"context"
"os"
"github.com/spf13/cobra"
"github.com/tzneal/supplant/model"
"github.com/tzneal/supplant/util"
"gopkg.in/yaml.v2"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
)
// createCmd represents the create command
var createCmd = &cobra.Command{
Use: "create [flags] config.yml",
Short: "create constructs a configuration file based on the cluster",
Long: `create constructs a configuration file by looking at the services
in the cluster. This is intended to provide a template to allow
easy construction of the configuration.`,
Args: cobra.ExactValidArgs(1),
Run: func(cmd *cobra.Command, args []string) {
f := cmdutil.NewFactory(kubeConfigFlags)
cs, err := f.KubernetesClientSet()
if err != nil {
util.LogError("error getting kubernetes client: %s", err)
return
}
ctx := context.Background()
svcList, err := cs.CoreV1().Services(*kubeConfigFlags.Namespace).List(ctx, metav1.ListOptions{})
if err != nil {
util.LogError("error listing services: %s", err)
return
}
cfg := model.Config{}
includeAll, _ := cmd.Flags().GetBool(flagAll)
pl := model.NewPortLookup(cs)
for _, svc := range svcList.Items {
// skip kube-system services by default
if skipByDefault(svc) && !includeAll {
continue
}
cfg.Supplant = append(cfg.Supplant, model.MapSupplantService(pl, svc))
cfg.External = append(cfg.External, model.MapExternalService(pl, svc))
}
writeConfig(cfg, args[0])
},
}
func skipByDefault(svc v1.Service) bool {
if svc.Namespace == "kube-system" {
return true
}
if svc.Namespace == "default" && svc.Name == "kubernetes" {
return true
}
return false
}
func writeConfig(cfg model.Config, outputFile string) {
fo, err := os.Create(outputFile)
if err != nil {
util.LogError("error opening %s: %s", outputFile, err)
return
}
defer fo.Close()
enc := yaml.NewEncoder(fo)
if err = enc.Encode(cfg); err != nil {
util.LogError("error encoding config: %s", err)
return
}
}
const flagAll = "all"
func init() {
configCmd.AddCommand(createCmd)
createCmd.Flags().BoolP(flagAll, "A", false, "If true, include items in the kube-system namespace")
}