Merge pull request #73831 from jennybuckley/apply-create-service

Honor forceAllowCreate in service registry
pull/564/head
Kubernetes Prow Robot 2019-02-14 13:59:44 -08:00 committed by GitHub
commit 1b8c8f1daf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 66 additions and 24 deletions

View File

@ -337,6 +337,18 @@ func (rs *REST) healthCheckNodePortUpdate(oldService, service *api.Service, node
func (rs *REST) Update(ctx context.Context, name string, objInfo rest.UpdatedObjectInfo, createValidation rest.ValidateObjectFunc, updateValidation rest.ValidateObjectUpdateFunc, forceAllowCreate bool, options *metav1.UpdateOptions) (runtime.Object, bool, error) {
oldObj, err := rs.services.Get(ctx, name, &metav1.GetOptions{})
if err != nil {
// Support create on update, if forced to.
if forceAllowCreate {
obj, err := objInfo.UpdatedObject(ctx, nil)
if err != nil {
return nil, false, err
}
createdObj, err := rs.Create(ctx, obj, createValidation, &metav1.CreateOptions{DryRun: options.DryRun})
if err != nil {
return nil, false, err
}
return createdObj, true, nil
}
return nil, false, err
}
oldService := oldObj.(*api.Service)

View File

@ -61,11 +61,15 @@ func TestApplyAlsoCreates(t *testing.T) {
_, client, closeFn := setup(t)
defer closeFn()
_, err := client.CoreV1().RESTClient().Patch(types.ApplyPatchType).
Namespace("default").
Resource("pods").
Name("test-pod").
Body([]byte(`{
testCases := []struct {
resource string
name string
body string
}{
{
resource: "pods",
name: "test-pod",
body: `{
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
@ -77,18 +81,44 @@ func TestApplyAlsoCreates(t *testing.T) {
"image": "test-image"
}]
}
}`)).
}`,
}, {
resource: "services",
name: "test-svc",
body: `{
"apiVersion": "v1",
"kind": "Service",
"metadata": {
"name": "test-svc"
},
"spec": {
"ports": [{
"port": 8080,
"protocol": "UDP"
}]
}
}`,
},
}
for _, tc := range testCases {
_, err := client.CoreV1().RESTClient().Patch(types.ApplyPatchType).
Namespace("default").
Resource(tc.resource).
Name(tc.name).
Body([]byte(tc.body)).
Do().
Get()
if err != nil {
t.Fatalf("Failed to create object using Apply patch: %v", err)
}
_, err = client.CoreV1().RESTClient().Get().Namespace("default").Resource("pods").Name("test-pod").Do().Get()
_, err = client.CoreV1().RESTClient().Get().Namespace("default").Resource(tc.resource).Name(tc.name).Do().Get()
if err != nil {
t.Fatalf("Failed to retrieve object: %v", err)
}
}
}
// TestCreateOnApplyFailsWithUID makes sure that PATCH requests with the apply content type
// will not create the object if it doesn't already exist and it specifies a UID