-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add container template configuration for Task
Container configuration defined on the `Task` will be propagated to all steps within the `Task`, and can be overridden on individual steps.
- Loading branch information
1 parent
24885a4
commit 4a84deb
Showing
11 changed files
with
5,320 additions
and
1,647 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
Copyright 2019 Knative Authors LLC | ||
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 merge | ||
|
||
import ( | ||
"encoding/json" | ||
"k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/util/strategicpatch" | ||
) | ||
|
||
// CombineStepsWithContainerTemplate takes a possibly nil container template and a list of step containers, merging each | ||
// of the step containers onto the container template, if it's not nil, and returning the resulting list. | ||
func CombineStepsWithContainerTemplate(template *v1.Container, steps []v1.Container) ([]v1.Container, error) { | ||
if template == nil { | ||
return steps, nil | ||
} | ||
|
||
// We need JSON bytes to generate a patch to merge the step containers onto the template container, so marshal the template. | ||
templateAsJSON, err := json.Marshal(template) | ||
if err != nil { | ||
return nil, err | ||
} | ||
// We need to do a three-way merge to actually combine the template and step containers, so we need an empty container | ||
// as the "original" | ||
emptyAsJSON, err := json.Marshal(&v1.Container{}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for i, s := range steps { | ||
// Marshal the step to JSON | ||
stepAsJSON, err := json.Marshal(s) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Get the patch meta for Container, which is needed for generating and applying the merge patch. | ||
patchSchema, err := strategicpatch.NewPatchMetaFromStruct(template) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Create a merge patch, with the empty JSON as the original, the step JSON as the modified, and the template | ||
// JSON as the current - this lets us do a deep merge of the template and step containers, with awareness of | ||
// the "patchMerge" tags. | ||
patch, err := strategicpatch.CreateThreeWayMergePatch(emptyAsJSON, stepAsJSON, templateAsJSON, patchSchema, true) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Actually apply the merge patch to the template JSON. | ||
mergedAsJSON, err := strategicpatch.StrategicMergePatchUsingLookupPatchMeta(templateAsJSON, patch, patchSchema) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Unmarshal the merged JSON to a Container pointer, and return it. | ||
merged := &v1.Container{} | ||
err = json.Unmarshal(mergedAsJSON, merged) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// If the container's args is nil, reset it to empty instead | ||
if merged.Args == nil && s.Args != nil { | ||
merged.Args = []string{} | ||
} | ||
|
||
steps[i] = *merged | ||
} | ||
return steps, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/* | ||
Copyright 2019 Knative Authors LLC | ||
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 merge | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/api/resource" | ||
) | ||
|
||
func TestCombineStepsWithContainerTemplate(t *testing.T) { | ||
resourceQuantityCmp := cmp.Comparer(func(x, y resource.Quantity) bool { | ||
return x.Cmp(y) == 0 | ||
}) | ||
|
||
for _, tc := range []struct { | ||
name string | ||
template *corev1.Container | ||
steps []corev1.Container | ||
expected []corev1.Container | ||
}{{ | ||
name: "nil-template", | ||
template: nil, | ||
steps: []corev1.Container{{ | ||
Image: "some-image", | ||
}}, | ||
expected: []corev1.Container{{ | ||
Image: "some-image", | ||
}}, | ||
}, { | ||
name: "not-overlapping", | ||
template: &corev1.Container{ | ||
Command: []string{"/somecmd"}, | ||
}, | ||
steps: []corev1.Container{{ | ||
Image: "some-image", | ||
}}, | ||
expected: []corev1.Container{{ | ||
Command: []string{"/somecmd"}, | ||
Image: "some-image", | ||
}}, | ||
}, { | ||
name: "overwriting-one-field", | ||
template: &corev1.Container{ | ||
Image: "some-image", | ||
Command: []string{"/somecmd"}, | ||
}, | ||
steps: []corev1.Container{{ | ||
Image: "some-other-image", | ||
}}, | ||
expected: []corev1.Container{{ | ||
Command: []string{"/somecmd"}, | ||
Image: "some-other-image", | ||
}}, | ||
}, { | ||
name: "merge-and-overwrite-slice", | ||
template: &corev1.Container{ | ||
Env: []corev1.EnvVar{ | ||
{ | ||
Name: "KEEP_THIS", | ||
Value: "A_VALUE", | ||
}, { | ||
Name: "SOME_KEY", | ||
Value: "ORIGINAL_VALUE", | ||
}, | ||
}, | ||
}, | ||
steps: []corev1.Container{{ | ||
Env: []corev1.EnvVar{ | ||
{ | ||
Name: "NEW_KEY", | ||
Value: "A_VALUE", | ||
}, { | ||
Name: "SOME_KEY", | ||
Value: "NEW_VALUE", | ||
}, | ||
}, | ||
}}, | ||
expected: []corev1.Container{{ | ||
Env: []corev1.EnvVar{ | ||
{ | ||
Name: "NEW_KEY", | ||
Value: "A_VALUE", | ||
}, { | ||
Name: "KEEP_THIS", | ||
Value: "A_VALUE", | ||
}, { | ||
Name: "SOME_KEY", | ||
Value: "NEW_VALUE", | ||
}, | ||
}, | ||
}}, | ||
}} { | ||
t.Run(tc.name, func(t *testing.T) { | ||
result, err := CombineStepsWithContainerTemplate(tc.template, tc.steps) | ||
if err != nil { | ||
t.Errorf("expected no error. Got error %v", err) | ||
} | ||
|
||
if d := cmp.Diff(tc.expected, result, resourceQuantityCmp); d != "" { | ||
t.Errorf("Combined steps don't match, diff: %s", d) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.