Skip to content

Commit b655b0e

Browse files
committed
feat: add targetRef field to ZTunnel CRD
This adds a `targetRef` field to the ZTunnel CRD, allowing users to keep their ZTunnel config in sync with what they already defined in the `Istio` or `IstioRevision` CRD. Signed-off-by: Daniel Grimm <[email protected]>
1 parent eb2b8c1 commit b655b0e

18 files changed

+240
-97
lines changed

api/v1/istiorevisiontags_types.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ const (
2828
// IstioRevisionTagSpec defines the desired state of IstioRevisionTag
2929
type IstioRevisionTagSpec struct {
3030
// +kubebuilder:validation:Required
31-
TargetRef IstioRevisionTagTargetReference `json:"targetRef"`
31+
TargetRef TargetReference `json:"targetRef"`
3232
}
3333

34-
// IstioRevisionTagTargetReference can reference either Istio or IstioRevision objects in the cluster. In the case of referencing an Istio object, the Sail Operator will automatically update the reference to the Istio object's Active Revision.
35-
type IstioRevisionTagTargetReference struct {
34+
// TargetReference can reference either Istio or IstioRevision objects in the cluster. In the case of referencing an Istio object, the Sail Operator will automatically update the reference to the Istio object's Active Revision.
35+
type TargetReference struct {
3636
// Kind is the kind of the target resource.
3737
//
3838
// +kubebuilder:validation:MinLength=1

api/v1/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.

api/v1alpha1/ztunnel_types.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ type ZTunnelSpec struct {
5353
// Defines the values to be passed to the Helm charts when installing Istio ztunnel.
5454
// +operator-sdk:csv:customresourcedefinitions:type=spec,displayName="Helm Values"
5555
Values *v1.ZTunnelValues `json:"values,omitempty"`
56+
57+
// The Istio control plane that this ZTunnel instance is associated with. Values relevant for ZTunnel will be copied from the referenced control plane.
58+
TargetRef *v1.TargetReference `json:"targetRef,omitempty"`
5659
}
5760

5861
// ZTunnelStatus defines the observed state of ZTunnel
@@ -68,6 +71,9 @@ type ZTunnelStatus struct {
6871

6972
// Reports the current state of the object.
7073
State ZTunnelConditionReason `json:"state,omitempty"`
74+
75+
// IstioRevision stores the name of the referenced IstioRevision
76+
IstioRevision string `json:"istioRevision"`
7177
}
7278

7379
// GetCondition returns the condition of the specified type

api/v1alpha1/zz_generated.deepcopy.go

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

bundle/manifests/sailoperator.io_istiorevisiontags.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ spec:
6464
description: IstioRevisionTagSpec defines the desired state of IstioRevisionTag
6565
properties:
6666
targetRef:
67-
description: IstioRevisionTagTargetReference can reference either
68-
Istio or IstioRevision objects in the cluster. In the case of referencing
69-
an Istio object, the Sail Operator will automatically update the
70-
reference to the Istio object's Active Revision.
67+
description: TargetReference can reference either Istio or IstioRevision
68+
objects in the cluster. In the case of referencing an Istio object,
69+
the Sail Operator will automatically update the reference to the
70+
Istio object's Active Revision.
7171
properties:
7272
kind:
7373
description: Kind is the kind of the target resource.

bundle/manifests/sailoperator.io_ztunnels.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,25 @@ spec:
9393
- remote
9494
- stable
9595
type: string
96+
targetRef:
97+
description: The Istio control plane that this ZTunnel instance is
98+
associated with. Values relevant for ZTunnel will be copied from
99+
the referenced control plane.
100+
properties:
101+
kind:
102+
description: Kind is the kind of the target resource.
103+
maxLength: 253
104+
minLength: 1
105+
type: string
106+
name:
107+
description: Name is the name of the target resource.
108+
maxLength: 253
109+
minLength: 1
110+
type: string
111+
required:
112+
- kind
113+
- name
114+
type: object
96115
values:
97116
description: Defines the values to be passed to the Helm charts when
98117
installing Istio ztunnel.
@@ -2445,6 +2464,9 @@ spec:
24452464
type: string
24462465
type: object
24472466
type: array
2467+
istioRevision:
2468+
description: IstioRevision stores the name of the referenced IstioRevision
2469+
type: string
24482470
observedGeneration:
24492471
description: |-
24502472
ObservedGeneration is the most recent generation observed for this
@@ -2456,6 +2478,8 @@ spec:
24562478
state:
24572479
description: Reports the current state of the object.
24582480
type: string
2481+
required:
2482+
- istioRevision
24592483
type: object
24602484
type: object
24612485
x-kubernetes-validations:

chart/crds/sailoperator.io_istiorevisiontags.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ spec:
6464
description: IstioRevisionTagSpec defines the desired state of IstioRevisionTag
6565
properties:
6666
targetRef:
67-
description: IstioRevisionTagTargetReference can reference either
68-
Istio or IstioRevision objects in the cluster. In the case of referencing
69-
an Istio object, the Sail Operator will automatically update the
70-
reference to the Istio object's Active Revision.
67+
description: TargetReference can reference either Istio or IstioRevision
68+
objects in the cluster. In the case of referencing an Istio object,
69+
the Sail Operator will automatically update the reference to the
70+
Istio object's Active Revision.
7171
properties:
7272
kind:
7373
description: Kind is the kind of the target resource.

chart/crds/sailoperator.io_ztunnels.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,25 @@ spec:
9393
- remote
9494
- stable
9595
type: string
96+
targetRef:
97+
description: The Istio control plane that this ZTunnel instance is
98+
associated with. Values relevant for ZTunnel will be copied from
99+
the referenced control plane.
100+
properties:
101+
kind:
102+
description: Kind is the kind of the target resource.
103+
maxLength: 253
104+
minLength: 1
105+
type: string
106+
name:
107+
description: Name is the name of the target resource.
108+
maxLength: 253
109+
minLength: 1
110+
type: string
111+
required:
112+
- kind
113+
- name
114+
type: object
96115
values:
97116
description: Defines the values to be passed to the Helm charts when
98117
installing Istio ztunnel.
@@ -2445,6 +2464,9 @@ spec:
24452464
type: string
24462465
type: object
24472466
type: array
2467+
istioRevision:
2468+
description: IstioRevision stores the name of the referenced IstioRevision
2469+
type: string
24482470
observedGeneration:
24492471
description: |-
24502472
ObservedGeneration is the most recent generation observed for this
@@ -2456,6 +2478,8 @@ spec:
24562478
state:
24572479
description: Reports the current state of the object.
24582480
type: string
2481+
required:
2482+
- istioRevision
24592483
type: object
24602484
type: object
24612485
x-kubernetes-validations:

controllers/istiorevisiontag/istiorevisiontag_controller.go

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func (r *Reconciler) doReconcile(ctx context.Context, tag *v1.IstioRevisionTag)
102102
}
103103

104104
log.Info("Retrieving referenced IstioRevision for IstioRevisionTag")
105-
rev, err := r.getIstioRevision(ctx, tag.Spec.TargetRef)
105+
rev, err := revision.GetIstioRevisionFromTargetReference(ctx, r.Client, tag.Spec.TargetRef)
106106
if rev == nil || err != nil {
107107
return nil, err
108108
}
@@ -157,32 +157,6 @@ func (r *Reconciler) validate(ctx context.Context, tag *v1.IstioRevisionTag) err
157157
return nil
158158
}
159159

160-
func (r *Reconciler) getIstioRevision(ctx context.Context, ref v1.IstioRevisionTagTargetReference) (*v1.IstioRevision, error) {
161-
var revisionName string
162-
if ref.Kind == v1.IstioRevisionKind {
163-
revisionName = ref.Name
164-
} else if ref.Kind == v1.IstioKind {
165-
i := v1.Istio{}
166-
err := r.Client.Get(ctx, types.NamespacedName{Name: ref.Name}, &i)
167-
if err != nil {
168-
return nil, err
169-
}
170-
if i.Status.ActiveRevisionName == "" {
171-
return nil, reconciler.NewTransientError("referenced Istio has no active revision")
172-
}
173-
revisionName = i.Status.ActiveRevisionName
174-
} else {
175-
return nil, reconciler.NewValidationError("unknown targetRef.kind")
176-
}
177-
178-
rev := v1.IstioRevision{}
179-
err := r.Client.Get(ctx, types.NamespacedName{Name: revisionName}, &rev)
180-
if err != nil {
181-
return nil, err
182-
}
183-
return &rev, nil
184-
}
185-
186160
func (r *Reconciler) installHelmCharts(ctx context.Context, tag *v1.IstioRevisionTag, rev *v1.IstioRevision) error {
187161
ownerReference := metav1.OwnerReference{
188162
APIVersion: v1.GroupVersion.String(),
@@ -401,7 +375,7 @@ func (r *Reconciler) isRevisionTagReferencedByWorkloads(ctx context.Context, tag
401375
}
402376
}
403377

404-
rev, err := r.getIstioRevision(ctx, tag.Spec.TargetRef)
378+
rev, err := revision.GetIstioRevisionFromTargetReference(ctx, r.Client, tag.Spec.TargetRef)
405379
if err != nil {
406380
return false, err
407381
}

controllers/istiorevisiontag/istiorevisiontag_controller_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ func TestDetermineInUseCondition(t *testing.T) {
218218
Name: tagName,
219219
},
220220
Spec: v1.IstioRevisionTagSpec{
221-
TargetRef: v1.IstioRevisionTagTargetReference{
221+
TargetRef: v1.TargetReference{
222222
Kind: "IstioRevision",
223223
Name: rev.Name,
224224
},
@@ -299,7 +299,7 @@ func TestValidation(t *testing.T) {
299299
Name: "default",
300300
},
301301
Spec: v1.IstioRevisionTagSpec{
302-
TargetRef: v1.IstioRevisionTagTargetReference{},
302+
TargetRef: v1.TargetReference{},
303303
},
304304
},
305305
expectedErrMessage: "spec.targetRef not set",
@@ -312,7 +312,7 @@ func TestValidation(t *testing.T) {
312312
Name: "default",
313313
},
314314
Spec: v1.IstioRevisionTagSpec{
315-
TargetRef: v1.IstioRevisionTagTargetReference{
315+
TargetRef: v1.TargetReference{
316316
Kind: "IstioRevision",
317317
Name: revName,
318318
},

0 commit comments

Comments
 (0)