dotnet restore not work with jenkins slave pod in kubernetes

6/19/2019

I am running CI with Jenkins slave pod in Kubernetes.The pipeline definition as follow(I omitted the less important parts):

#!/usr/bin/groovy

def label = "worker-${UUID.randomUUID().toString()}"


properties([
    parameters([
        gitParameter(name: 'BRANCH_NAME', defaultValue: 'master', selectedValue: 'DEFAULT', type: 'PT_BRANCH'),
        booleanParam(name: 'CAN_DOCKER_BUILD_AND_PUSH',defaultValue: true, description: 'build and push docker image'),
        booleanParam(name: 'CAN_DEPLOY_TO_DEV',defaultValue: true, description: 'deploy to dev')
   ])
])
podTemplate(label: label, serviceAccount: 'jenkins', containers: [
  containerTemplate(name: 'netcore21', image: 'mcr.microsoft.com/dotnet/core/sdk:2.1.700', ttyEnabled: true),
  containerTemplate(name: 'docker', image: 'docker:18.09.6', command: 'cat', ttyEnabled: true),  
  containerTemplate(name: 'helm', image: 'lachlanevenson/k8s-helm:v2.6.0', command: 'cat', ttyEnabled: true), 
],
volumes: [
  hostPathVolume(mountPath: '/var/run/docker.sock', hostPath: '/var/run/docker.sock'),
  hostPathVolume(mountPath: '/home/jenkins/.nuget/packages', hostPath: '/home/.nuget/packages/')
]){
    node(label) {

    stage('check out') {
        checkout scm
        sh "git checkout ${params.BRANCH_NAME}" 
    }

    stage('unit test') { 

    }

    stage('build'){
        container('netcore21') {
        sh """
        cd src/xxx
        dotnet restore
        dotnet build
        dotnet publish -c Release -o publish 
        """
    }
  }

    stage("docker build && docker push"){


    }

    stage("deploy"){

}
}

When executes dotnet restore Nuget complains about:

/usr/share/dotnet/sdk/2.1.700/NuGet.targets(121,5): error : Unable to load the service index for source https://api.nuget.org/v3/index.json. [/home/jenkins/workspace/xxxx.csproj]
/usr/share/dotnet/sdk/2.1.700/NuGet.targets(121,5): error :   No such device or address [/home/jenkins/workspace/xxx.csproj]

I wrote a simple console application to test the connective to the https://api.nuget.org/v3/index.json, the exception is thrown

root@jenkins-slave-05czw-2lmq5:~/workspace/test# cat Program.cs 
using System;
using System.Net.Http;
using System.Threading.Tasks;

namespace test
{
    class Program
    {
        static void Main(string[] args)
        {
          using(var client = new HttpClient())
          {
              var task  =  client.GetStringAsync("https://api.nuget.org/v3/index.json");
              task.Wait();
              Console.WriteLine(task.Result);
          }
        }
    }
}
root@jenkins-slave-05czw-2lmq5:~/workspace/test# dotnet run

Unhandled Exception: System.AggregateException: One or more errors occurred. (No such device or address) ---> System.Net.Http.HttpRequestException: No such device or address ---> System.Net.Sockets.SocketException: No such device or address
   at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
   at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
   at System.Threading.Tasks.ValueTask`1.get_Result()
   at System.Net.Http.HttpConnectionPool.CreateConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Threading.Tasks.ValueTask`1.get_Result()
   at System.Net.Http.HttpConnectionPool.WaitForCreatedConnectionAsync(ValueTask`1 creationTask)
   at System.Threading.Tasks.ValueTask`1.get_Result()
   at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
   at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Net.Http.HttpClient.FinishSendAsyncUnbuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
   at System.Net.Http.HttpClient.GetStringAsyncCore(Task`1 getTask)
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at System.Threading.Tasks.Task.Wait()
   at test.Program.Main(String[] args) in /home/jenkins/workspace/test/Program.cs:line 14

however, if using curl to visit https://api.nuget.org/v3/index.json, everything works as expectation

root@jenkins-slave-05czw-2lmq5:/home# curl -v https://api.nuget.org/v3/index.json
*   Trying 23.101.10.141...
* TCP_NODELAY set
* Connected to api.nuget.org (23.101.10.141) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH
* successfully set certificate verify locations:
*   CAfile: /etc/ssl/certs/ca-certificates.crt
  CApath: /etc/ssl/certs
* TLSv1.2 (OUT), TLS header, Certificate Status (22):
* TLSv1.2 (OUT), TLS handshake, Client hello (1):
* TLSv1.2 (IN), TLS handshake, Server hello (2):
* TLSv1.2 (IN), TLS handshake, Certificate (11):
* TLSv1.2 (IN), TLS handshake, Server key exchange (12):
* TLSv1.2 (IN), TLS handshake, Server finished (14):
* TLSv1.2 (OUT), TLS handshake, Client key exchange (16):
* TLSv1.2 (OUT), TLS change cipher, Client hello (1):
* TLSv1.2 (OUT), TLS handshake, Finished (20):
* TLSv1.2 (IN), TLS change cipher, Client hello (1):
* TLSv1.2 (IN), TLS handshake, Finished (20):
* SSL connection using TLSv1.2 / ECDHE-RSA-AES256-GCM-SHA384
* ALPN, server did not agree to a protocol
* Server certificate:
*  subject: CN=*.nuget.org
*  start date: Feb 23 01:19:59 2018 GMT
*  expire date: Feb 23 01:19:59 2020 GMT
*  subjectAltName: host "api.nuget.org" matched cert's "*.nuget.org"
*  issuer: C=US; ST=Washington; L=Redmond; O=Microsoft Corporation; OU=Microsoft IT; CN=Microsoft IT TLS CA 1
*  SSL certificate verify ok.
> GET /v3/index.json HTTP/1.1
> Host: api.nuget.org
> User-Agent: curl/7.52.1
> Accept: */*
> 
< HTTP/1.1 302 Redirect
< Content-Length: 163
< Content-Type: text/html; charset=UTF-8
< Location: https://nuget.cdn.azure.cn/v3/index.json
< Server: Microsoft-IIS/10.0
< Request-Context: appId=cid-v1:a54ed3cc-b9d9-4b03-919d-9c8f0100db4d
< Strict-Transport-Security: max-age=31536000
< Set-Cookie: ARRAffinity=18c76b6a356c9d2353a445d71b9faa6378ffc64d2584fc298de4c1d586edbf90;Path=/;HttpOnly;Domain=api.nuget.org
< Date: Wed, 19 Jun 2019 07:56:55 GMT
< 
<head><title>Document Moved</title></head>
* Curl_http_done: called premature == 0
* Connection #0 to host api.nuget.org left intact

I have test Jenkins slave pod with Go project and Java project, DotNet project was limited by this issue,

Is this a nuget bug or a network problem I have overlook ?

-- Blue Steel
.net
.net-core
jenkins
kubernetes
nuget

0 Answers