Skip to content

Commit

Permalink
Add the ability to set metadata (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Suderman authored Sep 28, 2020
1 parent 86f1d92 commit 0f08e1b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 23 deletions.
54 changes: 32 additions & 22 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,34 @@ import (
"fmt"
"os"

"github.com/fairwindsops/agones-allocator-client/pkg/allocator"
"github.com/fairwindsops/agones-allocator-client/pkg/ping"

pb "agones.dev/agones/pkg/allocation/go"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"k8s.io/klog"

"github.com/fairwindsops/agones-allocator-client/pkg/allocator"
"github.com/fairwindsops/agones-allocator-client/pkg/ping"
)

var (
version string
versionCommit string
keyFile string
certFile string
caCertFile string
hosts []string
pingServers map[string]string
namespace string
multicluster bool
demoCount int
demoDelay int
demoDuration int
labelSelector map[string]string
pingTargets []string
maxRetries int
protocol string
version string
versionCommit string
keyFile string
certFile string
caCertFile string
hosts []string
pingServers map[string]string
namespace string
multicluster bool
demoCount int
demoDelay int
demoDuration int
labelSelector map[string]string
pingTargets []string
maxRetries int
protocol string
metaLabels map[string]string
metaAnnotations map[string]string
)

func init() {
Expand All @@ -57,12 +60,14 @@ func init() {
rootCmd.PersistentFlags().StringToStringVar(&pingServers, "hosts-ping", nil, "A map hosts and and ping servers. If nil, you must set hosts.")
rootCmd.PersistentFlags().StringVarP(&namespace, "namespace", "n", "default", "The namespace of gameservers to request from")
rootCmd.PersistentFlags().BoolVarP(&multicluster, "multicluster", "m", false, "If true, multicluster allocation will be requested")
rootCmd.PersistentFlags().StringToStringVar(&labelSelector, "labels", nil, "A map of labels to match on the allocation.")
rootCmd.PersistentFlags().StringToStringVar(&labelSelector, "labels-required", nil, "A map of labels to match on the allocation.")
rootCmd.PersistentFlags().IntVar(&maxRetries, "max-retries", 10, "The maximum number of times to retry allocations.")

rootCmd.AddCommand(allocateCmd)
rootCmd.AddCommand(loadTestCmd)
allocateCmd.PersistentFlags().StringToStringVar(&metaLabels, "meta-labels", nil, "A map of labels to add to the gameserver on allocation")
allocateCmd.PersistentFlags().StringToStringVar(&metaAnnotations, "meta-annotations", nil, "A map of annotations to add to the gameserver on allocation")

rootCmd.AddCommand(loadTestCmd)
loadTestCmd.PersistentFlags().IntVarP(&demoCount, "count", "c", 10, "The number of connections to make during the demo.")
loadTestCmd.PersistentFlags().IntVar(&demoDelay, "delay", 2, "The number of seconds to wait between connections")
loadTestCmd.PersistentFlags().IntVarP(&demoDuration, "duration", "d", 10, "The number of seconds to leave each connection open.")
Expand Down Expand Up @@ -98,7 +103,6 @@ func init() {
}
}
}

}

var rootCmd = &cobra.Command{
Expand All @@ -125,6 +129,12 @@ var allocateCmd = &cobra.Command{
if err != nil {
klog.Fatal(err)
}

allocatorClient.MetaPatch = &pb.MetaPatch{
Labels: metaLabels,
Annotations: metaAnnotations,
}

allocation, err := allocatorClient.AllocateGameserverWithRetry()
if err != nil {
klog.Fatal(err)
Expand Down
4 changes: 3 additions & 1 deletion pkg/allocator/allocator.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ type Client struct {
MatchLabels map[string]string
// MaxRetries is the maximum number of times to retry allocations
MaxRetries int
// MetaPatch is metadata to set on the gameserver
MetaPatch *pb.MetaPatch
}

// Allocation is a game server allocation
Expand All @@ -69,7 +71,6 @@ type Allocation struct {

// NewClient builds a new client object
func NewClient(keyFile, certFile, cacertFile, namespace string, multiCluster bool, labelSelector map[string]string, hosts []string, pingHosts map[string]string, maxRetries int) (*Client, error) {

cert, err := ioutil.ReadFile(certFile)
if err != nil {
return nil, err
Expand Down Expand Up @@ -151,6 +152,7 @@ func (c *Client) allocateGameserver() (*Allocation, error) {
RequiredGameServerSelector: &pb.LabelSelector{
MatchLabels: c.MatchLabels,
},
MetaPatch: c.MetaPatch,
}

resp, err := c.makeRequest(request)
Expand Down

0 comments on commit 0f08e1b

Please sign in to comment.