Skip to content

Commit c748881

Browse files
authored
Merge pull request #855 from mortent/ChangeK8sSchemaSourceDefault
Change default k8s schema source to be the builtin schema
2 parents 8b57560 + edc00c0 commit c748881

File tree

3 files changed

+14
-28
lines changed

3 files changed

+14
-28
lines changed

internal/util/openapi/openapi.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,34 +23,31 @@ import (
2323
"sigs.k8s.io/kustomize/kyaml/openapi/kustomizationapi"
2424
)
2525

26+
const (
27+
SchemaSourceBuiltin = "builtin"
28+
SchemaSourceFile = "file"
29+
SchemaSourceCluster = "cluster"
30+
)
31+
2632
// ConfigureOpenAPI sets the openAPI schema in kyaml. It can either
2733
// fetch the schema from a cluster, read it from file, or just the
2834
// schema built into kyaml.
2935
func ConfigureOpenAPI(factory util.Factory, k8sSchemaSource, k8sSchemaPath string) error {
3036
switch k8sSchemaSource {
31-
case "":
32-
openAPISchema, err := FetchOpenAPISchemaFromCluster(factory)
33-
if err != nil {
34-
// The default behavior is to try to fetch the schema from the
35-
// cluster, but fall back on using the builtin one if that doesn't
36-
// work
37-
return nil
38-
}
39-
return ConfigureOpenAPISchema(openAPISchema)
40-
case "cluster":
37+
case SchemaSourceCluster:
4138
openAPISchema, err := FetchOpenAPISchemaFromCluster(factory)
4239
if err != nil {
4340
return fmt.Errorf("error fetching schema from cluster: %v", err)
4441
}
4542
return ConfigureOpenAPISchema(openAPISchema)
46-
case "file":
43+
case SchemaSourceFile:
4744
openAPISchema, err := ReadOpenAPISchemaFromDisk(k8sSchemaPath)
4845
if err != nil {
4946
return fmt.Errorf("error reading file at path %s: %v",
5047
k8sSchemaPath, err)
5148
}
5249
return ConfigureOpenAPISchema(openAPISchema)
53-
case "builtin":
50+
case SchemaSourceBuiltin:
5451
return nil
5552
default:
5653
return fmt.Errorf("unknown schema source %s. Must be one of file, cluster, builtin",

internal/util/openapi/openapi_test.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,10 @@ func TestSomething(t *testing.T) {
4141
expectError bool
4242
}{
4343
{
44-
name: "no schemaSource provided with fallback to builtin",
45-
schemaSource: "",
46-
response: &http.Response{StatusCode: http.StatusNotFound,
47-
Header: cmdtesting.DefaultHeader(), Body: cmdtesting.StringBody("")},
44+
name: "no schemaSource provided should lead to error",
45+
schemaSource: "",
4846
includesRefString: "#/definitions/io.k8s.api.core.v1.PodSpec",
49-
expectError: false,
50-
},
51-
{
52-
name: "no schemaSource provided with cluster available",
53-
schemaSource: "",
54-
response: &http.Response{StatusCode: http.StatusOK,
55-
Header: cmdtesting.DefaultHeader(), Body: getSchema(t, "clusterschema.json")},
56-
includesRefString: "#/definitions/io.k8s.clusterSchema",
57-
expectError: false,
47+
expectError: true,
5848
},
5949
{
6050
name: "schemaSource cluster with successful fetch",

run/run.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,8 @@ func GetMain() *cobra.Command {
141141
cmd.PersistentFlags().BoolVar(&cmdutil.StackOnError, "stack-trace", false,
142142
"print a stack-trace on failure")
143143

144-
cmd.PersistentFlags().StringVar(&cmdutil.K8sSchemaSource, "k8s-schema-source", "",
145-
"source for the kubernetes openAPI schema. Default is to first try to fetch "+
146-
"it from the cluster given by the context and fall back to the builtin schema")
144+
cmd.PersistentFlags().StringVar(&cmdutil.K8sSchemaSource, "k8s-schema-source",
145+
kptopenapi.SchemaSourceBuiltin, "source for the kubernetes openAPI schema")
147146
cmd.PersistentFlags().StringVar(&cmdutil.K8sSchemaPath, "k8s-schema-path",
148147
"./openapi.json", "path to the kubernetes openAPI schema file")
149148

0 commit comments

Comments
 (0)