Remove the GitOps Toolkit metadata from generated objects

Signed-off-by: Stefan Prodan <stefan.prodan@gmail.com>
This commit is contained in:
Stefan Prodan 2021-06-17 17:49:22 +03:00
parent 47de726345
commit c36a13ccff
No known key found for this signature in database
GPG Key ID: 3299AEB0E4085BAF
15 changed files with 63 additions and 24 deletions

View File

@ -262,7 +262,7 @@ func (c *DaemonSetController) createPrimaryDaemonSet(cd *flaggerv1.Canary, inclu
Name: primaryName,
Namespace: cd.Namespace,
Labels: makePrimaryLabels(labels, primaryLabelValue, label),
Annotations: canaryDae.Annotations,
Annotations: filterMetadata(canaryDae.Annotations),
OwnerReferences: []metav1.OwnerReference{
*metav1.NewControllerRef(cd, schema.GroupVersionKind{
Group: flaggerv1.SchemeGroupVersion.Group,

View File

@ -48,6 +48,9 @@ func TestDaemonSetController_Sync_ConsistentNaming(t *testing.T) {
primarySelectorValue := daePrimary.Spec.Selector.MatchLabels[dc.label]
sourceSelectorValue := dae.Spec.Selector.MatchLabels[dc.label]
assert.Equal(t, primarySelectorValue, fmt.Sprintf("%s-primary", sourceSelectorValue))
annotation := daePrimary.Annotations["kustomize.toolkit.fluxcd.io/checksum"]
assert.Equal(t, "", annotation)
}
func TestDaemonSetController_Sync_InconsistentNaming(t *testing.T) {

View File

@ -263,7 +263,7 @@ func (c *DeploymentController) createPrimaryDeployment(cd *flaggerv1.Canary, inc
Name: primaryName,
Namespace: cd.Namespace,
Labels: makePrimaryLabels(labels, primaryLabelValue, label),
Annotations: canaryDep.Annotations,
Annotations: filterMetadata(canaryDep.Annotations),
OwnerReferences: []metav1.OwnerReference{
*metav1.NewControllerRef(cd, schema.GroupVersionKind{
Group: flaggerv1.SchemeGroupVersion.Group,
@ -335,7 +335,7 @@ func (c *DeploymentController) reconcilePrimaryHpa(cd *flaggerv1.Canary, init bo
ObjectMeta: metav1.ObjectMeta{
Name: primaryHpaName,
Namespace: cd.Namespace,
Labels: hpa.Labels,
Labels: filterMetadata(hpa.Labels),
OwnerReferences: []metav1.OwnerReference{
*metav1.NewControllerRef(cd, schema.GroupVersionKind{
Group: flaggerv1.SchemeGroupVersion.Group,

View File

@ -48,6 +48,9 @@ func TestDeploymentController_Sync_ConsistentNaming(t *testing.T) {
primarySelectorValue := depPrimary.Spec.Selector.MatchLabels[dc.label]
assert.Equal(t, primarySelectorValue, fmt.Sprintf("%s-primary", dc.labelValue))
annotation := depPrimary.Annotations["kustomize.toolkit.fluxcd.io/checksum"]
assert.Equal(t, "", annotation)
hpaPrimary, err := mocks.kubeClient.AutoscalingV2beta2().HorizontalPodAutoscalers("default").Get(context.TODO(), "podinfo-primary", metav1.GetOptions{})
require.NoError(t, err)
assert.Equal(t, depPrimary.Name, hpaPrimary.Spec.ScaleTargetRef.Name)

View File

@ -419,6 +419,9 @@ func newDeploymentControllerTest(dc deploymentConfigs) *appsv1.Deployment {
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Name: dc.name,
Annotations: map[string]string{
"kustomize.toolkit.fluxcd.io/checksum": "0a40893bfdc545d62125bd3e74eeb2ebaa7097c2",
},
},
Spec: appsv1.DeploymentSpec{
Selector: &metav1.LabelSelector{

View File

@ -69,6 +69,8 @@ func getPorts(cd *flaggerv1.Canary, cs []corev1.Container) map[string]int32 {
return ports
}
const toolkitMarker = "toolkit.fluxcd.io"
// makeAnnotations appends an unique ID to annotations map
func makeAnnotations(annotations map[string]string) (map[string]string, error) {
idKey := "flagger-id"
@ -83,8 +85,7 @@ func makeAnnotations(annotations map[string]string) (map[string]string, error) {
id := fmt.Sprintf("%x-%x-%x-%x-%x", uuid[0:4], uuid[4:6], uuid[6:8], uuid[8:10], uuid[10:])
for k, v := range annotations {
// skip Flux GC markers
if strings.Contains(k, "/checksum") {
if strings.Contains(k, toolkitMarker) {
continue
}
if k != idKey {
@ -96,9 +97,23 @@ func makeAnnotations(annotations map[string]string) (map[string]string, error) {
return res, nil
}
func filterMetadata(meta map[string]string) map[string]string {
res := make(map[string]string)
for k, v := range meta {
if strings.Contains(k, toolkitMarker) {
continue
}
res[k] = v
}
return res
}
func includeLabelsByPrefix(labels map[string]string, includeLabelPrefixes []string) map[string]string {
filteredLabels := make(map[string]string)
for key, value := range labels {
if strings.Contains(key, toolkitMarker) {
continue
}
for _, includeLabelPrefix := range includeLabelPrefixes {
if includeLabelPrefix == "*" || strings.HasPrefix(key, includeLabelPrefix) {
filteredLabels[key] = value
@ -113,6 +128,9 @@ func includeLabelsByPrefix(labels map[string]string, includeLabelPrefixes []stri
func makePrimaryLabels(labels map[string]string, labelValue string, label string) map[string]string {
res := make(map[string]string)
for k, v := range labels {
if strings.Contains(k, toolkitMarker) {
continue
}
if k != label {
res[k] = v
}

View File

@ -211,7 +211,7 @@ func (i *IngressRouter) SetRoutes(
func (i *IngressRouter) makeAnnotations(annotations map[string]string) map[string]string {
res := make(map[string]string)
for k, v := range makeAnnotations(annotations) {
for k, v := range filterMetadata(annotations) {
if !strings.Contains(k, i.GetAnnotationWithPrefix("canary")) &&
!strings.Contains(k, "kubectl.kubernetes.io/last-applied-configuration") &&
!strings.Contains(k, i.GetAnnotationWithPrefix("canary-weight")) &&
@ -232,7 +232,7 @@ func (i *IngressRouter) makeAnnotations(annotations map[string]string) map[strin
func (i *IngressRouter) makeHeaderAnnotations(annotations map[string]string,
header string, headerValue string, headerRegex string, cookie string) map[string]string {
res := make(map[string]string)
for k, v := range makeAnnotations(annotations) {
for k, v := range filterMetadata(annotations) {
if !strings.Contains(v, i.GetAnnotationWithPrefix("canary")) {
res[k] = v
}

View File

@ -48,6 +48,9 @@ func TestIngressRouter_Reconcile(t *testing.T) {
inCanary, err := router.kubeClient.NetworkingV1().Ingresses("default").Get(context.TODO(), canaryName, metav1.GetOptions{})
require.NoError(t, err)
annotation := inCanary.Annotations["kustomize.toolkit.fluxcd.io/checksum"]
assert.Equal(t, "", annotation)
// test initialisation
assert.Equal(t, "true", inCanary.Annotations[canaryAn])
assert.Equal(t, "0", inCanary.Annotations[canaryWeightAn])
@ -181,6 +184,6 @@ func TestIngressRouter_ABTest(t *testing.T) {
// test initialisation
assert.Equal(t, "true", inCanary.Annotations[canaryAn])
assert.Equal(t, "test", inCanary.Annotations[table.annotation])
assert.Equal(t, "", inCanary.Annotations["kustomize.toolkit.fluxcd.io/checksum"])
}
}

View File

@ -260,7 +260,7 @@ func (ir *IstioRouter) reconcileVirtualService(canary *flaggerv1.Canary) error {
if vtClone.ObjectMeta.Annotations == nil {
vtClone.ObjectMeta.Annotations = make(map[string]string)
} else {
vtClone.ObjectMeta.Annotations = makeAnnotations(vtClone.ObjectMeta.Annotations)
vtClone.ObjectMeta.Annotations = filterMetadata(vtClone.ObjectMeta.Annotations)
}
vtClone.ObjectMeta.Annotations[configAnnotation] = string(b)

View File

@ -206,8 +206,8 @@ func (c *KubernetesDefaultRouter) reconcileService(canary *flaggerv1.Canary, nam
if svc.ObjectMeta.Annotations == nil {
svc.ObjectMeta.Annotations = make(map[string]string)
}
if diff := cmp.Diff(makeAnnotations(metadata.Annotations), svc.ObjectMeta.Annotations); diff != "" {
svcClone.ObjectMeta.Annotations = makeAnnotations(metadata.Annotations)
if diff := cmp.Diff(filterMetadata(metadata.Annotations), svc.ObjectMeta.Annotations); diff != "" {
svcClone.ObjectMeta.Annotations = filterMetadata(metadata.Annotations)
updateService = true
}
if diff := cmp.Diff(metadata.Labels, svc.ObjectMeta.Labels); diff != "" {

View File

@ -406,6 +406,9 @@ func newTestCanaryIngress() *flaggerv1.Canary {
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Name: "nginx",
Annotations: map[string]string{
"kustomize.toolkit.fluxcd.io/checksum": "0a40893bfdc545d62125bd3e74eeb2ebaa7097c2",
},
},
Spec: flaggerv1.CanarySpec{
TargetRef: flaggerv1.CrossNamespaceObjectReference{
@ -444,7 +447,8 @@ func newTestIngress() *netv1.Ingress {
Namespace: "default",
Name: "podinfo",
Annotations: map[string]string{
"kubernetes.io/ingress.class": "nginx",
"kubernetes.io/ingress.class": "nginx",
"kustomize.toolkit.fluxcd.io/checksum": "0a40893bfdc545d62125bd3e74eeb2ebaa7097c2",
},
},
Spec: netv1.IngressSpec{

View File

@ -129,7 +129,7 @@ func (skp *SkipperRouter) Reconcile(canary *flaggerv1.Canary) error {
if cmp.Diff(iClone.Spec, canaryIngress.Spec) != "" {
ingressClone := canaryIngress.DeepCopy()
ingressClone.Spec = iClone.Spec
ingressClone.Annotations = makeAnnotations(iClone.Annotations)
ingressClone.Annotations = filterMetadata(iClone.Annotations)
_, err := skp.kubeClient.NetworkingV1().Ingresses(canary.Namespace).Update(context.TODO(), ingressClone, metav1.UpdateOptions{})
if err != nil {

View File

@ -72,7 +72,7 @@ func (tr *TraefikRouter) Reconcile(canary *flaggerv1.Canary) error {
Name: apexName,
Namespace: canary.Namespace,
Labels: tsMetadata.Labels,
Annotations: makeAnnotations(tsMetadata.Annotations),
Annotations: filterMetadata(tsMetadata.Annotations),
OwnerReferences: []metav1.OwnerReference{
*metav1.NewControllerRef(canary, schema.GroupVersionKind{
Group: flaggerv1.SchemeGroupVersion.Group,

View File

@ -4,9 +4,14 @@ import (
"strings"
)
const toolkitMarker = "toolkit.fluxcd.io"
func includeLabelsByPrefix(labels map[string]string, includeLabelPrefixes []string) map[string]string {
filteredLabels := make(map[string]string)
for key, value := range labels {
if strings.Contains(key, toolkitMarker) {
continue
}
for _, includeLabelPrefix := range includeLabelPrefixes {
if includeLabelPrefix == "*" || strings.HasPrefix(key, includeLabelPrefix) {
filteredLabels[key] = value
@ -18,14 +23,13 @@ func includeLabelsByPrefix(labels map[string]string, includeLabelPrefixes []stri
return filteredLabels
}
func makeAnnotations(in map[string]string) map[string]string {
out := make(map[string]string)
for key, value := range in {
// skip Flux GC markers
if strings.Contains(key, "/checksum") {
func filterMetadata(meta map[string]string) map[string]string {
res := make(map[string]string)
for k, v := range meta {
if strings.Contains(k, toolkitMarker) {
continue
}
out[key] = value
res[k] = v
}
return out
return res
}

View File

@ -41,9 +41,10 @@ func TestIncludeLabelsByPrefix(t *testing.T) {
func TestIncludeLabelsByPrefixWithWildcard(t *testing.T) {
labels := map[string]string{
"foo": "foo-value",
"bar": "bar-value",
"lorem": "ipsum",
"foo": "foo-value",
"bar": "bar-value",
"lorem": "ipsum",
"kustomize.toolkit.fluxcd.io/checksum": "some",
}
includeLabelPrefix := []string{"*"}