<p>@Mkash, don't you know in advance what services you have in your <code>services.yaml</code> file?</p>
<p>Maintain a list in groovy, and see whether the input is in that list or not.</p>
<p>Something like:</p>
<pre><code>availableServices = [&quot;ew_ws_gateway&quot;, &quot;ingress&quot;]

services = [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;ew_ws_gateway&quot;]

services.each { svc -&gt;
    if (svc in availableServices) {
        println(&quot;Service exist&quot; + svc)
    }
}
</code></pre>
<p>The other option is to change the parameter from type of <code>text</code> to multiple-choice possibly using <a href="https://plugins.jenkins.io/extended-choice-parameter/" rel="nofollow noreferrer">https://plugins.jenkins.io/extended-choice-parameter/</a></p>

CC BY-SA 4.0
2020-09-10T07:06:43.803Z
2020-09-10T07:06:43.803Z
Ranjandas
<p>I provided the script to provision a new feature environment inside k8s it will create all the configs required, namespaces, resources and whatever...</p>
<p>in my script, I described this param:</p>
<pre><code>parameters {
text(name: 'SERVICE_LIST', defaultValue: '', description: 'Services to deploy, separated by line breaks.')}
</code></pre>
<p>when the developer is provisioning a new env, he will provide a list of services that he wants to enable on that environment</p>
<p>also in my script, I described the following piece of code which are enabling all services requested(its located inside the stage and step):</p>
<pre><code>def services = replaced.split('\\n')
   services.each { SERVICE -&gt;
    sh &quot;echo turning on service ${SERVICE}&quot;
    sh &quot;yq w -i  ${DEPLOY_ENV_FOLDER}/services.yaml '${SERVICE}.enabled' true&quot;}
</code></pre>
<p>this is an example of a services file:</p>
<p><a href="https://i.sstatic.net/GJ9wm.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/GJ9wm.png" alt="service file" /></a></p>
<p>so, when the developer provides the services that he wants, the script goes to this file, enable the services and apply to k8s</p>
<p>now my question is how to assure that the services provided by the developer are correct and that they exist in the file? otherwise, I need to abort the script. I tried to use <code>try{} catch{}</code> in the second piece of code above to define the exception for abort services which are provided by developers incorrectly, do you have any other solution?</p>

<jenkins><kubernetes><jenkins-pipeline><groovy>
How to handle services when the developers provide incorrectly in k8s
Mkash
# How to handle services when the developers provide incorrectly in k8s

(Asked by: username_0 on 2020-09-09)

I provided the script to provision a new feature environment inside k8s it will create all the configs required, namespaces, resources and whatever...
in my script, I described this param:
parameters {
text(name: 'SERVICE_LIST', defaultValue: '', description: 'Services to deploy, separated by line breaks.')}

when the developer is provisioning a new env, he will provide a list of services that he wants to enable on that environment
also in my script, I described the following piece of code which are enabling all services requested(its located inside the stage and step):
def services = replaced.split('\\n')
   services.each { SERVICE ->
    sh "echo turning on service ${SERVICE}"
    sh "yq w -i  ${DEPLOY_ENV_FOLDER}/services.yaml '${SERVICE}.enabled' true"}

this is an example of a services file:

so, when the developer provides the services that he wants, the script goes to this file, enable the services and apply to k8s
now my question is how to assure that the services provided by the developer are correct and that they exist in the file? otherwise, I need to abort the script. I tried to use try{} catch{} in the second piece of code above to define the exception for abort services which are provided by developers incorrectly, do you have any other solution?

**Tags:** jenkins, kubernetes, jenkins-pipeline, groovy


---

## 1 answers

### Answer by username_1 on 2020-09-10. Score: 1

@username_0, don't you know in advance what services you have in your services.yaml file?
Maintain a list in groovy, and see whether the input is in that list or not.
Something like:
availableServices = ["ew_ws_gateway", "ingress"]

services = ["a", "b", "c", "ew_ws_gateway"]

services.each { svc ->
    if (svc in availableServices) {
        println("Service exist" + svc)
    }
}

The other option is to change the parameter from type of text to multiple-choice possibly using https://plugins.jenkins.io/extended-choice-parameter/


---

