此Cron作业有什么问题,它没有运行。如何调试

时间:2019-07-25 15:44:32

标签: kubernetes cron google-kubernetes-engine kubernetes-helm kubernetes-cronjob

我正在使用Helm进行k8s部署,我需要一个可以访问URL的cron作业。我已经编写了脚本,并且如果我将其作为Shell脚本任务独立运行,则脚本会起作用。为什么cron作业无法在其中运行脚本。

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: sampletemplaterelease-myapi
  labels:
    app.kubernetes.io/name: myapi
    helm.sh/chart: myapi-0.1.0
    app.kubernetes.io/instance: sampletemplaterelease
    app.kubernetes.io/version: "1.0"
    app.kubernetes.io/managed-by: Tiller
spec:
  schedule: "*/5 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: hello
            image: busybox
            args:
            - /bin/bash
            - -c
            - |
              accessTokenBody=$(curl -X POST -d "client_id=sample&grant_type=sample&username=sample&password=sample&override=true" https://sample.com/sample/sample)
              accessToken=$(jq -r  '.access_token' <<< "${accessTokenBody}" )
              echo $accessToken
              sfSyncTriggerResult=$(curl -X POST -H "Content-Length: 0" -H "Authorization: Bearer $accessToken" https://sample.com/sample/sample)
              echo $sfSyncTriggerResult
              echo "${sfSyncTriggerResult}" | jq '.'
              errorCount=$(echo $sfSyncTriggerResult | jq '. | length')
              echo "Total Number Of Errors"
              echo $errorCount
              if [ "$errorCount" -gt 0 ]
                  then
                      echo "not working, exiting"
                      exit 1
                      break
              else
                      echo "Sync triggered successfully"
                  fi
          restartPolicy: OnFailure
  

kubectl记录podname:

% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1183    0  1053  100   130   1193    147 --:--:-- --:--:-- --:--:--  1339
/bin/bash: line 1: jq: command not found



  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0

/bin/bash: line 7: jq: command not found
/bin/bash: line 8: jq: command not found
Total Number Of Errors

Sync triggered successfully
/bin/bash: line 11: [: : integer expression expected

1 个答案:

答案 0 :(得分:1)

您可以通过使用具有DocumentReference的任何映像或在容器内部安装 services.AddAuthentication(options => { options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme; options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme; options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme; }).AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, options => { options.LoginPath = new PathString("/"); options.AccessDeniedPath = "/Identity/Account/AccessDenied"; options.Cookie = new CookieBuilder() { SecurePolicy = CookieSecurePolicy.SameAsRequest, Path = "/" }; options.SlidingExpiration = true; options.ExpireTimeSpan = TimeSpan.FromMinutes(sessionTimeout); }).AddOpenIdConnect(options => { options.ClientId = Configuration.GetValue<string>("oidc:ClientId"); options.ClientSecret = Configuration["oidc:ClientSecret"]; options.CallbackPath = new PathString("/auth/callback"); options.GetClaimsFromUserInfoEndpoint = true; options.Authority = Configuration["oidc:Authority"]; options.SignedOutRedirectUri = "/"; options.RequireHttpsMetadata = false; options.SaveTokens = true; options.UseTokenLifetime = true; var signingKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(Configuration["oidc:ClientSecret"])); options.TokenValidationParameters = new TokenValidationParameters { RequireSignedTokens = true, IssuerSigningKey = signingKey, ValidateAudience = true, ValidateIssuer = true, ValidateIssuerSigningKey = true, }; options.ResponseType = OpenIdConnectResponseType.Code; options.AuthenticationMethod = OpenIdConnectRedirectBehavior.RedirectGet; options.Scope.Clear(); options.Scope.Add("openid"); options.Scope.Add("email"); options.Scope.Add("profile"); options.Events = new OpenIdConnectEvents() { OnTicketReceived = context => { var identity = context.Principal.Identity as ClaimsIdentity; if (identity != null) { if (!context.Principal.HasClaim(c => c.Type == ClaimTypes.Name) && identity.HasClaim(c => c.Type == "name")) identity.AddClaim(new Claim(ClaimTypes.Name, identity.FindFirst("name").Value)); if (context.Properties.Items.ContainsKey(".TokenNames")) { string[] tokenNames = context.Properties.Items[".TokenNames"].Split(';'); foreach (var tokenName in tokenNames) { string tokenValue = context.Properties.Items[$".Token.{tokenName}"]; identity.AddClaim(new Claim(tokenName, tokenValue)); } } } var cp = new ClaimsPrincipal(identity); context.Principal = cp; return Task.CompletedTask; }, //OnTokenValidated = context => //{ // ClaimsIdentity identity = (ClaimsIdentity)context.Principal.Identity; // Claim Name = identity.FindFirst("preferred_username"); // Claim gender = identity.FindFirst(ClaimTypes.Gender); // Claim sub = identity.FindFirst(ClaimTypes.NameIdentifier); // var claimsToKeep = new List<Claim> { Name, gender, sub }; // var newIdentity = new ClaimsIdentity(claimsToKeep, identity.AuthenticationType); // context.Principal = new ClaimsPrincipal(newIdentity); // return Task.FromResult(0); //}, OnAuthenticationFailed = context => { context.Response.Redirect("/"); context.HandleResponse(); return Task.CompletedTask; }, OnRedirectToIdentityProvider = context => { context.ProtocolMessage.SetParameter("pfidpadapterid", Configuration["oidc:pfidpadapterid"]); return Task.FromResult(0); } }; }); 来实现。因此,我尝试的一种方法是使用jq作为容器映像而不是jq,然后在其中安装alpine。请参阅以下内容:

busybox