Files
zeekayandHanzo Dev 1ab6e799c1 cd: the module is github.com/hanzoai/cd, where it is actually hosted
go.mod still declared `module github.com/hanzoai/deploy` after the repo was
renamed. A Go module path is not cosmetic — it is the resolvable location. So
`go get github.com/hanzoai/cd` could not work, and anything importing this
module had to spell a name that no longer exists at that address. The build log
of v3.6.0 is where it surfaced: the compiler was still emitting
github.com/hanzoai/deploy/cmd/cd/commands.

3541 import paths across 690 .go, 19 .proto, 4 .yaml, 3 .sh, 2 .mod, Procfile
and Makefile. Plus the 62 ghcr.io/hanzoai/deploy image references, moved to
ghcr.io/hanzoai/cd to match hanzo.yml's build target — a distinct string from
the module path, replaced separately so neither could clobber the other.

go mod tidy clean, CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build ./... clean.

Co-authored-by: Hanzo Dev <[email protected]>
2026-07-28 21:16:24 -07:00

126 lines
3.7 KiB
Go

package cache
import (
"fmt"
"testing"
"github.com/hanzoai/cd/gitops-engine/pkg/utils/kube"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/client-go/rest"
)
func TestResourceOfGroupKind(t *testing.T) {
t.Parallel()
deploy := &appsv1.Deployment{
TypeMeta: metav1.TypeMeta{
APIVersion: "apps/v1",
Kind: "Deployment",
},
ObjectMeta: metav1.ObjectMeta{
Name: "deploy",
},
}
service := &corev1.Service{
TypeMeta: metav1.TypeMeta{
APIVersion: "",
Kind: "Service",
},
ObjectMeta: metav1.ObjectMeta{
Name: "service",
},
}
cluster := newCluster(t, deploy, service)
err := cluster.EnsureSynced()
require.NoError(t, err)
resources := cluster.FindResources("", ResourceOfGroupKind("apps", "Deployment"))
assert.Len(t, resources, 1)
assert.NotNil(t, resources[kube.NewResourceKey("apps", "Deployment", "", "deploy")])
}
func TestGetNamespaceResources(t *testing.T) {
t.Parallel()
defaultNamespaceTopLevel1 := &appsv1.Deployment{
TypeMeta: metav1.TypeMeta{
APIVersion: "apps/v1",
Kind: "Deployment",
},
ObjectMeta: metav1.ObjectMeta{
Name: "helm-guestbook1",
Namespace: "default",
},
}
defaultNamespaceTopLevel2 := &appsv1.Deployment{
TypeMeta: metav1.TypeMeta{
APIVersion: "apps/v1",
Kind: "Deployment",
},
ObjectMeta: metav1.ObjectMeta{
Name: "helm-guestbook2",
Namespace: "default",
},
}
kubesystemNamespaceTopLevel2 := &appsv1.Deployment{
TypeMeta: metav1.TypeMeta{
APIVersion: "apps/v1",
Kind: "Deployment",
},
ObjectMeta: metav1.ObjectMeta{
Name: "helm-guestbook3",
Namespace: "kube-system",
},
}
cluster := newCluster(t, defaultNamespaceTopLevel1, defaultNamespaceTopLevel2, kubesystemNamespaceTopLevel2)
err := cluster.EnsureSynced()
require.NoError(t, err)
resources := cluster.FindResources("default", TopLevelResource)
assert.Len(t, resources, 2)
assert.Equal(t, "helm-guestbook1", resources[getResourceKey(t, defaultNamespaceTopLevel1)].Ref.Name)
assert.Equal(t, "helm-guestbook2", resources[getResourceKey(t, defaultNamespaceTopLevel2)].Ref.Name)
resources = cluster.FindResources("kube-system", TopLevelResource)
assert.Len(t, resources, 1)
assert.Equal(t, "helm-guestbook3", resources[getResourceKey(t, kubesystemNamespaceTopLevel2)].Ref.Name)
}
func ExampleNewClusterCache_inspectNamespaceResources() {
// kubernetes cluster config here
config := &rest.Config{}
clusterCache := NewClusterCache(config,
// cache default namespace only
SetNamespaces([]string{"default", "kube-system"}),
// configure custom logic to cache resources manifest and additional metadata
SetPopulateResourceInfoHandler(func(un *unstructured.Unstructured, _ bool) (info any, cacheManifest bool) {
// if resource belongs to 'extensions' group then mark if with 'deprecated' label
if un.GroupVersionKind().Group == "extensions" {
info = []string{"deprecated"}
}
_, ok := un.GetLabels()["acme.io/my-label"]
// cache whole manifest if resource has label
cacheManifest = ok
return
}),
)
// Ensure cluster is synced before using it
if err := clusterCache.EnsureSynced(); err != nil {
panic(err)
}
// Iterate default namespace resources tree
for _, root := range clusterCache.FindResources("default", TopLevelResource) {
clusterCache.IterateHierarchyV2([]kube.ResourceKey{root.ResourceKey()}, func(resource *Resource, _ map[kube.ResourceKey]*Resource) bool {
fmt.Printf("resource: %s, info: %v\n", resource.Ref.String(), resource.Info)
return true
})
}
}