Merge pull request #2707 from mortent/FixDotInSetterName

Allow setters/substitutions with . in the name
This commit is contained in:
Kubernetes Prow Robot
2020-07-13 10:03:20 -07:00
committed by GitHub
4 changed files with 71 additions and 3 deletions

View File

@@ -372,6 +372,41 @@ metadata:
name: nginx-deployment
spec:
replicas: 3 # {"$openapi":"replicas"}
`,
},
{
name: "add setter with . in the name",
args: []string{"foo.bar", "3"},
input: `
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
`,
inputOpenAPI: `
apiVersion: v1alpha1
kind: Example
`,
expectedOpenAPI: `
apiVersion: v1alpha1
kind: Example
openAPI:
definitions:
io.k8s.cli.setters.foo.bar:
x-k8s-cli:
setter:
name: foo.bar
value: "3"
`,
expectedResources: `
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3 # {"$openapi":"foo.bar"}
`,
},
}

View File

@@ -155,8 +155,18 @@ func (fm *FieldMeta) Write(n *yaml.RNode) error {
if fm.Schema.Ref.String() != "" {
// Ex: {"$ref":"#/definitions/io.k8s.cli.setters.replicas"} should be converted to
// {"openAPI":"replicas"} and added to the line comment
arr := strings.Split(fm.Schema.Ref.String(), ".")
n.YNode().LineComment = fmt.Sprintf(`{"%s":"%s"}`, shortHandRef, arr[len(arr)-1])
ref := fm.Schema.Ref.String()
var shortHandRefValue string
switch {
case strings.HasPrefix(ref, DefinitionsPrefix+SetterDefinitionPrefix):
shortHandRefValue = strings.TrimPrefix(ref, DefinitionsPrefix+SetterDefinitionPrefix)
case strings.HasPrefix(ref, DefinitionsPrefix+SubstitutionDefinitionPrefix):
shortHandRefValue = strings.TrimPrefix(ref, DefinitionsPrefix+SubstitutionDefinitionPrefix)
default:
return fmt.Errorf("unexpected ref format: %s", ref)
}
n.YNode().LineComment = fmt.Sprintf(`{"%s":"%s"}`, shortHandRef,
shortHandRefValue)
} else {
n.YNode().LineComment = ""
}

View File

@@ -303,7 +303,7 @@ metadata:
//
//
{
name: "windows_line_ending",
name: "windows_line_ending",
input: "\r\n---\r\na: b # first resource\r\nc: d\r\n---\r\n# second resource\r\ne: f\r\ng:\r\n- h\r\n---\r\n\r\n---\r\n i: j",
expectedItems: []string{
`a: b # first resource

View File

@@ -169,6 +169,29 @@ metadata:
name: nginx-deployment
spec:
replicas: 3
`,
},
{
name: "ref has . in name of setter",
add: Add{
FieldValue: "3",
Ref: "#/definitions/io.k8s.cli.setters.foo.bar",
},
input: `
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
`,
expected: `
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3 # {"$openapi":"foo.bar"}
`,
},
}