Skip to content

Commit 11fc56b

Browse files
committed
feat: use git patch to patch vendor
Signed-off-by: Jack Yu <[email protected]>
1 parent 4c35fe4 commit 11fc56b

File tree

5 files changed

+295
-0
lines changed

5 files changed

+295
-0
lines changed

scripts/ci

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ set -e
33

44
cd $(dirname $0)
55

6+
./verify-vendor-patches apply
67
./build
78
./validate
89
./test
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
diff --git a/vendor/k8s.io/apimachinery/pkg/api/validation/objectmeta.go b/vendor/k8s.io/apimachinery/pkg/api/validation/objectmeta.go
2+
index 54a2883a..cc11c6a9 100644
3+
--- a/vendor/k8s.io/apimachinery/pkg/api/validation/objectmeta.go
4+
+++ b/vendor/k8s.io/apimachinery/pkg/api/validation/objectmeta.go
5+
@@ -252,7 +252,7 @@ func ValidateObjectMetaAccessorUpdate(newMeta, oldMeta metav1.Object, fldPath *
6+
allErrs = append(allErrs, ValidateImmutableField(newMeta.GetName(), oldMeta.GetName(), fldPath.Child("name"))...)
7+
allErrs = append(allErrs, ValidateImmutableField(newMeta.GetNamespace(), oldMeta.GetNamespace(), fldPath.Child("namespace"))...)
8+
allErrs = append(allErrs, ValidateImmutableField(newMeta.GetUID(), oldMeta.GetUID(), fldPath.Child("uid"))...)
9+
- allErrs = append(allErrs, ValidateImmutableField(newMeta.GetCreationTimestamp(), oldMeta.GetCreationTimestamp(), fldPath.Child("creationTimestamp"))...)
10+
+ //allErrs = append(allErrs, ValidateImmutableField(newMeta.GetCreationTimestamp(), oldMeta.GetCreationTimestamp(), fldPath.Child("creationTimestamp"))...)
11+
allErrs = append(allErrs, ValidateImmutableField(newMeta.GetDeletionTimestamp(), oldMeta.GetDeletionTimestamp(), fldPath.Child("deletionTimestamp"))...)
12+
allErrs = append(allErrs, ValidateImmutableField(newMeta.GetDeletionGracePeriodSeconds(), oldMeta.GetDeletionGracePeriodSeconds(), fldPath.Child("deletionGracePeriodSeconds"))...)
13+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
diff --git a/vendor/k8s.io/apiserver/pkg/registry/rest/meta.go b/vendor/k8s.io/apiserver/pkg/registry/rest/meta.go
2+
index fc4fc81e..1bdea92c 100644
3+
--- a/vendor/k8s.io/apiserver/pkg/registry/rest/meta.go
4+
+++ b/vendor/k8s.io/apiserver/pkg/registry/rest/meta.go
5+
@@ -28,7 +28,7 @@ var metav1Now = func() metav1.Time { return metav1.Now() }
6+
7+
// WipeObjectMetaSystemFields erases fields that are managed by the system on ObjectMeta.
8+
func WipeObjectMetaSystemFields(meta metav1.Object) {
9+
- meta.SetCreationTimestamp(metav1.Time{})
10+
+ //meta.SetCreationTimestamp(metav1.Time{})
11+
meta.SetUID("")
12+
meta.SetDeletionTimestamp(nil)
13+
meta.SetDeletionGracePeriodSeconds(nil)
14+
@@ -37,7 +37,9 @@ func WipeObjectMetaSystemFields(meta metav1.Object) {
15+
16+
// FillObjectMetaSystemFields populates fields that are managed by the system on ObjectMeta.
17+
func FillObjectMetaSystemFields(meta metav1.Object) {
18+
- meta.SetCreationTimestamp(metav1Now())
19+
+ if meta.GetCreationTimestamp().String() == "" {
20+
+ meta.SetCreationTimestamp(metav1.Now())
21+
+ }
22+
meta.SetUID(uuid.NewUUID())
23+
}
24+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
diff --git a/vendor/k8s.io/kubernetes/pkg/registry/core/pod/strategy.go b/vendor/k8s.io/kubernetes/pkg/registry/core/pod/strategy.go
2+
index 71b89e2f..1a706b07 100644
3+
--- a/vendor/k8s.io/kubernetes/pkg/registry/core/pod/strategy.go
4+
+++ b/vendor/k8s.io/kubernetes/pkg/registry/core/pod/strategy.go
5+
@@ -618,6 +618,8 @@ func LogLocation(
6+
if err != nil {
7+
return nil, nil, err
8+
}
9+
+ nodeInfo.Hostname = "localhost"
10+
+
11+
params := url.Values{}
12+
if opts.Follow {
13+
params.Add("follow", "true")

scripts/verify-vendor-patches

Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# Script to verify and apply vendor patches
5+
# This ensures critical patches to vendor files are maintained after running go mod vendor
6+
7+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
8+
PROJECT_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
9+
PATCH_DIR="${SCRIPT_DIR}/vendor-patches"
10+
11+
# Colors for output
12+
RED='\033[0;31m'
13+
GREEN='\033[0;32m'
14+
YELLOW='\033[1;33m'
15+
NC='\033[0m' # No Color
16+
17+
# Function to print colored messages
18+
print_error() {
19+
echo -e "${RED}ERROR: $1${NC}" >&2
20+
}
21+
22+
print_success() {
23+
echo -e "${GREEN}SUCCESS: $1${NC}"
24+
}
25+
26+
print_warning() {
27+
echo -e "${YELLOW}WARNING: $1${NC}"
28+
}
29+
30+
print_info() {
31+
echo "INFO: $1"
32+
}
33+
34+
# Function to check if a patch is applied
35+
check_patch() {
36+
local patch_file="$1"
37+
local patch_name="$(basename "$patch_file" .patch)"
38+
39+
print_info "Checking patch: ${patch_name}"
40+
41+
cd "${PROJECT_ROOT}"
42+
43+
# Try to apply the patch in reverse (dry-run) to check if it's already applied
44+
if git apply --reverse --check "${patch_file}" > /dev/null 2>&1; then
45+
print_success "Patch ${patch_name} is already applied"
46+
return 0
47+
fi
48+
49+
# Check if the patch can be applied
50+
if git apply --check "${patch_file}" > /dev/null 2>&1; then
51+
print_warning "Patch ${patch_name} is NOT applied but can be applied"
52+
return 1
53+
else
54+
print_error "Patch ${patch_name} cannot be applied (conflicts or already partially applied)"
55+
return 2
56+
fi
57+
}
58+
59+
# Function to apply a patch
60+
apply_patch() {
61+
local patch_file="$1"
62+
local patch_name="$(basename "$patch_file" .patch)"
63+
64+
print_info "Applying patch: ${patch_name}"
65+
66+
cd "${PROJECT_ROOT}"
67+
68+
if git apply "${patch_file}"; then
69+
print_success "Successfully applied patch: ${patch_name}"
70+
return 0
71+
else
72+
print_error "Failed to apply patch: ${patch_name}"
73+
return 1
74+
fi
75+
}
76+
77+
# Function to verify specific file changes
78+
verify_file_content() {
79+
local file="$1"
80+
local expected_pattern="$2"
81+
local description="$3"
82+
83+
if grep -q "${expected_pattern}" "${PROJECT_ROOT}/${file}"; then
84+
print_success "${description}"
85+
return 0
86+
else
87+
print_error "${description}"
88+
return 1
89+
fi
90+
}
91+
92+
# Main verification function
93+
verify_patches() {
94+
print_info "=========================================="
95+
print_info "Verifying vendor patches..."
96+
print_info "=========================================="
97+
echo ""
98+
99+
local all_good=true
100+
local needs_apply=false
101+
102+
# Check all patch files
103+
if [ ! -d "${PATCH_DIR}" ]; then
104+
print_error "Patch directory not found: ${PATCH_DIR}"
105+
exit 1
106+
fi
107+
108+
for patch_file in "${PATCH_DIR}"/*.patch; do
109+
if [ ! -f "${patch_file}" ]; then
110+
print_warning "No patch files found in ${PATCH_DIR}"
111+
continue
112+
fi
113+
114+
check_patch "${patch_file}"
115+
status=$?
116+
117+
if [ $status -eq 1 ]; then
118+
needs_apply=true
119+
all_good=false
120+
elif [ $status -eq 2 ]; then
121+
all_good=false
122+
fi
123+
124+
echo ""
125+
done
126+
127+
# Detailed verification for k8s-timestamp-preservation patch
128+
print_info "Performing detailed content verification..."
129+
echo ""
130+
131+
verify_file_content \
132+
"vendor/k8s.io/apimachinery/pkg/api/validation/objectmeta.go" \
133+
"//allErrs = append(allErrs, ValidateImmutableField(newMeta.GetCreationTimestamp()" \
134+
"CreationTimestamp validation is commented out"
135+
136+
verify_file_content \
137+
"vendor/k8s.io/apiserver/pkg/registry/rest/meta.go" \
138+
"//meta.SetCreationTimestamp(metav1.Time{})" \
139+
"CreationTimestamp wiping is commented out"
140+
141+
verify_file_content \
142+
"vendor/k8s.io/apiserver/pkg/registry/rest/meta.go" \
143+
'if meta.GetCreationTimestamp().String() == ""' \
144+
"CreationTimestamp conditional check exists"
145+
146+
verify_file_content \
147+
"vendor/k8s.io/kubernetes/pkg/registry/core/pod/strategy.go" \
148+
'nodeInfo.Hostname = "localhost"' \
149+
"NodeInfo hostname override exists"
150+
151+
echo ""
152+
print_info "=========================================="
153+
154+
if [ "$all_good" = true ]; then
155+
print_success "All vendor patches are correctly applied!"
156+
return 0
157+
elif [ "$needs_apply" = true ]; then
158+
print_warning "Some patches need to be applied"
159+
return 1
160+
else
161+
print_error "Some patches have conflicts or issues"
162+
return 2
163+
fi
164+
}
165+
166+
# Function to apply all patches
167+
apply_all_patches() {
168+
print_info "=========================================="
169+
print_info "Applying all vendor patches..."
170+
print_info "=========================================="
171+
echo ""
172+
173+
local applied_count=0
174+
local skipped_count=0
175+
local failed_count=0
176+
177+
for patch_file in "${PATCH_DIR}"/*.patch; do
178+
if [ ! -f "${patch_file}" ]; then
179+
print_warning "No patch files found in ${PATCH_DIR}"
180+
continue
181+
fi
182+
183+
local patch_name="$(basename "$patch_file")"
184+
185+
# Check if already applied
186+
cd "${PROJECT_ROOT}"
187+
if git apply --reverse --check "${patch_file}" > /dev/null 2>&1; then
188+
print_info "✓ Patch ${patch_name} is already applied, skipping..."
189+
((skipped_count++))
190+
else
191+
# Try to apply the patch
192+
if git apply "${patch_file}" 2>/dev/null; then
193+
print_success "✓ Successfully applied patch: ${patch_name}"
194+
((applied_count++))
195+
else
196+
print_warning "⚠ Could not apply patch: ${patch_name} (may be partially applied or have conflicts)"
197+
((failed_count++))
198+
fi
199+
fi
200+
201+
echo ""
202+
done
203+
204+
print_info "=========================================="
205+
print_info "Summary:"
206+
print_info " Applied: ${applied_count}"
207+
print_info " Already applied: ${skipped_count}"
208+
print_info " Failed/Partial: ${failed_count}"
209+
print_info "=========================================="
210+
211+
if [ $failed_count -gt 0 ]; then
212+
print_warning "Some patches could not be applied. Running detailed verification..."
213+
return 1
214+
else
215+
print_success "All patches processed successfully!"
216+
return 0
217+
fi
218+
}
219+
220+
# Main script logic
221+
main() {
222+
local command="${1:-verify}"
223+
224+
case "$command" in
225+
verify)
226+
verify_patches
227+
exit $?
228+
;;
229+
apply)
230+
apply_all_patches
231+
exit $?
232+
;;
233+
*)
234+
echo "Usage: $0 {verify|apply}"
235+
echo ""
236+
echo "Commands:"
237+
echo " verify - Check if patches are applied (default)"
238+
echo " apply - Apply all patches"
239+
exit 1
240+
;;
241+
esac
242+
}
243+
244+
main "$@"

0 commit comments

Comments
 (0)