Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow to link a lockable resource to a node #455

Open
monger39 opened this issue Jan 31, 2023 · 6 comments · May be fixed by #457
Open

allow to link a lockable resource to a node #455

monger39 opened this issue Jan 31, 2023 · 6 comments · May be fixed by #457
Labels
enhancement Good for Groovy shared library It might be done in groovy shared library much more easier than here lock Jenkins node Lockable resource interactions with Jenkins nodes.

Comments

@monger39
Copy link

What feature do you want to see added?

in our (mostly scripted) pipelines we need to use a single resource for which an agent can have between 0-4 of such resources.
Not each agent has such resource, some have 2, some 3, some 4. Typically a single resource is locked by one of the 'parallel' (sub-)builds of a pipeline. Agents typically have 6 execution slots. The resources are identified by a generic label.
The challenge for the pipeline job is to lock a free resource available through an online node where an execution slot is available.

In our current solution each resource has in its name the name of a node, and an index nr on that node; for example this could be "virtmachA_devres01", "virtmachA_devres02", .. where "virtmachA" is the name of an agent. Then, in the pipeline script, we request a lock with label "devres" (the label assigned to all the resources). Given the resource, we extract the agent name, and assign the particular code t that, using the "node(derived_agent) {...}".
This mostly works, unless the node is offline, or all execution slots are taken.
It is also annoying when the node has to be taken offline for maintenance; all resources for that agent need to be reserved manually.

My enhancement proposal is to extend the LockableResource with an (optional) 'agentname'. When defined:

  • getting (locking) the resource can only be done when the agent is online and has a free slot
  • the name of the agent is available through the resource.agent , thus allowing to schedule the workload on that agent.

This is maybe related to #341 and/or #309. However I think this proposal would need only a little work in the plugin, whereas it makes the pipeline scripts much easier and more robust.

Upstream changes

No response

@mPokornyETM
Copy link
Contributor

This is exact the reason, why I created the #341
I think all of the magic, what you explain can be done in groovy as well. An helper in our shared-library will be great.

import org.jenkins.plugins.lockableresources.LockableResourcesManager as LRM

@NonCPS
def getResource(String name)
{
  def allr = LRM.get().getResources();
  for(int i = 0; i < allr.size(); i++)
  {
    if ( allr[i] && (allr[i].getName() == name) )
      return allr[i];
  }

  return null;
}

def filteredNodes = [];
int expectedCount = 4;
// the best way to synchronize  code between jobs is to use lockable-resources ;-)
lock('get-nodes') {
  jenkins.model.Jenkins.instance.computers.each { c ->
     if (filteredNodes.size() == expectedCount)
       return;

      if (c == null || c.node == null) {
        return; // sometimes (no idea why) c.node is null
      }

      if (!c.isOnline()) {
        // is offline ignore it
        return;
      }

      String nodeName = c.node.selfLabel.name;
      
     der resource = getResource(nodeName);

     if (resource == null) {
        // resources does not exist, create it
        return;
     }

    if (resource.isLocked() || resource.isQueued() || resource.isReserved()) {
      // occupated
      return;
    }

     filteredNodes.push(nodeName);;
  }
}

echo 'my free and online nodes: ' + filteredNodes

ps: I type it here just so, without syntax check or what ever, But I thing as idea it shall helps

@mPokornyETM mPokornyETM added lock Jenkins node Lockable resource interactions with Jenkins nodes. Good for Groovy shared library It might be done in groovy shared library much more easier than here Triage Need to clarify, remove, close or whatever to clean up open issues / PRs labels Feb 1, 2023
@mPokornyETM mPokornyETM added this to the Triage milestone Feb 1, 2023
@mPokornyETM
Copy link
Contributor

I think I provide the functionality here https://github.com/mPokornyETM/jenkins-lockable-resources-shared-library/blob/feature/step/vars/lockNode.groovy
@monger39 Has you time to check it if it will work for you?

@mPokornyETM mPokornyETM removed the Triage Need to clarify, remove, close or whatever to clean up open issues / PRs label Feb 6, 2023
@monger39
Copy link
Author

monger39 commented Feb 6, 2023

Hi @mPokornyETM , I have tried understanding how the code would work to solve my problem, but unfortunately I don't see it ...
Could you provide a hint how to use the library ? I would try to test, but the real test is in an air-gapped system so takes a bit of time to get the stuff setup. Especially because I do not use shared libs at all (yet / still)...

@mPokornyETM mPokornyETM linked a pull request Feb 8, 2023 that will close this issue
21 tasks
@mPokornyETM
Copy link
Contributor

Yes of course. I will provide examples in the #457 . ! This is other location, because it will be much more easier to maintain it this way.
I am next week on vacation. Therefore I can continue later here.
Anyway I will write here when I am done.

@monger39
Copy link
Author

monger39 commented Apr 1, 2023

mea culpa @mPokornyETM for not replying sooner. I was having (and still have) severe problems getting a test environment up. Just want to let you know that I have not yet forgotten and will test as soon as my systems is up again.

@mPokornyETM
Copy link
Contributor

@monger39 is fine. We are in open sorece community, no presure here. When you has time it is welcome, when not it is accepted ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Good for Groovy shared library It might be done in groovy shared library much more easier than here lock Jenkins node Lockable resource interactions with Jenkins nodes.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants