forked from zarf-dev/zarf
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: wait using kstatus (zarf-dev#3043)
Signed-off-by: Austin Abro <[email protected]> Signed-off-by: ittacco <[email protected]>
- Loading branch information
1 parent
0de7dc8
commit 3b1ff1d
Showing
6 changed files
with
173 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// SPDX-FileCopyrightText: 2021-Present The Zarf Authors | ||
|
||
// Package healthchecks run kstatus style health checks on a list of objects | ||
package healthchecks | ||
|
||
import ( | ||
"context" | ||
|
||
pkgkubernetes "github.com/defenseunicorns/pkg/kubernetes" | ||
"github.com/zarf-dev/zarf/src/api/v1alpha1" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"sigs.k8s.io/cli-utils/pkg/kstatus/watcher" | ||
"sigs.k8s.io/cli-utils/pkg/object" | ||
) | ||
|
||
// Run waits for a list of objects to be reconciled | ||
func Run(ctx context.Context, watcher watcher.StatusWatcher, healthChecks []v1alpha1.NamespacedObjectKindReference) error { | ||
objs := []object.ObjMetadata{} | ||
for _, hc := range healthChecks { | ||
gv, err := schema.ParseGroupVersion(hc.APIVersion) | ||
if err != nil { | ||
return err | ||
} | ||
obj := object.ObjMetadata{ | ||
GroupKind: schema.GroupKind{ | ||
Group: gv.Group, | ||
Kind: hc.Kind, | ||
}, | ||
Namespace: hc.Namespace, | ||
Name: hc.Name, | ||
} | ||
objs = append(objs, obj) | ||
} | ||
err := pkgkubernetes.WaitForReady(ctx, watcher, objs) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// SPDX-FileCopyrightText: 2021-Present The Zarf Authors | ||
|
||
// Package healthchecks run kstatus style health checks on a list of objects | ||
package healthchecks | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/zarf-dev/zarf/src/api/v1alpha1" | ||
v1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/apimachinery/pkg/util/yaml" | ||
dynamicfake "k8s.io/client-go/dynamic/fake" | ||
"k8s.io/kubectl/pkg/scheme" | ||
"sigs.k8s.io/cli-utils/pkg/kstatus/watcher" | ||
"sigs.k8s.io/cli-utils/pkg/testutil" | ||
) | ||
|
||
var podCurrentYaml = ` | ||
apiVersion: v1 | ||
kind: Pod | ||
metadata: | ||
name: good-pod | ||
namespace: ns | ||
status: | ||
conditions: | ||
- type: Ready | ||
status: "True" | ||
phase: Running | ||
` | ||
|
||
var podYaml = ` | ||
apiVersion: v1 | ||
kind: Pod | ||
metadata: | ||
name: in-progress-pod | ||
namespace: ns | ||
` | ||
|
||
func TestRunHealthChecks(t *testing.T) { | ||
t.Parallel() | ||
tests := []struct { | ||
name string | ||
podYaml string | ||
expectErr error | ||
}{ | ||
{ | ||
name: "Pod is running", | ||
podYaml: podCurrentYaml, | ||
expectErr: nil, | ||
}, | ||
{ | ||
name: "Pod is never ready", | ||
podYaml: podYaml, | ||
expectErr: context.DeadlineExceeded, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
fakeClient := dynamicfake.NewSimpleDynamicClient(scheme.Scheme) | ||
fakeMapper := testutil.NewFakeRESTMapper( | ||
v1.SchemeGroupVersion.WithKind("Pod"), | ||
) | ||
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond) | ||
defer cancel() | ||
m := make(map[string]interface{}) | ||
err := yaml.Unmarshal([]byte(tt.podYaml), &m) | ||
require.NoError(t, err) | ||
pod := &unstructured.Unstructured{Object: m} | ||
statusWatcher := watcher.NewDefaultStatusWatcher(fakeClient, fakeMapper) | ||
podGVR := schema.GroupVersionResource{Group: "", Version: "v1", Resource: "pods"} | ||
require.NoError(t, fakeClient.Tracker().Create(podGVR, pod, pod.GetNamespace())) | ||
objs := []v1alpha1.NamespacedObjectKindReference{ | ||
{ | ||
APIVersion: pod.GetAPIVersion(), | ||
Kind: pod.GetKind(), | ||
Namespace: pod.GetNamespace(), | ||
Name: pod.GetName(), | ||
}, | ||
} | ||
err = Run(ctx, statusWatcher, objs) | ||
if tt.expectErr != nil { | ||
require.ErrorIs(t, err, tt.expectErr) | ||
return | ||
} | ||
require.NoError(t, err) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters