...

Source file src/edge-infra.dev/pkg/sds/ien/k8s/controllers/pxe/ssa.go

Documentation: edge-infra.dev/pkg/sds/ien/k8s/controllers/pxe

     1  package pxe
     2  
     3  import (
     4  	"context"
     5  
     6  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
     7  
     8  	"edge-infra.dev/pkg/k8s/decoder"
     9  	"edge-infra.dev/pkg/k8s/runtime/inventory"
    10  	"edge-infra.dev/pkg/k8s/runtime/sap"
    11  	"edge-infra.dev/pkg/k8s/unstructured"
    12  	"edge-infra.dev/pkg/lib/fog"
    13  	v1pxe "edge-infra.dev/pkg/sds/ien/k8s/controllers/pxe/apis/v1"
    14  	"edge-infra.dev/pkg/sds/ien/k8s/controllers/pxe/common"
    15  )
    16  
    17  type pxeManager struct {
    18  	resourceManager *sap.ResourceManager
    19  	pxe             *v1pxe.PXE
    20  }
    21  
    22  // newPXEManager returns a pxeManager configured with the provided resource
    23  // manager to manage the provided pxe resource
    24  func newPXEManager(mgr *sap.ResourceManager, pxe *v1pxe.PXE) pxeManager {
    25  	return pxeManager{
    26  		mgr,
    27  		pxe,
    28  	}
    29  }
    30  
    31  // apply will use server-side apply to apply the provided manifests to the API
    32  // server using the resource manager. When the resources have been applied, the
    33  // changeset is compared to the previous inventory for the PXE resource. Any
    34  // resources that are found in the inventory that are not in the changeset are
    35  // pruned
    36  func (m *pxeManager) apply(ctx context.Context, manifests ...[]byte) error {
    37  	var toApply []*unstructured.Unstructured
    38  	for _, m := range manifests {
    39  		uobjs, err := decoder.DecodeYAML(m)
    40  		if err != nil {
    41  			return err
    42  		}
    43  
    44  		toApply = append(toApply, uobjs...)
    45  	}
    46  
    47  	changeSet, err := m.resourceManager.ApplyAll(ctx, toApply, sap.ApplyOptions{})
    48  	if err != nil {
    49  		return err
    50  	}
    51  
    52  	fog.FromContext(ctx).Info("applied objects", "changeset", changeSet.ToMap())
    53  
    54  	// create new inventory and use this to prune the old one
    55  	inv := inventory.New(inventory.FromSapChangeSet(changeSet))
    56  	return m.prune(ctx, inv)
    57  }
    58  
    59  // prune will use server-side apply to delete resources that were in the old PXE
    60  // inventory but do not appear in the provided new inventory. The inventory
    61  // is updated to reflect the new set of resources managed by the PXE instance
    62  func (m *pxeManager) prune(ctx context.Context, newInv *inventory.ResourceInventory) (err error) {
    63  	defer func() {
    64  		if err == nil {
    65  			m.pxe.SetInventory(newInv)
    66  		}
    67  	}()
    68  
    69  	if m.pxe.GetInventory() == nil {
    70  		return nil
    71  	}
    72  
    73  	// find resources that don't exist in new inventory
    74  	diff, err := inventory.Diff(m.pxe.GetInventory(), newInv)
    75  	if err != nil {
    76  		return err
    77  	}
    78  
    79  	if len(diff) <= 0 {
    80  		return nil
    81  	}
    82  
    83  	opts := sap.DeleteOptions{
    84  		PropagationPolicy: metav1.DeletePropagationBackground,
    85  		// only include resources that were created and are managed by PXE controller
    86  		Inclusions: managedByPXEController(),
    87  		Exclusions: nil,
    88  	}
    89  
    90  	changeSet, err := m.resourceManager.DeleteAll(ctx, diff, opts)
    91  	if err != nil {
    92  		return err
    93  	}
    94  
    95  	fog.FromContext(ctx).Info("pruned objects", "changeset", changeSet.ToMap())
    96  
    97  	return nil
    98  }
    99  
   100  // managedByPXEController returns a label map for resources managed by the PXE
   101  // controller
   102  func managedByPXEController() map[string]string {
   103  	return map[string]string{
   104  		common.KubernetesManagerByLabel: common.PXEControllerName,
   105  	}
   106  }
   107  

View as plain text