Skip to content

Commit ede5745

Browse files
authored
Merge pull request #129 from zvlb/main
Refactor. Add tests
2 parents b8cbda6 + d707ed4 commit ede5745

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+722
-250
lines changed

api/v1alpha1/listener_webhook.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func (l *Listener) ValidateDelete(ctx context.Context, cl client.Client) error {
4545
virtualServices := &VirtualServiceList{}
4646
listOpts := []client.ListOption{
4747
client.InNamespace(l.Namespace),
48-
client.MatchingFields{options.VirtualServiceListenerFeild: l.Name},
48+
client.MatchingFields{options.VirtualServiceListenerNameFeild: l.Name},
4949
}
5050
if err := cl.List(ctx, virtualServices, listOpts...); err != nil {
5151
return err

api/v1alpha1/virtualservice_methods.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package v1alpha1
1919
import (
2020
"context"
2121
"encoding/json"
22+
"strings"
2223

2324
"github.com/kaasops/envoy-xds-controller/pkg/errors"
2425
"github.com/kaasops/envoy-xds-controller/pkg/utils/hash"
@@ -48,6 +49,45 @@ func (vs *VirtualService) SetValid(ctx context.Context, cl client.Client) error
4849
return cl.Status().Update(ctx, vs.DeepCopy())
4950
}
5051

52+
func (vs *VirtualService) SetValidWithUsedSecrets(ctx context.Context, cl client.Client, secrets []string) error {
53+
if vs.Status.Valid != nil && *vs.Status.Valid {
54+
return nil
55+
}
56+
valid := true
57+
vs.Status.Valid = &valid
58+
vs.Status.Error = nil
59+
60+
err := vs.setUsedSecrets(secrets)
61+
if err != nil {
62+
return err
63+
}
64+
65+
return cl.Status().Update(ctx, vs.DeepCopy())
66+
}
67+
68+
func (vs *VirtualService) setUsedSecrets(secrets []string) error {
69+
usedSecrets := []ResourceRef{}
70+
71+
for _, s := range secrets {
72+
splitS := strings.Split(s, "/")
73+
74+
if len(splitS) != 2 {
75+
return errors.New("something go wrong, when trying to get secret namespace and name")
76+
}
77+
78+
usedSecret := ResourceRef{
79+
Name: splitS[1],
80+
Namespace: &splitS[0],
81+
}
82+
83+
usedSecrets = append(usedSecrets, usedSecret)
84+
}
85+
86+
vs.Status.UsedSecrets = usedSecrets
87+
88+
return nil
89+
}
90+
5191
func (vs *VirtualService) SetInvalid(ctx context.Context, cl client.Client) error {
5292
if vs.Status.Valid != nil && !*vs.Status.Valid {
5393
return nil

api/v1alpha1/virtualservice_types.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,15 @@ type TlsConfig struct {
5454
}
5555

5656
type ResourceRef struct {
57-
Name string `json:"name,omitempty"`
58-
// Namespace string `json:"namespace,omitempty"`
57+
Name string `json:"name,omitempty"`
58+
Namespace *string `json:"namespace,omitempty"`
5959
}
6060

6161
// VirtualServiceStatus defines the observed state of VirtualService
6262
type VirtualServiceStatus struct {
63-
Error *string `json:"error,omitempty"`
64-
Valid *bool `json:"valid,omitempty"`
63+
Error *string `json:"error,omitempty"`
64+
Valid *bool `json:"valid,omitempty"`
65+
UsedSecrets []ResourceRef `json:"usedSecrets,omitempty"`
6566

6667
LastAppliedHash *uint32 `json:"lastAppliedHash,omitempty"`
6768
}

api/v1alpha1/virtualservice_webhook.go

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ import (
2929
"github.com/kaasops/envoy-xds-controller/pkg/options"
3030
"github.com/kaasops/envoy-xds-controller/pkg/utils"
3131
"github.com/kaasops/envoy-xds-controller/pkg/utils/k8s"
32-
corev1 "k8s.io/api/core/v1"
33-
api_errors "k8s.io/apimachinery/pkg/api/errors"
32+
3433
"k8s.io/apimachinery/pkg/types"
3534
"k8s.io/client-go/discovery"
3635
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -193,7 +192,7 @@ func (vs *VirtualService) checkIfDomainAlredyExist(
193192
virtualServices := &VirtualServiceList{}
194193
listOpts := []client.ListOption{
195194
client.InNamespace(vs.Namespace),
196-
client.MatchingFields{options.VirtualServiceListenerFeild: vs.Spec.Listener.Name},
195+
client.MatchingFields{options.VirtualServiceListenerNameFeild: vs.Spec.Listener.Name},
197196
}
198197
if err := cl.List(ctx, virtualServices, listOpts...); err != nil {
199198
return err
@@ -236,11 +235,19 @@ func (tc *TlsConfig) Validate(
236235
return errors.Wrap(err, "cannot get TlsConfig Type")
237236
}
238237

238+
// If Watch Namespaces set - try to found secret in all namespaces
239+
namespaces := cfg.GetWatchNamespaces()
240+
239241
switch tlsType {
240242
case SecretRefType:
241-
return validateSecretRef(ctx, client, vs.Namespace, tc.SecretRef)
243+
// If .Spec.TlsConfig.SecretRef.Namespace set - find secret only in this namespace
244+
if vs.Spec.TlsConfig.SecretRef.Namespace != nil {
245+
namespaces = []string{*vs.Spec.TlsConfig.SecretRef.Namespace}
246+
}
247+
248+
return validateSecretRef(ctx, client, namespaces, tc.SecretRef)
242249
case AutoDiscoveryType:
243-
return validateAutoDiscovery(ctx, vs, client)
250+
return validateAutoDiscovery(ctx, vs, namespaces, client)
244251
}
245252

246253
return errors.New("unexpected behavior when processing a TlsConfig Type")
@@ -249,31 +256,37 @@ func (tc *TlsConfig) Validate(
249256
func validateSecretRef(
250257
ctx context.Context,
251258
client client.Client,
252-
namespace string,
259+
namespaces []string,
253260
rr *ResourceRef,
254261
) error {
255-
secret := &corev1.Secret{}
256-
257-
err := client.Get(ctx, types.NamespacedName{Name: rr.Name, Namespace: namespace}, secret)
262+
secrets, err := k8s.GetCertificateSecrets(ctx, client, namespaces)
258263
if err != nil {
259-
if api_errors.IsNotFound(err) {
260-
return errors.Wrap(err, fmt.Sprintf("Secret %s from .Spec.TlsConfig.SecretRef.Name not found", rr.Name))
261-
}
262264
return err
263265
}
264266

265-
if secret.Type != corev1.SecretTypeTLS {
266-
return errors.Newf("Secret %s is not a TLS secret", rr.Name)
267+
for _, secret := range secrets {
268+
if rr.Namespace == nil {
269+
if secret.Name == rr.Name {
270+
return nil
271+
}
272+
} else {
273+
if secret.Namespace == *rr.Namespace {
274+
if secret.Name == rr.Name {
275+
return nil
276+
}
277+
}
278+
}
267279
}
268280

269-
// TODO (may be). Add check Certificate in secret have VS domain in DNS
281+
return errors.New(fmt.Sprintf("Secret %s/%s from .Spec.TlsConfig.SecretRef not found", *rr.Namespace, rr.Name))
270282

271-
return nil
283+
// TODO (may be). Add check Certificate in secret have VS domain in DNS
272284
}
273285

274286
func validateAutoDiscovery(
275287
ctx context.Context,
276288
vs *VirtualService,
289+
namespaces []string,
277290
client client.Client,
278291
) error {
279292
// Get Virtual Host from Virtual Service
@@ -283,7 +296,7 @@ func validateAutoDiscovery(
283296
}
284297

285298
// Create index for fast search certificate for domain
286-
index, err := k8s.IndexCertificateSecrets(ctx, client, vs.Namespace)
299+
index, err := k8s.IndexCertificateSecrets(ctx, client, namespaces)
287300
if err != nil {
288301
return errors.Wrap(err, "cannot generate TLS certificates index from Kubernetes secrets")
289302
}

api/v1alpha1/zz_generated.deepcopy.go

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

config/crd/bases/envoy.kaasops.io_virtualservices.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,25 @@ spec:
5757
properties:
5858
name:
5959
type: string
60+
namespace:
61+
type: string
6062
type: object
6163
additionalHttpFilters:
6264
items:
6365
properties:
6466
name:
6567
type: string
68+
namespace:
69+
type: string
6670
type: object
6771
type: array
6872
additionalRoutes:
6973
items:
7074
properties:
7175
name:
7276
type: string
77+
namespace:
78+
type: string
7379
type: object
7480
type: array
7581
httpFilters:
@@ -82,6 +88,8 @@ spec:
8288
properties:
8389
name:
8490
type: string
91+
namespace:
92+
type: string
8593
type: object
8694
tlsConfig:
8795
properties:
@@ -92,6 +100,8 @@ spec:
92100
properties:
93101
name:
94102
type: string
103+
namespace:
104+
type: string
95105
type: object
96106
type: object
97107
upgradeConfigs:
@@ -117,6 +127,15 @@ spec:
117127
lastAppliedHash:
118128
format: int32
119129
type: integer
130+
usedSecrets:
131+
items:
132+
properties:
133+
name:
134+
type: string
135+
namespace:
136+
type: string
137+
type: object
138+
type: array
120139
valid:
121140
type: boolean
122141
type: object

controllers/listener_controller.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func (r *ListenerReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
109109
virtualServices := &v1alpha1.VirtualServiceList{}
110110
listOpts := []client.ListOption{
111111
client.InNamespace(req.Namespace),
112-
client.MatchingFields{options.VirtualServiceListenerFeild: req.Name},
112+
client.MatchingFields{options.VirtualServiceListenerNameFeild: req.Name},
113113
}
114114
if err = r.List(ctx, virtualServices, listOpts...); err != nil {
115115
return ctrl.Result{}, errors.Wrap(err, errors.GetFromKubernetesMessage)
@@ -118,7 +118,7 @@ func (r *ListenerReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
118118
listener.Name = getResourceName(req.Namespace, req.Name)
119119

120120
// Create HashMap for fast searching of certificates
121-
index, err := k8s.IndexCertificateSecrets(ctx, r.Client, instance.Namespace)
121+
index, err := k8s.IndexCertificateSecrets(ctx, r.Client, r.Config.GetWatchNamespaces())
122122
if err != nil {
123123
return ctrl.Result{}, errors.Wrap(err, "cannot generate TLS certificates index from Kubernetes secrets")
124124
}
@@ -194,6 +194,20 @@ func (r *ListenerReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
194194

195195
chains = append(chains, filterChains...)
196196

197+
// If for this vs used some secrets (with certificates), add this information to status
198+
if virtSvc.CertificatesWithDomains != nil {
199+
i := 0
200+
keys := make([]string, len(virtSvc.CertificatesWithDomains))
201+
for k := range virtSvc.CertificatesWithDomains {
202+
keys[i] = k
203+
i++
204+
}
205+
if err := vs.SetValidWithUsedSecrets(ctx, r.Client, keys); err != nil {
206+
errs = append(errs, err)
207+
}
208+
continue
209+
}
210+
197211
if err := vs.SetValid(ctx, r.Client); err != nil {
198212
errs = append(errs, err)
199213
}
@@ -202,9 +216,8 @@ func (r *ListenerReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
202216

203217
// Check errors
204218
if len(errs) != 0 {
205-
r.log.Error(nil, "FilterChain build errors")
206219
for _, e := range errs {
207-
r.log.Error(e, "")
220+
r.log.Error(e, "FilterChain build errors")
208221
}
209222

210223
// Stop working with this NodeID
@@ -258,7 +271,7 @@ func (r *ListenerReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
258271
// SetupWithManager sets up the controller with the Manager.
259272
func (r *ListenerReconciler) SetupWithManager(mgr ctrl.Manager) error {
260273
// Add listener name to index
261-
if err := mgr.GetFieldIndexer().IndexField(context.Background(), &v1alpha1.VirtualService{}, options.VirtualServiceListenerFeild, func(rawObject client.Object) []string {
274+
if err := mgr.GetFieldIndexer().IndexField(context.Background(), &v1alpha1.VirtualService{}, options.VirtualServiceListenerNameFeild, func(rawObject client.Object) []string {
262275
virtualService := rawObject.(*v1alpha1.VirtualService)
263276
// if listener feild is empty use default listener name as index
264277
if virtualService.Spec.Listener == nil {

helm/charts/envoy-xds-controller/Chart.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ type: application
1515
# This is the chart version. This version number should be incremented each time you make changes
1616
# to the chart and its templates, including the app version.
1717
# Versions are expected to follow Semantic Versioning (https://semver.org/)
18-
version: "0.55"
18+
version: "0.56"
1919

2020
# This is the version number of the application being deployed. This version number should be
2121
# incremented each time you make changes to the application. Versions are not expected to
2222
# follow Semantic Versioning. They should reflect the version the application is using.
2323
# It is recommended to use it with quotes.
24-
appVersion: "v0.1.43"
24+
appVersion: "v0.2.0"
2525

2626
home: https://github.com/kaasops/envoy-xds-controller
2727
sources:

0 commit comments

Comments
 (0)