Skip to content

Commit bef1a4a

Browse files
Add cookie attributes to SessionAffinity
Signed-off-by: Renato Vassão <[email protected]>
1 parent 27daa2c commit bef1a4a

File tree

1 file changed

+53
-1
lines changed

1 file changed

+53
-1
lines changed

pkg/apis/flagger/v1beta1/canary.go

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,11 +290,30 @@ type CanaryAnalysis struct {
290290
type SessionAffinity struct {
291291
// CookieName is the key that will be used for the session affinity cookie.
292292
CookieName string `json:"cookieName,omitempty"`
293-
// MaxAge indicates the number of seconds until the session affinity cookie will expire.
294293
// ref: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie#attributes
294+
// Domain defines the host to which the cookie will be sent.
295+
// +optional
296+
Domain string `json:"domain,omitempty"`
297+
// HttpOnly forbids JavaScript from accessing the cookie, for example, through the Document.cookie property.
298+
// +optional
299+
HttpOnly bool `json:"httpOnly,omitempty"`
300+
// MaxAge indicates the number of seconds until the session affinity cookie will expire.
295301
// The default value is 86,400 seconds, i.e. a day.
296302
// +optional
297303
MaxAge int `json:"maxAge,omitempty"`
304+
// Partitioned indicates that the cookie should be stored using partitioned storage.
305+
// +optional
306+
Partitioned bool `json:"partitioned,omitempty"`
307+
// Path indicates the path that must exist in the requested URL for the browser to send the Cookie header.
308+
// +optional
309+
Path string `json:"path,omitempty"`
310+
// SameSite controls whether or not a cookie is sent with cross-site requests.
311+
// +optional
312+
// +kubebuilder:validation:Enum=Strict;Lax;None
313+
SameSite string `json:"sameSite,omitempty"`
314+
// Secure indicates that the cookie is sent to the server only when a request is made with the https: scheme (except on localhost)
315+
// +optional
316+
Secure bool `json:"secure,omitempty"`
298317
// PrimaryCookieName is the key that will be used for the primary session affinity cookie.
299318
// +optional
300319
PrimaryCookieName string `json:"primaryCookieName,omitempty"`
@@ -640,3 +659,36 @@ func (c *Canary) SkipAnalysis() bool {
640659
}
641660
return c.Spec.SkipAnalysis
642661
}
662+
663+
// BuildCookie returns the cookie that should be used as the value of a Set-Cookie header
664+
func (s *SessionAffinity) BuildCookie(cookieName string) string {
665+
cookie := fmt.Sprintf("%s; %s=%d", cookieName, "Max-Age",
666+
s.GetMaxAge(),
667+
)
668+
669+
if s.Domain != "" {
670+
cookie += fmt.Sprintf("; %s=%s", "Domain", s.Domain)
671+
}
672+
673+
if s.HttpOnly {
674+
cookie += fmt.Sprintf("; %s", "HttpOnly")
675+
}
676+
677+
if s.Partitioned {
678+
cookie += fmt.Sprintf("; %s", "Partitioned")
679+
}
680+
681+
if s.Path != "" {
682+
cookie += fmt.Sprintf("; %s=%s", "Path", s.Path)
683+
}
684+
685+
if s.SameSite != "" {
686+
cookie += fmt.Sprintf("; %s=%s", "SameSite", s.SameSite)
687+
}
688+
689+
if s.Secure {
690+
cookie += fmt.Sprintf("; %s", "Secure")
691+
}
692+
693+
return cookie
694+
}

0 commit comments

Comments
 (0)