-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathroot.go
160 lines (127 loc) · 5.83 KB
/
root.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package cmd
import (
"fmt"
"github.com/ca-gip/kotaplan/internal/services/aggregate"
"github.com/ca-gip/kotaplan/internal/services/k8s"
"github.com/ca-gip/kotaplan/internal/types"
"github.com/ca-gip/kotaplan/internal/utils"
"github.com/spf13/cobra"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/client-go/kubernetes"
"k8s.io/metrics/pkg/client/clientset/versioned"
"math"
"os"
"github.com/spf13/viper"
)
var cfgFile string
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "kotaplan",
Short: "Visualize resource consumption and generated ResourceQuota with recommend spec",
Long: ``,
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
cobra.CheckErr(rootCmd.Execute())
}
func init() {
cobra.OnInitialize(initConfig)
// Config
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.kotaplan.yaml)")
// kubectl
rootCmd.PersistentFlags().StringP("kubeconfig", "", k8s.DefaultKubeconfig(), "Path to a kubeconfig")
rootCmd.PersistentFlags().StringP("master", "", "", "Address of the Kubernetes API server. Overrides any value in kubeconfig")
// Default claim
rootCmd.PersistentFlags().Int64P("default-claim-memory", "", 0, "Amount of Memory for the default claim in GiB. (default 0)")
rootCmd.PersistentFlags().Int64P("default-claim-cpu", "", 0, "Amount of CPU for the default claim in Milli. ex 1000 = 1CPU. (default 0)")
// Max per Namespace
rootCmd.PersistentFlags().Float64P("ratio-namespace-memory", "", 1, "Ratio of the maximum amount of Memory that can be claim by a namespace. Ex: 0.5 meaning 50% of the cluster is claimable by a Namespace")
rootCmd.PersistentFlags().Float64P("ratio-namespace-cpu", "", 1, "Ratio of the maximum amount of CPU that can be claim by a namespace. Ex: 0.5 meaning 50% of the cluster is claimable by a Namespace")
// Over commit
rootCmd.PersistentFlags().Float64P("over-commit-memory", "", 1, "Ratio of the Memory over or under commit")
rootCmd.PersistentFlags().Float64P("over-commit-cpu", "", 1, "Ratio of the CPU over or under commit")
// Margin
rootCmd.PersistentFlags().Float64P("margin", "", 1.2, "Margin for the recommended spec")
// Label
rootCmd.PersistentFlags().StringP("labels", "l", "quota=managed", "Match namespace containing a label")
}
// initConfig reads in config file and ENV variables if set.
func initConfig() {
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := os.UserHomeDir()
cobra.CheckErr(err)
// Search config in home directory with name ".kotaplan" (without extension).
viper.AddConfigPath(home)
viper.SetConfigType("yaml")
viper.SetConfigName(".kotaplan")
}
viper.AutomaticEnv() // read in environment variables that match
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
}
}
func initClients(cmd *cobra.Command, args []string) (client *kubernetes.Clientset, metricsClient *versioned.Clientset) {
kubeconfig, _ := cmd.Flags().GetString("kubeconfig")
master, _ := cmd.Flags().GetString("master")
return k8s.ClientGen(&master, &kubeconfig)
}
func parseParameters(cmd *cobra.Command, args []string) *types.Parameters {
defaultClaimMemory, _ := cmd.Flags().GetInt64("default-claim-memory")
defaultClaimCpu, _ := cmd.Flags().GetInt64("default-claim-cpu")
ratioNamespaceMemory, _ := cmd.Flags().GetFloat64("ratio-namespace-memory")
ratioNamespaceCpu, _ := cmd.Flags().GetFloat64("ratio-namespace-cpu")
overCommitMemory, _ := cmd.Flags().GetFloat64("over-commit-memory")
overCommitCpu, _ := cmd.Flags().GetFloat64("over-commit-cpu")
margin, _ := cmd.Flags().GetFloat64("margin")
labels, _ := cmd.Flags().GetString("labels")
return &types.Parameters{
DefaultClaim: v1.ResourceList{
v1.ResourceCPU: *resource.NewMilliQuantity(defaultClaimCpu, resource.DecimalSI),
v1.ResourceMemory: *resource.NewQuantity(int64(float64(defaultClaimMemory)*math.Pow(2, 30)), resource.BinarySI),
},
RatioNsMemory: ratioNamespaceMemory,
RatioNsCpu: ratioNamespaceCpu,
OverCommitMemory: overCommitMemory,
OverCommitCpu: overCommitCpu,
Margin: margin,
Labels: utils.LabelsFromString(labels),
}
}
func newClusterStat(cluster *types.ClusterData, settings *types.Parameters) (stats types.ClusterStat) {
stats.NamespacesCount = aggregate.CountNs(cluster)
stats.MemAvailable = aggregate.MemNodes(cluster)
stats.CpuAvailable = aggregate.CpuNodes(cluster)
stats.NodesCount = aggregate.CountNodes(cluster)
for _, namespace := range cluster.Namespaces.Items {
stats.NamespacesStat = append(stats.NamespacesStat, *newNamespaceStat(namespace, cluster, stats, settings))
}
return
}
func newNamespaceStat(namespace v1.Namespace, cluster *types.ClusterData, stats types.ClusterStat, settings *types.Parameters) *types.NamespaceStat {
memReq := aggregate.MemRequestSumByNS(cluster.Pods, namespace)
memUse := aggregate.MemUsageByNS(cluster.PodsMetric, namespace)
cpuReq := aggregate.CpuRequestSumByNS(cluster.Pods, namespace)
cpuUse := aggregate.CpuUsageByNS(cluster.PodsMetric, namespace)
claimFit, spec := utils.CheckSpec(memReq, cpuReq, settings)
respectMaxNS := utils.CheckRespectMaxNS(spec, stats, settings)
return &types.NamespaceStat{
Name: namespace.Name,
PodCount: aggregate.PodCountByNS(cluster.Pods, namespace),
MemReq: memReq,
MemUse: memUse,
MemReqUse: utils.DivideAsPercent(memUse, memReq),
CpuReq: cpuReq,
CpuUse: cpuUse,
CpuReqUse: utils.DivideAsPercent(cpuUse, cpuReq),
ClaimFit: claimFit,
RespectMaxNS: respectMaxNS,
Spec: spec,
}
}