Skip to content

fix: Initialize TUF repository with user-provided PVC #1135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 48 additions & 47 deletions internal/controller/tuf/actions/tuf_init_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ import (
"github.com/securesign/operator/internal/labels"
"github.com/securesign/operator/internal/utils/kubernetes"
"github.com/securesign/operator/internal/utils/kubernetes/ensure"
"github.com/securesign/operator/internal/utils/kubernetes/job"
jobUtils "github.com/securesign/operator/internal/utils/kubernetes/job"
v2 "k8s.io/api/batch/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
apilabels "k8s.io/apimachinery/pkg/labels"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

Expand All @@ -41,61 +41,62 @@ func (i initJobAction) CanHandle(_ context.Context, instance *rhtasv1alpha1.Tuf)
}

func (i initJobAction) Handle(ctx context.Context, instance *rhtasv1alpha1.Tuf) *action.Result {
if instance.Spec.Pvc.Name != "" {
meta.SetStatusCondition(&instance.Status.Conditions, metav1.Condition{
Type: tufConstants.RepositoryCondition,
Status: metav1.ConditionTrue,
Reason: constants.Ready,
Message: "Using self-managed tuf repository.",
})
return i.StatusUpdate(ctx, instance)
jobLabels := labels.ForResource(tufConstants.ComponentName, tufConstants.InitJobName, instance.Name, instance.Status.PvcName)
initJobList := &v2.JobList{}
selector := apilabels.SelectorFromSet(jobLabels)
if err := kubernetes.FindByLabelSelector(ctx, i.Client, initJobList, instance.Namespace, selector.String()); err != nil {
return i.Error(ctx, err, instance)
}

if j, err := job.GetJob(ctx, i.Client, instance.Namespace, tufConstants.InitJobName); j != nil {
i.Logger.Info("Tuf tuf-repository-init is already present.", "Succeeded", j.Status.Succeeded, "Failures", j.Status.Failed)
if job.IsCompleted(*j) {
if !job.IsFailed(*j) {
meta.SetStatusCondition(&instance.Status.Conditions, metav1.Condition{
Type: tufConstants.RepositoryCondition,
Status: metav1.ConditionTrue,
Reason: constants.Ready,
Message: "tuf-repository-init job passed",
})
return i.StatusUpdate(ctx, instance)
} else {
err = fmt.Errorf("tuf-repository-init job failed")
meta.SetStatusCondition(&instance.Status.Conditions, metav1.Condition{
Type: tufConstants.RepositoryCondition,
Status: metav1.ConditionFalse,
Reason: constants.Failure,
Message: err.Error(),
})
return i.Error(ctx, reconcile.TerminalError(err), instance)
}
switch {
case len(initJobList.Items) > 1:
return i.Error(ctx, reconcile.TerminalError(fmt.Errorf("multiple %s jobs present", tufConstants.InitJobName)), instance)
case len(initJobList.Items) == 1:
return i.jobPresent(ctx, &initJobList.Items[0], instance)
default:
return i.ensureInitJob(ctx, jobLabels, instance)
}
}

func (i initJobAction) jobPresent(ctx context.Context, job *v2.Job, instance *rhtasv1alpha1.Tuf) *action.Result {
i.Logger.Info("Tuf tuf-repository-init is present.", "Succeeded", job.Status.Succeeded, "Failures", job.Status.Failed)
if jobUtils.IsCompleted(*job) {
if !jobUtils.IsFailed(*job) {
meta.SetStatusCondition(&instance.Status.Conditions, metav1.Condition{
Type: tufConstants.RepositoryCondition,
Status: metav1.ConditionTrue,
Reason: constants.Ready,
Message: "tuf-repository-init job passed",
})
return i.StatusUpdate(ctx, instance)
} else {
// job not completed yet
return i.Requeue()
err := fmt.Errorf("tuf-repository-init job failed")
meta.SetStatusCondition(&instance.Status.Conditions, metav1.Condition{
Type: tufConstants.RepositoryCondition,
Status: metav1.ConditionFalse,
Reason: constants.Failure,
Message: err.Error(),
})
return i.Error(ctx, reconcile.TerminalError(err), instance)
}
} else if client.IgnoreNotFound(err) != nil {
return i.Error(ctx, err, instance)

} else {
// job not completed yet
return i.Requeue()
}
}

l := labels.For(tufConstants.ComponentName, tufConstants.InitJobName, instance.Name)
pvc, err := kubernetes.GetPVC(ctx, i.Client, instance.Namespace, instance.Status.PvcName)
if err != nil {
return i.Error(ctx, fmt.Errorf("could not resolve PVC: %w", err), instance)
}
if _, err = kubernetes.CreateOrUpdate(ctx, i.Client,
func (i initJobAction) ensureInitJob(ctx context.Context, labels map[string]string, instance *rhtasv1alpha1.Tuf) *action.Result {

if _, err := kubernetes.CreateOrUpdate(ctx, i.Client,
&v2.Job{
ObjectMeta: metav1.ObjectMeta{
Name: tufConstants.InitJobName,
Namespace: instance.Namespace,
GenerateName: tufConstants.InitJobName + "-",
Namespace: instance.Namespace,
},
},
utils.EnsureTufInitJob(instance, tufConstants.RBACInitJobName, l),
ensure.ControllerReference[*v2.Job](pvc, i.Client),
ensure.Labels[*v2.Job](slices.Collect(maps.Keys(l)), l),
utils.EnsureTufInitJob(instance, tufConstants.RBACInitJobName, labels),
ensure.ControllerReference[*v2.Job](instance, i.Client),
ensure.Labels[*v2.Job](slices.Collect(maps.Keys(labels)), labels),
func(object *v2.Job) error {
ensure.SetProxyEnvs(object.Spec.Template.Spec.Containers)
return nil
Expand Down
20 changes: 15 additions & 5 deletions internal/controller/tuf/tuf_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
tufConstants "github.com/securesign/operator/internal/controller/tuf/constants"
"github.com/securesign/operator/internal/labels"
k8sTest "github.com/securesign/operator/internal/testing/kubernetes"
"github.com/securesign/operator/internal/utils/kubernetes"
apilabels "k8s.io/apimachinery/pkg/labels"

"github.com/securesign/operator/api/v1alpha1"
actions2 "github.com/securesign/operator/internal/controller/ctlog/actions"
Expand Down Expand Up @@ -166,14 +168,22 @@ var _ = Describe("TUF controller", func() {
})

By("Waiting until Tuf init job is created")
initJob := &batchv1.Job{}
Eventually(func() error {
e := suite.Client().Get(ctx, types.NamespacedName{Name: tufConstants.InitJobName, Namespace: namespace.Name}, initJob)
return e
}).Should(Not(HaveOccurred()))
found := &v1alpha1.Tuf{}
Eventually(func(g Gomega) string {
g.Expect(suite.Client().Get(ctx, typeNamespaceName, found)).Should(Succeed())
return found.Status.PvcName
}).ShouldNot(BeEmpty())
initJobList := &batchv1.JobList{}
Eventually(func() []batchv1.Job {
jobLabels := labels.ForResource(tufConstants.ComponentName, tufConstants.InitJobName, TufName, found.Status.PvcName)
selector := apilabels.SelectorFromSet(jobLabels)
Expect(kubernetes.FindByLabelSelector(ctx, suite.Client(), initJobList, TufNamespace, selector.String())).To(Succeed())
return initJobList.Items
}).Should(HaveLen(1))

By("Move to Job to completed")
// Workaround to succeed condition for Ready phase
initJob := &initJobList.Items[0]
initJob.Status.Conditions = []batchv1.JobCondition{
{Status: corev1.ConditionTrue, Type: batchv1.JobComplete, Reason: constants.Ready}}
Expect(suite.Client().Status().Update(ctx, initJob)).Should(Succeed())
Expand Down
9 changes: 8 additions & 1 deletion internal/controller/tuf/utils/tuf_init_job.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package utils

import (
"fmt"
"path/filepath"
"strings"

rhtasv1alpha1 "github.com/securesign/operator/api/v1alpha1"
"github.com/securesign/operator/internal/controller/tuf/constants"
Expand Down Expand Up @@ -63,7 +65,12 @@ func EnsureTufInitJob(instance *rhtasv1alpha1.Tuf, sa string, labels map[string]
container.Image = images.Registry.Get(images.Tuf)
env := kubernetes.FindEnvByNameOrCreate(container, "NAMESPACE")
env.Value = instance.Namespace
container.Args = args
container.Command = []string{"/bin/bash", "-c"}
container.Args = []string{
fmt.Sprintf("tuf-repo-init.sh %s; ", strings.Join(args, " ")) +
"exit_code=$?; " +
"if [ $exit_code -eq 2 ]; then exit 0; else exit $exit_code; fi",
}
container.VolumeMounts = []v1.VolumeMount{
{
Name: "tuf-secrets",
Expand Down
191 changes: 191 additions & 0 deletions test/e2e/securesign_1855_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
//go:build integration

package e2e

import (
"context"

"github.com/securesign/operator/internal/controller/tuf/constants"
"github.com/securesign/operator/internal/labels"
"github.com/securesign/operator/internal/utils/kubernetes"
"github.com/securesign/operator/internal/utils/kubernetes/job"
testSupportKubernetes "github.com/securesign/operator/test/e2e/support/kubernetes"
"github.com/securesign/operator/test/e2e/support/tas/tuf"
v3 "k8s.io/api/batch/v1"
"k8s.io/apimachinery/pkg/api/resource"
apilabels "k8s.io/apimachinery/pkg/labels"
"sigs.k8s.io/controller-runtime/pkg/client"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/securesign/operator/api/v1alpha1"
"github.com/securesign/operator/test/e2e/support"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const (
tufPvcName = "tuf-init-test"
)

var _ = Describe("Securesign tuf-repository init test", Ordered, func() {
cli, _ := support.CreateClient()
ctx := context.TODO()

var namespace *v1.Namespace
var s *v1alpha1.Tuf

AfterEach(func() {
if CurrentSpecReport().Failed() && support.IsCIEnvironment() {
support.DumpNamespace(ctx, cli, namespace.Name)
}
})

BeforeAll(func() {
namespace = support.CreateTestNamespace(ctx, cli)
DeferCleanup(func() {
_ = cli.Delete(ctx, namespace)
})
s = createInstance("test", namespace.Name)
})

Describe("Install with empty pre-created tuf volume", func() {
It("create mock secret", func() {
pub, priv, crt, err := support.CreateCertificates(false)
Expect(err).NotTo(HaveOccurred())
Expect(cli.Create(ctx, &v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: namespace.Name,
},
Data: map[string][]byte{"public": pub, "private": priv, "cert": crt},
})).To(Succeed())
})

It("Create tuf PVC and Tuf resource", func() {
Expect(cli.Create(ctx, &v1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: tufPvcName,
Namespace: namespace.Name,
},
Spec: v1.PersistentVolumeClaimSpec{
AccessModes: []v1.PersistentVolumeAccessMode{v1.ReadWriteOnce},
Resources: v1.VolumeResourceRequirements{
Requests: v1.ResourceList{
v1.ResourceStorage: resource.MustParse("1Gi"),
},
},
},
})).To(Succeed())
Expect(cli.Create(ctx, s)).To(Succeed())
})

It("Tuf is running", func() {
tuf.Verify(ctx, cli, namespace.Name, s.Name)
})

It("Tuf repository was initialized", func() {
verifyInitJob(ctx, cli, namespace.Name, s.Name, "Initializing empty repository")
})

It("Delete Tuf", func() {
name := s.Name
Expect(cli.Delete(ctx, s)).To(Succeed())
Eventually(cli.Get).WithArguments(ctx, client.ObjectKey{Name: name, Namespace: s.Namespace}, s).ShouldNot(Succeed())

Eventually(func(g Gomega) []v1.PersistentVolumeClaim {
pvcList := &v1.PersistentVolumeClaimList{}
g.Expect(cli.List(ctx, pvcList, client.InNamespace(namespace.Name))).To(Succeed())
return pvcList.Items
}).Should(HaveLen(1), "Tuf PVC retained")

Eventually(func(g Gomega) []v3.Job {
jobLabels := labels.ForResource(constants.ComponentName, constants.InitJobName, name, tufPvcName)
initJobList := &v3.JobList{}
selector := apilabels.SelectorFromSet(jobLabels)
g.Expect(kubernetes.FindByLabelSelector(ctx, cli, initJobList, namespace.Name, selector.String())).To(Succeed())
return initJobList.Items
}).Should(BeEmpty())
})

It("Create Tuf with already initialized repo", func() {
s = createInstance("test1", namespace.Name)
e := cli.Create(ctx, s)
Expect(e).To(Succeed())
tuf.Verify(ctx, cli, namespace.Name, s.Name)
verifyInitJob(ctx, cli, namespace.Name, s.Name, "Repo seems to already be initialized")
})

})
})

func createInstance(name, ns string) *v1alpha1.Tuf {
return &v1alpha1.Tuf{
ObjectMeta: metav1.ObjectMeta{
Namespace: ns,
Name: name,
},
Spec: v1alpha1.TufSpec{
Keys: []v1alpha1.TufKey{
{
Name: "rekor.pub",
SecretRef: &v1alpha1.SecretKeySelector{
LocalObjectReference: v1alpha1.LocalObjectReference{
Name: "test",
},
Key: "public",
},
},
{
Name: "ctfe.pub",
SecretRef: &v1alpha1.SecretKeySelector{
LocalObjectReference: v1alpha1.LocalObjectReference{
Name: "test",
},
Key: "public",
},
},
{
Name: "fulcio_v1.crt.pem",
SecretRef: &v1alpha1.SecretKeySelector{
LocalObjectReference: v1alpha1.LocalObjectReference{
Name: "test",
},
Key: "cert",
},
},
{
Name: "tsa.certchain.pem",
SecretRef: &v1alpha1.SecretKeySelector{
LocalObjectReference: v1alpha1.LocalObjectReference{
Name: "test",
},
Key: "cert",
},
},
},
Pvc: v1alpha1.TufPvc{
Name: tufPvcName,
},
},
}
}

func verifyInitJob(ctx context.Context, cli client.Client, ns, name, logOutput string) {
jobLabels := labels.ForResource(constants.ComponentName, constants.InitJobName, name, tufPvcName)
initJobList := &v3.JobList{}
selector := apilabels.SelectorFromSet(jobLabels)
Expect(kubernetes.FindByLabelSelector(ctx, cli, initJobList, ns, selector.String())).To(Succeed())

Expect(initJobList.Items).To(HaveLen(1))
Expect(job.IsCompleted(initJobList.Items[0])).To(BeTrue())
Expect(job.IsFailed(initJobList.Items[0])).To(BeFalse())

podList := &v1.PodList{}
Expect(kubernetes.FindByLabelSelector(ctx, cli, podList, ns, "job-name = "+initJobList.Items[0].Name)).To(Succeed())
Expect(podList.Items).To(HaveLen(1))

logs, err := testSupportKubernetes.GetPodLogs(ctx, podList.Items[0].Name, "tuf-init", ns)
Expect(err).NotTo(HaveOccurred())
Expect(logs).To(ContainSubstring(logOutput))
}
Loading