mirror of
https://github.com/hanzoai/deploy.git
synced 2026-10-07 08:44:50 +00:00
* feat(api): hard-fork CRD API group argoproj.io -> deploy.hanzo.ai
Hanzo CD hard-fork, phase 1.1. Single source of truth is
pkg/apis/application/register.go's Group constant (Application/AppProject/
ApplicationSet all derive their FullName from it); every other Go call-site
that hardcoded the literal group string is updated to match: the 3 generated
informer GVRs, the app-of-apps nested-Application detector in types.go, the
resource-tree Application-kind cache populator, the 3 NotFound error
constructors in server/application, and the applicationset-controller's
leader-election Lease name/ID (kept in lockstep across Go source + RBAC
resourceNames, or leader election would get RBAC-denied on startup).
CRD YAMLs (application/appproject/applicationset) and every RBAC apiGroups
rule that actually governs our 3 CRDs are repointed to deploy.hanzo.ai, across
both manifests/base/* (namespaced Roles) and manifests/cluster-rbac/* (cluster
scope). Two adjacent things that share the "argoproj.io" string but are NOT
ours are deliberately left untouched: gitops-engine's health-check dispatch
for Argo Workflows' `Workflow` kind, and argocd-server-clusterrole's second
apiGroups block granting read on Argo Workflows' `workflows` resource — both
belong to a separate, unrelated Argoproj-family project we are not forking.
Also untouched, per explicit scope: annotation/label keys (argocd.argoproj.io/*)
and finalizers (*.argoproj.io) that track already-applied live resources, and
argocd-cm.yaml's resource.customizations.ignoreResourceUpdates keys (a narrow
app-of-apps diffing nicety, not build- or RBAC-relevant).
CGO_ENABLED=0 go build ./... verified green.
* ci: wire canonical hanzo.yml + cicd.yml (Hanzo CD image build)
Phase 1.3. Follows the same one-way pattern every hanzo/lux/zoo repo uses:
hanzo.yml declares the image (root Dockerfile -> ghcr.io/hanzoai/deploy) and a
fast go-build sanity test (the multi-stage Dockerfile build is the slow, real
gate); cicd.yml is the ~10-line trigger shim importing hanzoai/ci's reusable
build workflow on the arc pool, secrets from KMS only.
No `deploy:` block yet, matching the hanzoai/cms build-only-first-cutover
pattern: this repo's own live hanzo-cd namespace runs the whole Hanzo fleet's
GitOps, so the prod image/CRD swap is a deliberate separate action (see the
[HELD] universe PR), never an automatic rollout on merge. Every PR/push still
builds and pushes an immutable ghcr.io/hanzoai/deploy:sha-<7>-amd64 image.
* feat(ui): rebrand Argo CD -> Hanzo CD (title, favicon, logo, sidebar)
Phase 1.2. User-facing "Argo CD"/"ArgoCD" text replaced with "Hanzo CD"
across the login page, help modal, settings screens, version-info panel,
sync-options warning, fullscreen-logs title, app.tsx page titles, and
index.html's <title>/noscript text (dropped the now-wrong upstream CLI
doc link rather than invent a hanzo.ai URL that doesn't exist).
Logo/favicon assets replaced with the canonical Hanzo mark (never
hand-drawn, copied from ~/work/hanzo/logo/dist): the two real usages of
Argo's logo collapse onto one Hanzo glyph (mono + white variants) at the
same 5 call sites (login icon + footer, login.scss $logoPath/
$logoNegativePath, sidebar text-logo), preserving the existing CSS
invert() filters so both the light login panel and dark sidebar still
render correctly. Old argologo.svg/argo_o.svg/argo.png deleted (no
longer referenced). favicon-{16x16,32x32}.png added at
ui/src/assets/favicon/ (index.html's links to that path were previously
dead - the directory didn't exist in this checkout). package.json name
argo-cd-ui -> hanzo-cd-ui.
Left alone, deliberately: the two Kubernetes resource-kind icon sets
under ui/src/assets/images/resources/ (unrelated to branding), and
upstream doc/project links (argoproj.io, argo-cd.readthedocs.io) since
we have no equivalent hanzo.ai docs to point to yet - except the login
footer logo's outbound link, which now points to hanzo.ai instead of
argoproj.io (a Hanzo-branded mark linking to the upstream project it
was forked from read as an obvious leftover).
* feat(module): rename Go module github.com/argoproj/argo-cd/v3 -> github.com/hanzoai/deploy
Phase 2. No version suffix on the new path (org policy: never publish our
own Go modules above v1.x.x / with a v2+ suffix) — this fork starts a fresh
v0/v1 module identity rather than carrying the v3 the upstream fork point
had reached.
Mechanical, symmetric rewrite: 704 .go files (import statements only; two
disjoint substitutions applied specific-first so the nested gitops-engine
module path never double-matches), plus go.mod (module line, the
gitops-engine require+replace pair, version reset to v0.0.0-... since a
suffix-less path can't carry a v3 semver), gitops-engine/go.mod, Makefile
(ldflags PACKAGE + 2 TEST_MODULE filters), .golangci.yaml, .goreleaser.yaml,
.mockery.yaml (+ gitops-engine's), Procfile, the codegen/proto generation
scripts (hack/generate-proto.sh, update-codegen.sh, update-openapi.sh), and
20 .proto files' go_package options. Also one OpenTelemetry tracer name
string in gitops-engine/pkg/sync/sync_context.go (a cosmestic label, not an
import, but worth keeping consistent).
CGO_ENABLED=0 go build ./... verified green for both the root module and
the nested gitops-engine module; go vet clean on both; go mod tidy run
(network available) to reconcile go.sum, dependency set unchanged.
Left alone, deliberately: doc-only mentions (docs/snyk/*.html scan reports,
gitops-engine/README.md), a comment-only reference in
.github/workflows/release.yaml, and one Renovate bot config line — none are
build- or codegen-load-bearing. Also left alone: comments citing upstream
GitHub issue URLs (github.com/argoproj/argo-cd/issues/NNNNN) and a handful
of test fixtures using "https://github.com/argoproj/argo-cd.git" as a
representative example repo URL — both are accurate historical/test-data
references, not stale self-references.
---------
Co-authored-by: Hanzo Dev <[email protected]>
678 lines
20 KiB
Go
678 lines
20 KiB
Go
package kustomize
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
"k8s.io/apimachinery/pkg/util/intstr"
|
|
|
|
"github.com/hanzoai/deploy/pkg/apis/application/v1alpha1"
|
|
"github.com/hanzoai/deploy/util/exec"
|
|
"github.com/hanzoai/deploy/util/git"
|
|
)
|
|
|
|
const (
|
|
kustomization1 = "kustomization_yaml"
|
|
kustomization3 = "force_common"
|
|
kustomization4 = "custom_version"
|
|
kustomization5 = "kustomization_yaml_patches"
|
|
kustomization6 = "kustomization_yaml_components"
|
|
kustomization7 = "label_without_selector"
|
|
kustomization8 = "kustomization_yaml_patches_empty"
|
|
kustomization9 = "kustomization_yaml_components_monorepo"
|
|
)
|
|
|
|
func testDataDir(tb testing.TB, testData string) (string, error) {
|
|
tb.Helper()
|
|
res := tb.TempDir()
|
|
_, err := exec.RunCommand("cp", exec.CmdOpts{}, "-r", "./testdata/"+testData, filepath.Join(res, "testdata"))
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return path.Join(res, "testdata"), nil
|
|
}
|
|
|
|
func TestKustomizeBuild(t *testing.T) {
|
|
appPath, err := testDataDir(t, kustomization1)
|
|
require.NoError(t, err)
|
|
namePrefix := "namePrefix-"
|
|
nameSuffix := "-nameSuffix"
|
|
namespace := "custom-namespace"
|
|
kustomize := NewKustomizeApp(appPath, appPath, git.NopCreds{}, "", "", "", "")
|
|
env := &v1alpha1.Env{
|
|
&v1alpha1.EnvEntry{Name: "ARGOCD_APP_NAME", Value: "argo-cd-tests"},
|
|
}
|
|
kustomizeSource := v1alpha1.ApplicationSourceKustomize{
|
|
NamePrefix: namePrefix,
|
|
NameSuffix: nameSuffix,
|
|
Images: v1alpha1.KustomizeImages{"nginx:1.15.5"},
|
|
CommonLabels: map[string]string{
|
|
"app.kubernetes.io/managed-by": "argo-cd",
|
|
"app.kubernetes.io/part-of": "${ARGOCD_APP_NAME}",
|
|
},
|
|
CommonAnnotations: map[string]string{
|
|
"app.kubernetes.io/managed-by": "argo-cd",
|
|
"app.kubernetes.io/part-of": "${ARGOCD_APP_NAME}",
|
|
},
|
|
Namespace: namespace,
|
|
CommonAnnotationsEnvsubst: true,
|
|
Replicas: []v1alpha1.KustomizeReplica{
|
|
{
|
|
Name: "nginx-deployment",
|
|
Count: intstr.FromInt32(2),
|
|
},
|
|
{
|
|
Name: "web",
|
|
Count: intstr.FromString("4"),
|
|
},
|
|
},
|
|
}
|
|
objs, images, _, err := kustomize.Build(&kustomizeSource, nil, env, &BuildOpts{
|
|
KubeVersion: "1.27", APIVersions: []string{"foo", "bar"},
|
|
})
|
|
require.NoError(t, err)
|
|
if err != nil {
|
|
assert.Len(t, objs, 2)
|
|
assert.Len(t, images, 2)
|
|
}
|
|
for _, obj := range objs {
|
|
fmt.Println(obj.GetAnnotations())
|
|
switch obj.GetKind() {
|
|
case "StatefulSet":
|
|
assert.Equal(t, namePrefix+"web"+nameSuffix, obj.GetName())
|
|
assert.Equal(t, map[string]string{
|
|
"app.kubernetes.io/managed-by": "argo-cd",
|
|
"app.kubernetes.io/part-of": "argo-cd-tests",
|
|
}, obj.GetLabels())
|
|
assert.Equal(t, map[string]string{
|
|
"app.kubernetes.io/managed-by": "argo-cd",
|
|
"app.kubernetes.io/part-of": "argo-cd-tests",
|
|
}, obj.GetAnnotations())
|
|
replicas, ok, err := unstructured.NestedInt64(obj.Object, "spec", "replicas")
|
|
require.NoError(t, err)
|
|
require.True(t, ok)
|
|
assert.Equal(t, int64(4), replicas)
|
|
assert.Equal(t, namespace, obj.GetNamespace())
|
|
case "Deployment":
|
|
assert.Equal(t, namePrefix+"nginx-deployment"+nameSuffix, obj.GetName())
|
|
assert.Equal(t, map[string]string{
|
|
"app": "nginx",
|
|
"app.kubernetes.io/managed-by": "argo-cd",
|
|
"app.kubernetes.io/part-of": "argo-cd-tests",
|
|
}, obj.GetLabels())
|
|
assert.Equal(t, map[string]string{
|
|
"app.kubernetes.io/managed-by": "argo-cd",
|
|
"app.kubernetes.io/part-of": "argo-cd-tests",
|
|
}, obj.GetAnnotations())
|
|
replicas, ok, err := unstructured.NestedInt64(obj.Object, "spec", "replicas")
|
|
require.NoError(t, err)
|
|
require.True(t, ok)
|
|
assert.Equal(t, int64(2), replicas)
|
|
assert.Equal(t, namespace, obj.GetNamespace())
|
|
}
|
|
}
|
|
|
|
for _, image := range images {
|
|
if image == "nginx" {
|
|
assert.Equal(t, "1.15.5", image)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestFailKustomizeBuild(t *testing.T) {
|
|
appPath, err := testDataDir(t, kustomization1)
|
|
require.NoError(t, err)
|
|
kustomize := NewKustomizeApp(appPath, appPath, git.NopCreds{}, "", "", "", "")
|
|
kustomizeSource := v1alpha1.ApplicationSourceKustomize{
|
|
Replicas: []v1alpha1.KustomizeReplica{
|
|
{
|
|
Name: "nginx-deployment",
|
|
Count: intstr.Parse("garbage"),
|
|
},
|
|
},
|
|
}
|
|
_, _, _, err = kustomize.Build(&kustomizeSource, nil, nil, nil)
|
|
assert.EqualError(t, err, "expected integer value for count. Received: garbage")
|
|
}
|
|
|
|
func TestIsKustomization(t *testing.T) {
|
|
assert.True(t, IsKustomization("kustomization.yaml"))
|
|
assert.True(t, IsKustomization("kustomization.yml"))
|
|
assert.True(t, IsKustomization("Kustomization"))
|
|
assert.False(t, IsKustomization("rubbish.yml"))
|
|
}
|
|
|
|
func TestParseKustomizeBuildOptions(t *testing.T) {
|
|
built := parseKustomizeBuildOptions(t.Context(), &kustomize{path: "guestbook"}, "-v 6 --logtostderr", &BuildOpts{
|
|
KubeVersion: "1.27", APIVersions: []string{"foo", "bar"},
|
|
})
|
|
// Helm is not enabled so helm options are not in the params
|
|
assert.Equal(t, []string{"build", "guestbook", "-v", "6", "--logtostderr"}, built)
|
|
}
|
|
|
|
func TestParseKustomizeBuildHelmOptions(t *testing.T) {
|
|
built := parseKustomizeBuildOptions(t.Context(), &kustomize{path: "guestbook"}, "-v 6 --logtostderr --enable-helm", &BuildOpts{
|
|
KubeVersion: "1.27",
|
|
APIVersions: []string{"foo", "bar"},
|
|
})
|
|
assert.Equal(t, []string{
|
|
"build", "guestbook",
|
|
"-v", "6", "--logtostderr", "--enable-helm",
|
|
"--helm-kube-version", "1.27",
|
|
"--helm-api-versions", "foo", "--helm-api-versions", "bar",
|
|
}, built)
|
|
}
|
|
|
|
func TestVersion(t *testing.T) {
|
|
ver, err := Version()
|
|
require.NoError(t, err)
|
|
assert.NotEmpty(t, ver)
|
|
}
|
|
|
|
func TestVersionWithBinaryPath(t *testing.T) {
|
|
ver, err := versionWithBinaryPath(t.Context(), &kustomize{binaryPath: "kustomize"})
|
|
require.NoError(t, err)
|
|
assert.NotEmpty(t, ver)
|
|
}
|
|
|
|
func TestGetSemver(t *testing.T) {
|
|
ver, err := getSemver(t.Context(), &kustomize{})
|
|
require.NoError(t, err)
|
|
assert.NotEmpty(t, ver)
|
|
}
|
|
|
|
func TestKustomizeBuildForceCommonLabels(t *testing.T) {
|
|
type testCase struct {
|
|
TestData string
|
|
KustomizeSource v1alpha1.ApplicationSourceKustomize
|
|
ExpectedLabels map[string]string
|
|
ExpectErr bool
|
|
Env *v1alpha1.Env
|
|
}
|
|
testCases := []testCase{
|
|
{
|
|
TestData: kustomization3,
|
|
KustomizeSource: v1alpha1.ApplicationSourceKustomize{
|
|
ForceCommonLabels: true,
|
|
CommonLabels: map[string]string{
|
|
"foo": "edited",
|
|
"test": "${ARGOCD_APP_NAME}",
|
|
},
|
|
},
|
|
ExpectedLabels: map[string]string{
|
|
"app": "nginx",
|
|
"foo": "edited",
|
|
"test": "argo-cd-tests",
|
|
},
|
|
Env: &v1alpha1.Env{
|
|
&v1alpha1.EnvEntry{
|
|
Name: "ARGOCD_APP_NAME",
|
|
Value: "argo-cd-tests",
|
|
},
|
|
},
|
|
},
|
|
{
|
|
TestData: kustomization3,
|
|
KustomizeSource: v1alpha1.ApplicationSourceKustomize{
|
|
ForceCommonLabels: false,
|
|
CommonLabels: map[string]string{
|
|
"foo": "edited",
|
|
},
|
|
},
|
|
ExpectErr: true,
|
|
Env: &v1alpha1.Env{
|
|
&v1alpha1.EnvEntry{
|
|
Name: "ARGOCD_APP_NAME",
|
|
Value: "argo-cd-tests",
|
|
},
|
|
},
|
|
},
|
|
}
|
|
for _, tc := range testCases {
|
|
appPath, err := testDataDir(t, tc.TestData)
|
|
require.NoError(t, err)
|
|
kustomize := NewKustomizeApp(appPath, appPath, git.NopCreds{}, "", "", "", "")
|
|
objs, _, _, err := kustomize.Build(&tc.KustomizeSource, nil, tc.Env, nil)
|
|
switch tc.ExpectErr {
|
|
case true:
|
|
require.Error(t, err)
|
|
default:
|
|
require.NoError(t, err)
|
|
if assert.Len(t, objs, 1) {
|
|
assert.Equal(t, tc.ExpectedLabels, objs[0].GetLabels())
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestKustomizeBuildForceCommonAnnotations(t *testing.T) {
|
|
type testCase struct {
|
|
TestData string
|
|
KustomizeSource v1alpha1.ApplicationSourceKustomize
|
|
ExpectedAnnotations map[string]string
|
|
ExpectErr bool
|
|
Env *v1alpha1.Env
|
|
}
|
|
testCases := []testCase{
|
|
{
|
|
TestData: kustomization3,
|
|
KustomizeSource: v1alpha1.ApplicationSourceKustomize{
|
|
ForceCommonAnnotations: true,
|
|
CommonAnnotations: map[string]string{
|
|
"one": "edited",
|
|
"two": "${test}",
|
|
"three": "$ARGOCD_APP_NAME",
|
|
},
|
|
CommonAnnotationsEnvsubst: false,
|
|
},
|
|
ExpectedAnnotations: map[string]string{
|
|
"baz": "quux",
|
|
"one": "edited",
|
|
"two": "${test}",
|
|
"three": "$ARGOCD_APP_NAME",
|
|
},
|
|
Env: &v1alpha1.Env{
|
|
&v1alpha1.EnvEntry{
|
|
Name: "ARGOCD_APP_NAME",
|
|
Value: "argo-cd-tests",
|
|
},
|
|
},
|
|
},
|
|
{
|
|
TestData: kustomization3,
|
|
KustomizeSource: v1alpha1.ApplicationSourceKustomize{
|
|
ForceCommonAnnotations: true,
|
|
CommonAnnotations: map[string]string{
|
|
"one": "edited",
|
|
"two": "${test}",
|
|
"three": "$ARGOCD_APP_NAME",
|
|
},
|
|
CommonAnnotationsEnvsubst: true,
|
|
},
|
|
ExpectedAnnotations: map[string]string{
|
|
"baz": "quux",
|
|
"one": "edited",
|
|
"two": "",
|
|
"three": "argo-cd-tests",
|
|
},
|
|
Env: &v1alpha1.Env{
|
|
&v1alpha1.EnvEntry{
|
|
Name: "ARGOCD_APP_NAME",
|
|
Value: "argo-cd-tests",
|
|
},
|
|
},
|
|
},
|
|
{
|
|
TestData: kustomization3,
|
|
KustomizeSource: v1alpha1.ApplicationSourceKustomize{
|
|
ForceCommonAnnotations: false,
|
|
CommonAnnotations: map[string]string{
|
|
"one": "edited",
|
|
},
|
|
CommonAnnotationsEnvsubst: true,
|
|
},
|
|
ExpectErr: true,
|
|
Env: &v1alpha1.Env{
|
|
&v1alpha1.EnvEntry{
|
|
Name: "ARGOCD_APP_NAME",
|
|
Value: "argo-cd-tests",
|
|
},
|
|
},
|
|
},
|
|
}
|
|
for _, tc := range testCases {
|
|
appPath, err := testDataDir(t, tc.TestData)
|
|
require.NoError(t, err)
|
|
kustomize := NewKustomizeApp(appPath, appPath, git.NopCreds{}, "", "", "", "")
|
|
objs, _, _, err := kustomize.Build(&tc.KustomizeSource, nil, tc.Env, nil)
|
|
switch tc.ExpectErr {
|
|
case true:
|
|
require.Error(t, err)
|
|
default:
|
|
require.NoError(t, err)
|
|
if assert.Len(t, objs, 1) {
|
|
assert.Equal(t, tc.ExpectedAnnotations, objs[0].GetAnnotations())
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestKustomizeLabelWithoutSelector(t *testing.T) {
|
|
type testCase struct {
|
|
TestData string
|
|
KustomizeSource v1alpha1.ApplicationSourceKustomize
|
|
ExpectedMetadataLabels map[string]string
|
|
ExpectedSelectorLabels map[string]string
|
|
ExpectedTemplateLabels map[string]string
|
|
ExpectErr bool
|
|
Env *v1alpha1.Env
|
|
}
|
|
testCases := []testCase{
|
|
{
|
|
TestData: kustomization7,
|
|
KustomizeSource: v1alpha1.ApplicationSourceKustomize{
|
|
CommonLabels: map[string]string{
|
|
"foo": "bar",
|
|
},
|
|
LabelWithoutSelector: true,
|
|
},
|
|
ExpectedMetadataLabels: map[string]string{"app": "nginx", "managed-by": "helm", "foo": "bar"},
|
|
ExpectedSelectorLabels: map[string]string{"app": "nginx"},
|
|
ExpectedTemplateLabels: map[string]string{"app": "nginx"},
|
|
Env: &v1alpha1.Env{
|
|
&v1alpha1.EnvEntry{
|
|
Name: "ARGOCD_APP_NAME",
|
|
Value: "argo-cd-tests",
|
|
},
|
|
},
|
|
},
|
|
{
|
|
TestData: kustomization7,
|
|
KustomizeSource: v1alpha1.ApplicationSourceKustomize{
|
|
CommonLabels: map[string]string{
|
|
"managed-by": "argocd",
|
|
},
|
|
LabelWithoutSelector: true,
|
|
ForceCommonLabels: true,
|
|
},
|
|
ExpectedMetadataLabels: map[string]string{"app": "nginx", "managed-by": "argocd"},
|
|
ExpectedSelectorLabels: map[string]string{"app": "nginx"},
|
|
ExpectedTemplateLabels: map[string]string{"app": "nginx"},
|
|
Env: &v1alpha1.Env{
|
|
&v1alpha1.EnvEntry{
|
|
Name: "ARGOCD_APP_NAME",
|
|
Value: "argo-cd-tests",
|
|
},
|
|
},
|
|
},
|
|
{
|
|
TestData: kustomization7,
|
|
KustomizeSource: v1alpha1.ApplicationSourceKustomize{
|
|
CommonLabels: map[string]string{
|
|
"foo": "bar",
|
|
},
|
|
LabelWithoutSelector: true,
|
|
LabelIncludeTemplates: true,
|
|
},
|
|
ExpectedMetadataLabels: map[string]string{"app": "nginx", "managed-by": "helm", "foo": "bar"},
|
|
ExpectedSelectorLabels: map[string]string{"app": "nginx"},
|
|
ExpectedTemplateLabels: map[string]string{"app": "nginx", "foo": "bar"},
|
|
Env: &v1alpha1.Env{
|
|
&v1alpha1.EnvEntry{
|
|
Name: "ARGOCD_APP_NAME",
|
|
Value: "argo-cd-tests",
|
|
},
|
|
},
|
|
},
|
|
{
|
|
TestData: kustomization7,
|
|
KustomizeSource: v1alpha1.ApplicationSourceKustomize{
|
|
CommonLabels: map[string]string{
|
|
"managed-by": "argocd",
|
|
},
|
|
LabelWithoutSelector: true,
|
|
LabelIncludeTemplates: true,
|
|
ForceCommonLabels: true,
|
|
},
|
|
ExpectedMetadataLabels: map[string]string{"app": "nginx", "managed-by": "argocd"},
|
|
ExpectedSelectorLabels: map[string]string{"app": "nginx"},
|
|
ExpectedTemplateLabels: map[string]string{"app": "nginx", "managed-by": "argocd"},
|
|
Env: &v1alpha1.Env{
|
|
&v1alpha1.EnvEntry{
|
|
Name: "ARGOCD_APP_NAME",
|
|
Value: "argo-cd-tests",
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
appPath, err := testDataDir(t, tc.TestData)
|
|
require.NoError(t, err)
|
|
kustomize := NewKustomizeApp(appPath, appPath, git.NopCreds{}, "", "", "", "")
|
|
objs, _, _, err := kustomize.Build(&tc.KustomizeSource, nil, tc.Env, nil)
|
|
|
|
switch tc.ExpectErr {
|
|
case true:
|
|
require.Error(t, err)
|
|
default:
|
|
require.NoError(t, err)
|
|
if assert.Len(t, objs, 1) {
|
|
obj := objs[0]
|
|
sl, found, err := unstructured.NestedStringMap(obj.Object, "spec", "selector", "matchLabels")
|
|
require.NoError(t, err)
|
|
assert.True(t, found)
|
|
tl, found, err := unstructured.NestedStringMap(obj.Object, "spec", "template", "metadata", "labels")
|
|
require.NoError(t, err)
|
|
assert.True(t, found)
|
|
assert.Equal(t, tc.ExpectedMetadataLabels, obj.GetLabels())
|
|
assert.Equal(t, tc.ExpectedSelectorLabels, sl)
|
|
assert.Equal(t, tc.ExpectedTemplateLabels, tl)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestKustomizeCustomVersion(t *testing.T) {
|
|
appPath, err := testDataDir(t, kustomization1)
|
|
require.NoError(t, err)
|
|
kustomizePath, err := testDataDir(t, kustomization4)
|
|
require.NoError(t, err)
|
|
envOutputFile := kustomizePath + "/env_output"
|
|
kustomize := NewKustomizeApp(appPath, appPath, git.NopCreds{}, "", kustomizePath+"/kustomize.special", "", "")
|
|
kustomizeSource := v1alpha1.ApplicationSourceKustomize{
|
|
Version: "special",
|
|
}
|
|
env := &v1alpha1.Env{
|
|
&v1alpha1.EnvEntry{Name: "ARGOCD_APP_NAME", Value: "argo-cd-tests"},
|
|
}
|
|
objs, images, _, err := kustomize.Build(&kustomizeSource, nil, env, nil)
|
|
require.NoError(t, err)
|
|
if err != nil {
|
|
assert.Len(t, objs, 2)
|
|
assert.Len(t, images, 2)
|
|
}
|
|
|
|
content, err := os.ReadFile(envOutputFile)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "ARGOCD_APP_NAME=argo-cd-tests\n", string(content))
|
|
}
|
|
|
|
func TestKustomizeBuildComponents(t *testing.T) {
|
|
appPath, err := testDataDir(t, kustomization6)
|
|
require.NoError(t, err)
|
|
kustomize := NewKustomizeApp(appPath, appPath, git.NopCreds{}, "", "", "", "")
|
|
|
|
kustomizeSource := v1alpha1.ApplicationSourceKustomize{
|
|
Components: []string{"./components", "./missing-components"},
|
|
IgnoreMissingComponents: false,
|
|
}
|
|
_, _, _, err = kustomize.Build(&kustomizeSource, nil, nil, nil)
|
|
require.Error(t, err)
|
|
|
|
kustomizeSource = v1alpha1.ApplicationSourceKustomize{
|
|
Components: []string{"./components", "./missing-components"},
|
|
IgnoreMissingComponents: true,
|
|
}
|
|
objs, _, _, err := kustomize.Build(&kustomizeSource, nil, nil, nil)
|
|
require.NoError(t, err)
|
|
obj := objs[0]
|
|
assert.Equal(t, "nginx-deployment", obj.GetName())
|
|
assert.Equal(t, map[string]string{
|
|
"app": "nginx",
|
|
}, obj.GetLabels())
|
|
replicas, ok, err := unstructured.NestedInt64(obj.Object, "spec", "replicas")
|
|
require.NoError(t, err)
|
|
require.True(t, ok)
|
|
assert.Equal(t, int64(3), replicas)
|
|
}
|
|
|
|
func TestKustomizeBuildComponentsMonoRepo(t *testing.T) {
|
|
rootPath, err := testDataDir(t, kustomization9)
|
|
require.NoError(t, err)
|
|
appPath := path.Join(rootPath, "envs/inseng-pdx-egert-sandbox/namespaces/inst-system/apps/hello-world")
|
|
kustomize := NewKustomizeApp(rootPath, appPath, git.NopCreds{}, "", "", "", "")
|
|
kustomizeSource := v1alpha1.ApplicationSourceKustomize{
|
|
Components: []string{"../../../../../../kustomize/components/all"},
|
|
IgnoreMissingComponents: true,
|
|
}
|
|
objs, _, _, err := kustomize.Build(&kustomizeSource, nil, nil, nil)
|
|
require.NoError(t, err)
|
|
obj := objs[2]
|
|
require.Equal(t, "hello-world-kustomize", obj.GetName())
|
|
require.Equal(t, map[string]string{
|
|
"app.kubernetes.io/name": "hello-world-kustomize",
|
|
"app.kubernetes.io/owner": "fire-team",
|
|
}, obj.GetLabels())
|
|
replicas, ok, err := unstructured.NestedSlice(obj.Object, "spec", "template", "spec", "tolerations")
|
|
require.NoError(t, err)
|
|
require.True(t, ok)
|
|
require.Len(t, replicas, 1)
|
|
require.Equal(t, "my-special-toleration", replicas[0].(map[string]any)["key"])
|
|
require.Equal(t, "Exists", replicas[0].(map[string]any)["operator"])
|
|
}
|
|
|
|
func TestKustomizeBuildPatches(t *testing.T) {
|
|
appPath, err := testDataDir(t, kustomization5)
|
|
require.NoError(t, err)
|
|
kustomize := NewKustomizeApp(appPath, appPath, git.NopCreds{}, "", "", "", "")
|
|
|
|
kustomizeSource := v1alpha1.ApplicationSourceKustomize{
|
|
Patches: []v1alpha1.KustomizePatch{
|
|
{
|
|
Patch: `[ { "op": "replace", "path": "/spec/template/spec/containers/0/ports/0/containerPort", "value": 443 }, { "op": "replace", "path": "/spec/template/spec/containers/0/name", "value": "test" }]`,
|
|
Target: &v1alpha1.KustomizeSelector{
|
|
KustomizeResId: v1alpha1.KustomizeResId{
|
|
KustomizeGvk: v1alpha1.KustomizeGvk{
|
|
Kind: "Deployment",
|
|
},
|
|
Name: "nginx-deployment",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
objs, _, _, err := kustomize.Build(&kustomizeSource, nil, nil, nil)
|
|
require.NoError(t, err)
|
|
obj := objs[0]
|
|
containers, found, err := unstructured.NestedSlice(obj.Object, "spec", "template", "spec", "containers")
|
|
require.NoError(t, err)
|
|
assert.True(t, found)
|
|
|
|
ports, found, err := unstructured.NestedSlice(
|
|
containers[0].(map[string]any),
|
|
"ports",
|
|
)
|
|
assert.True(t, found)
|
|
require.NoError(t, err)
|
|
|
|
port, found, err := unstructured.NestedInt64(
|
|
ports[0].(map[string]any),
|
|
"containerPort",
|
|
)
|
|
|
|
assert.True(t, found)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, int64(443), port)
|
|
|
|
name, found, err := unstructured.NestedString(
|
|
containers[0].(map[string]any),
|
|
"name",
|
|
)
|
|
assert.True(t, found)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "test", name)
|
|
}
|
|
|
|
func TestFailKustomizeBuildPatches(t *testing.T) {
|
|
appPath, err := testDataDir(t, kustomization8)
|
|
require.NoError(t, err)
|
|
kustomize := NewKustomizeApp(appPath, appPath, git.NopCreds{}, "", "", "", "")
|
|
|
|
kustomizeSource := v1alpha1.ApplicationSourceKustomize{
|
|
Patches: []v1alpha1.KustomizePatch{
|
|
{
|
|
Patch: `[ { "op": "replace", "path": "/spec/template/spec/containers/0/ports/0/containerPort", "value": 443 }, { "op": "replace", "path": "/spec/template/spec/containers/0/name", "value": "test" }]`,
|
|
Target: &v1alpha1.KustomizeSelector{
|
|
KustomizeResId: v1alpha1.KustomizeResId{
|
|
KustomizeGvk: v1alpha1.KustomizeGvk{
|
|
Kind: "Deployment",
|
|
},
|
|
Name: "nginx-deployment",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
_, _, _, err = kustomize.Build(&kustomizeSource, nil, nil, nil)
|
|
require.EqualError(t, err, "kustomization file not found in the path")
|
|
}
|
|
|
|
func TestKustomizeBuildComponentsNoFoundComponents(t *testing.T) {
|
|
appPath, err := testDataDir(t, kustomization6)
|
|
require.NoError(t, err)
|
|
kustomize := NewKustomizeApp(appPath, appPath, git.NopCreds{}, "", "", "", "")
|
|
|
|
// Test with non-existent components and IgnoreMissingComponents = true
|
|
// This should result in foundComponents being empty, so no "edit add component" command should be executed
|
|
kustomizeSource := v1alpha1.ApplicationSourceKustomize{
|
|
Components: []string{"./non-existent-component1", "./non-existent-component2"},
|
|
IgnoreMissingComponents: true,
|
|
}
|
|
_, _, commands, err := kustomize.Build(&kustomizeSource, nil, nil, nil)
|
|
require.NoError(t, err)
|
|
|
|
// Verify that no "edit add component" command was executed
|
|
for _, cmd := range commands {
|
|
assert.NotContains(t, cmd, "edit add component", "kustomize edit add component should not be invoked when foundComponents is empty")
|
|
}
|
|
}
|
|
|
|
func Test_getImageParameters_sorted(t *testing.T) {
|
|
apps := []*unstructured.Unstructured{
|
|
{
|
|
Object: map[string]any{
|
|
"kind": "Deployment",
|
|
"spec": map[string]any{
|
|
"template": map[string]any{
|
|
"spec": map[string]any{
|
|
"containers": []any{
|
|
map[string]any{
|
|
"name": "nginx",
|
|
"image": "nginx:1.15.6",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
Object: map[string]any{
|
|
"kind": "Deployment",
|
|
"spec": map[string]any{
|
|
"template": map[string]any{
|
|
"spec": map[string]any{
|
|
"containers": []any{
|
|
map[string]any{
|
|
"name": "nginx",
|
|
"image": "nginx:1.15.5",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
params := getImageParameters(apps)
|
|
assert.Equal(t, []string{"nginx:1.15.5", "nginx:1.15.6"}, params)
|
|
}
|