Skip to content

Instantly share code, notes, and snippets.

@richardcase
Created September 27, 2021 14:18
Show Gist options
  • Save richardcase/896c28aa6759e49d468085cbff2867cf to your computer and use it in GitHub Desktop.
Save richardcase/896c28aa6759e49d468085cbff2867cf to your computer and use it in GitHub Desktop.
Tink Client Example
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"log"
"os"
"github.com/tinkerbell/tink/client"
"github.com/tinkerbell/tink/protos/hardware"
"github.com/tinkerbell/tink/protos/packet"
)
func main() {
os.Setenv("TINKERBELL_CERT_URL", "http://192.168.8.3:42114/cert")
os.Setenv("TINKERBELL_GRPC_AUTHORITY", "192.168.8.3:42113")
tinkClient, err := client.TinkHardwareClient()
if err != nil {
log.Fatalln(err)
}
ctx := context.Background()
empty := &hardware.Empty{}
resp, err := tinkClient.All(ctx, empty)
if err != nil {
log.Fatalln(err)
}
for {
in, err := resp.Recv()
if err == io.EOF {
break
}
if err != nil {
log.Fatalln(err)
}
meta := &packet.Metadata{}
err = json.Unmarshal([]byte(in.Metadata), meta)
if err != nil {
log.Fatalln(err)
}
reigniteHost := containsTag(meta.Instance.Tags, "reignite")
fmt.Printf("hostname=%s,ip=%s,reignite host=%t\n", meta.Instance.Hostname, meta.Instance.GetIps(), reigniteHost)
}
resp.CloseSend()
}
func containsTag(tags []string, tagToFind string) bool {
for _, tag := range tags {
if tag == tagToFind {
return true
}
}
return false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment