Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing GlanceAPI webhook #363

Merged
merged 1 commit into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions api/v1beta1/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,16 @@ func SetupDefaults() {
SetupGlanceDefaults(glanceDefaults)
}

// SetupAPIDefaults - initializes any CRD field defaults based on environment variables (the defaulting mechanism itself is implemented via webhooks)
func SetupAPIDefaults() {
// Acquire environmental defaults and initialize GlanceAPI defaults with them
glanceAPIDefaults := GlanceAPIDefaults{
ContainerImageURL: util.GetEnvVar("RELATED_IMAGE_GLANCE_API_IMAGE_URL_DEFAULT", GlanceAPIContainerImage),
}

SetupGlanceAPIDefaults(glanceAPIDefaults)
}

// GetAdminServiceClient - get an admin serviceClient for the Glance instance
func GetAdminServiceClient(
ctx context.Context,
Expand Down
93 changes: 93 additions & 0 deletions api/v1beta1/glanceapi_webhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
Copyright 2022.

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 v1beta1

import (
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
)

// GlanceAPIDefaults -
type GlanceAPIDefaults struct {
ContainerImageURL string
}

var glanceAPIDefaults GlanceAPIDefaults
fmount marked this conversation as resolved.
Show resolved Hide resolved

// log is for logging in this package.
var glanceapilog = logf.Log.WithName("glanceapi-resource")

// SetupGlanceAPIDefaults - initialize GlanceAPI spec defaults for use with either internal or external webhooks
func SetupGlanceAPIDefaults(defaults GlanceAPIDefaults) {
glanceAPIDefaults = defaults
glancelog.Info("Glance defaults initialized", "defaults", defaults)
}
Comment on lines +36 to +40
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we also need something that pulls-in the ENV vars and sets them in the GlanceAPIDefaults, like we do here [1]. Maybe I just am missing where you're already doing this, however.

[1]

func SetupDefaults() {
// Acquire environmental defaults and initialize Glance defaults with them
glanceDefaults := GlanceDefaults{
ContainerImageURL: util.GetEnvVar("RELATED_IMAGE_GLANCE_API_IMAGE_URL_DEFAULT", GlanceAPIContainerImage),
}
SetupGlanceDefaults(glanceDefaults)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, I need that function too in common.go. Updating the patch in a moment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


// SetupWebhookWithManager sets up the webhook with the Manager
func (r *GlanceAPI) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
Complete()
}

//+kubebuilder:webhook:path=/mutate-glance-openstack-org-v1beta1-glanceapi,mutating=true,failurePolicy=fail,sideEffects=None,groups=glance.openstack.org,resources=glanceapis,verbs=create;update,versions=v1beta1,name=mglanceapi.kb.io,admissionReviewVersions=v1

var _ webhook.Defaulter = &GlanceAPI{}

// Default implements webhook.Defaulter so a webhook will be registered for the type
func (r *GlanceAPI) Default() {
glancelog.Info("default", "name", r.Name)

r.Spec.Default()
}

// Default - set defaults for this Glance spec
func (spec *GlanceAPISpec) Default() {
if spec.GlanceAPITemplate.ContainerImage == "" {
spec.GlanceAPITemplate.ContainerImage = glanceAPIDefaults.ContainerImageURL
}
}

//+kubebuilder:webhook:path=/validate-glance-openstack-org-v1beta1-glanceapi,mutating=false,failurePolicy=fail,sideEffects=None,groups=glance.openstack.org,resources=glanceapis,verbs=create;update,versions=v1beta1,name=vglanceapi.kb.io,admissionReviewVersions=v1

var _ webhook.Validator = &GlanceAPI{}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (r *GlanceAPI) ValidateCreate() error {
glancelog.Info("validate create", "name", r.Name)

// TODO(user): fill in your validation logic upon object creation.
return nil
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (r *GlanceAPI) ValidateUpdate(old runtime.Object) error {
glancelog.Info("validate update", "name", r.Name)

// TODO(user): fill in your validation logic upon object update.
return nil
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (r *GlanceAPI) ValidateDelete() error {
glancelog.Info("validate delete", "name", r.Name)

// TODO(user): fill in your validation logic upon object deletion.
return nil
}
15 changes: 15 additions & 0 deletions api/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion config/samples/glance_v1beta1_glanceapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ metadata:
spec:
serviceUser: glance
serviceAccount: glance
containerImage: quay.io/podified-antelope-centos9/openstack-glance-api:current-podified
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dprince we can use this patch as a follow up of PR #358 and remove this line here using and not fail because of the glanceAPI webhook.

customServiceConfig: |
[DEFAULT]
debug = true
Expand Down
40 changes: 40 additions & 0 deletions config/webhook/manifests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,26 @@ webhooks:
resources:
- glances
sideEffects: None
- admissionReviewVersions:
- v1
clientConfig:
service:
name: webhook-service
namespace: system
path: /mutate-glance-openstack-org-v1beta1-glanceapi
failurePolicy: Fail
name: mglanceapi.kb.io
rules:
- apiGroups:
- glance.openstack.org
apiVersions:
- v1beta1
operations:
- CREATE
- UPDATE
resources:
- glanceapis
sideEffects: None
---
apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingWebhookConfiguration
Expand Down Expand Up @@ -52,3 +72,23 @@ webhooks:
resources:
- glances
sideEffects: None
- admissionReviewVersions:
- v1
clientConfig:
service:
name: webhook-service
namespace: system
path: /validate-glance-openstack-org-v1beta1-glanceapi
failurePolicy: Fail
name: vglanceapi.kb.io
rules:
- apiGroups:
- glance.openstack.org
apiVersions:
- v1beta1
operations:
- CREATE
- UPDATE
resources:
- glanceapis
sideEffects: None
2 changes: 2 additions & 0 deletions hack/clean_local_webhook.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ set -ex

oc delete validatingwebhookconfiguration/vglance.kb.io --ignore-not-found
oc delete mutatingwebhookconfiguration/mglance.kb.io --ignore-not-found
oc delete validatingwebhookconfiguration/vglanceapi.kb.io --ignore-not-found
oc delete mutatingwebhookconfiguration/mglanceapi.kb.io --ignore-not-found
56 changes: 56 additions & 0 deletions hack/configure_local_webhook.sh
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,62 @@ webhooks:
scope: '*'
sideEffects: None
timeoutSeconds: 10
---
apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingWebhookConfiguration
metadata:
name: vglanceapi.kb.io
webhooks:
- admissionReviewVersions:
- v1
clientConfig:
caBundle: ${CA_BUNDLE}
url: https://${CRC_IP}:9443/validate-glance-openstack-org-v1beta1-glanceapi
failurePolicy: Fail
matchPolicy: Equivalent
name: vglanceapi.kb.io
objectSelector: {}
rules:
- apiGroups:
- glance.openstack.org
apiVersions:
- v1beta1
operations:
- CREATE
- UPDATE
resources:
- glanceapis
scope: '*'
sideEffects: None
timeoutSeconds: 10
---
apiVersion: admissionregistration.k8s.io/v1
kind: MutatingWebhookConfiguration
metadata:
name: mglanceapi.kb.io
webhooks:
- admissionReviewVersions:
- v1
clientConfig:
caBundle: ${CA_BUNDLE}
url: https://${CRC_IP}:9443/mutate-glance-openstack-org-v1beta1-glanceapi
failurePolicy: Fail
matchPolicy: Equivalent
name: mglanceapi.kb.io
objectSelector: {}
rules:
- apiGroups:
- glance.openstack.org
apiVersions:
- v1beta1
operations:
- CREATE
- UPDATE
resources:
- glanceapis
scope: '*'
sideEffects: None
timeoutSeconds: 10
EOF_CAT

oc apply -n openstack -f ${TMPDIR}/patch_webhook_configurations.yaml
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ func main() {
setupLog.Error(err, "unable to create webhook", "webhook", "Glance")
os.Exit(1)
}
if err = (&glancev1.GlanceAPI{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "GlanceAPI")
os.Exit(1)
}
checker = mgr.GetWebhookServer().StartedChecker()
}
//+kubebuilder:scaffold:builder
Expand Down
2 changes: 1 addition & 1 deletion pkg/glance/pvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// Pvc - creates and returns a PVC object for a backing store
// GetPvc - creates and returns a PVC object for a backing store
func GetPvc(api *glancev1.GlanceAPI, labels map[string]string, pvcType PvcType) corev1.PersistentVolumeClaim {
// By default we point to a local storage pvc request
// that will be customized in case the pvc is requested
Expand Down
2 changes: 1 addition & 1 deletion pkg/glanceapi/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const (
GlanceAPIHttpdCommand = "/usr/sbin/httpd -DFOREGROUND"
)

// Deployment func
// StatefulSet func
func StatefulSet(
instance *glancev1.GlanceAPI,
configHash string,
Expand Down
2 changes: 2 additions & 0 deletions test/functional/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ var _ = BeforeSuite(func() {
err = (&glancev1.Glance{}).SetupWebhookWithManager(k8sManager)
Expect(err).NotTo(HaveOccurred())

err = (&glancev1.GlanceAPI{}).SetupWebhookWithManager(k8sManager)
Expect(err).NotTo(HaveOccurred())
go func() {
defer GinkgoRecover()
err = k8sManager.Start(ctx)
Expand Down