mirror of
https://github.com/cozystack/cozystack.git
synced 2026-11-21 12:13:55 +09:00
refactor(lineage): resolve a HelmRelease's application in a helper
The walk used a loop that always ended in its first pass to get "break to the fallback" control flow. A function returning whether it resolved the application says the same without the loop. A test pins that each failure to resolve still falls back to the HelmRelease's own label and that a resolved one ends the walk. Assisted-by: LLM Signed-off-by: Aleksei Sviridkin <f@lex.la>
This commit is contained in:
2 files changed
+99
-30
No files matched your search
@@ -0,0 +1,70 @@
|
||||
package lineage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
helmv2 "github.com/fluxcd/helm-controller/api/v2"
|
||||
"k8s.io/apimachinery/pkg/api/meta"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
dynamicfake "k8s.io/client-go/dynamic/fake"
|
||||
)
|
||||
|
||||
// namedAppMapper maps a HelmRelease to the Kubernetes application it names;
|
||||
// a HelmRelease missing from the map fails to map.
|
||||
type namedAppMapper map[string]string
|
||||
|
||||
func (m namedAppMapper) Map(hr *helmv2.HelmRelease) (string, string, string, error) {
|
||||
app, ok := m[hr.Name]
|
||||
if !ok {
|
||||
return "", "", "", fmt.Errorf("no application for HelmRelease %s", hr.Name)
|
||||
}
|
||||
return "apps.cozystack.io/v1alpha1", "Kubernetes", strings.TrimSuffix(hr.Name, app), nil
|
||||
}
|
||||
|
||||
// A HelmRelease that cannot be resolved to its application hands the walk to
|
||||
// the HelmRelease its label names; one that resolves ends the walk there.
|
||||
func TestWalkHelmReleaseFallsBackToItsLabel(t *testing.T) {
|
||||
parent := map[string]string{HRLabel: "kubernetes-b"}
|
||||
unconvertible := capkObject(HRAPIVersion, HRKind, "kubernetes-a", parent, nil)
|
||||
unconvertible.Object["spec"] = "not an object"
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
hr *unstructured.Unstructured
|
||||
mapped namedAppMapper
|
||||
want []string
|
||||
}{
|
||||
{name: "mapped", hr: capkObject(HRAPIVersion, HRKind, "kubernetes-a", parent, nil), mapped: namedAppMapper{"kubernetes-a": "a", "kubernetes-b": "b"}, want: []string{"a"}},
|
||||
{name: "unmappable", hr: capkObject(HRAPIVersion, HRKind, "kubernetes-a", parent, nil), mapped: namedAppMapper{"kubernetes-b": "b"}, want: []string{"b"}},
|
||||
{name: "application missing", hr: capkObject(HRAPIVersion, HRKind, "kubernetes-a", parent, nil), mapped: namedAppMapper{"kubernetes-a": "absent", "kubernetes-b": "b"}, want: []string{"b"}},
|
||||
{name: "unconvertible", hr: unconvertible, mapped: namedAppMapper{"kubernetes-a": "a", "kubernetes-b": "b"}, want: []string{"b"}},
|
||||
{name: "unmappable without a label", hr: capkObject(HRAPIVersion, HRKind, "kubernetes-a", nil, nil), mapped: namedAppMapper{"kubernetes-b": "b"}, want: nil},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
objs := []runtime.Object{
|
||||
capkObject("apps.cozystack.io/v1alpha1", "Kubernetes", "a", nil, nil),
|
||||
capkObject("apps.cozystack.io/v1alpha1", "Kubernetes", "b", nil, nil),
|
||||
capkObject(HRAPIVersion, HRKind, "kubernetes-b", nil, nil),
|
||||
}
|
||||
mapper := meta.NewDefaultRESTMapper(nil)
|
||||
for _, o := range objs {
|
||||
mapper.Add(o.GetObjectKind().GroupVersionKind(), meta.RESTScopeNamespace)
|
||||
}
|
||||
dyn := dynamicfake.NewSimpleDynamicClient(runtime.NewScheme(), objs...)
|
||||
var got []string
|
||||
for _, id := range WalkOwnershipGraph(context.Background(), dyn, mapper, tc.mapped, tc.hr) {
|
||||
got = append(got, id.Kind+"/"+id.Name)
|
||||
}
|
||||
var want []string
|
||||
for _, name := range tc.want {
|
||||
want = append(want, "Kubernetes/"+name)
|
||||
}
|
||||
if strings.Join(got, ",") != strings.Join(want, ",") {
|
||||
t.Fatalf("WalkOwnershipGraph(%s) = %v, want %v", tc.name, got, want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
+29
-30
@@ -113,36 +113,8 @@ func WalkOwnershipGraph(
|
||||
return
|
||||
}
|
||||
|
||||
// I want "if err1 != nil go to next block, if err2 != nil, go to next block, etc semantics",
|
||||
// like an early return from a function, but if all checks succeed, I don't want to do the rest
|
||||
// of the function, so it's a `for { if err { break } if othererr { break } if allgood { return }
|
||||
for {
|
||||
if obj.GetAPIVersion() != HRAPIVersion || obj.GetKind() != HRKind {
|
||||
break
|
||||
}
|
||||
hr := helmReleaseFromUnstructured(obj)
|
||||
if hr == nil {
|
||||
break
|
||||
}
|
||||
a, k, p, err := appMapper.Map(hr)
|
||||
if err != nil {
|
||||
l.Error(err, "failed to map HelmRelease to app")
|
||||
break
|
||||
}
|
||||
ownerObj, err := getUnstructuredObject(ctx, client, mapper, state.cache, a, k, obj.GetNamespace(), strings.TrimPrefix(obj.GetName(), p))
|
||||
if err != nil {
|
||||
l.Error(err, "couldn't get unstructured object", "APIVersion", a, "Kind", k, "Name", strings.TrimPrefix(obj.GetName(), p))
|
||||
break
|
||||
}
|
||||
// successfully mapped a HelmRelease to a custom app, no need to continue
|
||||
out = append(out,
|
||||
ObjectID{
|
||||
APIVersion: ownerObj.GetAPIVersion(),
|
||||
Kind: ownerObj.GetKind(),
|
||||
Namespace: ownerObj.GetNamespace(),
|
||||
Name: ownerObj.GetName(),
|
||||
},
|
||||
)
|
||||
if app, ok := helmReleaseApp(ctx, client, mapper, appMapper, state.cache, obj); ok {
|
||||
out = append(out, app)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -160,6 +132,33 @@ func WalkOwnershipGraph(
|
||||
return
|
||||
}
|
||||
|
||||
// helmReleaseApp resolves a HelmRelease to the application it deploys. When
|
||||
// it cannot, the walk goes on through the HelmRelease's own label instead.
|
||||
func helmReleaseApp(ctx context.Context, client dynamic.Interface, mapper meta.RESTMapper, appMapper AppMapper, cache *ObjectCache, obj *unstructured.Unstructured) (ObjectID, bool) {
|
||||
hr := helmReleaseFromUnstructured(obj)
|
||||
if hr == nil {
|
||||
return ObjectID{}, false
|
||||
}
|
||||
l := log.FromContext(ctx)
|
||||
a, k, p, err := appMapper.Map(hr)
|
||||
if err != nil {
|
||||
l.Error(err, "failed to map HelmRelease to app")
|
||||
return ObjectID{}, false
|
||||
}
|
||||
name := strings.TrimPrefix(obj.GetName(), p)
|
||||
app, err := getUnstructuredObject(ctx, client, mapper, cache, a, k, obj.GetNamespace(), name)
|
||||
if err != nil {
|
||||
l.Error(err, "couldn't get unstructured object", "APIVersion", a, "Kind", k, "Name", name)
|
||||
return ObjectID{}, false
|
||||
}
|
||||
return ObjectID{
|
||||
APIVersion: app.GetAPIVersion(),
|
||||
Kind: app.GetKind(),
|
||||
Namespace: app.GetNamespace(),
|
||||
Name: app.GetName(),
|
||||
}, true
|
||||
}
|
||||
|
||||
// kubevirtMachineName returns the KubevirtMachine a CAPK-created
|
||||
// VirtualMachine belongs to. It only follows a KubevirtMachine in the VM's own
|
||||
// namespace: CAPK can also run against an external infrastructure cluster,
|
||||
|
||||
Reference in new issue
Block a user