-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat] Add OBJKey validating webhook to check for non-empty secret da…
…ta format with cluster resourceset
- Loading branch information
Showing
9 changed files
with
227 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
Copyright 2023 Akamai Technologies, Inc. | ||
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 v1alpha2 | ||
|
||
import ( | ||
"fmt" | ||
|
||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/apimachinery/pkg/util/validation/field" | ||
clusteraddonsv1 "sigs.k8s.io/cluster-api/exp/addons/api/v1beta1" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
logf "sigs.k8s.io/controller-runtime/pkg/log" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
) | ||
|
||
// log is for logging in this package. | ||
var linodeobjectstoragekeylog = logf.Log.WithName("linodeobjectstoragekey-resource") | ||
|
||
// SetupWebhookWithManager will setup the manager to manage the webhooks | ||
func (r *LinodeObjectStorageKey) SetupWebhookWithManager(mgr ctrl.Manager) error { | ||
return ctrl.NewWebhookManagedBy(mgr). | ||
For(r). | ||
Complete() | ||
} | ||
|
||
// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation. | ||
//+kubebuilder:webhook:path=/validate-infrastructure-cluster-x-k8s-io-v1alpha2-linodeobjectstoragekey,mutating=false,failurePolicy=fail,sideEffects=None,groups=infrastructure.cluster.x-k8s.io,resources=linodeobjectstoragekeys,verbs=create;update,versions=v1alpha2,name=vlinodeobjectstoragekey.kb.io,admissionReviewVersions=v1 | ||
|
||
var _ webhook.Validator = &LinodeObjectStorageKey{} | ||
|
||
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type | ||
func (r *LinodeObjectStorageKey) ValidateCreate() (admission.Warnings, error) { | ||
linodeobjectstoragekeylog.Info("validate create", "name", r.Name) | ||
|
||
return r.validateLinodeObjectStorageKey() | ||
} | ||
|
||
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type | ||
func (r *LinodeObjectStorageKey) ValidateUpdate(old runtime.Object) (admission.Warnings, error) { | ||
linodeobjectstoragekeylog.Info("validate update", "name", r.Name) | ||
|
||
return r.validateLinodeObjectStorageKey() | ||
} | ||
|
||
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type | ||
func (r *LinodeObjectStorageKey) ValidateDelete() (admission.Warnings, error) { | ||
return nil, nil | ||
} | ||
|
||
func (r *LinodeObjectStorageKey) validateLinodeObjectStorageKey() (admission.Warnings, error) { | ||
var errs field.ErrorList | ||
|
||
if r.Spec.SecretType == clusteraddonsv1.ClusterResourceSetSecretType && len(r.Spec.SecretDataFormat) == 0 { | ||
errs = append(errs, field.Invalid( | ||
field.NewPath("spec").Child("secretDataFormat"), | ||
r.Spec.SecretDataFormat, | ||
fmt.Sprintf("must not be empty with Secret type %s", clusteraddonsv1.ClusterResourceSetSecretType), | ||
)) | ||
} | ||
|
||
if len(errs) > 0 { | ||
return nil, apierrors.NewInvalid(schema.GroupKind{Group: "infrastructure.cluster.x-k8s.io", Kind: "LinodeObjectStorageKey"}, r.Name, errs) | ||
} | ||
|
||
return nil, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
Copyright 2023 Akamai Technologies, Inc. | ||
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 v1alpha2 | ||
|
||
import ( | ||
"errors" | ||
"strings" | ||
"testing" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
clusteraddonsv1 "sigs.k8s.io/cluster-api/exp/addons/api/v1beta1" | ||
) | ||
|
||
func TestValidateLinodeObjectStorageKey(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := []struct { | ||
name string | ||
spec LinodeObjectStorageKeySpec | ||
err error | ||
}{ | ||
{ | ||
name: "opaque", | ||
spec: LinodeObjectStorageKeySpec{ | ||
SecretType: corev1.SecretTypeOpaque, | ||
}, | ||
err: nil, | ||
}, | ||
{ | ||
name: "resourceset with empty secret data format", | ||
spec: LinodeObjectStorageKeySpec{ | ||
SecretType: clusteraddonsv1.ClusterResourceSetSecretType, | ||
SecretDataFormat: map[string]string{}, | ||
}, | ||
err: errors.New("must not be empty with Secret type"), | ||
}, | ||
{ | ||
name: "valid resourceset", | ||
spec: LinodeObjectStorageKeySpec{ | ||
SecretType: clusteraddonsv1.ClusterResourceSetSecretType, | ||
SecretDataFormat: map[string]string{ | ||
"file.yaml": "kind: Secret", | ||
}, | ||
}, | ||
err: nil, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
testcase := tt | ||
|
||
t.Run(testcase.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
key := LinodeObjectStorageKey{ | ||
Spec: testcase.spec, | ||
} | ||
|
||
_, err := key.validateLinodeObjectStorageKey() | ||
if err != nil { | ||
if testcase.err == nil { | ||
t.Fatal(err) | ||
} | ||
if errStr := testcase.err.Error(); !strings.Contains(err.Error(), errStr) { | ||
t.Errorf("error did not contain substring '%s'", errStr) | ||
} | ||
} else if testcase.err != nil { | ||
t.Fatal("expected an error") | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
config/crd/patches/cainjection_in_linodeobjectstoragekeys.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# The following patch adds a directive for certmanager to inject CA into the CRD | ||
apiVersion: apiextensions.k8s.io/v1 | ||
kind: CustomResourceDefinition | ||
metadata: | ||
annotations: | ||
cert-manager.io/inject-ca-from: CERTIFICATE_NAMESPACE/CERTIFICATE_NAME | ||
name: linodeobjectstoragekeys.infrastructure.cluster.x-k8s.io |
16 changes: 16 additions & 0 deletions
16
config/crd/patches/webhook_in_linodeobjectstoragekeys.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# The following patch enables a conversion webhook for the CRD | ||
apiVersion: apiextensions.k8s.io/v1 | ||
kind: CustomResourceDefinition | ||
metadata: | ||
name: linodeobjectstoragekeys.infrastructure.cluster.x-k8s.io | ||
spec: | ||
conversion: | ||
strategy: Webhook | ||
webhook: | ||
clientConfig: | ||
service: | ||
namespace: system | ||
name: webhook-service | ||
path: /convert | ||
conversionReviewVersions: | ||
- v1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters