Skip to content

Commit

Permalink
Upgrade controller-runtime to v0.7.0
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Prodan <[email protected]>
  • Loading branch information
stefanprodan committed Dec 18, 2020
1 parent f0a3147 commit 5b81fb9
Show file tree
Hide file tree
Showing 12 changed files with 463 additions and 283 deletions.
4 changes: 2 additions & 2 deletions api/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ go 1.15

require (
github.com/fluxcd/pkg/apis/meta v0.5.0
github.com/fluxcd/pkg/runtime v0.4.0
github.com/fluxcd/pkg/runtime v0.5.1-0.20201217125143-9e1fe564d778
k8s.io/api v0.19.4
k8s.io/apimachinery v0.19.4
sigs.k8s.io/controller-runtime v0.6.4
sigs.k8s.io/controller-runtime v0.7.0
)
150 changes: 92 additions & 58 deletions api/go.sum

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions api/v1beta1/kustomization_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,9 @@ func (in *Kustomization) GetStatusConditions() *[]metav1.Condition {
}

const (
// SourceIndexKey is the key used for indexing kustomizations
// GitRepositoryIndexKey is the key used for indexing kustomizations
// based on their Git sources.
SourceIndexKey string = ".metadata.source"
GitRepositoryIndexKey string = ".metadata.git"
// BucketIndexKey is the key used for indexing kustomizations
// based on their S3 sources.
BucketIndexKey string = ".metadata.bucket"
Expand Down
8 changes: 4 additions & 4 deletions config/rbac/leader_election_role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ metadata:
name: leader-election-role
rules:
- apiGroups:
- ""
- coordination.k8s.io
resources:
- configmaps
- leases
verbs:
- get
- list
Expand All @@ -17,9 +17,9 @@ rules:
- patch
- delete
- apiGroups:
- ""
- coordination.k8s.io
resources:
- configmaps/status
- leases/status
verbs:
- get
- update
Expand Down
241 changes: 59 additions & 182 deletions controllers/kustomization_controller.go

Large diffs are not rendered by default.

139 changes: 139 additions & 0 deletions controllers/kustomization_indexers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
Copyright 2020 The Flux 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 controllers

import (
"context"
"fmt"

"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1beta1"
"github.com/fluxcd/pkg/runtime/dependency"
sourcev1 "github.com/fluxcd/source-controller/api/v1beta1"
)

func (r *KustomizationReconciler) requestsForGitRepositoryRevisionChange(obj client.Object) []reconcile.Request {
repo, ok := obj.(*sourcev1.GitRepository)
if !ok {
panic(fmt.Sprintf("Expected a GitRepository but got a %T", obj))
}
// If we do not have an artifact, we have no requests to make
if repo.GetArtifact() == nil {
return nil
}

ctx := context.Background()
var list kustomizev1.KustomizationList
if err := r.List(ctx, &list, client.MatchingFields{
kustomizev1.GitRepositoryIndexKey: ObjectKey(obj).String(),
}); err != nil {
return nil
}
var dd []dependency.Dependent
for _, d := range list.Items {
// If the revision of the artifact equals to the last attempted revision,
// we should not make a request for this Kustomization
if repo.GetArtifact().Revision == d.Status.LastAttemptedRevision {
continue
}
dd = append(dd, d)
}
sorted, err := dependency.Sort(dd)
if err != nil {
return nil
}
reqs := make([]reconcile.Request, len(sorted), len(sorted))
for i := range sorted {
reqs[i].NamespacedName.Name = sorted[i].Name
reqs[i].NamespacedName.Namespace = sorted[i].Namespace
}
return reqs
}

func (r *KustomizationReconciler) indexByGitRepository(o client.Object) []string {
k, ok := o.(*kustomizev1.Kustomization)
if !ok {
panic(fmt.Sprintf("Expected a Kustomization, got %T", o))
}

if k.Spec.SourceRef.Kind == sourcev1.GitRepositoryKind {
namespace := k.GetNamespace()
if k.Spec.SourceRef.Namespace != "" {
namespace = k.Spec.SourceRef.Namespace
}
return []string{fmt.Sprintf("%s/%s", namespace, k.Spec.SourceRef.Name)}
}

return nil
}

func (r *KustomizationReconciler) requestsForBucketRevisionChange(obj client.Object) []reconcile.Request {
bucket, ok := obj.(*sourcev1.Bucket)
if !ok {
panic(fmt.Sprintf("Expected a Bucket but got a %T", obj))
}
// If we do not have an artifact, we have no requests to make
if bucket.GetArtifact() == nil {
return nil
}

ctx := context.Background()
var list kustomizev1.KustomizationList
if err := r.List(ctx, &list, client.MatchingFields{
kustomizev1.BucketIndexKey: ObjectKey(obj).String(),
}); err != nil {
return nil
}
var dd []dependency.Dependent
for _, d := range list.Items {
// If the revision of the artifact equals to the last attempted revision,
// we should not make a request for this Kustomization
if bucket.GetArtifact().Revision == d.Status.LastAttemptedRevision {
continue
}
dd = append(dd, d)
}
sorted, err := dependency.Sort(dd)
if err != nil {
return nil
}
reqs := make([]reconcile.Request, len(sorted), len(sorted))
for i := range sorted {
reqs[i].NamespacedName.Name = sorted[i].Name
reqs[i].NamespacedName.Namespace = sorted[i].Namespace
}
return reqs
}

func (r *KustomizationReconciler) indexByBucket(o client.Object) []string {
k, ok := o.(*kustomizev1.Kustomization)
if !ok {
panic(fmt.Sprintf("Expected a Kustomization, got %T", o))
}

if k.Spec.SourceRef.Kind == sourcev1.BucketKind {
namespace := k.GetNamespace()
if k.Spec.SourceRef.Namespace != "" {
namespace = k.Spec.SourceRef.Namespace
}
return []string{fmt.Sprintf("%s/%s", namespace, k.Spec.SourceRef.Name)}
}

return nil
}
10 changes: 1 addition & 9 deletions controllers/source_predicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type SourceRevisionChangePredicate struct {
}

func (SourceRevisionChangePredicate) Update(e event.UpdateEvent) bool {
if e.MetaOld == nil || e.MetaNew == nil {
if e.ObjectOld == nil || e.ObjectNew == nil {
return false
}

Expand All @@ -53,11 +53,3 @@ func (SourceRevisionChangePredicate) Update(e event.UpdateEvent) bool {

return false
}

func (SourceRevisionChangePredicate) Create(e event.CreateEvent) bool {
return false
}

func (SourceRevisionChangePredicate) Delete(e event.DeleteEvent) bool {
return false
}
5 changes: 3 additions & 2 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ func TestAPIs(t *testing.T) {
}

var _ = BeforeSuite(func(done Done) {
logf.SetLogger(zap.LoggerTo(GinkgoWriter, true))
logf.SetLogger(
zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)),
)

By("bootstrapping test environment")
t := true
Expand Down Expand Up @@ -90,7 +92,6 @@ var _ = BeforeSuite(func(done Done) {

err = (&KustomizationReconciler{
Client: k8sManager.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("Kustomization"),
Scheme: scheme.Scheme,
EventRecorder: k8sManager.GetEventRecorderFor("kustomize-controller"),
ExternalEventRecorder: nil,
Expand Down
14 changes: 13 additions & 1 deletion controllers/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ limitations under the License.

package controllers

import "strings"
import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"strings"
)

// parseApplyOutput extracts the objects and the action
// performed by kubectl e.g.:
Expand Down Expand Up @@ -69,3 +73,11 @@ func containsString(slice []string, s string) bool {
}
return false
}

// ObjectKey returns client.ObjectKey for the object.
func ObjectKey(object metav1.Object) client.ObjectKey {
return client.ObjectKey{
Namespace: object.GetNamespace(),
Name: object.GetName(),
}
}
15 changes: 6 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ require (
github.com/cyphar/filepath-securejoin v0.2.2
github.com/fluxcd/kustomize-controller/api v0.5.2
github.com/fluxcd/pkg/apis/meta v0.5.0
github.com/fluxcd/pkg/runtime v0.4.0
github.com/fluxcd/pkg/runtime v0.5.1-0.20201217125143-9e1fe564d778
github.com/fluxcd/pkg/testserver v0.0.2
github.com/fluxcd/pkg/untar v0.0.5
github.com/fluxcd/source-controller/api v0.5.5
github.com/go-logr/logr v0.2.1
github.com/go-logr/logr v0.3.0
github.com/howeyc/gopass v0.0.0-20170109162249-bf9dde6d0d2c
github.com/onsi/ginkgo v1.12.1
github.com/onsi/gomega v1.10.1
github.com/onsi/ginkgo v1.14.1
github.com/onsi/gomega v1.10.2
github.com/spf13/pflag v1.0.5
go.mozilla.org/gopgagent v0.0.0-20170926210634-4d7ea76ff71a
go.mozilla.org/sops/v3 v3.6.1
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
Expand All @@ -26,11 +27,7 @@ require (
k8s.io/cli-runtime v0.19.4 // indirect
k8s.io/client-go v0.19.4
sigs.k8s.io/cli-utils v0.19.2
sigs.k8s.io/controller-runtime v0.6.4
sigs.k8s.io/controller-runtime v0.7.0
sigs.k8s.io/kustomize/api v0.7.0
sigs.k8s.io/yaml v1.2.0
)

// controller-runtime v0.6.4 bug:
// v1.ListOptions is not suitable for converting to \"helm.toolkit.fluxcd.io/v2beta1\" in scheme \"pkg/runtime/scheme.go:101
replace sigs.k8s.io/controller-runtime => sigs.k8s.io/controller-runtime v0.6.3
Loading

0 comments on commit 5b81fb9

Please sign in to comment.