Skip to content

Commit 3de51e7

Browse files
author
Sanskar Jaiswal
committed
add support for cross-namespace sourceRef in ImageUpdateAutomation
ImageUpdateAutomation objects can now refer to GitRepository objects in other namespaces. Implemented by switching sourceRef from a SourceReference to a dependency.CrossNamespaceDependencyReference. Signed-off-by: Sanskar Jaiswal <[email protected]>
1 parent 524b603 commit 3de51e7

File tree

7 files changed

+325
-106
lines changed

7 files changed

+325
-106
lines changed

api/v1beta1/imageupdateautomation_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type ImageUpdateAutomationSpec struct {
2929
// SourceRef refers to the resource giving access details
3030
// to a git repository.
3131
// +required
32-
SourceRef SourceReference `json:"sourceRef"`
32+
SourceRef CrossNamespaceSourceReference `json:"sourceRef"`
3333
// GitSpec contains all the git-specific definitions. This is
3434
// technically optional, but in practice mandatory until there are
3535
// other kinds of source allowed.

api/v1beta1/reference.go

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2020 The Flux authors
2+
Copyright 2020, 2021 The Flux authors
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -16,20 +16,33 @@ limitations under the License.
1616

1717
package v1beta1
1818

19-
// SourceReference contains enough information to let you locate the
20-
// typed, referenced source object.
21-
type SourceReference struct {
22-
// API version of the referent
19+
import "fmt"
20+
21+
// CrossNamespaceSourceReference contains enough information to let you locate the
22+
// typed Kubernetes resource object at cluster level.
23+
type CrossNamespaceSourceReference struct {
24+
// API version of the referent.
2325
// +optional
2426
APIVersion string `json:"apiVersion,omitempty"`
2527

26-
// Kind of the referent
28+
// Kind of the referent.
2729
// +kubebuilder:validation:Enum=GitRepository
2830
// +kubebuilder:default=GitRepository
2931
// +required
3032
Kind string `json:"kind"`
3133

32-
// Name of the referent
34+
// Name of the referent.
3335
// +required
3436
Name string `json:"name"`
37+
38+
// Namespace of the referent, defaults to the namespace of the Kubernetes resource object that contains the reference.
39+
// +optional
40+
Namespace string `json:"namespace,omitempty"`
41+
}
42+
43+
func (s *CrossNamespaceSourceReference) String() string {
44+
if s.Namespace != "" {
45+
return fmt.Sprintf("%s/%s/%s", s.Kind, s.Namespace, s.Name)
46+
}
47+
return fmt.Sprintf("%s/%s", s.Kind, s.Name)
3548
}

api/v1beta1/zz_generated.deepcopy.go

Lines changed: 15 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/image.toolkit.fluxcd.io_imageupdateautomations.yaml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -645,16 +645,20 @@ spec:
645645
to a git repository.
646646
properties:
647647
apiVersion:
648-
description: API version of the referent
648+
description: API version of the referent.
649649
type: string
650650
kind:
651651
default: GitRepository
652-
description: Kind of the referent
652+
description: Kind of the referent.
653653
enum:
654654
- GitRepository
655655
type: string
656656
name:
657-
description: Name of the referent
657+
description: Name of the referent.
658+
type: string
659+
namespace:
660+
description: Namespace of the referent, defaults to the namespace
661+
of the Kubernetes resource object that contains the reference.
658662
type: string
659663
required:
660664
- kind

controllers/imageupdateautomation_controller.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,22 +162,27 @@ func (r *ImageUpdateAutomationReconciler) Reconcile(ctx context.Context, req ctr
162162
if kind := auto.Spec.SourceRef.Kind; kind != sourcev1.GitRepositoryKind {
163163
return failWithError(fmt.Errorf("source kind %q not supported", kind))
164164
}
165+
165166
gitSpec := auto.Spec.GitSpec
166167
if gitSpec == nil {
167168
return failWithError(fmt.Errorf("source kind %s neccessitates field .spec.git", sourcev1.GitRepositoryKind))
168169
}
169170

170171
var origin sourcev1.GitRepository
172+
gitRepoNamespace := req.Namespace
173+
if auto.Spec.SourceRef.Namespace != "" {
174+
gitRepoNamespace = auto.Spec.SourceRef.Namespace
175+
}
171176
originName := types.NamespacedName{
172177
Name: auto.Spec.SourceRef.Name,
173-
Namespace: auto.GetNamespace(),
178+
Namespace: gitRepoNamespace,
174179
}
175180
debuglog.Info("fetching git repository", "gitrepository", originName)
176181

177182
if err := r.Get(ctx, originName, &origin); err != nil {
178183
if client.IgnoreNotFound(err) == nil {
179184
imagev1.SetImageUpdateAutomationReadiness(&auto, metav1.ConditionFalse, imagev1.GitNotAvailableReason, "referenced git repository is missing")
180-
log.Error(err, "referenced git repository does not exist")
185+
log.Error(err, fmt.Sprintf("referenced git repository %s does not exist.", originName.String()))
181186
if err := r.patchStatus(ctx, req, auto.Status); err != nil {
182187
return ctrl.Result{Requeue: true}, err
183188
}

0 commit comments

Comments
 (0)