-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add validating webhook for repository server CR (#2281)
* add validating webhook for repository server CR Signed-off-by: Amruta Kale <amruta.kale@veeam.com> * remove unncessary changes Signed-off-by: Amruta Kale <amruta.kale@veeam.com> * add comment for in values.yaml for validating webhook flag * Validating webhook can be enabled for versions < 1.25 Signed-off-by: Amruta Kale <amruta.kale@veeam.com> * add missing error messages * 1. move common utilities validatingwebhook pkgs 2. rename the filenames Signed-off-by: Amruta Kale <amruta.kale@veeam.com> * change names of the port in service.yaml Signed-off-by: Amruta Kale <amruta.kale@veeam.com> * move the validating webhook flag from repositoryservercontroller field to an independent field * fix custom validator logic * address cosmetic changes --------- Signed-off-by: Amruta Kale <amruta.kale@veeam.com>
- Loading branch information
1 parent
a52fa96
commit 19cc9a4
Showing
11 changed files
with
212 additions
and
14 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
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
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
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
File renamed without changes.
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,55 @@ | ||
// Copyright 2023 The Kanister 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 validatingwebhook | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/kanisterio/kanister/pkg/apis/cr/v1alpha1" | ||
"github.com/pkg/errors" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook" | ||
) | ||
|
||
type RepositoryServerValidator struct{} | ||
|
||
var _ webhook.CustomValidator = &RepositoryServerValidator{} | ||
|
||
//+kubebuilder:webhook:path=/validate/v1alpha1/repositoryserver,mutating=false,failurePolicy=fail,sideEffects=None,groups=cr.kanister.io,resources=repositoryservers,verbs=update,versions=v1alpha1,name=repositoryserver.cr.kanister.io,admissionReviewVersions=v1 | ||
|
||
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type | ||
func (r *RepositoryServerValidator) ValidateCreate(ctx context.Context, obj runtime.Object) error { | ||
return nil | ||
} | ||
|
||
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type | ||
func (r *RepositoryServerValidator) ValidateUpdate(ctx context.Context, old runtime.Object, new runtime.Object) error { | ||
oldrs, ook := old.(*v1alpha1.RepositoryServer) | ||
newrs, nok := new.(*v1alpha1.RepositoryServer) | ||
if !ook || !nok { | ||
return errors.New("Either updated object or the old object is not of type RepositoryServer.cr.kanister.io") | ||
} | ||
errMsg := fmt.Sprintf("RepositoryServer.cr.kanister.io \"%s\" is invalid: spec.repository.rootPath: Invalid value, Value is immutable", newrs.Name) | ||
if oldrs.Spec.Repository.RootPath != newrs.Spec.Repository.RootPath { | ||
return errors.New(errMsg) | ||
} | ||
return nil | ||
} | ||
|
||
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type | ||
func (r *RepositoryServerValidator) ValidateDelete(ctx context.Context, obj runtime.Object) error { | ||
return 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,30 @@ | ||
// Copyright 2023 The Kanister 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 validatingwebhook | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
) | ||
|
||
const WHCertsDir = "/var/run/webhook/serving-cert" | ||
|
||
func IsCACertMounted() bool { | ||
if _, err := os.Stat(fmt.Sprintf("%s/%s", WHCertsDir, "tls.crt")); err != nil { | ||
return false | ||
} | ||
|
||
return true | ||
} |