Skip to content

Commit

Permalink
refactor: FindImages to return errors immediatly
Browse files Browse the repository at this point in the history
Signed-off-by: Philip Laine <[email protected]>
  • Loading branch information
phillebaba committed Aug 14, 2024
1 parent 51fcc03 commit 0826764
Show file tree
Hide file tree
Showing 21 changed files with 770 additions and 131 deletions.
211 changes: 96 additions & 115 deletions src/pkg/packager/prepare.go

Large diffs are not rendered by default.

160 changes: 146 additions & 14 deletions src/pkg/packager/prepare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"

"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"

"github.com/zarf-dev/zarf/src/pkg/lint"
"github.com/zarf-dev/zarf/src/test/testutil"
Expand All @@ -20,23 +21,154 @@ func TestFindImages(t *testing.T) {

lint.ZarfSchema = testutil.LoadSchema(t, "../../../zarf.schema.json")

cfg := &types.PackagerConfig{
CreateOpts: types.ZarfCreateOptions{
BaseDir: "../../../examples/dos-games/",
tests := []struct {
name string
cfg *types.PackagerConfig
expectedErr string
expectedImages map[string][]string
}{
{
name: "agent deployment",
cfg: &types.PackagerConfig{
CreateOpts: types.ZarfCreateOptions{
BaseDir: "./testdata/find-images/agent",
},
},
expectedImages: map[string][]string{
"baseline": {
"ghcr.io/zarf-dev/zarf/agent:v0.38.1",
"ghcr.io/zarf-dev/zarf/agent:sha256-f8b1c2f99349516ae1bd0711a19697abcc41555076b0ae90f1a70ca6b50dcbd8.sig",
},
},
},
{
name: "helm chart",
cfg: &types.PackagerConfig{
CreateOpts: types.ZarfCreateOptions{
BaseDir: "./testdata/find-images/helm-chart",
},
},
expectedImages: map[string][]string{
"baseline": {
"nginx:1.16.0",
"busybox",
},
},
},
{
name: "image not found",
cfg: &types.PackagerConfig{
CreateOpts: types.ZarfCreateOptions{
BaseDir: "./testdata/find-images/agent",
},
FindImagesOpts: types.ZarfFindImagesOptions{
Why: "foobar",
},
},
expectedErr: "image foobar not found in any charts or manifests",
},
{
name: "invalid helm repository",
cfg: &types.PackagerConfig{
CreateOpts: types.ZarfCreateOptions{
BaseDir: "./testdata/find-images/invalid-helm-repo",
},
FindImagesOpts: types.ZarfFindImagesOptions{
RepoHelmChartPath: "test",
},
},
expectedErr: "cannot convert the Git repository https://github.com/zarf-dev/zarf-public-test.git to a Helm chart without a version tag",
},
{
name: "invalid manifest yaml",
cfg: &types.PackagerConfig{
CreateOpts: types.ZarfCreateOptions{
BaseDir: "./testdata/find-images/invalid-manifest-yaml",
},
},
expectedErr: "failed to unmarshal manifest: error converting YAML to JSON: yaml: line 12: could not find expected ':'",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p, err := New(tt.cfg)
require.NoError(t, err)
images, err := p.FindImages(ctx)
if tt.expectedErr != "" {
require.EqualError(t, err, tt.expectedErr)
return
}
require.NoError(t, err)
require.Equal(t, len(tt.expectedImages), len(images))
for k, v := range tt.expectedImages {
require.ElementsMatch(t, v, images[k])
}
})
}
p, err := New(cfg)
require.NoError(t, err)
images, err := p.FindImages(ctx)
require.NoError(t, err)
expectedImages := map[string][]string{
"baseline": {
"ghcr.io/zarf-dev/doom-game:0.0.1",
"ghcr.io/zarf-dev/doom-game:sha256-7464ecc8a7172fce5c2ad631fc2a1b8572c686f4bf15c4bd51d7d6c9f0c460a7.sig",
}

func TestBuildImageMap(t *testing.T) {
t.Parallel()

podSpec := corev1.PodSpec{
InitContainers: []corev1.Container{
{
Image: "init-image",
},
{
Image: "duplicate-image",
},
},
Containers: []corev1.Container{

{
Image: "container-image",
},
{
Image: "alpine:latest",
},
},
EphemeralContainers: []corev1.EphemeralContainer{
{
EphemeralContainerCommon: corev1.EphemeralContainerCommon{
Image: "ephemeral-image",
},
},
{
EphemeralContainerCommon: corev1.EphemeralContainerCommon{
Image: "duplicate-image",
},
},
},
}
imgMap := appendToImageMap(map[string]bool{}, podSpec)
expectedImgMap := map[string]bool{
"init-image": true,
"duplicate-image": true,
"container-image": true,
"alpine:latest": true,
"ephemeral-image": true,
}
require.Equal(t, expectedImgMap, imgMap)
}

func TestGetSortedImages(t *testing.T) {
t.Parallel()

matchedImages := map[string]bool{
"C": true,
"A": true,
"E": true,
"D": true,
}
require.Equal(t, len(expectedImages), len(images))
for k, v := range expectedImages {
require.ElementsMatch(t, v, images[k])
maybeImages := map[string]bool{
"Z": true,
"A": true,
"B": true,
}
sortedMatchedImages, sortedMaybeImages := getSortedImages(matchedImages, maybeImages)
expectedSortedMatchedImages := []string{"A", "C", "D", "E"}
require.Equal(t, expectedSortedMatchedImages, sortedMatchedImages)
expectedSortedMaybeImages := []string{"B", "Z"}
require.Equal(t, expectedSortedMaybeImages, sortedMaybeImages)
}
16 changes: 16 additions & 0 deletions src/pkg/packager/testdata/find-images/agent/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: agent
spec:
selector:
matchLabels:
app: agent
template:
metadata:
labels:
app: agent
spec:
containers:
- name: agent
image: ghcr.io/zarf-dev/zarf/agent:v0.38.1
12 changes: 12 additions & 0 deletions src/pkg/packager/testdata/find-images/agent/zarf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
kind: ZarfPackageConfig
metadata:
name: agent
version: 1.0.0
components:
- name: baseline
required: true
manifests:
- name: agent
namespace: default
files:
- deployment.yaml
23 changes: 23 additions & 0 deletions src/pkg/packager/testdata/find-images/helm-chart/chart/.helmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Patterns to ignore when building packages.
# This supports shell glob matching, relative path matching, and
# negation (prefixed with !). Only one pattern per line.
.DS_Store
# Common VCS dirs
.git/
.gitignore
.bzr/
.bzrignore
.hg/
.hgignore
.svn/
# Common backup files
*.swp
*.bak
*.tmp
*.orig
*~
# Various IDEs
.project
.idea/
*.tmproj
.vscode/
24 changes: 24 additions & 0 deletions src/pkg/packager/testdata/find-images/helm-chart/chart/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
apiVersion: v2
name: chart
description: A Helm chart for Kubernetes

# A chart can be either an 'application' or a 'library' chart.
#
# Application charts are a collection of templates that can be packaged into versioned archives
# to be deployed.
#
# Library charts provide useful utilities or functions for the chart developer. They're included as
# a dependency of application charts to inject those utilities and functions into the rendering
# pipeline. Library charts do not define any templates and therefore cannot be deployed.
type: application

# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 0.1.0

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
# follow Semantic Versioning. They should reflect the version the application is using.
# It is recommended to use it with quotes.
appVersion: "1.16.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
1. Get the application URL by running these commands:
{{- if .Values.ingress.enabled }}
{{- range $host := .Values.ingress.hosts }}
{{- range .paths }}
http{{ if $.Values.ingress.tls }}s{{ end }}://{{ $host.host }}{{ .path }}
{{- end }}
{{- end }}
{{- else if contains "NodePort" .Values.service.type }}
export NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ include "chart.fullname" . }})
export NODE_IP=$(kubectl get nodes --namespace {{ .Release.Namespace }} -o jsonpath="{.items[0].status.addresses[0].address}")
echo http://$NODE_IP:$NODE_PORT
{{- else if contains "LoadBalancer" .Values.service.type }}
NOTE: It may take a few minutes for the LoadBalancer IP to be available.
You can watch its status by running 'kubectl get --namespace {{ .Release.Namespace }} svc -w {{ include "chart.fullname" . }}'
export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ include "chart.fullname" . }} --template "{{"{{ range (index .status.loadBalancer.ingress 0) }}{{.}}{{ end }}"}}")
echo http://$SERVICE_IP:{{ .Values.service.port }}
{{- else if contains "ClusterIP" .Values.service.type }}
export POD_NAME=$(kubectl get pods --namespace {{ .Release.Namespace }} -l "app.kubernetes.io/name={{ include "chart.name" . }},app.kubernetes.io/instance={{ .Release.Name }}" -o jsonpath="{.items[0].metadata.name}")
export CONTAINER_PORT=$(kubectl get pod --namespace {{ .Release.Namespace }} $POD_NAME -o jsonpath="{.spec.containers[0].ports[0].containerPort}")
echo "Visit http://127.0.0.1:8080 to use your application"
kubectl --namespace {{ .Release.Namespace }} port-forward $POD_NAME 8080:$CONTAINER_PORT
{{- end }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{{/*
Expand the name of the chart.
*/}}
{{- define "chart.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If release name contains chart name it will be used as a full name.
*/}}
{{- define "chart.fullname" -}}
{{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- $name := default .Chart.Name .Values.nameOverride }}
{{- if contains $name .Release.Name }}
{{- .Release.Name | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
{{- end }}
{{- end }}
{{- end }}

{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "chart.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Common labels
*/}}
{{- define "chart.labels" -}}
helm.sh/chart: {{ include "chart.chart" . }}
{{ include "chart.selectorLabels" . }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}

{{/*
Selector labels
*/}}
{{- define "chart.selectorLabels" -}}
app.kubernetes.io/name: {{ include "chart.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}

{{/*
Create the name of the service account to use
*/}}
{{- define "chart.serviceAccountName" -}}
{{- if .Values.serviceAccount.create }}
{{- default (include "chart.fullname" .) .Values.serviceAccount.name }}
{{- else }}
{{- default "default" .Values.serviceAccount.name }}
{{- end }}
{{- end }}
Loading

0 comments on commit 0826764

Please sign in to comment.