Skip to content

Commit b2f9b85

Browse files
authored
Merge pull request #3025 from yue9944882/add-secret-patch
Fixes secret deep-compare issue and track the change by patches
2 parents 8851eed + 3a9db03 commit b2f9b85

File tree

5 files changed

+138
-1
lines changed

5 files changed

+138
-1
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
Copyright 2024 The Kubernetes Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
package io.kubernetes.client.custom;
14+
15+
import java.util.Arrays;
16+
import java.util.Map;
17+
18+
public class MapUtils {
19+
public static boolean equals(Map<String, byte[]> map1, Map<String, byte[]> map2) {
20+
if (map1 == map2) {
21+
return true; // Both pointing to the same instance
22+
}
23+
if (map1 == null || map2 == null || map1.size() != map2.size()) {
24+
return false; // One is null or their sizes are different
25+
}
26+
for (String key : map1.keySet()) {
27+
if (!map2.containsKey(key) || !Arrays.equals(map1.get(key), map2.get(key))) {
28+
// Key doesn't exist in map2 or the byte arrays are not equal
29+
return false;
30+
}
31+
}
32+
// All keys matched and their corresponding byte arrays are equal
33+
return true;
34+
}
35+
}

kubernetes/src/main/java/io/kubernetes/client/openapi/models/V1Secret.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.google.gson.annotations.SerializedName;
2020
import com.google.gson.stream.JsonReader;
2121
import com.google.gson.stream.JsonWriter;
22+
import io.kubernetes.client.custom.MapUtils;
2223
import io.kubernetes.client.openapi.models.V1ObjectMeta;
2324
import java.io.IOException;
2425
import java.util.HashMap;
@@ -259,7 +260,7 @@ public boolean equals(Object o) {
259260
}
260261
V1Secret v1Secret = (V1Secret) o;
261262
return Objects.equals(this.apiVersion, v1Secret.apiVersion) &&
262-
Objects.equals(this.data, v1Secret.data) &&
263+
MapUtils.equals(this.data, v1Secret.data) &&
263264
Objects.equals(this.immutable, v1Secret.immutable) &&
264265
Objects.equals(this.kind, v1Secret.kind) &&
265266
Objects.equals(this.metadata, v1Secret.metadata) &&
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
Copyright 2024 The Kubernetes Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
package io.kubernetes.client.custom;
14+
15+
import org.junit.Test;
16+
17+
import java.util.HashMap;
18+
import java.util.Map;
19+
20+
import static org.junit.Assert.assertFalse;
21+
import static org.junit.Assert.assertTrue;
22+
23+
public class MapUtilsTest {
24+
@Test
25+
public void testEquals() {
26+
Map<String, byte[]> left = new HashMap<String, byte[]>() {{
27+
put("foo", "bar".getBytes());
28+
}};
29+
Map<String, byte[]> right = new HashMap<String, byte[]>() {{
30+
put("foo", "bar".getBytes());
31+
}};
32+
assertTrue(MapUtils.equals(left, right));
33+
}
34+
35+
@Test
36+
public void testNotEquals() {
37+
Map<String, byte[]> left = new HashMap<String, byte[]>() {{
38+
put("foo", "baz".getBytes());
39+
}};
40+
Map<String, byte[]> right = new HashMap<String, byte[]>() {{
41+
put("foo", "bar".getBytes());
42+
}};
43+
assertFalse(MapUtils.equals(left, right));
44+
}
45+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
Copyright 2024 The Kubernetes Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
package io.kubernetes.client.openapi;
14+
15+
import io.kubernetes.client.openapi.models.V1Secret;
16+
import org.junit.Test;
17+
18+
import static org.junit.Assert.assertEquals;
19+
20+
import java.util.HashMap;
21+
22+
public class V1SecretTest {
23+
@Test
24+
public void testV1SecretEquality() {
25+
V1Secret left = new V1Secret()
26+
.data(new HashMap<String, byte[]>() {{
27+
put("foo", "bar".getBytes());
28+
}});
29+
V1Secret right = new V1Secret()
30+
.data(new HashMap<String, byte[]>() {{
31+
put("foo", "bar".getBytes());
32+
}});
33+
assertEquals(left, right);
34+
}
35+
}

scripts/patches/secret.diff

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
diff --git a/kubernetes/src/main/java/io/kubernetes/client/openapi/models/V1Secret.java b/kubernetes/src/main/java/io/kubernetes/client/openapi/models/V1Secret.java
2+
index 8fdadaac6..5fe296242 100644
3+
--- a/kubernetes/src/main/java/io/kubernetes/client/openapi/models/V1Secret.java
4+
+++ b/kubernetes/src/main/java/io/kubernetes/client/openapi/models/V1Secret.java
5+
@@ -19,6 +19,7 @@ import com.google.gson.annotations.JsonAdapter;
6+
import com.google.gson.annotations.SerializedName;
7+
import com.google.gson.stream.JsonReader;
8+
import com.google.gson.stream.JsonWriter;
9+
+import io.kubernetes.client.custom.MapUtils;
10+
import io.kubernetes.client.openapi.models.V1ObjectMeta;
11+
import java.io.IOException;
12+
import java.util.HashMap;
13+
@@ -259,7 +260,7 @@ public class V1Secret implements io.kubernetes.client.common.KubernetesObject {
14+
}
15+
V1Secret v1Secret = (V1Secret) o;
16+
return Objects.equals(this.apiVersion, v1Secret.apiVersion) &&
17+
- Objects.equals(this.data, v1Secret.data) &&
18+
+ MapUtils.equals(this.data, v1Secret.data) &&
19+
Objects.equals(this.immutable, v1Secret.immutable) &&
20+
Objects.equals(this.kind, v1Secret.kind) &&
21+
Objects.equals(this.metadata, v1Secret.metadata) &&

0 commit comments

Comments
 (0)