Skip to content

Commit

Permalink
Merge pull request #5 from mkmik/annomerge
Browse files Browse the repository at this point in the history
Fix annotation merge and knot8 pull with Knot8file
  • Loading branch information
Marko Mikulicic authored Sep 14, 2020
2 parents a907261 + ab1ee00 commit e9f6f66
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
6 changes: 4 additions & 2 deletions cmd/knot8/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ func renderOriginalAnnoBody(fields map[string]Field) ([]byte, error) {

type PullCmd struct {
CommonFlags
CommonSchemaFlags
Upstream string `arg:"" help:"Upstream file/URL." type:"file"`
}

Expand All @@ -283,7 +284,7 @@ func (s *PullCmd) Run(ctx *Context) error {
return fmt.Errorf("pull/merge with %d files currently not supported", len(s.Paths))
}

manifestSetC, err := openFields(s.Paths, "")
manifestSetC, err := openFields(s.Paths, s.Schema)
if err != nil {
return err
}
Expand All @@ -304,7 +305,7 @@ func (s *PullCmd) Run(ctx *Context) error {
return err
}

manifestSetU, err := openFields([]string{upstream.Name()}, "")
manifestSetU, err := openFields([]string{upstream.Name()}, s.Schema)
if err != nil {
return err
}
Expand Down Expand Up @@ -479,6 +480,7 @@ func openFields(paths []string, schema string) (*ManifestSet, error) {
return nil, err
}
ms = ms.Intersect(manifests)
manifests.MergeAnnotations(ms)
ext, err := parseFields(ms)
if err != nil {
return nil, err
Expand Down
27 changes: 25 additions & 2 deletions cmd/knot8/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ func parseManifests(f *shadowFile) (Manifests, error) {
}

for k := range m.Metadata.Annotations {
c := strings.SplitN(k, "/", 2)
if !strings.HasSuffix(c[0], annoDomain) {
if !isOurAnnotation(k) {
delete(m.Metadata.Annotations, k)
}
}
Expand Down Expand Up @@ -134,3 +133,27 @@ func (ms Manifests) Intersect(src Manifests) Manifests {
}
return res
}

// MergeAnnotations copies annotations from the src into ms if they exist
func (ms Manifests) MergeAnnotations(src Manifests) {
anns := map[FQN]map[string]string{}
for _, s := range src {
anns[s.FQN()] = s.Metadata.Annotations
}

for _, d := range ms {
for k, v := range anns[d.FQN()] {
if isOurAnnotation(k) {
if d.Metadata.Annotations == nil {
d.Metadata.Annotations = map[string]string{}
}
d.Metadata.Annotations[k] = v
}
}
}
}

func isOurAnnotation(a string) bool {
c := strings.SplitN(a, "/", 2)
return strings.HasSuffix(c[0], annoDomain)
}

0 comments on commit e9f6f66

Please sign in to comment.