forked from openshift/origin
/
server.go
161 lines (136 loc) · 4.23 KB
/
server.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
161
/*
Copyright 2017 The Kubernetes 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 app
import (
"fmt"
"os"
"github.com/renstrom/dedent"
"github.com/spf13/cobra"
restclient "k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
clientset "k8s.io/kubernetes/pkg/client/clientset_generated/clientset"
_ "k8s.io/kubernetes/plugin/pkg/scheduler/algorithmprovider"
"github.com/kubernetes-incubator/cluster-capacity/cmd/cluster-capacity/app/options"
"github.com/kubernetes-incubator/cluster-capacity/pkg/framework"
"github.com/kubernetes-incubator/cluster-capacity/pkg/utils"
)
var (
clusterCapacityLong = dedent.Dedent(`
Cluster-capacity simulates an API server with initial state copied from the Kubernetes enviroment
with its configuration specified in KUBECONFIG. The simulated API server tries to schedule the number of
pods specified by --max-limits flag. If the --max-limits flag is not specified, pods are scheduled until
the simulated API server runs out of resources.
`)
)
func NewClusterCapacityCommand() *cobra.Command {
opt := options.NewClusterCapacityOptions()
cmd := &cobra.Command{
Use: "cluster-capacity --kubeconfig KUBECONFIG --podspec PODSPEC",
Short: "Cluster-capacity is used for simulating scheduling of one or multiple pods",
Long: clusterCapacityLong,
Run: func(cmd *cobra.Command, args []string) {
err := Validate(opt)
if err != nil {
fmt.Println(err)
cmd.Help()
return
}
err = Run(opt)
if err != nil {
fmt.Println(err)
}
},
}
opt.AddFlags(cmd.Flags())
return cmd
}
func Validate(opt *options.ClusterCapacityOptions) error {
if len(opt.PodSpecFile) == 0 {
return fmt.Errorf("Pod spec file is missing")
}
_, present := os.LookupEnv("CC_INCLUSTER")
if !present {
if len(opt.Kubeconfig) == 0 {
return fmt.Errorf("kubeconfig is missing")
}
}
return nil
}
func Run(opt *options.ClusterCapacityOptions) error {
conf := options.NewClusterCapacityConfig(opt)
err := conf.ParseAPISpec()
if err != nil {
return fmt.Errorf("Failed to parse pod spec file: %v ", err)
}
err = conf.SetDefaultScheduler()
if err != nil {
return fmt.Errorf("Failed to set default scheduler config: %v ", err)
}
err = conf.ParseAdditionalSchedulerConfigs()
if err != nil {
return fmt.Errorf("Failed to parse config file: %v ", err)
}
var cfg *restclient.Config
if len(conf.Options.Kubeconfig) != 0 {
master, err := utils.GetMasterFromKubeConfig(conf.Options.Kubeconfig)
if err != nil {
return fmt.Errorf("Failed to parse kubeconfig file: %v ", err)
}
cfg, err = clientcmd.BuildConfigFromFlags(master, conf.Options.Kubeconfig)
if err != nil {
return fmt.Errorf("Unable to build config: %v", err)
}
} else {
cfg, err = restclient.InClusterConfig()
if err != nil {
return fmt.Errorf("Unable to build in cluster config: %v", err)
}
}
conf.KubeClient, err = clientset.NewForConfig(cfg)
if err != nil {
return err
}
report, err := runSimulator(conf, true)
if err != nil {
return err
}
if err := framework.ClusterCapacityReviewPrint(report, conf.Options.Verbose, conf.Options.OutputFormat); err != nil {
return fmt.Errorf("Error while printing: %v", err)
}
return nil
}
func runSimulator(s *options.ClusterCapacityConfig, syncWithClient bool) (*framework.ClusterCapacityReview, error) {
cc, err := framework.New(s.DefaultScheduler, s.Pod, s.Options.MaxLimit)
if err != nil {
return nil, err
}
for i := 0; i < len(s.Schedulers); i++ {
if err = cc.AddScheduler(s.Schedulers[i]); err != nil {
return nil, err
}
}
if syncWithClient {
err = cc.SyncWithClient(s.KubeClient)
} else {
err = cc.SyncWithStore(s.ResourceStore)
}
if err != nil {
return nil, err
}
err = cc.Run()
if err != nil {
return nil, err
}
report := cc.Report()
return report, nil
}