Skip to content

Commit a84d74c

Browse files
authored
Merge pull request #17 from phoracek/configurable_pull_policy
configure ImagePullPolicy via operator CR
2 parents 69ecfeb + 87ea033 commit a84d74c

File tree

10 files changed

+79
-6
lines changed

10 files changed

+79
-6
lines changed

Gopkg.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,20 @@ spec:
5757
Additionally, container image used to deliver this plugin can be set using
5858
`LINUX_BRIDGE_IMAGE` environment variable in operator deployment manifest.
5959

60+
## Image Pull Policy
61+
62+
Administrator can specify [image pull policy](https://kubernetes.io/docs/concepts/containers/images/)
63+
for deployed components. Default is `IfNotPresent`.
64+
65+
```yaml
66+
apiVersion: networkaddonsoperator.network.kubevirt.io/v1alpha1
67+
kind: NetworkAddonsConfig
68+
metadata:
69+
name: cluster
70+
spec:
71+
imagePullPolicy: Always
72+
```
73+
6074
# Deployment
6175

6276
First install the operator itself:

config-example.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ metadata:
55
spec:
66
multus: {}
77
linuxBridge: {}
8+
imagePullPolicy: Always

data/multus/002-multus.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ spec:
4747
command: ["/entrypoint.sh"]
4848
args: ["--multus-conf-file=auto"]
4949
image: {{ .MultusImage }}
50+
imagePullPolicy: {{ .ImagePullPolicy }}
5051
resources:
5152
requests:
5253
cpu: "100m"

deploy/cluster-network-addons-operator_03_deployment.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ spec:
2525
cpu: 10m
2626
memory: 50Mi
2727
env:
28-
- name: IMAGE_PULL_POLICY
29-
value: "Always"
3028
- name: MULTUS_IMAGE
3129
value: "docker.io/nfvpe/multus:latest"
3230
- name: LINUX_BRIDGE_IMAGE

pkg/apis/networkaddonsoperator/v1alpha1/networkaddonsconfig_types.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import (
77
// NetworkAddonsConfigSpec defines the desired state of NetworkAddonsConfig
88
// +k8s:openapi-gen=true
99
type NetworkAddonsConfigSpec struct {
10-
Multus *Multus `json:"multus,omitempty"`
11-
LinuxBridge *LinuxBridge `json:"linuxBridge,omitempty"`
10+
Multus *Multus `json:"multus,omitempty"`
11+
LinuxBridge *LinuxBridge `json:"linuxBridge,omitempty"`
12+
ImagePullPolicy string `json:"imagePullPolicy,omitempty"`
1213
}
1314

1415
// +k8s:openapi-gen=true

pkg/network/image-pull-policy.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package network
2+
3+
import (
4+
"github.com/pkg/errors"
5+
v1 "k8s.io/api/core/v1"
6+
7+
opv1alpha1 "github.com/kubevirt/cluster-network-addons-operator/pkg/apis/networkaddonsoperator/v1alpha1"
8+
)
9+
10+
const defaultImagePullPolicy = string(v1.PullIfNotPresent)
11+
12+
func validateImagePullPolicy(conf *opv1alpha1.NetworkAddonsConfigSpec) []error {
13+
if conf.ImagePullPolicy == "" {
14+
return []error{}
15+
}
16+
17+
if valid := verifyPullPolicyType(conf.ImagePullPolicy); !valid {
18+
return []error{errors.Errorf("requested imagePullPolicy '%s' is not valid", conf.ImagePullPolicy)}
19+
}
20+
21+
return []error{}
22+
}
23+
24+
func fillDefaultsImagePullPolicy(conf, previous *opv1alpha1.NetworkAddonsConfigSpec) {
25+
if conf.ImagePullPolicy == "" {
26+
if previous != nil && previous.ImagePullPolicy != "" {
27+
conf.ImagePullPolicy = previous.ImagePullPolicy
28+
} else {
29+
conf.ImagePullPolicy = defaultImagePullPolicy
30+
}
31+
}
32+
}
33+
34+
func changeSafeImagePullPolicy(prev, next *opv1alpha1.NetworkAddonsConfigSpec) []error {
35+
if prev.ImagePullPolicy != "" && prev.ImagePullPolicy != next.ImagePullPolicy {
36+
return []error{errors.Errorf("cannot modify ImagePullPolicy configuration once components were deployed")}
37+
}
38+
return nil
39+
}
40+
41+
// Verify if the value is a valid PullPolicy
42+
func verifyPullPolicyType(policy string) bool {
43+
imagePullPolicy := v1.PullPolicy(policy)
44+
switch imagePullPolicy {
45+
case v1.PullAlways:
46+
return true
47+
case v1.PullNever:
48+
return true
49+
case v1.PullIfNotPresent:
50+
return true
51+
default:
52+
return false
53+
}
54+
}

pkg/network/linux-bridge.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func renderLinuxBridge(conf *opv1alpha1.NetworkAddonsConfigSpec, manifestDir str
2828
// render the manifests on disk
2929
data := render.MakeRenderData()
3030
data.Data["LinuxBridgeImage"] = os.Getenv("LINUX_BRIDGE_IMAGE")
31-
data.Data["ImagePullPolicy"] = os.Getenv("IMAGE_PULL_POLICY")
31+
data.Data["ImagePullPolicy"] = conf.ImagePullPolicy
3232
data.Data["EnableSCC"] = enableSCC
3333

3434
objs, err := render.RenderDir(filepath.Join(manifestDir, "linux-bridge"), &data)

pkg/network/multus.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ func renderMultus(conf *opv1alpha1.NetworkAddonsConfigSpec, manifestDir string,
4444
// render manifests from disk
4545
data := render.MakeRenderData()
4646
data.Data["MultusImage"] = os.Getenv("MULTUS_IMAGE")
47+
data.Data["ImagePullPolicy"] = conf.ImagePullPolicy
4748
data.Data["EnableSCC"] = enableSCC
4849

4950
objs, err := render.RenderDir(filepath.Join(manifestDir, "multus"), &data)

pkg/network/network.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ func Validate(conf *opv1alpha1.NetworkAddonsConfigSpec, openshiftNetworkConfig *
2222
errs := []error{}
2323

2424
errs = append(errs, validateMultus(conf, openshiftNetworkConfig)...)
25+
errs = append(errs, validateImagePullPolicy(conf)...)
2526

2627
if len(errs) > 0 {
2728
return errors.Errorf("invalid configuration: %v", errs)
@@ -35,7 +36,7 @@ func Validate(conf *opv1alpha1.NetworkAddonsConfigSpec, openshiftNetworkConfig *
3536
// Defaults are carried forward from previous if it is provided. This is so we
3637
// can change defaults as we move forward, but won't disrupt existing clusters.
3738
func FillDefaults(conf, previous *opv1alpha1.NetworkAddonsConfigSpec) {
38-
// TODO
39+
fillDefaultsImagePullPolicy(conf, previous)
3940
}
4041

4142
// IsChangeSafe checks to see if the change between prev and next are allowed
@@ -54,6 +55,7 @@ func IsChangeSafe(prev, next *opv1alpha1.NetworkAddonsConfigSpec) error {
5455

5556
errs = append(errs, changeSafeMultus(prev, next)...)
5657
errs = append(errs, changeSafeLinuxBridge(prev, next)...)
58+
errs = append(errs, changeSafeImagePullPolicy(prev, next)...)
5759

5860
if len(errs) > 0 {
5961
return errors.Errorf("invalid configuration: %v", errs)

0 commit comments

Comments
 (0)