Skip to content

Commit

Permalink
Added 'versioned-explicit-ref' annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
100mik committed Sep 20, 2021
1 parent 6c75699 commit 09853d1
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
70 changes: 70 additions & 0 deletions pkg/kapp/diff/explicit_versioned_ref.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright 2020 VMware, Inc.
// SPDX-License-Identifier: Apache-2.0

package diff

import (
"fmt"
)

type VersionedRefDesc struct {
Namespace string `json:"namespace"`
APIGroup string `json:"apiGroup"`
Kind string `json:"kind"`
Name string `json:"name"`
}

type ExplicitVersionedRefAnn struct {
References []VersionedRefDesc `json:"references"`
}

type ExplicitVersionedRef struct {
Resource VersionedResource
Annotation ExplicitVersionedRefAnn
}

const (
explicitReferenceKey = "kapp.k14s.io/versioned-explicit-ref"
)

func NewExplicitVersionedRef(res VersionedResource, annotation ExplicitVersionedRefAnn) *ExplicitVersionedRef {
return &ExplicitVersionedRef{res, annotation}
}

func (e *ExplicitVersionedRef) IsReferenced() bool {
refList := e.refStringList()
resRef := e.resourceRefString()

for _, v := range refList {
if v == resRef {
return true
}
}

return false
}

func (e *ExplicitVersionedRef) refStringList() []string {
list := []string{}
for _, v := range e.Annotation.References {
if v.APIGroup == "core" {
v.APIGroup = ""
}

list = append(list, fmt.Sprintf("%s/%s/%s/%s", v.Namespace, v.APIGroup, v.Kind, v.Name))
}
return list
}

func (e *ExplicitVersionedRef) resourceRefString() string {
return e.Resource.UniqVersionedKey().String()
}

/*
Annotation with a list of references in JSON format:
kapp.k14s.io/versioned-explicit-ref: '{ "references": [ { "namespace": <resource-namespace>, "apiGroup": <resource-apiGroup>, "kind" : <resource-kind>, "name": <resource-name> } ] }'
"namespace" need not be assigned a value for cluster-scoped resources.
"apiGroup" need not be assigned a value for resources belonging to "core" API group.
*/
24 changes: 24 additions & 0 deletions pkg/kapp/diff/versioned_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,35 @@ func (d VersionedResource) updateAffected(rule ctlconf.TemplateRule, rs []ctlres
ReplacementFunc: d.buildObjRefReplacementFunc(affectedObjRef),
}

resourceExplicitKey := fmt.Sprintf("%s.%s", explicitReferenceKey, ctlres.NewUniqueResourceKey(d.res).String())
explicitReferenceMod := ctlres.StringMapAppendMod{
ResourceMatcher: ctlres.AllMatcher{},
Path: ctlres.NewPathFromStrings([]string{"metadata", "labels"}),
KVs: map[string]string{
resourceExplicitKey: d.res.Name(),
},
}

for _, res := range rs {
err := mod.Apply(res)
if err != nil {
return err
}

if val, found := res.Annotations()[explicitReferenceKey]; found {
annotation := ExplicitVersionedRefAnn{}
err := json.Unmarshal([]byte(val), &annotation)
if err != nil {
return fmt.Errorf("Error unmarshalling explicit references : %s", err)
}

if NewExplicitVersionedRef(d, annotation).IsReferenced() {
err = explicitReferenceMod.Apply(res)
if err != nil {
return err
}
}
}
}
}

Expand Down

0 comments on commit 09853d1

Please sign in to comment.