mirror of
https://github.com/hanzoai/cd.git
synced 2026-10-30 14:56:45 +00:00
Rename test names, comments, fixture data, and mock GitHub/webhook responses that still said Argo/ArgoCD/argocd/argoproj in our own voice, matching the hanzocd-* binary/configmap names and the apps.hanzo.ai CRD/annotation domain already established elsewhere in the fork. Also drop the dated argov1alpha1/argoprojiov1alpha1 import aliases for our own v1alpha1 API package. Restore the CRD groups a prior blind rename had mangled on a vendored third-party Argo Workflows example manifest back to the real argoproj.io, and fix several places where an earlier half-finished rename had gone silently wrong instead of just being cosmetic: stale CD_BINARY_NAME values and a stale --argocd-repo-server flag no longer recognized by the binary, cert filenames and an ssh_known_hosts alias that didn't match what's actually on disk, a namespace and a kustomize image substitution that were silent no-ops, and a test assertion regex that could never match its own fixture. External lookup keys we don't control are left byte-exact: quay.io/argoprojlabs images, the docker.io/argoproj CI builder image, live argoproj/argo-workflows release URLs, the generated ArgoprojV1alpha1() clientset method, and the Makefile-coupled /tmp/argo-e2e and GOPATH mount conventions.
219 lines
8.5 KiB
Go
219 lines
8.5 KiB
Go
package utils
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
stderrors "errors"
|
|
"fmt"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
"k8s.io/apimachinery/pkg/api/errors"
|
|
"k8s.io/apimachinery/pkg/api/resource"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
"k8s.io/apimachinery/pkg/conversion"
|
|
"k8s.io/apimachinery/pkg/fields"
|
|
"k8s.io/apimachinery/pkg/labels"
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
|
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
|
|
|
|
"github.com/hanzoai/cd/pkg/apis/application/v1alpha1"
|
|
"github.com/hanzoai/cd/util/cd"
|
|
"github.com/hanzoai/cd/util/cd/diff"
|
|
"github.com/hanzoai/cd/util/cd/normalizers"
|
|
)
|
|
|
|
var appEquality = conversion.EqualitiesOrDie(
|
|
func(a, b resource.Quantity) bool {
|
|
// Ignore formatting, only care that numeric value stayed the same.
|
|
// TODO: if we decide it's important, it should be safe to start comparing the format.
|
|
//
|
|
// Uninitialized quantities are equivalent to 0 quantities.
|
|
return a.Cmp(b) == 0
|
|
},
|
|
func(a, b metav1.MicroTime) bool {
|
|
return a.UTC().Equal(b.UTC())
|
|
},
|
|
func(a, b metav1.Time) bool {
|
|
return a.UTC().Equal(b.UTC())
|
|
},
|
|
func(a, b labels.Selector) bool {
|
|
return a.String() == b.String()
|
|
},
|
|
func(a, b fields.Selector) bool {
|
|
return a.String() == b.String()
|
|
},
|
|
func(a, b v1alpha1.ApplicationDestination) bool {
|
|
return a.Namespace == b.Namespace && a.Name == b.Name && a.Server == b.Server
|
|
},
|
|
)
|
|
|
|
// BuildIgnoreDiffConfig constructs a DiffConfig from the ApplicationSet's ignoreDifferences rules.
|
|
// Returns nil when ignoreDifferences is empty.
|
|
func BuildIgnoreDiffConfig(ignoreDifferences v1alpha1.ApplicationSetIgnoreDifferences, ignoreNormalizerOpts normalizers.IgnoreNormalizerOpts) (diff.DiffConfig, error) {
|
|
if len(ignoreDifferences) == 0 {
|
|
return nil, nil
|
|
}
|
|
return diff.NewDiffConfigBuilder().
|
|
WithDiffSettings(ignoreDifferences.ToApplicationIgnoreDifferences(), nil, false, ignoreNormalizerOpts).
|
|
WithNoCache().
|
|
Build()
|
|
}
|
|
|
|
// CreateOrUpdate overrides "sigs.k8s.io/controller-runtime" function
|
|
// in sigs.k8s.io/controller-runtime/pkg/controller/controllerutil/controllerutil.go
|
|
// to add equality for v1alpha1.ApplicationDestination
|
|
// v1alpha1.ApplicationDestination has a private variable, so the default
|
|
// implementation fails to compare it.
|
|
//
|
|
// CreateOrUpdate creates or updates the given object in the Kubernetes
|
|
// cluster. The object's desired state must be reconciled with the existing
|
|
// state inside the passed in callback MutateFn.
|
|
//
|
|
// diffConfig must be built once per reconcile cycle via BuildIgnoreDiffConfig and may be nil
|
|
// when there are no ignoreDifferences rules. obj.Spec must already be normalized by the caller
|
|
// via NormalizeApplicationSpec before this function is called; the live object fetched from the
|
|
// cluster is normalized internally.
|
|
//
|
|
// The MutateFn is called regardless of creating or updating an object.
|
|
//
|
|
// It returns the executed operation and an error.
|
|
func CreateOrUpdate(ctx context.Context, logCtx *log.Entry, c client.Client, diffConfig diff.DiffConfig, obj *v1alpha1.Application, f controllerutil.MutateFn) (controllerutil.OperationResult, error) {
|
|
key := client.ObjectKeyFromObject(obj)
|
|
if err := c.Get(ctx, key, obj); err != nil {
|
|
if !errors.IsNotFound(err) {
|
|
return controllerutil.OperationResultNone, err
|
|
}
|
|
if err := mutate(f, key, obj); err != nil {
|
|
return controllerutil.OperationResultNone, err
|
|
}
|
|
if err := c.Create(ctx, obj); err != nil {
|
|
return controllerutil.OperationResultNone, err
|
|
}
|
|
return controllerutil.OperationResultCreated, nil
|
|
}
|
|
|
|
normalizedLive := obj.DeepCopy()
|
|
|
|
// Mutate the live object to match the desired state.
|
|
if err := mutate(f, key, obj); err != nil {
|
|
return controllerutil.OperationResultNone, err
|
|
}
|
|
|
|
// Normalize the live spec to avoid spurious diffs from unimportant differences (e.g. nil vs
|
|
// empty SyncPolicy). obj.Spec is already normalized by the caller; only the live side needs it.
|
|
normalizedLive.Spec = *cd.NormalizeApplicationSpec(&normalizedLive.Spec)
|
|
|
|
// Apply ignoreApplicationDifferences rules to remove ignored fields from both the live and the desired state. This
|
|
// prevents those differences from appearing in the diff and therefore in the patch.
|
|
err := applyIgnoreDifferences(diffConfig, normalizedLive, obj)
|
|
if err != nil {
|
|
return controllerutil.OperationResultNone, fmt.Errorf("failed to apply ignore differences: %w", err)
|
|
}
|
|
|
|
// Note: if the informer cache holds a stale entry for an application that no longer exists on
|
|
// the API server, DeepEqual may match against that stale entry and we skip Patch here. The
|
|
// eviction in cacheSyncingClient only runs on NotFound from a write operation, so this edge
|
|
// case is not covered and relies on Kubernetes propagating the delete event to the informer.
|
|
if appEquality.DeepEqual(normalizedLive, obj) {
|
|
return controllerutil.OperationResultNone, nil
|
|
}
|
|
|
|
patch := client.MergeFrom(normalizedLive)
|
|
if log.IsLevelEnabled(log.DebugLevel) {
|
|
LogPatch(logCtx, patch, obj)
|
|
}
|
|
if err := c.Patch(ctx, obj, patch); err != nil {
|
|
return controllerutil.OperationResultNone, err
|
|
}
|
|
return controllerutil.OperationResultUpdated, nil
|
|
}
|
|
|
|
func LogPatch(logCtx *log.Entry, patch client.Patch, obj *v1alpha1.Application) {
|
|
patchBytes, err := patch.Data(obj)
|
|
if err != nil {
|
|
logCtx.Errorf("failed to generate patch: %v", err)
|
|
}
|
|
// Get the patch as a plain object so it is easier to work with in json logs.
|
|
var patchObj map[string]any
|
|
err = json.Unmarshal(patchBytes, &patchObj)
|
|
if err != nil {
|
|
logCtx.Errorf("failed to unmarshal patch: %v", err)
|
|
}
|
|
logCtx.WithField("patch", patchObj).Debug("patching application")
|
|
}
|
|
|
|
// mutate wraps a MutateFn and applies validation to its result
|
|
func mutate(f controllerutil.MutateFn, key client.ObjectKey, obj client.Object) error {
|
|
if err := f(); err != nil {
|
|
return fmt.Errorf("error while wrapping using MutateFn: %w", err)
|
|
}
|
|
if newKey := client.ObjectKeyFromObject(obj); key != newKey {
|
|
return stderrors.New("MutateFn cannot mutate object name and/or object namespace")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// applyIgnoreDifferences applies the ignore differences rules to the found application. It modifies the applications in place.
|
|
// diffConfig may be nil, in which case this is a no-op.
|
|
func applyIgnoreDifferences(diffConfig diff.DiffConfig, found *v1alpha1.Application, generatedApp *v1alpha1.Application) error {
|
|
if diffConfig == nil {
|
|
return nil
|
|
}
|
|
|
|
generatedAppCopy := generatedApp.DeepCopy()
|
|
unstructuredFound, err := appToUnstructured(found)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to convert found application to unstructured: %w", err)
|
|
}
|
|
unstructuredGenerated, err := appToUnstructured(generatedApp)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to convert found application to unstructured: %w", err)
|
|
}
|
|
result, err := diff.Normalize([]*unstructured.Unstructured{unstructuredFound}, []*unstructured.Unstructured{unstructuredGenerated}, diffConfig)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to normalize application spec: %w", err)
|
|
}
|
|
if len(result.Lives) != 1 {
|
|
return fmt.Errorf("expected 1 normalized application, got %d", len(result.Lives))
|
|
}
|
|
foundJSONNormalized, err := json.Marshal(result.Lives[0].Object)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to marshal normalized app to json: %w", err)
|
|
}
|
|
foundNormalized := &v1alpha1.Application{}
|
|
err = json.Unmarshal(foundJSONNormalized, &foundNormalized)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to unmarshal normalized app to json: %w", err)
|
|
}
|
|
if len(result.Targets) != 1 {
|
|
return fmt.Errorf("expected 1 normalized application, got %d", len(result.Targets))
|
|
}
|
|
foundNormalized.DeepCopyInto(found)
|
|
generatedJSONNormalized, err := json.Marshal(result.Targets[0].Object)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to marshal normalized app to json: %w", err)
|
|
}
|
|
generatedAppNormalized := &v1alpha1.Application{}
|
|
err = json.Unmarshal(generatedJSONNormalized, &generatedAppNormalized)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to unmarshal normalized app json to structured app: %w", err)
|
|
}
|
|
generatedAppNormalized.DeepCopyInto(generatedApp)
|
|
// Prohibit jq queries from mutating silly things.
|
|
generatedApp.TypeMeta = generatedAppCopy.TypeMeta
|
|
generatedApp.Name = generatedAppCopy.Name
|
|
generatedApp.Namespace = generatedAppCopy.Namespace
|
|
generatedApp.Operation = generatedAppCopy.Operation
|
|
return nil
|
|
}
|
|
|
|
func appToUnstructured(app client.Object) (*unstructured.Unstructured, error) {
|
|
u, err := runtime.DefaultUnstructuredConverter.ToUnstructured(app)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to convert app object to unstructured: %w", err)
|
|
}
|
|
return &unstructured.Unstructured{Object: u}, nil
|
|
}
|