Skip to content

Commit

Permalink
feat: use owner reference to link result ConfigMap with Job
Browse files Browse the repository at this point in the history
  • Loading branch information
osmman committed Mar 3, 2025
1 parent 0ce937c commit 0d33cbe
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 31 deletions.
22 changes: 14 additions & 8 deletions internal/controller/common/action/tree/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,10 @@ func (i resolveTree[T]) handleJob(ctx context.Context, instance T) *action.Resul
return i.Error(ctx, reconcile.TerminalError(fmt.Errorf("could not get configmap: %w", err)), instance)
}

if val, ok := configMap.GetAnnotations()[jobReferenceAnnotation]; ok && val != "" {
return i.Continue()
for _, ref := range configMap.GetOwnerReferences() {
if ref.Kind == "Job" {
return i.Continue()
}
}

trillianService := wrapped.GetTrillianService()
Expand Down Expand Up @@ -242,11 +244,10 @@ func (i resolveTree[T]) handleJob(ctx context.Context, instance T) *action.Resul
})
}

configMapAnnotations := map[string]string{
jobReferenceAnnotation: job.GetName(),
}
if _, err = kubernetes.CreateOrUpdate(ctx, i.Client, configMap,
ensure.Annotations[*corev1.ConfigMap](maps.Keys(configMapAnnotations), configMapAnnotations),
func(object *corev1.ConfigMap) error {
return controllerutil.SetOwnerReference(job, object, i.Client.Scheme())
},
); err != nil {
return i.Error(ctx, fmt.Errorf("could not update annotations on %s ConfigMap: %w", configMap.GetName(), err), instance,
metav1.Condition{
Expand All @@ -270,7 +271,6 @@ func (i resolveTree[T]) handleJob(ctx context.Context, instance T) *action.Resul
func (i resolveTree[T]) handleJobFinished(ctx context.Context, instance T) *action.Result {
var (
jobName string
ok bool
err error
)

Expand All @@ -283,7 +283,13 @@ func (i resolveTree[T]) handleJobFinished(ctx context.Context, instance T) *acti
return i.Error(ctx, reconcile.TerminalError(fmt.Errorf("could not get configmap: %w", err)), instance)
}

if jobName, ok = configMap.GetAnnotations()[jobReferenceAnnotation]; !ok || jobName == "" {
for _, ref := range configMap.GetOwnerReferences() {
if ref.Kind == "Job" {
jobName = ref.Name
break
}
}
if jobName == "" {
return i.Requeue()
}

Expand Down
38 changes: 21 additions & 17 deletions internal/controller/common/action/tree/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

. "github.com/onsi/gomega"
"github.com/onsi/gomega/gstruct"
testAction "github.com/securesign/operator/internal/testing/action"
v1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -220,8 +221,8 @@ func testCreateJob(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Name: nnResult.Name,
Namespace: nnResult.Namespace,
Annotations: map[string]string{
jobReferenceAnnotation: "job-name",
OwnerReferences: []metav1.OwnerReference{
{Kind: "Job", Name: "job-name"},
},
},
}
Expand Down Expand Up @@ -255,7 +256,10 @@ func testCreateJob(t *testing.T) {

cm := &corev1.ConfigMap{}
g.Expect(c.Get(ctx, nnResult, cm)).To(Succeed())
g.Expect(cm.Annotations[jobReferenceAnnotation]).To(Equal(jobName))
g.Expect(cm.GetOwnerReferences()).To(ContainElements(gstruct.MatchFields(gstruct.IgnoreExtras, gstruct.Fields{
"Kind": Equal("Job"),
"Name": Equal(jobName),
})))
},
},
},
Expand Down Expand Up @@ -303,8 +307,8 @@ func testMonitorJob(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Name: nnResult.Name,
Namespace: nnResult.Namespace,
Annotations: map[string]string{
jobReferenceAnnotation: "job",
OwnerReferences: []metav1.OwnerReference{
{Kind: "Job", Name: "job"},
},
},
}
Expand All @@ -323,8 +327,8 @@ func testMonitorJob(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Name: nnResult.Name,
Namespace: nnResult.Namespace,
Annotations: map[string]string{
jobReferenceAnnotation: "job",
OwnerReferences: []metav1.OwnerReference{
{Kind: "Job", Name: "job"},
},
},
}
Expand All @@ -351,8 +355,8 @@ func testMonitorJob(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Name: nnResult.Name,
Namespace: nnResult.Namespace,
Annotations: map[string]string{
jobReferenceAnnotation: "job",
OwnerReferences: []metav1.OwnerReference{
{Kind: "Job", Name: "job"},
},
},
}
Expand Down Expand Up @@ -389,8 +393,8 @@ func testMonitorJob(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Name: nnResult.Name,
Namespace: nnResult.Namespace,
Annotations: map[string]string{
jobReferenceAnnotation: "job",
OwnerReferences: []metav1.OwnerReference{
{Kind: "Job", Name: "job"},
},
},
}
Expand Down Expand Up @@ -442,8 +446,8 @@ func testExtractResult(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Name: nnResult.Name,
Namespace: nnResult.Namespace,
Annotations: map[string]string{
jobReferenceAnnotation: "job",
OwnerReferences: []metav1.OwnerReference{
{Kind: "Job", Name: "job"},
},
},
}
Expand All @@ -462,8 +466,8 @@ func testExtractResult(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Name: nnResult.Name,
Namespace: nnResult.Namespace,
Annotations: map[string]string{
jobReferenceAnnotation: "job",
OwnerReferences: []metav1.OwnerReference{
{Kind: "Job", Name: "job"},
},
},
Data: map[string]string{
Expand All @@ -487,8 +491,8 @@ func testExtractResult(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Name: nnResult.Name,
Namespace: nnResult.Namespace,
Annotations: map[string]string{
jobReferenceAnnotation: "job",
OwnerReferences: []metav1.OwnerReference{
{Kind: "Job", Name: "job"},
},
},
Data: map[string]string{
Expand Down
11 changes: 5 additions & 6 deletions internal/controller/common/action/tree/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ import (
var jobScript []byte

const (
RBACNameMask = "%s-createtree-job"
JobNameMask = "%s-createtree-job-"
JobCondition = "Tree"
configMapResultMask = "%s-%s-createtree-result"
configMapResultField = "tree_id"
jobReferenceAnnotation = "rhtas.redhat.com/createtree-job"
RBACNameMask = "%s-createtree-job"
JobNameMask = "%s-createtree-job-"
JobCondition = "Tree"
configMapResultMask = "%s-%s-createtree-result"
configMapResultField = "tree_id"
)

func Wrapper[T tlsAwareObject](getTree, getStatusTree func(T) *int64, setStatusTree func(T, *int64), getTrillianService func(T) *v1alpha1.TrillianService) func(T) *wrapper[T] {
Expand Down

0 comments on commit 0d33cbe

Please sign in to comment.