|
1 | 1 | /*
|
2 |
| - * Copyright 2021 Red Hat, Inc. and/or its affiliates |
3 |
| - * and other contributors as indicated by the @author tags. |
| 2 | + * Copyright 2021 Red Hat, Inc. and/or its affiliates and other contributors as indicated by |
| 3 | + * the @author tags. |
4 | 4 | *
|
5 |
| - * Licensed under the Apache License, Version 2.0 (the "License"); |
6 |
| - * you may not use this file except in compliance with the License. |
7 |
| - * You may obtain a copy of the License at |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
| 6 | + * in compliance with the License. You may obtain a copy of the License at |
8 | 7 | *
|
9 | 8 | * http://www.apache.org/licenses/LICENSE-2.0
|
10 | 9 | *
|
11 |
| - * Unless required by applicable law or agreed to in writing, software |
12 |
| - * distributed under the License is distributed on an "AS IS" BASIS, |
13 |
| - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 |
| - * See the License for the specific language governing permissions and |
15 |
| - * limitations under the License. |
| 10 | + * Unless required by applicable law or agreed to in writing, software distributed under the License |
| 11 | + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 12 | + * or implied. See the License for the specific language governing permissions and limitations under |
| 13 | + * the License. |
16 | 14 | */
|
17 | 15 |
|
18 | 16 | package io.javaoperatorsdk.operator.processing.dependent.kubernetes;
|
19 | 17 |
|
20 |
| -import com.fasterxml.jackson.databind.ObjectMapper; |
21 |
| -import com.fasterxml.jackson.databind.node.ObjectNode; |
| 18 | +import java.util.Objects; |
| 19 | + |
22 | 20 | import io.fabric8.kubernetes.api.model.HasMetadata;
|
23 | 21 | import io.fabric8.kubernetes.api.model.ObjectMeta;
|
24 | 22 | import io.javaoperatorsdk.operator.processing.event.source.filter.OnUpdateFilter;
|
25 | 23 |
|
26 |
| -import java.util.Objects; |
| 24 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 25 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
27 | 26 |
|
28 | 27 | /**
|
29 |
| - * addresses the additional events noted on https://github.com/fabric8io/kubernetes-client/issues/5215 |
| 28 | + * addresses the additional events noted on |
| 29 | + * https://github.com/fabric8io/kubernetes-client/issues/5215 |
30 | 30 | */
|
31 | 31 | public class MetadataAwareOnUpdateFilter<T extends HasMetadata> implements OnUpdateFilter<T> {
|
32 | 32 |
|
33 |
| - private static final ObjectMapper mapper = new ObjectMapper(); |
34 |
| - |
35 |
| - @Override |
36 |
| - public boolean accept(HasMetadata newResource, HasMetadata oldResource) { |
37 |
| - ObjectMeta newMetadata = newResource.getMetadata(); |
38 |
| - ObjectMeta oldMetadata = oldResource.getMetadata(); |
39 |
| - // quick check if anything meaningful has changed |
40 |
| - if (!Objects.equals(newMetadata.getAnnotations(), oldMetadata.getAnnotations()) |
41 |
| - || !Objects.equals(newMetadata.getLabels(), oldMetadata.getLabels()) |
42 |
| - || !Objects.equals(newMetadata.getGeneration(), oldMetadata.getGeneration())) { |
43 |
| - return true; |
44 |
| - } |
45 |
| - // check everything else besides the metadata |
46 |
| - // since the hierarchy of model may not implement hashCode/equals, we'll convert to a generic form |
47 |
| - // that should be less expensive than full serialization |
48 |
| - var newNode = (ObjectNode)mapper.valueToTree(newResource); |
49 |
| - newNode.remove("metadata"); |
50 |
| - var oldNode = (ObjectNode)mapper.valueToTree(oldResource); |
51 |
| - oldNode.remove("metadata"); |
52 |
| - return !oldNode.equals(newNode); |
| 33 | + private static final ObjectMapper mapper = new ObjectMapper(); |
| 34 | + |
| 35 | + @Override |
| 36 | + public boolean accept(HasMetadata newResource, HasMetadata oldResource) { |
| 37 | + ObjectMeta newMetadata = newResource.getMetadata(); |
| 38 | + ObjectMeta oldMetadata = oldResource.getMetadata(); |
| 39 | + // quick check if anything meaningful has changed |
| 40 | + if (!Objects.equals(newMetadata.getAnnotations(), oldMetadata.getAnnotations()) |
| 41 | + || !Objects.equals(newMetadata.getLabels(), oldMetadata.getLabels()) |
| 42 | + || !Objects.equals(newMetadata.getGeneration(), oldMetadata.getGeneration())) { |
| 43 | + return true; |
53 | 44 | }
|
| 45 | + // check everything else besides the metadata |
| 46 | + // since the hierarchy of model may not implement hashCode/equals, we'll convert to a generic |
| 47 | + // form |
| 48 | + // that should be less expensive than full serialization |
| 49 | + var newNode = (ObjectNode) mapper.valueToTree(newResource); |
| 50 | + newNode.remove("metadata"); |
| 51 | + var oldNode = (ObjectNode) mapper.valueToTree(oldResource); |
| 52 | + oldNode.remove("metadata"); |
| 53 | + return !oldNode.equals(newNode); |
| 54 | + } |
54 | 55 |
|
55 | 56 | }
|
0 commit comments