Skip to content
This repository has been archived by the owner on Mar 8, 2022. It is now read-only.

Commit

Permalink
Add annotate binary
Browse files Browse the repository at this point in the history
This takes yaml manifests and adds the same annotations as the webhook
handler would. This is useful to test and validate manifests, e.g:

./annotate .ci/workflow.yaml  | argo lint /dev/stdin
  • Loading branch information
discordianfish committed Oct 30, 2019
1 parent e042236 commit d740752
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
68 changes: 68 additions & 0 deletions cmd/annotate/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package main

import (
"flag"
"fmt"
"log"
"net/url"
"os"
"strings"

"github.com/google/go-github/v24/github"
handler "github.com/itskoko/k8s-webhook-handler"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

func main() {
var (
evType = flag.String("-type", "push", "Event type")
action = flag.String("-action", "", "Event action")
revision = flag.String("-revision", "0000000000000000000000000000000000000000", "Revision")
ref = flag.String("-ref", "refs/heads/master", "Ref")
before = flag.String("-before", "0000000000000000000000000000000000000000", "Before")
repoURL = flag.String("-url", "git://github.com/itskoko/k8s-webhook-handler.git", "git URL")
sshUser = flag.String("-ssh-user", "git", "SSH user")
)
flag.Parse()
files := flag.Args()
if len(files) == 0 {
log.Fatal("Usage: annotate [flags] file-to-annotate [more-files-to-annotate...]")
}
u, err := url.Parse(*repoURL)
if err != nil {
log.Fatal(err)
}

var (
sshURL = fmt.Sprintf("%s@%s:%s", *sshUser, u.Host, u.Path)
fullName = strings.TrimSuffix(u.Path, ".git")
)

repo := &github.Repository{
FullName: &fullName,
GitURL: repoURL,
SSHURL: &sshURL,
}
annotations := (&handler.Event{
Type: *evType,
Action: *action,
Revision: *revision,
Ref: *ref,
Before: *before,
Repository: repo,
}).Annotations()

for _, file := range flag.Args() {
fh, err := os.Open(file)
if err != nil {
log.Fatalf("Couldn't read file %s: %s", err)
}
defer fh.Close()
obj, err := handler.Decode(fh)
meta.NewAccessor().SetAnnotations(obj, annotations)
if err := unstructured.UnstructuredJSONScheme.Encode(obj, os.Stdout); err != nil {
log.Fatal(err)
}
}
}
8 changes: 7 additions & 1 deletion loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package handler
import (
"context"
"fmt"
"io"
"io/ioutil"
"net/url"
"os"
Expand Down Expand Up @@ -68,7 +69,12 @@ func (l *GithubLoader) Load(ctx context.Context, repo, path, ref string) (runtim
return nil, fmt.Errorf("Couldn't get file %s from %s/%s at %s: %s", path, owner, name, ref, err)
}
defer file.Close()
content, err := ioutil.ReadAll(file)
return Decode(file)
}

// Decode reads a reader and parses the stream as runtime.Object.
func Decode(r io.Reader) (runtime.Object, error) {
content, err := ioutil.ReadAll(r)
if err != nil {
return nil, fmt.Errorf("Couldn't read file: %s", err)
}
Expand Down

0 comments on commit d740752

Please sign in to comment.