From c88a391caa88178806103e29ccfed2c7215d04e1 Mon Sep 17 00:00:00 2001 From: Yousong Zhou Date: Thu, 5 Sep 2019 06:39:57 +0000 Subject: [PATCH 1/2] lbagent: reuse mcclient client session --- pkg/lbagent/api.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/pkg/lbagent/api.go b/pkg/lbagent/api.go index 6a362f96db..da76816adf 100644 --- a/pkg/lbagent/api.go +++ b/pkg/lbagent/api.go @@ -45,6 +45,8 @@ type ApiHelper struct { haState string haStateProvider HaStateProvider + + mcclientSession *mcclient.ClientSession } func NewApiHelper(opts *Options) (*ApiHelper, error) { @@ -103,10 +105,19 @@ func (h *ApiHelper) SetHaStateProvider(hsp HaStateProvider) { } func (h *ApiHelper) adminClientSession(ctx context.Context) *mcclient.ClientSession { + s := h.mcclientSession + if s != nil { + token := s.GetToken() + expires := token.GetExpires() + if time.Now().Add(time.Hour).After(expires) { + return s + } + } + region := h.opts.CommonOptions.Region apiVersion := "v2" - s := auth.GetAdminSession(ctx, region, apiVersion) - return s + h.mcclientSession = auth.GetAdminSession(ctx, region, apiVersion) + return h.mcclientSession } func (h *ApiHelper) agentPeekOnce(ctx context.Context) (*models.LoadbalancerAgent, error) { From 173a316f3eb8cb55df4a0d2cd11d0ab32766a8ee Mon Sep 17 00:00:00 2001 From: Yousong Zhou Date: Thu, 5 Sep 2019 06:46:39 +0000 Subject: [PATCH 2/2] lbagent: fix possible out of bound index access --- pkg/lbagent/models/haproxy.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pkg/lbagent/models/haproxy.go b/pkg/lbagent/models/haproxy.go index d41ea9db7b..2f03aa897f 100644 --- a/pkg/lbagent/models/haproxy.go +++ b/pkg/lbagent/models/haproxy.go @@ -74,7 +74,7 @@ func (b *LoadbalancerCorpus) GenHaproxyConfigs(dir string, opts *AgentParams) (* } for _, lbcert := range b.LoadbalancerCertificates { d := []byte(lbcert.Certificate) - if d[len(d)-1] != '\n' { + if len(d) > 0 && d[len(d)-1] != '\n' { d = append(d, '\n') } d = append(d, []byte(lbcert.PrivateKey)...) @@ -154,7 +154,9 @@ func (b *LoadbalancerCorpus) GenHaproxyConfigs(dir string, opts *AgentParams) (* r.LoadbalancersEnabled = append(r.LoadbalancersEnabled, lb) d := buf.Bytes() - d = d[:len(d)-2] + if len(d) > 1 { // this is for sure because of "\n\n" + d = d[:len(d)-2] + } fn := fmt.Sprintf("%s.%s", lb.Id, agentutils.HaproxyCfgExt) p := filepath.Join(dir, fn) err := ioutil.WriteFile(p, d, agentutils.FileModeFile)