mirror of
https://github.com/cozystack/cozystack.git
synced 2026-11-21 12:13:55 +09:00
Reconcile the tap digest-pin change with #5071 (no-install-on-connect), now merged into main. Keep this branch's resetTapPinState, the verify-before-pull Trust text, and the index-README trust model, while adopting #5071's wording that installing (not tapping) runs charts and dropping the stale "re-registration" phrasing from the shared tap.go and rest.go comments. Conflicts resolved in cmd/cozypkg/cmd/tap.go, pkg/registry/core/tap/rest.go, and hack/packages-index/README.md. Assisted-by: LLM Signed-off-by: IvanHunters <xorokhotnikov@gmail.com>
475 lines
19 KiB
Go
475 lines
19 KiB
Go
/*
|
|
Copyright 2025 The Cozystack Authors.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package operator
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"net"
|
|
"net/http"
|
|
neturl "net/url"
|
|
"os"
|
|
"regexp"
|
|
"strings"
|
|
"time"
|
|
|
|
sourcev1 "github.com/fluxcd/source-controller/api/v1"
|
|
corev1 "k8s.io/api/core/v1"
|
|
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
"k8s.io/apimachinery/pkg/types"
|
|
"k8s.io/client-go/tools/record"
|
|
ctrl "sigs.k8s.io/controller-runtime"
|
|
"sigs.k8s.io/controller-runtime/pkg/builder"
|
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
|
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
|
|
"sigs.k8s.io/controller-runtime/pkg/log"
|
|
"sigs.k8s.io/controller-runtime/pkg/predicate"
|
|
|
|
cozyv1alpha1 "github.com/cozystack/cozystack/api/v1alpha1"
|
|
"github.com/cozystack/cozystack/internal/marketplace/collision"
|
|
"github.com/cozystack/cozystack/internal/marketplace/tapconst"
|
|
)
|
|
|
|
const (
|
|
tapFieldOwner = "cozystack-tap-materializer"
|
|
tapWaitRequeue = 20 * time.Second
|
|
tapFetchLimit = maxArtifactBytes
|
|
)
|
|
|
|
// TapMaterializerReconciler watches community-tap OCIRepositories and
|
|
// materializes the PackageSource(s) their artifact carries, so a tap connected
|
|
// from the dashboard becomes installable without the API pulling the artifact.
|
|
type TapMaterializerReconciler struct {
|
|
client.Client
|
|
Scheme *runtime.Scheme
|
|
// Recorder surfaces materialization failures (e.g. a name collision with a
|
|
// core component) as Events on the tap source. Optional; nil disables Events.
|
|
Recorder record.EventRecorder
|
|
// Fetch downloads a Flux artifact tarball. Defaults to HTTP; overridable in tests.
|
|
Fetch func(ctx context.Context, url string) ([]byte, error)
|
|
}
|
|
|
|
func (r *TapMaterializerReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
|
|
logger := log.FromContext(ctx)
|
|
|
|
var repo sourcev1.OCIRepository
|
|
if err := r.Get(ctx, req.NamespacedName, &repo); err != nil {
|
|
return ctrl.Result{}, client.IgnoreNotFound(err)
|
|
}
|
|
if repo.Labels[tapconst.Label] != "true" {
|
|
return ctrl.Result{}, nil
|
|
}
|
|
|
|
// Deletion: clean up materialized PackageSources, then release the finalizer.
|
|
if !repo.DeletionTimestamp.IsZero() {
|
|
if controllerutil.ContainsFinalizer(&repo, tapconst.Finalizer) {
|
|
if err := r.deleteMaterialized(ctx, repo.Name); err != nil {
|
|
return ctrl.Result{}, err
|
|
}
|
|
controllerutil.RemoveFinalizer(&repo, tapconst.Finalizer)
|
|
if err := r.Update(ctx, &repo); err != nil {
|
|
return ctrl.Result{}, err
|
|
}
|
|
}
|
|
return ctrl.Result{}, nil
|
|
}
|
|
|
|
if !controllerutil.ContainsFinalizer(&repo, tapconst.Finalizer) {
|
|
controllerutil.AddFinalizer(&repo, tapconst.Finalizer)
|
|
if err := r.Update(ctx, &repo); err != nil {
|
|
return ctrl.Result{}, err
|
|
}
|
|
return ctrl.Result{RequeueAfter: time.Second}, nil
|
|
}
|
|
|
|
art := repo.Status.Artifact
|
|
if art == nil || art.URL == "" || art.Digest == "" {
|
|
// Source not pulled yet (or digest not yet reported); wait for
|
|
// source-controller. Requiring the digest keeps materialization
|
|
// fail-closed on the integrity check.
|
|
return ctrl.Result{RequeueAfter: tapWaitRequeue}, nil
|
|
}
|
|
// Act only on an artifact that reflects the current spec. After a re-connect
|
|
// changes spec.ref, source-controller must re-resolve before status.artifact
|
|
// describes the new reference; acting early would materialize and pin the
|
|
// previous revision (issue #4979).
|
|
if !r.artifactReady(&repo, art.Revision) {
|
|
return ctrl.Result{RequeueAfter: tapWaitRequeue}, nil
|
|
}
|
|
if repo.Annotations[tapconst.MaterializedRevisionAnnotation] == art.Revision {
|
|
// Already materialized at this revision. A tap connected before digest
|
|
// pinning existed still follows its tag (no digest on spec.ref); pin the
|
|
// current digest now so moving the tag upstream cannot pull new content on
|
|
// the next revision. A source that already carries a digest needs nothing.
|
|
if ref := repo.Spec.Reference; ref == nil || ref.Digest == "" {
|
|
pinTapDigest(&repo, art.Revision)
|
|
if err := r.Update(ctx, &repo); err != nil {
|
|
return ctrl.Result{}, err
|
|
}
|
|
}
|
|
return ctrl.Result{}, nil
|
|
}
|
|
|
|
fetch := r.Fetch
|
|
if fetch == nil {
|
|
fetch = httpFetch
|
|
}
|
|
// The artifact URL is a cluster-DNS name (flux.<ns>.svc/...). cozystack-operator
|
|
// runs with hostNetwork, so it resolves against the host's DNS, not CoreDNS,
|
|
// and that name does not resolve. Rewrite the host to the Service ClusterIP,
|
|
// which a hostNetwork pod can reach directly; on any failure fall back to the
|
|
// original URL so clusters where DNS does work are unaffected.
|
|
data, err := fetch(ctx, r.resolveArtifactURL(ctx, art.URL))
|
|
if err != nil {
|
|
return ctrl.Result{}, fmt.Errorf("fetch artifact for tap %s: %w", repo.Name, err)
|
|
}
|
|
|
|
tmp, err := os.MkdirTemp("", "tap-materialize-")
|
|
if err != nil {
|
|
return ctrl.Result{}, err
|
|
}
|
|
defer func() { _ = os.RemoveAll(tmp) }()
|
|
if err := verifyAndExtract(data, art.Digest, tmp); err != nil {
|
|
return ctrl.Result{}, fmt.Errorf("extract artifact for tap %s: %w", repo.Name, err)
|
|
}
|
|
sources, err := parsePackageSourcesFromTree(tmp)
|
|
if err != nil {
|
|
return ctrl.Result{}, fmt.Errorf("parse artifact for tap %s: %w", repo.Name, err)
|
|
}
|
|
|
|
// A tapped repository keeps its own declared name. Refuse the whole
|
|
// materialization if any declared name clashes with a core component (or
|
|
// another tap), surfacing the reason on the source instead of silently
|
|
// overwriting an official PackageSource via ForceOwnership.
|
|
for i := range sources {
|
|
if err := collision.PackageSourceName(ctx, r.Client, sources[i].GetName(), repo.Name); err != nil {
|
|
return r.failMaterialize(ctx, &repo, err)
|
|
}
|
|
}
|
|
|
|
applied := make(map[string]bool, len(sources))
|
|
for i := range sources {
|
|
ps := sources[i].DeepCopy()
|
|
origPath := "/"
|
|
if ps.Spec.SourceRef != nil && ps.Spec.SourceRef.Path != "" {
|
|
origPath = ps.Spec.SourceRef.Path
|
|
}
|
|
rewriteForMaterialize(ps, repo.Name, repo.Namespace, origPath)
|
|
if ps.Labels == nil {
|
|
ps.Labels = map[string]string{}
|
|
}
|
|
ps.Labels[tapconst.Label] = "true"
|
|
if ps.Annotations == nil {
|
|
ps.Annotations = map[string]string{}
|
|
}
|
|
ps.Annotations[tapconst.SourceAnnotation] = repo.Name
|
|
if err := r.Patch(ctx, ps, client.Apply, client.FieldOwner(tapFieldOwner), client.ForceOwnership); err != nil {
|
|
return ctrl.Result{}, fmt.Errorf("materialize PackageSource %s: %w", ps.GetName(), err)
|
|
}
|
|
// Connecting a repository materializes its PackageSources and nothing
|
|
// else: no Package, HelmRelease or ApplicationDefinition is created here.
|
|
// Auto-registering on connect (removed) created a tap-managed Package whose
|
|
// install-marked components Flux deployed under its cluster-admin service
|
|
// account, so a publisher's install chart ran on connect with no operator
|
|
// step (issue #4978). Installing is now explicit: `cozypkg add` creates the
|
|
// Package. Populating the catalog from PackageSource metadata without an
|
|
// install is the long-term shape in issue #4359.
|
|
applied[ps.GetName()] = true
|
|
logger.Info("materialized PackageSource from tap", "name", ps.GetName(), "tap", repo.Name)
|
|
}
|
|
if len(sources) == 0 {
|
|
logger.Info("tap artifact carried no PackageSource", "tap", repo.Name, "revision", art.Revision)
|
|
}
|
|
// Materialization succeeded: clear any collision error from a prior revision.
|
|
delete(repo.Annotations, tapconst.MaterializeErrorAnnotation)
|
|
|
|
// Prune PackageSources this tap materialized from an earlier revision that
|
|
// the current artifact no longer contains (including a rename when the
|
|
// single/multi package count flips), so a removed package leaves the
|
|
// catalog. Installed Packages are left in place.
|
|
if err := r.pruneMaterialized(ctx, repo.Name, applied); err != nil {
|
|
return ctrl.Result{}, err
|
|
}
|
|
|
|
// Pin the resolved digest so the source stops following a mutable tag: the
|
|
// tag may move upstream, but the cluster keeps pulling this exact content
|
|
// until an explicit re-connect. This runs only after verifyAndExtract
|
|
// accepted the artifact, so the digest pinned is the digest of verified
|
|
// content. A user-supplied explicit digest is never rewritten.
|
|
pinTapDigest(&repo, art.Revision)
|
|
|
|
// Stamp the revision so an unchanged artifact is not re-pulled every resync.
|
|
if repo.Annotations == nil {
|
|
repo.Annotations = map[string]string{}
|
|
}
|
|
repo.Annotations[tapconst.MaterializedRevisionAnnotation] = art.Revision
|
|
if err := r.Update(ctx, &repo); err != nil {
|
|
return ctrl.Result{}, err
|
|
}
|
|
return ctrl.Result{}, nil
|
|
}
|
|
|
|
// artifactReady reports whether status.artifact reflects the current spec
|
|
// generation and, for a tag-only source, the tag the source now follows. It
|
|
// gates materialization and pinning so a re-connect's new spec is never acted on
|
|
// with the previous revision's artifact. source-controller formats a tag
|
|
// revision as '<tag>@<digest>', so a tag-only ref requires that prefix; a source
|
|
// pinned to a digest reports a bare digest revision and needs only the
|
|
// generation check.
|
|
func (r *TapMaterializerReconciler) artifactReady(repo *sourcev1.OCIRepository, revision string) bool {
|
|
if repo.Status.ObservedGeneration != repo.Generation {
|
|
return false
|
|
}
|
|
if ref := repo.Spec.Reference; ref != nil && ref.Tag != "" && ref.Digest == "" {
|
|
return strings.HasPrefix(revision, ref.Tag+"@")
|
|
}
|
|
return true
|
|
}
|
|
|
|
// manifestDigestRe matches a full OCI manifest digest (sha256:<64 hex>).
|
|
var manifestDigestRe = regexp.MustCompile(`^sha256:[0-9a-f]{64}$`)
|
|
|
|
// pinTapDigest pins a tag-following tap source to the digest Flux resolved for
|
|
// the current artifact, so moving the tag upstream no longer changes what the
|
|
// cluster pulls. source-controller formats an OCI tag revision as
|
|
// '<tag>@<digest>' and a digest revision as the bare '<digest>', so the
|
|
// manifest digest is the segment after the last '@'.
|
|
//
|
|
// A user-supplied explicit digest (ref.digest set without the operator's pin
|
|
// annotation) is left untouched: the operator pins only what a tag resolved to,
|
|
// or re-pins its own prior pin. A revision without a well-formed digest leaves
|
|
// the source following its tag rather than writing a malformed reference (the
|
|
// caller requires a non-empty art.Digest, so this is defensive).
|
|
func pinTapDigest(repo *sourcev1.OCIRepository, revision string) {
|
|
parts := strings.Split(revision, "@")
|
|
digest := parts[len(parts)-1]
|
|
if !manifestDigestRe.MatchString(digest) {
|
|
return
|
|
}
|
|
ref := repo.Spec.Reference
|
|
// A user-supplied explicit digest carries no matching operator pin
|
|
// annotation; never rewrite it.
|
|
if ref != nil && ref.Digest != "" && repo.Annotations[tapconst.PinnedDigestAnnotation] != ref.Digest {
|
|
return
|
|
}
|
|
if ref != nil && ref.Digest == digest && repo.Annotations[tapconst.PinnedDigestAnnotation] == digest {
|
|
return // already pinned to this digest
|
|
}
|
|
if ref == nil {
|
|
ref = &sourcev1.OCIRepositoryRef{}
|
|
repo.Spec.Reference = ref
|
|
}
|
|
ref.Digest = digest
|
|
if repo.Annotations == nil {
|
|
repo.Annotations = map[string]string{}
|
|
}
|
|
repo.Annotations[tapconst.PinnedDigestAnnotation] = digest
|
|
}
|
|
|
|
// failMaterialize records a materialization failure (e.g. a name collision with
|
|
// a core component) on the tap source so it surfaces on the Tap resource and the
|
|
// dashboard, and emits a warning Event. It deliberately does not stamp the
|
|
// materialized revision, so a corrected artifact (which carries a new revision)
|
|
// is retried; a collision does not otherwise self-resolve, so it returns without
|
|
// requeueing to avoid a hot loop.
|
|
func (r *TapMaterializerReconciler) failMaterialize(ctx context.Context, repo *sourcev1.OCIRepository, cause error) (ctrl.Result, error) {
|
|
if repo.Annotations == nil {
|
|
repo.Annotations = map[string]string{}
|
|
}
|
|
repo.Annotations[tapconst.MaterializeErrorAnnotation] = cause.Error()
|
|
if r.Recorder != nil {
|
|
r.Recorder.Event(repo, corev1.EventTypeWarning, "MaterializeFailed", cause.Error())
|
|
}
|
|
if err := r.Update(ctx, repo); err != nil {
|
|
return ctrl.Result{}, err
|
|
}
|
|
log.FromContext(ctx).Info("tap materialization blocked", "tap", repo.Name, "reason", cause.Error())
|
|
return ctrl.Result{}, nil
|
|
}
|
|
|
|
// deleteMaterialized removes every PackageSource materialized from the given tap
|
|
// source, and each one's tap-managed registration Package. A user's own Package
|
|
// (no tap label) is left in place.
|
|
func (r *TapMaterializerReconciler) deleteMaterialized(ctx context.Context, sourceName string) error {
|
|
return r.pruneMaterialized(ctx, sourceName, nil)
|
|
}
|
|
|
|
// pruneMaterialized deletes the PackageSources materialized from sourceName
|
|
// except those whose names are in keep (nil keep deletes all of them).
|
|
func (r *TapMaterializerReconciler) pruneMaterialized(ctx context.Context, sourceName string, keep map[string]bool) error {
|
|
var list cozyv1alpha1.PackageSourceList
|
|
if err := r.List(ctx, &list, client.MatchingLabels{tapconst.Label: "true"}); err != nil {
|
|
return err
|
|
}
|
|
for i := range list.Items {
|
|
ps := &list.Items[i]
|
|
if ps.Annotations[tapconst.SourceAnnotation] != sourceName || keep[ps.Name] {
|
|
continue
|
|
}
|
|
// Delete the registration Package BEFORE the PackageSource. If the
|
|
// Package delete fails transiently, the PackageSource is still present so
|
|
// the next reconcile lists it and retries; deleting the PackageSource
|
|
// first would leave a stranded Package no later pass can find.
|
|
if err := r.deleteRegistrationPackage(ctx, ps.Name, sourceName); err != nil {
|
|
return err
|
|
}
|
|
if err := r.Delete(ctx, ps); err != nil && client.IgnoreNotFound(err) != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// deleteRegistrationPackage removes the tap-managed registration Package for a
|
|
// PackageSource, but only when it is a managed registration of the given source
|
|
// (owned AND still at the auto default variant), so a Package a later tap created
|
|
// under a reused name, or one a user pinned to a variant, is never deleted by an
|
|
// earlier tap's teardown.
|
|
func (r *TapMaterializerReconciler) deleteRegistrationPackage(ctx context.Context, name, sourceName string) error {
|
|
// Delete only a Package this source still manages, leaving a user's own or
|
|
// pinned Package (markers shed) in place. Teardown races the handover that
|
|
// `cozypkg add --allow-privileged` performs (an Update that sheds the tap
|
|
// markers): were it to land between the Get and the Delete, a plain
|
|
// owned-delete would remove the just-confirmed user Package. Re-read and
|
|
// re-classify on a conflict, and delete only the exact object classified via a
|
|
// UID + ResourceVersion precondition, the same guard the untap and dashboard
|
|
// delete paths use.
|
|
for attempt := 0; ; attempt++ {
|
|
var pkg cozyv1alpha1.Package
|
|
if err := r.Get(ctx, types.NamespacedName{Name: name}, &pkg); err != nil {
|
|
return client.IgnoreNotFound(err)
|
|
}
|
|
if !collision.ManagedRegistration(&pkg, sourceName, pkg.Spec.Variant) {
|
|
return nil
|
|
}
|
|
err := r.Delete(ctx, &pkg, client.Preconditions{UID: &pkg.UID, ResourceVersion: &pkg.ResourceVersion})
|
|
if err == nil || apierrors.IsNotFound(err) {
|
|
return nil
|
|
}
|
|
if apierrors.IsConflict(err) && attempt < 4 {
|
|
continue
|
|
}
|
|
return err
|
|
}
|
|
}
|
|
|
|
// resolveArtifactURL rewrites a cluster-DNS artifact host (e.g.
|
|
// flux.cozy-fluxcd.svc) to the backing Service's ClusterIP, so the hostNetwork
|
|
// operator pod can reach it without CoreDNS. It fails open: any parse/lookup
|
|
// problem (non-cluster host, Service missing, headless/empty ClusterIP) returns
|
|
// the original URL unchanged, so this only ever adds a reachable path.
|
|
//
|
|
// Failing open here is safe ONLY because integrity is enforced downstream:
|
|
// verifyAndExtract rejects an absent or mismatched digest (see
|
|
// tapmaterializer_artifact.go). Resolving to the wrong host therefore yields a
|
|
// digest-mismatch error, never a silent content substitution. If the digest
|
|
// handling is ever relaxed, this fail-open must be revisited.
|
|
func (r *TapMaterializerReconciler) resolveArtifactURL(ctx context.Context, rawURL string) string {
|
|
logger := log.FromContext(ctx)
|
|
u, err := neturl.Parse(rawURL)
|
|
if err != nil {
|
|
return rawURL
|
|
}
|
|
svc, ns, ok := parseClusterServiceHost(u.Hostname())
|
|
if !ok {
|
|
return rawURL
|
|
}
|
|
var service corev1.Service
|
|
if err := r.Get(ctx, types.NamespacedName{Name: svc, Namespace: ns}, &service); err != nil {
|
|
logger.V(1).Info("artifact host not resolved to ClusterIP; using DNS name", "host", u.Hostname(), "err", err.Error())
|
|
return rawURL
|
|
}
|
|
ip := service.Spec.ClusterIP
|
|
if ip == "" || ip == corev1.ClusterIPNone {
|
|
return rawURL
|
|
}
|
|
rewritten, err := rewriteURLHost(rawURL, ip)
|
|
if err != nil {
|
|
return rawURL
|
|
}
|
|
return rewritten
|
|
}
|
|
|
|
// parseClusterServiceHost recognises an in-cluster Service DNS name of the form
|
|
// <service>.<namespace>.svc or <service>.<namespace>.svc.cluster.local and
|
|
// returns the service and namespace.
|
|
func parseClusterServiceHost(host string) (service, namespace string, ok bool) {
|
|
host = strings.TrimSuffix(host, ".")
|
|
labels := strings.Split(host, ".")
|
|
// <svc>.<ns>.svc[.cluster.local]
|
|
if len(labels) < 3 || labels[2] != "svc" {
|
|
return "", "", false
|
|
}
|
|
if len(labels) > 3 {
|
|
rest := strings.Join(labels[3:], ".")
|
|
if rest != "cluster.local" {
|
|
return "", "", false
|
|
}
|
|
}
|
|
if labels[0] == "" || labels[1] == "" {
|
|
return "", "", false
|
|
}
|
|
return labels[0], labels[1], true
|
|
}
|
|
|
|
// rewriteURLHost replaces the host of rawURL with newHost, preserving scheme,
|
|
// port, path and query.
|
|
func rewriteURLHost(rawURL, newHost string) (string, error) {
|
|
u, err := neturl.Parse(rawURL)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if port := u.Port(); port != "" {
|
|
u.Host = net.JoinHostPort(newHost, port)
|
|
} else {
|
|
u.Host = newHost
|
|
}
|
|
return u.String(), nil
|
|
}
|
|
|
|
// httpFetch downloads a Flux artifact tarball, bounded in size and time.
|
|
func httpFetch(ctx context.Context, url string) ([]byte, error) {
|
|
ctx, cancel := context.WithTimeout(ctx, 2*time.Minute)
|
|
defer cancel()
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
resp, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer func() { _ = resp.Body.Close() }()
|
|
if resp.StatusCode != http.StatusOK {
|
|
return nil, fmt.Errorf("unexpected status %s fetching %s", resp.Status, url)
|
|
}
|
|
return io.ReadAll(io.LimitReader(resp.Body, tapFetchLimit+1))
|
|
}
|
|
|
|
// SetupWithManager wires the reconciler to community-tap OCIRepositories only.
|
|
func (r *TapMaterializerReconciler) SetupWithManager(mgr ctrl.Manager) error {
|
|
isTap := predicate.NewPredicateFuncs(func(o client.Object) bool {
|
|
return o.GetLabels()[tapconst.Label] == "true"
|
|
})
|
|
return ctrl.NewControllerManagedBy(mgr).
|
|
Named("cozystack-tap-materializer").
|
|
For(&sourcev1.OCIRepository{}, builder.WithPredicates(isTap)).
|
|
Complete(r)
|
|
}
|