Skip to content

Commit

Permalink
Add meta.Duration as known type for diffing
Browse files Browse the repository at this point in the history
So that this can be used for custom resources. The motivation for this
is issue argoproj#6008: with this change one should be able to use the known
type to ensure equal durations don't present a diff, e,g. via adding to
`argocd-cm`:

    data:
      resource.customizations.knownTypeFields.cert-manager.io_Certificate: |
        - field: spec.duration
          type: meta/v1/Duration

This change is based on 5b464c9, though
I've included the API version in the type path to be consistent with the
generated type (i.e. `meta/v1/Duration` and not `meta/Duration`).

For documentation I've just manually listed the extra types that aren't
auto-generated, though this requires having to keep this list and the
code in-sync but this is probably not a big issue since the number of
extra types is not frequently changed.

Signed-off-by: Matthew Hughes <matthewhughes934@gmail.com>
  • Loading branch information
matthewhughes934 committed Dec 10, 2023
1 parent 07a2e64 commit b1e2346
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
5 changes: 4 additions & 1 deletion docs/user-guide/diffing.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,7 @@ data:
type: core/v1/PodSpec
```

The list of supported Kubernetes types is available in [diffing_known_types.txt](https://raw.githubusercontent.com/argoproj/argo-cd/master/util/argo/normalizers/diffing_known_types.txt)
The list of supported Kubernetes types is available in [diffing_known_types.txt](https://raw.githubusercontent.com/argoproj/argo-cd/master/util/argo/normalizers/diffing_known_types.txt) an additionally:

* `core/Quantity`
* `meta/v1/duration`
4 changes: 4 additions & 0 deletions util/argo/normalizers/knowntypes_normalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
log "github.com/sirupsen/logrus"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"

Expand All @@ -32,6 +33,9 @@ func init() {
knownTypes["core/Quantity"] = func() interface{} {
return &resource.Quantity{}
}
knownTypes["meta/v1/Duration"] = func() interface{} {
return &metav1.Duration{}
}
}

// NewKnownTypesNormalizer create a normalizer that re-format custom resource fields using built-in Kubernetes types.
Expand Down
27 changes: 27 additions & 0 deletions util/argo/normalizers/knowntypes_normalizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/argoproj/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/yaml"
Expand Down Expand Up @@ -228,6 +229,32 @@ spec:
assert.Equal(t, "1250M", ram)
}

func TestNormalize_Duration(t *testing.T) {
rollout := mustUnmarshalYAML(`apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
name: my-cert
spec:
duration: 8760h
`)
normalizer, err := NewKnownTypesNormalizer(map[string]v1alpha1.ResourceOverride{
"cert-manager.io/Certificate": {
KnownTypeFields: []v1alpha1.KnownTypeField{{
Type: "meta/v1/Duration",
Field: "spec.duration",
}},
},
})
require.NoError(t, err)

require.NoError(t, normalizer.Normalize(rollout))

duration, ok, err := unstructured.NestedFieldNoCopy(rollout.Object, "spec", "duration")
require.NoError(t, err)
require.True(t, ok)
require.Equal(t, "8760h0m0s", duration)
}

func TestFieldDoesNotExist(t *testing.T) {
rollout := mustUnmarshalYAML(someCRDYaml)
normalizer, err := NewKnownTypesNormalizer(map[string]v1alpha1.ResourceOverride{
Expand Down

0 comments on commit b1e2346

Please sign in to comment.