...

Source file src/edge-infra.dev/pkg/sds/k8s/daemonsetdns/daemonsetdnstest/daemonsetdnstest.go

Documentation: edge-infra.dev/pkg/sds/k8s/daemonsetdns/daemonsetdnstest

     1  package daemonsetdnstest
     2  
     3  import (
     4  	"bytes"
     5  	"embed"
     6  	"fmt"
     7  	"io/fs"
     8  	"strings"
     9  
    10  	"sigs.k8s.io/controller-runtime/pkg/client"
    11  	"sigs.k8s.io/kustomize/kyaml/kio"
    12  
    13  	"edge-infra.dev/pkg/k8s/decoder"
    14  	"edge-infra.dev/pkg/k8s/runtime/sap"
    15  	"edge-infra.dev/pkg/k8s/unstructured"
    16  	"edge-infra.dev/test/f2"
    17  	"edge-infra.dev/test/f2/x/ktest"
    18  )
    19  
    20  // testdata/base_manifests.yaml  -- the contents of this file are generated by a genrule
    21  //
    22  //go:embed testdata
    23  var manifestsData embed.FS
    24  
    25  var (
    26  	// Loaded by LoadManifest and then applied in Install
    27  	// TODO(aw185187): This isn't safe for concurrent tests on the same cluster
    28  	// using the DNS component.
    29  	manifests []*unstructured.Unstructured
    30  )
    31  
    32  // LoadManifests is a [edge-infra.dev/test/f2.SetupFn] to be used to load
    33  // DaemonSet DNS component manifests as part of framework initialization.
    34  //
    35  // Example Framework setup:
    36  //
    37  //		var f f2.Framework
    38  //		func TestMain(m *testing.M) {
    39  //			f = f2.New(
    40  //	      context.Background(),
    41  //				f2.WithExtensions(
    42  //					ktest.New(),
    43  //				),
    44  //			).
    45  //			Setup(func(ctx f2.Context) (f2.Context, error) {
    46  //				if !integration.IsL2() {
    47  //					return ctx, fmt.Errorf("%w: requires L2 integration test level", f2.ErrSkip)
    48  //				}
    49  //				return ctx, nil
    50  //			}).
    51  //			Setup(daemonsetdnstest.LoadManifests, daemonsetdnstest.Install)
    52  //
    53  // For each Feature that depends on the DNS component, add [Install] to the
    54  // feature's Setup functions.
    55  func LoadManifests(ctx f2.Context) (f2.Context, error) {
    56  	manifestsBytes, err := fs.ReadFile(manifestsData, "testdata/local_manifests.yaml")
    57  	if err != nil {
    58  		return ctx, fmt.Errorf("failed to read manifests: %w", err)
    59  	}
    60  
    61  	manifests, err = processManifests(string(manifestsBytes))
    62  	if err != nil {
    63  		return ctx, err
    64  	}
    65  
    66  	return ctx, nil
    67  }
    68  
    69  // Install is a [edge-infra.dev/test/f2.SetupFn] to be used to apply the DNS
    70  // component and wait for it to be ready.
    71  //
    72  // If the DaemonSet DNS component already exists in the cluster this function
    73  // will make no changes to the current version, otherwise it will install the
    74  // DaemonSet DNS component.
    75  //
    76  // TODO: allow forcing overwriting existing components via cli flag
    77  func Install(ctx f2.Context) (f2.Context, error) {
    78  	// TODO: a lot of this is duplicated from novnc_test. Move into kssert?
    79  	k, err := ktest.FromContext(ctx)
    80  	if err != nil {
    81  		return ctx, err
    82  	}
    83  
    84  	mgr, err := sap.NewResourceManagerFromConfig(k.Env.Config, client.Options{}, ktest.FieldManagerOwner())
    85  	if err != nil {
    86  		return ctx, err
    87  	}
    88  
    89  	cs, err := mgr.ApplyAllStaged(ctx, manifests, sap.ApplyOptions{
    90  		Force:       true,
    91  		WaitTimeout: k.Timeout,
    92  	})
    93  	if err != nil {
    94  		return ctx, err
    95  	}
    96  
    97  	return ctx, mgr.WaitForSet(ctx, cs.ToObjMetadataSet(), sap.WaitOptions{
    98  		Timeout: k.Timeout,
    99  	})
   100  }
   101  
   102  func processManifests(manifests string) ([]*unstructured.Unstructured, error) {
   103  	reader := kio.ByteReader{
   104  		Reader: strings.NewReader(manifests),
   105  	}
   106  
   107  	out, err := reader.Read()
   108  	if err != nil {
   109  		return nil, err
   110  	}
   111  
   112  	buf := bytes.Buffer{}
   113  	writer := kio.ByteWriter{
   114  		Writer: &buf,
   115  	}
   116  
   117  	err = writer.Write(out)
   118  	if err != nil {
   119  		return nil, err
   120  	}
   121  
   122  	return decoder.DecodeYAML(buf.Bytes())
   123  }
   124  

View as plain text