Forum Discussion

Sake_Blok's avatar
Sake_Blok
Icon for Nimbostratus rankNimbostratus
Sep 30, 2005

Dynamically selecting a class (based on poolname)

Hi,

Is it possible to dynamically select a class? I want to use one class per pool which lists a cookie per server and select that server based on the cookie. Using passive cookies is not an option since I cannot change the cookie-values on the server. Of course I can write an iRule per virtual server, but it would be nicer to have a general one that is able to select the appropriate class.

It would be something like this:


rule prefCookie-iRule {
   when CLIENT_ACCEPTED {
      set defpool [LB::server pool]
      log local0. "defpool selected for -> $defpool"
   }
   when HTTP_REQUEST {
     if { [HTTP::cookie exists "lbsid"] } {
        set tmp [findclass [HTTP::cookie "lbsid"] ::$defpool " "]
        if { $tmp ne "" } {
           log local0. $tmp              
           node $tmp
        } else {
           log local0. "[HTTP::cookie "lbsid"] is not a valid cookie-value!"
           pool $defpool
        }
     } else {
        log local0. "No lbsid cookie found!"
        pool $defpool
     }
   }
   when LB_FAILED {
      log local0. "LB failed, so chose a new member!"
      pool $defpool
      LB::reselect
   }
}

The problem is that the line 'set tmp [findclass [HTTP::cookie "lbsid"] ::$defpool " "]' does not work, it looks like this line is interpreted __before__ $defpool is set. How can I solve this? I guess I need a command that has a class as output and a string as input, but is there anything capable of doing that?

Cheers, Sake

4 Replies

  • drteeth_127330's avatar
    drteeth_127330
    Historic F5 Account
    You also should be able to do this with the subst command which might not be as heavy weight as eval. subst performs variable substitution, so the argument is expanded twice, once by the TCL interpreter and once by subst. Something like this should work:

    
    set tmp [findclass [HTTP::cookie "lbsid"] [subst $::$defpool] " "]
  • drteeth_127330's avatar
    drteeth_127330
    Historic F5 Account
    Did you try my original suggestion?

    
    set tmp [findclass [HTTP::cookie "lbsid"] [subst $::$defpool] " "]

    What you posted is subtly different.
  • unRuleY_95363's avatar
    unRuleY_95363
    Historic F5 Account
    You need to have a $ in front of the ::.

     

    Please use Dr.Teeth's suggestion and watch the placement of the punctuation.
  • unRuleY_95363's avatar
    unRuleY_95363
    Historic F5 Account
    BTW, you could still use subst, but like this:

    set tmp [eval [subst {findclass [HTTP::cookie "lbsid"] \$::$defpool " "}]]

    This also doesn't require the extra variable.