Skip to content
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

refactor(controller): rework annotation mechanics #1856

Merged
merged 18 commits into from
Apr 18, 2024
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
87 changes: 82 additions & 5 deletions api/v1alpha1/annotations.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
package v1alpha1

const (
AnnotationKeyDescription = "kargo.akuity.io/description"
import "encoding/json"

const (
// AnnotationKeyRefresh is an annotation key that can be set on a resource
// to trigger a refresh of the resource by the controller. The value of the
// annotation is interpreted as a token, and any change to the value of the
// annotation should trigger a reconciliation of the resource.
AnnotationKeyRefresh = "kargo.akuity.io/refresh"

AnnotationKeyReverify = "kargo.akuity.io/reverify"
AnnotationKeyReverifyActor = "kargo.akuity.io/reverify-actor"
AnnotationKeyAbort = "kargo.akuity.io/abort"
// AnnotationKeyReverify is an annotation key that can be set on a Stage
// resource to trigger the re-verification of its Freight. The value of the
// annotation should either be the ID of the verification to be reverified,
// or a JSON object with the structure of the VerificationRequest.
AnnotationKeyReverify = "kargo.akuity.io/reverify"

// AnnotationKeyAbort is an annotation key that can be set on a Stage
// resource to abort the verification of its Freight. The value of the
// annotation must be set to the identifier of the verification to be
// aborted.
AnnotationKeyAbort = "kargo.akuity.io/abort"

AnnotationKeyDescription = "kargo.akuity.io/description"

AnnotationKeyOIDCEmails = "rbac.kargo.akuity.io/email"
AnnotationKeyOIDCGroups = "rbac.kargo.akuity.io/groups"
Expand All @@ -29,3 +43,66 @@
AnnotationKeyEventVerificationStartTime = "event.kargo.akuity.io/verification-start-time"
AnnotationKeyEventVerificationFinishTime = "event.kargo.akuity.io/verification-finish-time"
)

// RefreshAnnotationValue returns the value of the AnnotationKeyRefresh
// annotation which can be used to detect changes, and a boolean indicating
// whether the annotation was present.
func RefreshAnnotationValue(annotations map[string]string) (string, bool) {
requested, ok := annotations[AnnotationKeyRefresh]
return requested, ok
}

// ReverifyAnnotationValue returns the value of the AnnotationKeyReverify
// annotation, which can be used to determine whether the verification of a
// Freight should be rerun, and a boolean indicating whether the annotation was
// present.
//
// If the value of the annotation is a valid JSON object, it is unmarshalled
// into a VerificationRequest struct. Otherwise, the value is treated as the ID
// of the verification to be reverified and set as the ID field of the returned
// VerificationRequest.
func ReverifyAnnotationValue(annotations map[string]string) (*VerificationRequest, bool) {
requested, ok := annotations[AnnotationKeyReverify]
if !ok {
return nil, ok
}
var vr VerificationRequest
if b := []byte(requested); json.Valid(b) {
if err := json.Unmarshal(b, &vr); err != nil {
return nil, false

Check warning on line 72 in api/v1alpha1/annotations.go

View check run for this annotation

Codecov / codecov/patch

api/v1alpha1/annotations.go#L72

Added line #L72 was not covered by tests
}
} else {
vr.ID = requested
}
if !vr.HasID() {
return nil, false

Check warning on line 78 in api/v1alpha1/annotations.go

View check run for this annotation

Codecov / codecov/patch

api/v1alpha1/annotations.go#L78

Added line #L78 was not covered by tests
}
return &vr, ok
}

// AbortAnnotationValue returns the value of the AnnotationKeyAbort annotation
// which can be used to abort the verification of a Freight, and a boolean
// indicating whether the annotation was present.
//
// If the value of the annotation is a valid JSON object, it is unmarshalled
// into a VerificationRequest struct. Otherwise, the value is treated as the ID
// of the verification to be aborted and set as the ID field of the returned
// VerificationRequest.
func AbortAnnotationValue(annotations map[string]string) (*VerificationRequest, bool) {
requested, ok := annotations[AnnotationKeyAbort]
if !ok {
return nil, ok
}
var vr VerificationRequest
if b := []byte(requested); json.Valid(b) {
if err := json.Unmarshal(b, &vr); err != nil {
return nil, false

Check warning on line 99 in api/v1alpha1/annotations.go

View check run for this annotation

Codecov / codecov/patch

api/v1alpha1/annotations.go#L99

Added line #L99 was not covered by tests
}
} else {
vr.ID = requested
}
if !vr.HasID() {
return nil, false
}
return &vr, ok
}
95 changes: 95 additions & 0 deletions api/v1alpha1/annotations_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package v1alpha1

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestRefreshAnnotationValue(t *testing.T) {
t.Run("has refresh annotation", func(t *testing.T) {
result, ok := RefreshAnnotationValue(map[string]string{
AnnotationKeyRefresh: "foo",
})
require.True(t, ok)
require.Equal(t, "foo", result)
})

t.Run("does not have refresh annotation", func(t *testing.T) {
result, ok := RefreshAnnotationValue(nil)
require.False(t, ok)
require.Empty(t, result)
})

t.Run("has refresh annotation with empty value", func(t *testing.T) {
result, ok := RefreshAnnotationValue(map[string]string{
AnnotationKeyRefresh: "",
})
require.True(t, ok)
require.Empty(t, result)
})
}

func TestReverifyAnnotationValue(t *testing.T) {
t.Run("has reverify annotation with valid JSON", func(t *testing.T) {
result, ok := ReverifyAnnotationValue(map[string]string{
AnnotationKeyReverify: `{"id":"foo"}`,
})
require.True(t, ok)
require.Equal(t, "foo", result.ID)
})

t.Run("has reverify annotation with ID string", func(t *testing.T) {
result, ok := ReverifyAnnotationValue(map[string]string{
AnnotationKeyReverify: "foo",
})
require.True(t, ok)
require.Equal(t, "foo", result.ID)
})

t.Run("does not have reverify annotation", func(t *testing.T) {
result, ok := ReverifyAnnotationValue(nil)
require.False(t, ok)
require.Nil(t, result)
})

t.Run("has reverify annotation with empty ID", func(t *testing.T) {
result, ok := ReverifyAnnotationValue(map[string]string{
AnnotationKeyAbort: "",
})
require.False(t, ok)
require.Nil(t, result)
})
}

func TestAbortAnnotationValue(t *testing.T) {
t.Run("has abort annotation with valid JSON", func(t *testing.T) {
result, ok := AbortAnnotationValue(map[string]string{
AnnotationKeyAbort: `{"id":"foo"}`,
})
require.True(t, ok)
require.Equal(t, "foo", result.ID)
})

t.Run("has abort annotation with ID string", func(t *testing.T) {
result, ok := AbortAnnotationValue(map[string]string{
AnnotationKeyAbort: "foo",
})
require.True(t, ok)
require.Equal(t, "foo", result.ID)
})

t.Run("does not have abort annotation", func(t *testing.T) {
result, ok := AbortAnnotationValue(nil)
require.False(t, ok)
require.Nil(t, result)
})

t.Run("has abort annotation with empty ID", func(t *testing.T) {
result, ok := AbortAnnotationValue(map[string]string{
AnnotationKeyAbort: "",
})
require.False(t, ok)
require.Nil(t, result)
})
}
Loading
Loading