Skip to content

Commit

Permalink
Move API v1alpha1 to v1alpha2
Browse files Browse the repository at this point in the history
The image automation part of the API has changed structure (see [1]),
and had a version bump from `v1alpha1` to `v1alpha2`. Since the types
here are also in `image.toolkit.fluxcd.io`, there will be less
complication if they also get a version bump even though they aren't
changing.

For the automation types, leaving the old version `v1alpha1`
definitions in place leads to Kubernetes trying to upgrade objects and
dropping most fields (which have changed location), so they have been
removed. That would not happen with the types here, but for
consistency I am removing the old API version here too.

[1] fluxcd/image-automation-controller#139

Signed-off-by: Michael Bridgen <[email protected]>
  • Loading branch information
squaremo committed Apr 19, 2021
1 parent 919d446 commit bb1e44c
Show file tree
Hide file tree
Showing 17 changed files with 69 additions and 69 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ manifests: controller-gen

# Generate API reference documentation
api-docs: gen-crd-api-reference-docs
$(API_REF_GEN) -api-dir=./api/v1alpha1 -config=./hack/api-docs/config.json -template-dir=./hack/api-docs/template -out-file=./docs/api/image-reflector.md
$(API_REF_GEN) -api-dir=./api/v1alpha2 -config=./hack/api-docs/config.json -template-dir=./hack/api-docs/template -out-file=./docs/api/image-reflector.md

# Run go mod tidy
tidy:
Expand Down
4 changes: 2 additions & 2 deletions PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ repo: github.com/fluxcd/image-reflector-controller
resources:
- group: image
kind: ImageRepository
version: v1alpha1
version: v1alpha2
- group: image
kind: ImagePolicy
version: v1alpha1
version: v1alpha2
version: "2"
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
30 changes: 15 additions & 15 deletions controllers/imagepolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import (
"github.com/fluxcd/pkg/runtime/events"
"github.com/fluxcd/pkg/runtime/metrics"

imagev1alpha1 "github.com/fluxcd/image-reflector-controller/api/v1alpha1"
imagev1 "github.com/fluxcd/image-reflector-controller/api/v1alpha2"
"github.com/fluxcd/image-reflector-controller/internal/policy"
)

Expand All @@ -64,7 +64,7 @@ type ImagePolicyReconciler struct {
func (r *ImagePolicyReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
reconcileStart := time.Now()

var pol imagev1alpha1.ImagePolicy
var pol imagev1.ImagePolicy
if err := r.Get(ctx, req.NamespacedName, &pol); err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
}
Expand All @@ -81,13 +81,13 @@ func (r *ImagePolicyReconciler) Reconcile(ctx context.Context, req ctrl.Request)
}
defer r.recordReadinessMetric(ctx, &pol)

var repo imagev1alpha1.ImageRepository
var repo imagev1.ImageRepository
if err := r.Get(ctx, types.NamespacedName{
Namespace: pol.Namespace,
Name: pol.Spec.ImageRepositoryRef.Name,
}, &repo); err != nil {
if client.IgnoreNotFound(err) == nil {
imagev1alpha1.SetImagePolicyReadiness(
imagev1.SetImagePolicyReadiness(
&pol,
metav1.ConditionFalse,
meta.DependencyNotReadyReason,
Expand All @@ -105,7 +105,7 @@ func (r *ImagePolicyReconciler) Reconcile(ctx context.Context, req ctrl.Request)
// if the image repo hasn't been scanned, don't bother
if repo.Status.CanonicalImageName == "" {
msg := "referenced ImageRepository has not been scanned yet"
imagev1alpha1.SetImagePolicyReadiness(
imagev1.SetImagePolicyReadiness(
&pol,
metav1.ConditionFalse,
meta.DependencyNotReadyReason,
Expand Down Expand Up @@ -142,7 +142,7 @@ func (r *ImagePolicyReconciler) Reconcile(ctx context.Context, req ctrl.Request)
}
}
if err != nil {
imagev1alpha1.SetImagePolicyReadiness(
imagev1.SetImagePolicyReadiness(
&pol,
metav1.ConditionFalse,
meta.ReconciliationFailedReason,
Expand All @@ -158,7 +158,7 @@ func (r *ImagePolicyReconciler) Reconcile(ctx context.Context, req ctrl.Request)
if latest == "" {
msg := "no image found for policy"
pol.Status.LatestImage = ""
imagev1alpha1.SetImagePolicyReadiness(
imagev1.SetImagePolicyReadiness(
&pol,
metav1.ConditionFalse,
meta.ReconciliationFailedReason,
Expand All @@ -174,7 +174,7 @@ func (r *ImagePolicyReconciler) Reconcile(ctx context.Context, req ctrl.Request)

msg := fmt.Sprintf("Latest image tag for '%s' resolved to: %s", repo.Spec.Image, latest)
pol.Status.LatestImage = repo.Spec.Image + ":" + latest
imagev1alpha1.SetImagePolicyReadiness(
imagev1.SetImagePolicyReadiness(
&pol,
metav1.ConditionTrue,
meta.ReconciliationSucceededReason,
Expand All @@ -192,17 +192,17 @@ func (r *ImagePolicyReconciler) Reconcile(ctx context.Context, req ctrl.Request)
func (r *ImagePolicyReconciler) SetupWithManager(mgr ctrl.Manager) error {
// index the policies by which image repo they point at, so that
// it's easy to list those out when an image repo changes.
if err := mgr.GetFieldIndexer().IndexField(context.Background(), &imagev1alpha1.ImagePolicy{}, imageRepoKey, func(obj client.Object) []string {
pol := obj.(*imagev1alpha1.ImagePolicy)
if err := mgr.GetFieldIndexer().IndexField(context.Background(), &imagev1.ImagePolicy{}, imageRepoKey, func(obj client.Object) []string {
pol := obj.(*imagev1.ImagePolicy)
return []string{pol.Spec.ImageRepositoryRef.Name}
}); err != nil {
return err
}

return ctrl.NewControllerManagedBy(mgr).
For(&imagev1alpha1.ImagePolicy{}).
For(&imagev1.ImagePolicy{}).
Watches(
&source.Kind{Type: &imagev1alpha1.ImageRepository{}},
&source.Kind{Type: &imagev1.ImageRepository{}},
handler.EnqueueRequestsFromMapFunc(r.imagePoliciesForRepository),
).
Complete(r)
Expand All @@ -212,7 +212,7 @@ func (r *ImagePolicyReconciler) SetupWithManager(mgr ctrl.Manager) error {

func (r *ImagePolicyReconciler) imagePoliciesForRepository(obj client.Object) []reconcile.Request {
ctx := context.Background()
var policies imagev1alpha1.ImagePolicyList
var policies imagev1.ImagePolicyList
if err := r.List(ctx, &policies, client.InNamespace(obj.GetNamespace()),
client.MatchingFields{imageRepoKey: obj.GetName()}); err != nil {
return nil
Expand All @@ -226,7 +226,7 @@ func (r *ImagePolicyReconciler) imagePoliciesForRepository(obj client.Object) []
}

// event emits a Kubernetes event and forwards the event to notification controller if configured
func (r *ImagePolicyReconciler) event(ctx context.Context, policy imagev1alpha1.ImagePolicy, severity, msg string) {
func (r *ImagePolicyReconciler) event(ctx context.Context, policy imagev1.ImagePolicy, severity, msg string) {
if r.EventRecorder != nil {
r.EventRecorder.Event(&policy, "Normal", severity, msg)
}
Expand All @@ -242,7 +242,7 @@ func (r *ImagePolicyReconciler) event(ctx context.Context, policy imagev1alpha1.
}
}

func (r *ImagePolicyReconciler) recordReadinessMetric(ctx context.Context, policy *imagev1alpha1.ImagePolicy) {
func (r *ImagePolicyReconciler) recordReadinessMetric(ctx context.Context, policy *imagev1.ImagePolicy) {
if r.MetricsRecorder == nil {
return
}
Expand Down
34 changes: 17 additions & 17 deletions controllers/imagerepository_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import (
"github.com/fluxcd/pkg/runtime/metrics"
"github.com/fluxcd/pkg/runtime/predicates"

imagev1alpha1 "github.com/fluxcd/image-reflector-controller/api/v1alpha1"
imagev1 "github.com/fluxcd/image-reflector-controller/api/v1alpha2"
)

// These are intended to match the keys used in e.g.,
Expand Down Expand Up @@ -90,7 +90,7 @@ func (r *ImageRepositoryReconciler) Reconcile(ctx context.Context, req ctrl.Requ
// is usually made explicit by _also_ returning
// `ctrl.Result{Requeue: true}`.

var imageRepo imagev1alpha1.ImageRepository
var imageRepo imagev1.ImageRepository
if err := r.Get(ctx, req.NamespacedName, &imageRepo); err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
}
Expand All @@ -101,7 +101,7 @@ func (r *ImageRepositoryReconciler) Reconcile(ctx context.Context, req ctrl.Requ

if imageRepo.Spec.Suspend {
msg := "ImageRepository is suspended, skipping reconciliation"
imagev1alpha1.SetImageRepositoryReadiness(
imagev1.SetImageRepositoryReadiness(
&imageRepo,
metav1.ConditionFalse,
meta.SuspendedReason,
Expand All @@ -128,10 +128,10 @@ func (r *ImageRepositoryReconciler) Reconcile(ctx context.Context, req ctrl.Requ

ref, err := name.ParseReference(imageRepo.Spec.Image)
if err != nil {
imagev1alpha1.SetImageRepositoryReadiness(
imagev1.SetImageRepositoryReadiness(
&imageRepo,
metav1.ConditionFalse,
imagev1alpha1.ImageURLInvalidReason,
imagev1.ImageURLInvalidReason,
err.Error(),
)
if err := r.Status().Update(ctx, &imageRepo); err != nil {
Expand Down Expand Up @@ -177,7 +177,7 @@ func (r *ImageRepositoryReconciler) Reconcile(ctx context.Context, req ctrl.Requ
return ctrl.Result{RequeueAfter: when}, nil
}

func (r *ImageRepositoryReconciler) scan(ctx context.Context, imageRepo *imagev1alpha1.ImageRepository, ref name.Reference) error {
func (r *ImageRepositoryReconciler) scan(ctx context.Context, imageRepo *imagev1.ImageRepository, ref name.Reference) error {
timeout := imageRepo.GetTimeout()
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
Expand All @@ -189,7 +189,7 @@ func (r *ImageRepositoryReconciler) scan(ctx context.Context, imageRepo *imagev1
Namespace: imageRepo.GetNamespace(),
Name: imageRepo.Spec.SecretRef.Name,
}, &authSecret); err != nil {
imagev1alpha1.SetImageRepositoryReadiness(
imagev1.SetImageRepositoryReadiness(
imageRepo,
metav1.ConditionFalse,
meta.ReconciliationFailedReason,
Expand All @@ -199,7 +199,7 @@ func (r *ImageRepositoryReconciler) scan(ctx context.Context, imageRepo *imagev1
}
auth, err := authFromSecret(authSecret, ref)
if err != nil {
imagev1alpha1.SetImageRepositoryReadiness(
imagev1.SetImageRepositoryReadiness(
imageRepo,
metav1.ConditionFalse,
meta.ReconciliationFailedReason,
Expand All @@ -219,7 +219,7 @@ func (r *ImageRepositoryReconciler) scan(ctx context.Context, imageRepo *imagev1
Namespace: imageRepo.GetNamespace(),
Name: imageRepo.Spec.CertSecretRef.Name,
}, &certSecret); err != nil {
imagev1alpha1.SetImageRepositoryReadiness(
imagev1.SetImageRepositoryReadiness(
imageRepo,
metav1.ConditionFalse,
meta.ReconciliationFailedReason,
Expand All @@ -238,7 +238,7 @@ func (r *ImageRepositoryReconciler) scan(ctx context.Context, imageRepo *imagev1

tags, err := remote.ListWithContext(ctx, ref.Context(), options...)
if err != nil {
imagev1alpha1.SetImageRepositoryReadiness(
imagev1.SetImageRepositoryReadiness(
imageRepo,
metav1.ConditionFalse,
meta.ReconciliationFailedReason,
Expand All @@ -253,7 +253,7 @@ func (r *ImageRepositoryReconciler) scan(ctx context.Context, imageRepo *imagev1
}

scanTime := metav1.Now()
imageRepo.Status.LastScanResult = &imagev1alpha1.ScanResult{
imageRepo.Status.LastScanResult = &imagev1.ScanResult{
TagCount: len(tags),
ScanTime: scanTime,
}
Expand All @@ -265,7 +265,7 @@ func (r *ImageRepositoryReconciler) scan(ctx context.Context, imageRepo *imagev1
imageRepo.Status.SetLastHandledReconcileRequest(token)
}

imagev1alpha1.SetImageRepositoryReadiness(
imagev1.SetImageRepositoryReadiness(
imageRepo,
metav1.ConditionTrue,
meta.ReconciliationSucceededReason,
Expand Down Expand Up @@ -311,7 +311,7 @@ func transportFromSecret(certSecret *corev1.Secret) (*http.Transport, error) {
// shouldScan takes an image repo and the time now, and says whether
// the repository should be scanned now, and how long to wait for the
// next scan.
func (r *ImageRepositoryReconciler) shouldScan(repo imagev1alpha1.ImageRepository, now time.Time) (bool, time.Duration, error) {
func (r *ImageRepositoryReconciler) shouldScan(repo imagev1.ImageRepository, now time.Time) (bool, time.Duration, error) {
scanInterval := repo.Spec.Interval.Duration

// never scanned; do it now
Expand Down Expand Up @@ -354,7 +354,7 @@ func (r *ImageRepositoryReconciler) shouldScan(repo imagev1alpha1.ImageRepositor

func (r *ImageRepositoryReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&imagev1alpha1.ImageRepository{}).
For(&imagev1.ImageRepository{}).
WithEventFilter(predicate.Or(predicate.GenerationChangedPredicate{}, predicates.ReconcileRequestedPredicate{})).
Complete(r)
}
Expand Down Expand Up @@ -387,7 +387,7 @@ func authFromSecret(secret corev1.Secret, ref name.Reference) (authn.Authenticat
}

// event emits a Kubernetes event and forwards the event to notification controller if configured
func (r *ImageRepositoryReconciler) event(ctx context.Context, repo imagev1alpha1.ImageRepository, severity, msg string) {
func (r *ImageRepositoryReconciler) event(ctx context.Context, repo imagev1.ImageRepository, severity, msg string) {
if r.EventRecorder != nil {
r.EventRecorder.Eventf(&repo, "Normal", severity, msg)
}
Expand All @@ -405,7 +405,7 @@ func (r *ImageRepositoryReconciler) event(ctx context.Context, repo imagev1alpha
}
}

func (r *ImageRepositoryReconciler) recordReadinessMetric(ctx context.Context, repo *imagev1alpha1.ImageRepository) {
func (r *ImageRepositoryReconciler) recordReadinessMetric(ctx context.Context, repo *imagev1.ImageRepository) {
if r.MetricsRecorder == nil {
return
}
Expand All @@ -425,7 +425,7 @@ func (r *ImageRepositoryReconciler) recordReadinessMetric(ctx context.Context, r
}
}

func (r *ImageRepositoryReconciler) recordSuspension(ctx context.Context, imageRepo imagev1alpha1.ImageRepository) {
func (r *ImageRepositoryReconciler) recordSuspension(ctx context.Context, imageRepo imagev1.ImageRepository) {
if r.MetricsRecorder == nil {
return
}
Expand Down
26 changes: 13 additions & 13 deletions controllers/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"

imagev1alpha1 "github.com/fluxcd/image-reflector-controller/api/v1alpha1"
imagev1 "github.com/fluxcd/image-reflector-controller/api/v1alpha2"
// +kubebuilder:scaffold:imports
)

Expand All @@ -51,8 +51,8 @@ var _ = Describe("ImagePolicy controller", func() {
versions := []string{"0.1.0", "0.1.1", "0.2.0", "1.0.0", "1.0.1", "1.0.2", "1.1.0-alpha"}
imgRepo := loadImages(registryServer, "test-semver-policy-"+randStringRunes(5), versions)

repo := imagev1alpha1.ImageRepository{
Spec: imagev1alpha1.ImageRepositorySpec{
repo := imagev1.ImageRepository{
Spec: imagev1.ImageRepositorySpec{
Interval: metav1.Duration{Duration: reconciliationInterval},
Image: imgRepo,
},
Expand Down Expand Up @@ -81,13 +81,13 @@ var _ = Describe("ImagePolicy controller", func() {
Name: "random-pol-" + randStringRunes(5),
Namespace: imageObjectName.Namespace,
}
pol := imagev1alpha1.ImagePolicy{
Spec: imagev1alpha1.ImagePolicySpec{
pol := imagev1.ImagePolicy{
Spec: imagev1.ImagePolicySpec{
ImageRepositoryRef: meta.LocalObjectReference{
Name: imageObjectName.Name,
},
Policy: imagev1alpha1.ImagePolicyChoice{
SemVer: &imagev1alpha1.SemVerPolicy{
Policy: imagev1.ImagePolicyChoice{
SemVer: &imagev1.SemVerPolicy{
Range: "1.0.x",
},
},
Expand Down Expand Up @@ -116,8 +116,8 @@ var _ = Describe("ImagePolicy controller", func() {
versions := []string{"xenial", "yakkety", "zesty", "artful", "bionic"}
imgRepo := loadImages(registryServer, "test-alphabetical-policy-"+randStringRunes(5), versions)

repo := imagev1alpha1.ImageRepository{
Spec: imagev1alpha1.ImageRepositorySpec{
repo := imagev1.ImageRepository{
Spec: imagev1.ImageRepositorySpec{
Interval: metav1.Duration{Duration: reconciliationInterval},
Image: imgRepo,
},
Expand Down Expand Up @@ -146,13 +146,13 @@ var _ = Describe("ImagePolicy controller", func() {
Name: "random-pol-" + randStringRunes(5),
Namespace: imageObjectName.Namespace,
}
pol := imagev1alpha1.ImagePolicy{
Spec: imagev1alpha1.ImagePolicySpec{
pol := imagev1.ImagePolicy{
Spec: imagev1.ImagePolicySpec{
ImageRepositoryRef: meta.LocalObjectReference{
Name: imageObjectName.Name,
},
Policy: imagev1alpha1.ImagePolicyChoice{
Alphabetical: &imagev1alpha1.AlphabeticalPolicy{},
Policy: imagev1.ImagePolicyChoice{
Alphabetical: &imagev1.AlphabeticalPolicy{},
},
},
}
Expand Down
Loading

0 comments on commit bb1e44c

Please sign in to comment.