forked from kubernetes/ingress-gce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate.go
177 lines (151 loc) · 4.78 KB
/
validate.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
Copyright 2018 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 (
"context"
"flag"
"fmt"
"os"
"path/filepath"
"regexp"
"github.com/kr/pretty"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
backendconfig "k8s.io/ingress-gce/pkg/backendconfig/client/clientset/versioned"
"k8s.io/ingress-gce/pkg/e2e"
"k8s.io/ingress-gce/pkg/fuzz"
"k8s.io/ingress-gce/pkg/fuzz/features"
// Pull in the auth library for GCP.
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
)
var (
validateOptions struct {
kubeconfig string
ns string
name string
listFeatures bool
featureRegex string
project string
}
// ValidateFlagSet is the flag set for the validate subcommand.
ValidateFlagSet = flag.NewFlagSet("validate", flag.ExitOnError)
)
func init() {
if home := homeDir(); home != "" {
ValidateFlagSet.StringVar(&validateOptions.kubeconfig, "kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
} else {
ValidateFlagSet.StringVar(&validateOptions.kubeconfig, "kubeconfig", "", "absolute path to the kubeconfig file")
}
ValidateFlagSet.StringVar(&validateOptions.name, "name", "", "name of the Ingress object to validate")
ValidateFlagSet.StringVar(&validateOptions.ns, "ns", "default", "namespace of the Ingress object to validate")
ValidateFlagSet.BoolVar(&validateOptions.listFeatures, "listFeatures", false, "list features available to be validated")
ValidateFlagSet.StringVar(&validateOptions.featureRegex, "featureRegex", "", "features matching regex will be included in validation")
ValidateFlagSet.StringVar(&validateOptions.project, "project", "", "GCP project where the load balancer will be created")
// Merges in the global flags into the subcommand FlagSet.
flag.VisitAll(func(f *flag.Flag) {
ValidateFlagSet.Var(f.Value, f.Name, f.Usage)
})
}
// Validate the load balancer matches the Ingress spec.
func Validate() {
if validateOptions.listFeatures {
fmt.Println("Feature names:")
for _, f := range features.All {
fmt.Println(f.Name())
}
os.Exit(0)
}
for _, o := range []struct {
flag, val string
}{
{validateOptions.name, "-name"},
{validateOptions.project, "-project"},
} {
if o.val == "" {
fmt.Fprintf(ValidateFlagSet.Output(), "You must specify the %s flag.\n", o.flag)
os.Exit(1)
}
}
config, err := clientcmd.BuildConfigFromFlags("", validateOptions.kubeconfig)
if err != nil {
panic(err.Error())
}
gce, err := e2e.NewCloud(validateOptions.project)
if err != nil {
panic(err)
}
env, err := fuzz.NewDefaultValidatorEnv(config, validateOptions.ns, gce)
if err != nil {
panic(err)
}
var fs []fuzz.Feature
if validateOptions.featureRegex == "" {
fs = features.All
} else {
fregexp := regexp.MustCompile(validateOptions.featureRegex)
for _, f := range features.All {
if fregexp.Match([]byte(f.Name())) {
fs = append(fs, f)
}
}
}
var fsNames []string
for _, f := range fs {
fsNames = append(fsNames, f.Name())
}
fmt.Printf("Features = %v\n\n", fsNames)
k8s := k8sClientSet(config)
ing, err := k8s.Extensions().Ingresses(validateOptions.ns).Get(validateOptions.name, metav1.GetOptions{})
if err != nil {
panic(err)
}
fmt.Printf("Ingress =\n%s\n\n", pretty.Sprint(*ing))
iv, err := fuzz.NewIngressValidator(env, ing, fs, nil)
if err != nil {
panic(err)
}
result := iv.Check(context.Background())
fmt.Printf("Result =\n%s\n", pretty.Sprint(*result))
if result.Err != nil {
os.Exit(1)
}
vip := ing.Status.LoadBalancer.Ingress[0].IP
gclb, err := fuzz.GCLBForVIP(context.Background(), gce, vip, []fuzz.FeatureValidator{})
if err != nil {
panic(err)
}
fmt.Printf("GCP resources = \n%s\n", pretty.Sprint(gclb))
}
func homeDir() string {
if h := os.Getenv("HOME"); h != "" {
return h
}
return os.Getenv("USERPROFILE") // windows
}
func k8sClientSet(config *rest.Config) *kubernetes.Clientset {
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
return clientset
}
func backendConfigClientset(config *rest.Config) *backendconfig.Clientset {
clientset, err := backendconfig.NewForConfig(config)
if err != nil {
panic(err.Error())
}
return clientset
}