Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement container image setters #194

Merged
merged 4 commits into from
Dec 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ jobs:
run: |
kubectl -n impersonation apply -f ./config/testdata/impersonation
kubectl -n impersonation wait kustomizations/podinfo --for=condition=ready --timeout=4m
- name: Run image overide tests
run: |
kubectl -n override-test apply -f ./config/testdata/overrides
kubectl -n override-test wait kustomizations/podinfo --for=condition=ready --timeout=1m
ACTUAL_TAG=$(kubectl -n override-test get deployments podinfo -o jsonpath='{.spec.template.spec.containers[0].image}' | cut -f2 -d ":")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before this check, we need to wait for the kustomization to reconcile first with:

kubectl -n override-test wait kustomizations/podinfo --for=condition=ready --timeout=1m

if [[ $ACTUAL_TAG != "5.0.0" ]]; then echo "Image tag did not override" && exit 1; fi
- name: Logs
run: |
kubectl -n kustomize-system logs deploy/source-controller
Expand Down
19 changes: 19 additions & 0 deletions api/v1beta1/kustomization_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ type KustomizationSpec struct {
// +optional
HealthChecks []CrossNamespaceObjectReference `json:"healthChecks,omitempty"`

// A list of images that is used for changing the image name/tag in the kustomization yaml.
// +optional
Images []Image `json:"images,omitempty"`

// The name of the Kubernetes service account to impersonate
// when reconciling this Kustomization.
// +optional
Expand Down Expand Up @@ -113,6 +117,21 @@ type Decryption struct {
SecretRef *corev1.LocalObjectReference `json:"secretRef,omitempty"`
}

// Image contains the name, new name and new tag that will replace the original image.
type Image struct {
// Name of the image to be replaced.
// +required
Name string `json:"name"`

// NewName is the name of the image used to replace the original one.
// +required
NewName string `json:"newName"`

// NewTag is the tag used to replace the original tag.
// +required
NewTag string `json:"newTag"`
}

// KubeConfig references a Kubernetes secret that contains a kubeconfig file.
type KubeConfig struct {
// SecretRef holds the name to a secret that contains a 'value' key with
Expand Down
20 changes: 20 additions & 0 deletions api/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions config/crd/bases/kustomize.toolkit.fluxcd.io_kustomizations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,30 @@ spec:
- name
type: object
type: array
images:
description: A list of images that is used for changing the image
name/tag in the kustomization yaml.
items:
description: Image contains the name, new name and new tag that
will replace the original image.
properties:
name:
description: Name of the image to be replaced.
type: string
newName:
description: NewName is the name of the image used to replace
the original one.
type: string
newTag:
description: NewTag is the tag used to replace the original
tag.
type: string
required:
- name
- newName
- newTag
type: object
type: array
interval:
description: The interval at which to reconcile the kustomization.
type: string
Expand Down
34 changes: 34 additions & 0 deletions config/testdata/overrides/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
apiVersion: v1
kind: Namespace
metadata:
name: override-test
---
apiVersion: source.toolkit.fluxcd.io/v1beta1
kind: GitRepository
metadata:
name: podinfo
namespace: override-test
spec:
interval: 5m
url: https://github.com/stefanprodan/podinfo
ref:
branch: master
---
apiVersion: kustomize.toolkit.fluxcd.io/v1beta1
kind: Kustomization
metadata:
name: podinfo
namespace: override-test
spec:
targetNamespace: override-test
interval: 5m
path: "./kustomize"
prune: true
sourceRef:
kind: GitRepository
name: podinfo
validation: client
images:
- name: ghcr.io/stefanprodan/podinfo
newName: ghcr.io/stefanprodan/podinfo
newTag: 5.0.0
23 changes: 23 additions & 0 deletions controllers/kustomization_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,19 @@ func (kg *KustomizeGenerator) WriteFile(dirPath string) (string, error) {
kus.Namespace = kg.kustomization.Spec.TargetNamespace
}

for _, image := range kg.kustomization.Spec.Images {
newImage := kustypes.Image{
Name: image.Name,
NewName: image.NewName,
NewTag: image.NewTag,
}
if exists, index := checkKustomizeImageExists(kus.Images, image.Name); exists {
kus.Images[index] = newImage
} else {
kus.Images = append(kus.Images, newImage)
}
}

kd, err := yaml.Marshal(kus)
if err != nil {
return "", err
Expand All @@ -104,6 +117,16 @@ func (kg *KustomizeGenerator) WriteFile(dirPath string) (string, error) {
return checksum, ioutil.WriteFile(kfile, kd, os.ModePerm)
}

func checkKustomizeImageExists(images []kustypes.Image, imageName string) (bool, int) {
for i, image := range images {
if imageName == image.Name {
return true, i
}
}

return false, -1
}

func (kg *KustomizeGenerator) generateKustomization(dirPath string) error {
fs := filesys.MakeFsOnDisk()
kfile := filepath.Join(dirPath, kustomizationFileName)
Expand Down
82 changes: 82 additions & 0 deletions docs/api/kustomize.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,20 @@ bool
</tr>
<tr>
<td>
<code>images</code><br>
<em>
<a href="#kustomize.toolkit.fluxcd.io/v1beta1.Image">
[]Image
</a>
</em>
</td>
<td>
<em>(Optional)</em>
<p>A list of images that is used for changing the image name/tag in the kustomization yaml.</p>
</td>
</tr>
<tr>
<td>
<code>serviceAccountName</code><br>
<em>
string
Expand Down Expand Up @@ -443,6 +457,60 @@ Kubernetes core/v1.LocalObjectReference
</table>
</div>
</div>
<h3 id="kustomize.toolkit.fluxcd.io/v1beta1.Image">Image
</h3>
<p>
(<em>Appears on:</em>
<a href="#kustomize.toolkit.fluxcd.io/v1beta1.KustomizationSpec">KustomizationSpec</a>)
</p>
<p>Image contains the name, new name and new tag that will replace the original image.</p>
<div class="md-typeset__scrollwrap">
<div class="md-typeset__table">
<table>
<thead>
<tr>
<th>Field</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<code>name</code><br>
<em>
string
</em>
</td>
<td>
<p>Name of the image to be replaced.</p>
</td>
</tr>
<tr>
<td>
<code>newName</code><br>
<em>
string
</em>
</td>
<td>
<p>NewName is the name of the image used to replace the original one.</p>
</td>
</tr>
<tr>
<td>
<code>newTag</code><br>
<em>
string
</em>
</td>
<td>
<p>NewTag is the tag used to replace the original tag.</p>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<h3 id="kustomize.toolkit.fluxcd.io/v1beta1.KubeConfig">KubeConfig
</h3>
<p>
Expand Down Expand Up @@ -597,6 +665,20 @@ bool
</tr>
<tr>
<td>
<code>images</code><br>
<em>
<a href="#kustomize.toolkit.fluxcd.io/v1beta1.Image">
[]Image
</a>
</em>
</td>
<td>
<em>(Optional)</em>
<p>A list of images that is used for changing the image name/tag in the kustomization yaml.</p>
</td>
</tr>
<tr>
<td>
<code>serviceAccountName</code><br>
<em>
string
Expand Down
22 changes: 22 additions & 0 deletions docs/spec/v1beta1/kustomization.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ type KustomizationSpec struct {
// A list of resources to be included in the health assessment.
// +optional
HealthChecks []CrossNamespaceObjectReference `json:"healthChecks,omitempty"`

// A list of images that is used for changing the image name/tag in the kustomization yaml.
// +optional
Images []Image `json:"images,omitempty"`

// The name of the Kubernetes service account to impersonate
// when reconciling this Kustomization.
Expand Down Expand Up @@ -108,6 +112,24 @@ type KubeConfig struct {
}
```

The image type contains the name, new name and new tag that will replace the original image.

```go
type Image struct {
// Name of the image to be replaced.
// +required
Name string `json:"name"`

// NewName is the name of the image used to replace the original one.
// +required
NewName string `json:"newName"`

// NewTag is the tag used to replace the original tag.
// +required
NewTag string `json:"newTag"`
}
```

The status sub-resource records the result of the last reconciliation:

```go
Expand Down