Ok I tested this out, but still not working. The new rule just doesn’t get added and I get no error back
Here is the full code (you can ignore a lot of as code above doing the upgrade will be used once I can get the upgrade to work):
package main
import (
"fmt"
"os"
"time"
"reflect"
rancher "github.com/rancher/go-rancher/v2"
log "github.com/Sirupsen/logrus"
"github.com/urfave/cli"
)
type Client struct {
client *rancher.RancherClient
}
var (
VERSION = "v0.0.0-dev"
)
var withoutPagination *rancher.ListOpts
func createClient(rancherURL, accessKey, secretKey string) (*rancher.RancherClient) {
client, err := rancher.NewRancherClient(&rancher.ClientOpts{
Url: rancherURL,
AccessKey: accessKey,
SecretKey: secretKey,
Timeout: time.Second * 5,
})
if err != nil {
log.Errorf("Failed to create a client for rancher, error: %s", err)
os.Exit(1)
}
return client
}
func listRancherLoadBalancerServices(client *rancher.RancherClient) []*rancher.LoadBalancerService {
var servicesList []*rancher.LoadBalancerService
services, err := client.LoadBalancerService.List(withoutPagination)
if err != nil {
log.Errorf("cannot get services: %+v", err)
}
for k := range services.Data {
servicesList = append(servicesList, &services.Data[k])
}
return servicesList
}
func beforeApp(c *cli.Context) error {
if c.GlobalBool("debug") {
log.SetLevel(log.DebugLevel)
}
return nil
}
func main() {
app := cli.NewApp()
app.Name = "rancher-svcd"
app.Version = VERSION
app.Usage = "rancher-svcd"
app.Action = start
app.Before = beforeApp
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "debug,d",
},
cli.StringFlag{
Name: "metadata-url",
Value: "http://169.254.169.250/2016-07-29",
Usage: "Provide full URL of Metadata",
EnvVar: "RANCHER_METADATA_URL",
},
cli.StringFlag{
Name: "router-service-tag",
Value: "services_router",
Usage: "Tag used to identify the load balancer serviced used for routing",
EnvVar: "ROUTER_SERVICE_TAG",
},
cli.StringFlag{
Name: "lb-id",
Usage: "Public rancher load balancer ID",
},
cli.StringFlag{
Name: "rancher-url",
Value: "http://localhost:8080/",
Usage: "Provide full URL of rancher server",
EnvVar: "RANCHER_URL",
},
cli.StringFlag{
Name: "rancher-access-key",
Usage: "Rancher Access Key",
EnvVar: "RANCHER_ACCESS_KEY",
},
cli.StringFlag{
Name: "rancher-secret-key",
Usage: "Rancher Secret Key",
EnvVar: "RANCHER_SECRET_KEY",
},
}
app.Run(os.Args)
}
func start(c *cli.Context) error {
log.Info("starting up")
// create the rancher client
rancherClient := createClient(c.String("rancher-url"),
c.String("rancher-access-key"),
c.String("rancher-secret-key"))
var servicesRouter *rancher.LoadBalancerService
var targetLBs []*rancher.LoadBalancerService
loadBalancerServices := listRancherLoadBalancerServices(rancherClient)
// get the lb
if len(c.String("lb-id")) > 0 {
loadBalancerService, err := rancherClient.LoadBalancerService.ById(c.String("lb-id"))
if err != nil {
panic(err)
}
log.Debug(loadBalancerService)
log.Debug(loadBalancerService.LaunchConfig.Labels)
} else {
for _, s := range loadBalancerServices {
log.Debug("processing ", s.Uuid)
log.Debug("data ", s)
log.Debug("labels: ", s.LaunchConfig.Labels)
for k, v := range s.LaunchConfig.Labels {
// this lb is a service router
if k == "services_router" && v == "true" {
servicesRouter = s
log.Debug(reflect.TypeOf(s))
log.Debug(reflect.TypeOf(servicesRouter))
log.Debug(s)
break
}
// this lb is a service target
if k == "platform_identifier" {
log.Debug("found a target service ", s)
targetLBs = append(targetLBs, s)
}
}
}
}
// by now we should have the service router resource!
//if servicesRouter ? {
// log.Errorf("no services router! found")
// os.Exit(2)
//}
log.Info("using services router, ", servicesRouter.Name)
log.Info("found ", len(targetLBs), " target service(s)")
log.Debug("target LBs", targetLBs)
var requestHost string
// for each target
for i, t := range targetLBs {
var platformId string
var platformDnsDomain string
log.Debug(i, t)
// construct the request host
for k, v := range t.LaunchConfig.Labels {
log.Debug(k)
if k == "platform_identifier" {
log.Debug(v)
log.Debug(reflect.TypeOf(v))
platformId = fmt.Sprint(v)
}
if k == "platform_dns_domain" {
log.Debug(v)
log.Debug(reflect.TypeOf(v))
platformDnsDomain = fmt.Sprint(v)
}
}
requestHost = fmt.Sprintf(platformId + "." + platformDnsDomain)
log.Info("request host: ", requestHost)
}
log.Info("servicesRouter LbConfig", servicesRouter.LbConfig)
log.Info("servicesRouter LinkedServices", servicesRouter.LinkedServices)
// create port rule client
portRule := rancher.PortRule{
Protocol: "https",
Hostname: "flaccid-is-learning",
Path: "",
Priority: 10,
SourcePort: 443,
TargetPort: 80,
ServiceId: "1s1128",
}
log.Info("new port rule ", portRule)
// The strategy allows you to upgrade
strategy := &rancher.ServiceUpgrade{
InServiceStrategy: &rancher.InServiceUpgradeStrategy{
BatchSize: 1,
StartFirst: true,
LaunchConfig: servicesRouter.LaunchConfig,
},
}
// Here we can see the new rule added to the current list of rules
log.Info("updating the services router")
servicesRouter.LbConfig.PortRules = append(servicesRouter.LbConfig.PortRules, portRule)
log.Infof("New service Rules: %+v", servicesRouter.LbConfig.PortRules)
update, err := rancherClient.LoadBalancerService.ActionUpgrade(servicesRouter, strategy)
if err != nil {
panic(err)
} else {
// Notice here it isn't present in the returned object
log.Infof("update complete: +%v", update.LbConfig.PortRules)
}
return nil
}