Skip to content

Commit a3fff6b

Browse files
committed
remove pdb
1 parent eed564f commit a3fff6b

File tree

5 files changed

+12
-174
lines changed

5 files changed

+12
-174
lines changed

README.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ In certain cases there is a need to filter out certain fields when the patch gen
6565
To help in these scenarios there are the following options to be used when calculating diffs:
6666
- `IgnoreStatusFields`
6767
- `IgnoreVolumeClaimTemplateTypeMetaAndStatus`
68-
- `IgnorePDBSelector`
6968
- `IgnoreField("field-name-to-ignore")`
7069

7170
Example:
@@ -88,11 +87,6 @@ This CalculateOptions removes status fields from both objects before comparing.
8887

8988
This CalculateOption clears volumeClaimTemplate fields from both objects before comparing (applies to statefulsets).
9089

91-
#### IgnorePdbSelector
92-
93-
Checks `selector` fields of PDB objects before comparing and removes them if they match. `reflect.DeepEquals` is used for the equality check.
94-
This is required because map fields using `patchStrategy:"replace"` will always diff regardless if they are otherwise equal.
95-
9690
#### IgnoreField("field-name-to-ignore")
9791

9892
This CalculateOption removes the field provided (as a string) in the call before comparing them. A common usage might be to remove the metadata fields by using the `IgnoreField("metadata")` option.

docs/legacy.md

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ The legacy version was last available with the 0.1.1 version.
2020
- PersistentVolumeClaim
2121
- Service
2222
- ServiceAccount
23-
- PodDisruptionBudget
2423
- Unstructured
2524
- Node
2625

@@ -33,11 +32,11 @@ objectMatcher.Match(e.ObjectOld, e.ObjectNew)
3332

3433
### The idea
3534

36-
There are existing libraries in the wild that can calculate a patch by giving them two different objects. If the patch is empty the two objects match and we are ready, right?
35+
There are existing libraries in the wild that can calculate a patch by giving them two different objects. If the patch is empty the two objects match and we are ready, right?
3736
Well not quite. JSON Merge Patch, defined by [rfc7396](https://tools.ietf.org/html/rfc7396) replaces lists completely which is not always what we need. Kubernetes defines
3837
and uses a modified version called [strategic merge patch](https://github.com/kubernetes/community/blob/master/contributors/devel/sig-api-machinery/strategic-merge-patch.md).
3938

40-
Strategic Merge Patch extends the JSON Merge Patch format by adding explicit directives for deleting, replacing, ordering and merging lists.
39+
Strategic Merge Patch extends the JSON Merge Patch format by adding explicit directives for deleting, replacing, ordering and merging lists.
4140
It uses the go struct tag of the API objects to determine what lists should be merged and which ones should not. Worth to note it's not tied to
4241
Kubernetes objects only, so we can use this for matching custom go structs as well.
4342

@@ -46,21 +45,21 @@ Kubernetes objects only, so we can use this for matching custom go structs as we
4645
#### Defaults and version compatibility
4746

4847
As outlined previously Kubernetes objects are amended with different default values when submitted. For example PodSpec.RestartPolicy will be set to "Always" when
49-
ommitted from the object, so we will have a mismatch when we try to compare it later. This library uses the same functions to set the default values on the local
50-
objects before matching so that they won't differ.
48+
ommitted from the object, so we will have a mismatch when we try to compare it later. This library uses the same functions to set the default values on the local
49+
objects before matching so that they won't differ.
5150

52-
Since the defaults functions are defined in the main kubernetes repo, there is a higher chance that objects decorated using these functions will be incompatible
51+
Since the defaults functions are defined in the main kubernetes repo, there is a higher chance that objects decorated using these functions will be incompatible
5352
when comparing them with objects coming from different server versions. Also since the library depends on the kubernetes repo it is more tightly coupled to it's
5453
version.
5554

56-
To preserve compatibility between the client and server versions [.circleci/config.yml](.circleci/config.yml) contains jobs that run the integration test suite against Kubernetes
55+
To preserve compatibility between the client and server versions [.circleci/config.yml](.circleci/config.yml) contains jobs that run the integration test suite against Kubernetes
5756
versions from 1.10 to 1.14. The library itself is known and tested to be working with operators depending on Kubernetes client version 1.12 and 1.13.
5857

5958
#### Generated values
6059

61-
There are values that are generated by the API Server dynamically. To workaround this the library removes null fields from the patch as long as it's not inside a list.
62-
(In case of lists, even if we remove null fields we would still left with `setElementOrder` directives)
63-
This works as long as we don't set/unset complete fields on the objects conditionally, because in that case we would miss to detect a change to unset something.
60+
There are values that are generated by the API Server dynamically. To workaround this the library removes null fields from the patch as long as it's not inside a list.
61+
(In case of lists, even if we remove null fields we would still left with `setElementOrder` directives)
62+
This works as long as we don't set/unset complete fields on the objects conditionally, because in that case we would miss to detect a change to unset something.
6463

65-
In case a field gets removed from somewhere inside a list we have to explicitly tell to ignore it. One example is NodePort in Service objects, see [service.go](service.go).
64+
In case a field gets removed from somewhere inside a list we have to explicitly tell to ignore it. One example is NodePort in Service objects, see [service.go](service.go).
6665
Another example is Volume and VolumeMount generated automatically for the service account token, see [pod.go](pod.go).

patch/ignorepdb.go

Lines changed: 0 additions & 84 deletions
This file was deleted.

tests/integration_test.go

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
appsv1 "k8s.io/api/apps/v1"
2525
"k8s.io/api/autoscaling/v2beta1"
2626
v1 "k8s.io/api/core/v1"
27-
v1beta12 "k8s.io/api/policy/v1beta1"
2827
rbacv1 "k8s.io/api/rbac/v1"
2928
crdv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
3029
"k8s.io/apimachinery/pkg/api/resource"
@@ -704,58 +703,6 @@ func TestIntegration(t *testing.T) {
704703
},
705704
},
706705
}),
707-
NewTestMatch("pdb match",
708-
&v1beta12.PodDisruptionBudget{
709-
TypeMeta: metav1.TypeMeta{
710-
APIVersion: v1beta12.SchemeGroupVersion.String(),
711-
Kind: "PodDisruptionBudget",
712-
},
713-
ObjectMeta: standardObjectMeta(),
714-
Spec: v1beta12.PodDisruptionBudgetSpec{
715-
MinAvailable: intstrRef(intstr.FromInt(1)),
716-
Selector: &metav1.LabelSelector{
717-
MatchLabels: map[string]string{
718-
"app": "example",
719-
},
720-
},
721-
},
722-
}),
723-
NewTestDiff("pdb diff on matchlabels",
724-
&v1beta12.PodDisruptionBudget{
725-
TypeMeta: metav1.TypeMeta{
726-
APIVersion: v1beta12.SchemeGroupVersion.String(),
727-
Kind: "PodDisruptionBudget",
728-
},
729-
ObjectMeta: standardObjectMeta(),
730-
Spec: v1beta12.PodDisruptionBudgetSpec{
731-
MinAvailable: intstrRef(intstr.FromInt(1)),
732-
Selector: &metav1.LabelSelector{
733-
MatchLabels: map[string]string{
734-
"app": "example",
735-
},
736-
},
737-
},
738-
}).
739-
withRemoteChange(func(i interface{}) {
740-
pdb := i.(*v1beta12.PodDisruptionBudget)
741-
pdb.Spec.Selector.MatchLabels = map[string]string{
742-
"app": "example2",
743-
}
744-
}),
745-
NewTestMatch("pdb match even though status changes",
746-
&v1beta12.PodDisruptionBudget{
747-
ObjectMeta: standardObjectMeta(),
748-
Spec: v1beta12.PodDisruptionBudgetSpec{
749-
MinAvailable: intstrRef(intstr.FromInt(1)),
750-
},
751-
}).
752-
withRemoteChange(func(i interface{}) {
753-
pdb := i.(*v1beta12.PodDisruptionBudget)
754-
pdb.Status.CurrentHealthy = 1
755-
pdb.Status.DesiredHealthy = 1
756-
pdb.Status.ExpectedPods = 1
757-
pdb.Status.ObservedGeneration = 1
758-
}),
759706
NewTestMatch("pvc match",
760707
&v1.PersistentVolumeClaim{
761708
ObjectMeta: standardObjectMeta(),

tests/main_test.go

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,13 @@ import (
2323
"testing"
2424

2525
"emperror.dev/errors"
26-
"github.com/cisco-open/k8s-objectmatcher/patch"
2726
admregv1 "k8s.io/api/admissionregistration/v1"
2827
appsv1 "k8s.io/api/apps/v1"
2928
"k8s.io/api/autoscaling/v2beta1"
3029
v1 "k8s.io/api/core/v1"
31-
policyv1beta1 "k8s.io/api/policy/v1beta1"
3230
rbacv1 "k8s.io/api/rbac/v1"
3331
crdv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
3432
apiextension "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
35-
"k8s.io/apimachinery/pkg/api/meta"
3633
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3734
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
3835
"k8s.io/apimachinery/pkg/runtime"
@@ -41,6 +38,8 @@ import (
4138
"k8s.io/client-go/kubernetes"
4239
"k8s.io/client-go/tools/clientcmd"
4340
"k8s.io/klog/v2"
41+
42+
"github.com/cisco-open/k8s-objectmatcher/patch"
4443
)
4544

4645
var (
@@ -196,7 +195,6 @@ func testMatchOnObject(testItem *TestItem, ignoreField string) error {
196195
opts := []patch.CalculateOption{
197196
patch.IgnoreStatusFields(),
198197
patch.IgnoreVolumeClaimTemplateTypeMetaAndStatus(),
199-
patch.IgnorePDBSelector(),
200198
patch.IgnoreField(ignoreField),
201199
}
202200

@@ -354,22 +352,6 @@ func testMatchOnObject(testItem *TestItem, ignoreField string) error {
354352
log.Printf("Failed to remove object %s %+v", existing.GetName(), err)
355353
}
356354
}()
357-
case *policyv1beta1.PodDisruptionBudget:
358-
existing, err = testContext.Client.PolicyV1beta1().PodDisruptionBudgets(newObject.GetNamespace()).Create(context.Background(), newObject.(*policyv1beta1.PodDisruptionBudget), metav1.CreateOptions{})
359-
if err != nil {
360-
return errors.WrapWithDetails(err, "failed to create object", "object", newObject)
361-
}
362-
defer func() {
363-
err = testContext.Client.PolicyV1beta1().PodDisruptionBudgets(newObject.GetNamespace()).Delete(context.Background(), existing.GetName(), deleteOptions)
364-
if err != nil {
365-
log.Printf("Failed to remove object %s %+v", existing.GetName(), err)
366-
}
367-
}()
368-
// In case of PodDisruptionBudget resources `patchStrategy:"replace"` results in a constant diff from kubernetes version 1.21
369-
// The IgnorePDBSelector CalculateOption can only help in this situation if either the current or modified object contains gvk information, this is why it's set here explicitly
370-
te, _ := meta.TypeAccessor(existing)
371-
te.SetKind("PodDisruptionBudget")
372-
te.SetAPIVersion("policy/v1beta1")
373355
case *v1.PersistentVolumeClaim:
374356
existing, err = testContext.Client.CoreV1().PersistentVolumeClaims(newObject.GetNamespace()).Create(context.Background(), newObject.(*v1.PersistentVolumeClaim), metav1.CreateOptions{})
375357
if err != nil {

0 commit comments

Comments
 (0)