package daemonsetdnstest import ( "bytes" "embed" "fmt" "io/fs" "strings" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/kustomize/kyaml/kio" "edge-infra.dev/pkg/k8s/decoder" "edge-infra.dev/pkg/k8s/runtime/sap" "edge-infra.dev/pkg/k8s/unstructured" "edge-infra.dev/test/f2" "edge-infra.dev/test/f2/x/ktest" ) // testdata/base_manifests.yaml -- the contents of this file are generated by a genrule // //go:embed testdata var manifestsData embed.FS var ( // Loaded by LoadManifest and then applied in Install // TODO(aw185187): This isn't safe for concurrent tests on the same cluster // using the DNS component. manifests []*unstructured.Unstructured ) // LoadManifests is a [edge-infra.dev/test/f2.SetupFn] to be used to load // DaemonSet DNS component manifests as part of framework initialization. // // Example Framework setup: // // var f f2.Framework // func TestMain(m *testing.M) { // f = f2.New( // context.Background(), // f2.WithExtensions( // ktest.New(), // ), // ). // Setup(func(ctx f2.Context) (f2.Context, error) { // if !integration.IsL2() { // return ctx, fmt.Errorf("%w: requires L2 integration test level", f2.ErrSkip) // } // return ctx, nil // }). // Setup(daemonsetdnstest.LoadManifests, daemonsetdnstest.Install) // // For each Feature that depends on the DNS component, add [Install] to the // feature's Setup functions. func LoadManifests(ctx f2.Context) (f2.Context, error) { manifestsBytes, err := fs.ReadFile(manifestsData, "testdata/local_manifests.yaml") if err != nil { return ctx, fmt.Errorf("failed to read manifests: %w", err) } manifests, err = processManifests(string(manifestsBytes)) if err != nil { return ctx, err } return ctx, nil } // Install is a [edge-infra.dev/test/f2.SetupFn] to be used to apply the DNS // component and wait for it to be ready. // // If the DaemonSet DNS component already exists in the cluster this function // will make no changes to the current version, otherwise it will install the // DaemonSet DNS component. // // TODO: allow forcing overwriting existing components via cli flag func Install(ctx f2.Context) (f2.Context, error) { // TODO: a lot of this is duplicated from novnc_test. Move into kssert? k, err := ktest.FromContext(ctx) if err != nil { return ctx, err } mgr, err := sap.NewResourceManagerFromConfig(k.Env.Config, client.Options{}, ktest.FieldManagerOwner()) if err != nil { return ctx, err } cs, err := mgr.ApplyAllStaged(ctx, manifests, sap.ApplyOptions{ Force: true, WaitTimeout: k.Timeout, }) if err != nil { return ctx, err } return ctx, mgr.WaitForSet(ctx, cs.ToObjMetadataSet(), sap.WaitOptions{ Timeout: k.Timeout, }) } func processManifests(manifests string) ([]*unstructured.Unstructured, error) { reader := kio.ByteReader{ Reader: strings.NewReader(manifests), } out, err := reader.Read() if err != nil { return nil, err } buf := bytes.Buffer{} writer := kio.ByteWriter{ Writer: &buf, } err = writer.Write(out) if err != nil { return nil, err } return decoder.DecodeYAML(buf.Bytes()) }