...

Source file src/edge-infra.dev/pkg/edge/bootstrapping/kustomizations.go

Documentation: edge-infra.dev/pkg/edge/bootstrapping

     1  package bootstrapping
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"os"
     7  
     8  	kustomizeApi "github.com/fluxcd/kustomize-controller/api/v1"
     9  	"github.com/go-logr/logr"
    10  	apierrors "k8s.io/apimachinery/pkg/api/errors"
    11  	"k8s.io/apimachinery/pkg/types"
    12  	"sigs.k8s.io/controller-runtime/pkg/client"
    13  	"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
    14  
    15  	"edge-infra.dev/pkg/edge/api/services"
    16  )
    17  
    18  func CleanUpKustomizations(ctx context.Context, path string, bucketName, clusterVersion string, log logr.Logger, cl client.Client) error {
    19  	mapKustomizations := map[string]string{}
    20  	ks, err := services.CreateKustomizations(ctx, bucketName, path, clusterVersion)
    21  	if err != nil {
    22  		return err
    23  	}
    24  	for _, kustomization := range ks {
    25  		fluxKustomization := &kustomizeApi.Kustomization{}
    26  		if err := json.Unmarshal([]byte(kustomization), fluxKustomization); err != nil {
    27  			log.Error(err, "failed to marshall kustomizations")
    28  			continue
    29  		}
    30  
    31  		log.Info("Installing Flux Kustomize resource!!")
    32  
    33  		existingKustomization := &kustomizeApi.Kustomization{}
    34  		if err := cl.Get(ctx, types.NamespacedName{
    35  			Name:      fluxKustomization.Name,
    36  			Namespace: fluxKustomization.Namespace,
    37  		}, existingKustomization); err != nil {
    38  			if apierrors.IsNotFound(err) {
    39  				if err := cl.Create(ctx, fluxKustomization); err != nil {
    40  					log.Error(err, "unable to create flux kustomization", "name", fluxKustomization.Name, "namespace", fluxKustomization.Namespace)
    41  					return err
    42  				}
    43  				return nil
    44  			}
    45  			log.Error(err, "failed to get existing kustomization", "name", fluxKustomization.Name, "namespace", fluxKustomization.Namespace)
    46  			return err
    47  		}
    48  		existingKustomization.TypeMeta = fluxKustomization.TypeMeta
    49  		existingKustomization.ObjectMeta.Name = fluxKustomization.ObjectMeta.Name
    50  		existingKustomization.ObjectMeta.Namespace = fluxKustomization.ObjectMeta.Namespace
    51  		existingKustomization.Spec = fluxKustomization.Spec
    52  		existingKustomization.Status = fluxKustomization.Status
    53  		if err := cl.Update(ctx, existingKustomization); err != nil {
    54  			log.Error(err, "failed to update existing kustomization", "name", existingKustomization.Name, "namespace", existingKustomization.Namespace)
    55  			return err
    56  		}
    57  
    58  		log.Info("kustomization applied")
    59  		mapKustomizations[existingKustomization.Name] = ""
    60  	}
    61  
    62  	badKustosRemoved := false
    63  	for !badKustosRemoved {
    64  		badKustosRemoved = true
    65  		listKustomizations := &kustomizeApi.KustomizationList{}
    66  		if err = cl.List(ctx, listKustomizations); err != nil {
    67  			log.Error(err, "failed to get all kustomizations")
    68  			os.Exit(1)
    69  		}
    70  
    71  		for _, existingKustomization := range listKustomizations.Items {
    72  			if !isExpectedKustomization(mapKustomizations, existingKustomization.Name) {
    73  				badKustosRemoved = badKustosRemoved && removeUnexpectKustomizations(ctx, existingKustomization, cl, log)
    74  			}
    75  		}
    76  	}
    77  	return err
    78  }
    79  
    80  func isExpectedKustomization(existing map[string]string, checkKustomization string) bool {
    81  	_, ok := existing[checkKustomization]
    82  	return ok
    83  }
    84  
    85  func removeUnexpectKustomizations(ctx context.Context, existingKustomization kustomizeApi.Kustomization, cl client.Client, log logr.Logger) bool {
    86  	if _, ok := existingKustomization.Labels["kustomize.toolkit.fluxcd.io/name"]; !ok { //only delete if kustomization not synced by flux
    87  		badKustomization := existingKustomization.DeepCopy()
    88  		if badKustomization.Spec.Prune {
    89  			if _, err := controllerutil.CreateOrUpdate(ctx, cl, badKustomization, func() error {
    90  				badKustomization.Spec.Prune = false
    91  				return nil
    92  			}); err != nil {
    93  				log.Error(err, "failed to update bad kustomization")
    94  				os.Exit(1)
    95  			}
    96  			return false
    97  		}
    98  		if err := cl.Delete(ctx, badKustomization); err != nil {
    99  			log.Error(err, "failed to delete bad kustomization")
   100  			os.Exit(1)
   101  		}
   102  		return true
   103  	}
   104  	return true
   105  }
   106  

View as plain text