Skip to content
This repository was archived by the owner on Oct 2, 2024. It is now read-only.

Commit d05470c

Browse files
authored
Merge pull request #12 from gbevan/D20180909_img_pull_policy
added support for image-pull-policy
2 parents 2fbb47b + 98818a0 commit d05470c

File tree

2 files changed

+46
-27
lines changed

2 files changed

+46
-27
lines changed

clientapi/clientapi.go

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"fmt"
3030
"io"
3131
"io/ioutil"
32+
"log"
3233
"net/http"
3334
"os"
3435
"path/filepath"
@@ -59,36 +60,38 @@ func Debug(format string, a ...interface{}) {
5960

6061
// APIRequest structure the job request passed to the client api
6162
type APIRequest struct {
62-
AppRoleID *string
63-
AppSecretID *string // AppRole auth or Token
64-
Token *string
65-
GoStintRole *string
66-
JobJSON *string // request can be whole JSON:
67-
QName *string // or can be passed as parameters:
68-
ContainerImage *string
69-
Content *string
70-
EntryPoint *string
71-
Run *string
72-
WorkingDir *string
73-
EnvVars *string
74-
SecretRefs *string
75-
SecretFileType *string
76-
ContOnWarnings *bool
77-
URL *string
78-
VaultURL *string
63+
AppRoleID *string
64+
AppSecretID *string // AppRole auth or Token
65+
Token *string
66+
GoStintRole *string
67+
JobJSON *string // request can be whole JSON:
68+
QName *string // or can be passed as parameters:
69+
ContainerImage *string
70+
ImagePullPolicy *string
71+
Content *string
72+
EntryPoint *string
73+
Run *string
74+
WorkingDir *string
75+
EnvVars *string
76+
SecretRefs *string
77+
SecretFileType *string
78+
ContOnWarnings *bool
79+
URL *string
80+
VaultURL *string
7981
}
8082

8183
type job struct {
82-
QName string `json:"qname"`
83-
ContainerImage string `json:"container_image"`
84-
Content string `json:"content"`
85-
EntryPoint []string `json:"entrypoint"`
86-
Run []string `json:"run"`
87-
WorkingDir string `json:"working_directory"`
88-
EnvVars []string `json:"env_vars"`
89-
SecretRefs []string `json:"secret_refs"`
90-
SecretFileType string `json:"secret_file_type"`
91-
ContOnWarnings bool `json:"cont_on_warnings"`
84+
QName string `json:"qname"`
85+
ContainerImage string `json:"container_image"`
86+
ImagePullPolicy string `json:"image_pull_policy"`
87+
Content string `json:"content"`
88+
EntryPoint []string `json:"entrypoint"`
89+
Run []string `json:"run"`
90+
WorkingDir string `json:"working_directory"`
91+
EnvVars []string `json:"env_vars"`
92+
SecretRefs []string `json:"secret_refs"`
93+
SecretFileType string `json:"secret_file_type"`
94+
ContOnWarnings bool `json:"cont_on_warnings"`
9295
}
9396

9497
// func buildJob(c APIRequest) (*[]byte, error) {
@@ -108,6 +111,9 @@ func buildJob(c APIRequest) (*job, error) {
108111
if *c.ContainerImage != "" {
109112
j.ContainerImage = *c.ContainerImage
110113
}
114+
if *c.ImagePullPolicy != "" {
115+
j.ImagePullPolicy = *c.ImagePullPolicy
116+
}
111117
if *c.Content != "" {
112118
j.Content = *c.Content
113119
}
@@ -341,6 +347,14 @@ func RunJob(c *APIRequest, debugLogging bool, pollSecs int, waitFor bool) (*GetR
341347
}
342348
apiToken := sec.Auth.ClientToken
343349

350+
defer func() {
351+
debug("Revoking the minimal authentication token after use")
352+
_, err = vc.Logical().Write("auth/token/revoke-self", nil)
353+
if err != nil {
354+
log.Printf("Error: revoking token after job completed: %s", err)
355+
}
356+
}()
357+
344358
debug("Getting Wrapped Secret_ID for the AppRole")
345359
vc.SetWrappingLookupFunc(func(op, path string) string { return "1h" })
346360
sec, err = vc.Logical().Write(

main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ func validate(c clientapi.APIRequest) error {
6262
return fmt.Errorf("vault-token cannot be used with vault-roleid")
6363
}
6464

65+
if *c.ImagePullPolicy != "IfNotPresent" && *c.ImagePullPolicy != "Always" {
66+
return fmt.Errorf("invalid image-pull-policy, must be 'IfNotPresetn' or 'Always'")
67+
}
68+
6569
return nil
6670
}
6771

@@ -100,6 +104,7 @@ func main() {
100104

101105
c.QName = flag.String("qname", "", "Job Queue to submit to, overrides value in job-json")
102106
c.ContainerImage = flag.String("image", "", "Docker image to run job within, overrides value in job-json")
107+
c.ImagePullPolicy = flag.String("image-pull-policy", "IfNotPresent", "Docker image pull policy: IfNotPresent or Always")
103108
c.Content = flag.String("content", "", "Folder or targz to inject into the container relative to root '/' folder, overrides value in job-json")
104109
c.EntryPoint = flag.String("entrypoint", "", "JSON array of string parts defining the container's entrypoint, e.g.: '[\"ansible\"]', overrides value in job-json")
105110
c.Run = flag.String("run", "", "JSON array of string parts defining the command to run in the container - aka the job, e.g.: '[\"-m\", \"ping\", \"127.0.0.1\"]', overrides value in job-json")

0 commit comments

Comments
 (0)