Skip to content

Commit

Permalink
Added webhook support for Default Images
Browse files Browse the repository at this point in the history
Signed-off-by: Chandan Kumar <[email protected]>
  • Loading branch information
raukadah committed Dec 16, 2024
1 parent 214e8bd commit f456227
Show file tree
Hide file tree
Showing 22 changed files with 298 additions and 20 deletions.
4 changes: 4 additions & 0 deletions api/bases/watcher.openstack.org_watcherapis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ spec:
spec:
description: WatcherAPISpec defines the desired state of WatcherAPI
properties:
containerImage:
description: The service specific Container Image URL (will be set
to environmental default if empty)
type: string
databaseAccount:
default: watcher
description: DatabaseAccount - MariaDBAccount CR name used for watcher
Expand Down
6 changes: 3 additions & 3 deletions api/bases/watcher.openstack.org_watcherappliers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ spec:
spec:
description: WatcherApplierSpec defines the desired state of WatcherApplier
properties:
foo:
description: Foo is an example field of WatcherApplier. Edit watcherapplier_types.go
to remove/update
containerImage:
description: The service specific Container Image URL (will be set
to environmental default if empty)
type: string
type: object
status:
Expand Down
6 changes: 3 additions & 3 deletions api/bases/watcher.openstack.org_watcherdecisionengines.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ spec:
spec:
description: WatcherDecisionEngineSpec defines the desired state of WatcherDecisionEngine
properties:
foo:
description: Foo is an example field of WatcherDecisionEngine. Edit
watcherdecisionengine_types.go to remove/update
containerImage:
description: The service specific Container Image URL (will be set
to environmental default if empty)
type: string
type: object
status:
Expand Down
12 changes: 12 additions & 0 deletions api/bases/watcher.openstack.org_watchers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ spec:
spec:
description: WatcherSpec defines the desired state of Watcher
properties:
apiContainerImageURL:
description: APIContainerImageURL
type: string
applierContainerImageURL:
description: ApplierContainerImageURL
type: string
databaseAccount:
default: watcher
description: DatabaseAccount - MariaDBAccount CR name used for watcher
Expand All @@ -49,6 +55,9 @@ spec:
MariaDB instance name
Required to use the mariadb-operator instance to create the DB and user
type: string
decisionengineContainerImageURL:
description: DecisionEngineContainerImageURL
type: string
passwordSelectors:
default:
service: WatcherPassword
Expand All @@ -72,7 +81,10 @@ spec:
description: Secret containing all passwords / keys needed
type: string
required:
- apiContainerImageURL
- applierContainerImageURL
- databaseInstance
- decisionengineContainerImageURL
- rabbitMqClusterName
type: object
status:
Expand Down
60 changes: 60 additions & 0 deletions api/v1beta1/common_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ limitations under the License.

package v1beta1

import "os"

// Container image fall-back defaults
const (
WatcherAPIContainerImage = "quay.io/podified-antelope-centos9/openstack-watcher-api:current-podified"
WatcherDecisionEngineContainerImage = "quay.io/podified-antelope-centos9/openstack-watcher-decision-engine:current-podified"
WatcherApplierContainerImage = "quay.io/podified-antelope-centos9/openstack-watcher-applier:current-podified"
)

// WatcherCommon defines a spec based reusable for all the CRDs
type WatcherCommon struct {
// +kubebuilder:validation:Optional
Expand Down Expand Up @@ -59,3 +68,54 @@ type PasswordSelector struct {
// Service - Selector to get the watcher service user password from the Secret
Service string `json:"service"`
}

// WatcherContainerImages
type WatcherContainerImages struct {
// +kubebuilder:validation:Optional
// The service specific Container Image URL (will be set to environmental default if empty)
ContainerImage string `json:"containerImage"`
}

type WatcherImages struct {
// +kubebuilder:validation:Required
// APIContainerImageURL
APIContainerImageURL string `json:"apiContainerImageURL"`

// +kubebuilder:validation:Required
// DecisionEngineContainerImageURL
DecisionEngineContainerImageURL string `json:"decisionengineContainerImageURL"`

// +kubebuilder:validation:Required
// ApplierContainerImageURL
ApplierContainerImageURL string `json:"applierContainerImageURL"`
}

func (r *WatcherImages) Default(defaults WatcherDefaults) {
if r.APIContainerImageURL == "" {
r.APIContainerImageURL = defaults.APIContainerImageURL
}
if r.DecisionEngineContainerImageURL == "" {
r.DecisionEngineContainerImageURL = defaults.DecisionEngineContainerImageURL
}
if r.ApplierContainerImageURL == "" {
r.ApplierContainerImageURL = defaults.ApplierContainerImageURL
}
}

// GetEnvDefault - Get the value associated with key from environment variables, but use baseDefault as a value in the case of an empty string
func GetEnvDefault(key string, baseDefault string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return baseDefault
}

func SetupDefaults() {
// Acquire environmental defaults and initialize Nova defaults with them
watcherDefaults := WatcherDefaults{
APIContainerImageURL: GetEnvDefault("WATCHER_API_IMAGE_URL_DEFAULT", WatcherAPIContainerImage),
ApplierContainerImageURL: GetEnvDefault("WATCHER_APPLIER_IMAGE_URL_DEFAULT", WatcherApplierContainerImage),
DecisionEngineContainerImageURL: GetEnvDefault("WATCHER_DECISION_ENGINE_IMAGE_URL_DEFAULT", WatcherDecisionEngineContainerImage),
}
SetupWatcherDefaults(watcherDefaults)
}
7 changes: 3 additions & 4 deletions api/v1beta1/watcher_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ type WatcherSpec struct {
// Important: Run "make" to regenerate code after modifying this file

WatcherTemplate `json:",inline"`

// +kubebuilder:validation:Required
WatcherImages `json:",inline"`
}

// WatcherStatus defines the observed state of Watcher
Expand Down Expand Up @@ -65,7 +68,3 @@ type WatcherList struct {
func init() {
SchemeBuilder.Register(&Watcher{}, &WatcherList{})
}

// SetupDefaults - initializes any CRD field defaults based on environment variables (the defaulting mechanism itself is implemented via webhooks)
func SetupDefaults() {
}
20 changes: 20 additions & 0 deletions api/v1beta1/watcher_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,23 @@ import (
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

// WatcherDefaults -
type WatcherDefaults struct {
APIContainerImageURL string
DecisionEngineContainerImageURL string
ApplierContainerImageURL string
}

var watcherDefaults WatcherDefaults

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

func SetupWatcherDefaults(defaults WatcherDefaults) {
watcherDefaults = defaults
watcherlog.Info("Watcher defaults initialized", "defaults", defaults)
}

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

var _ webhook.Defaulter = &Watcher{}
Expand All @@ -34,6 +48,12 @@ var _ webhook.Defaulter = &Watcher{}
func (r *Watcher) Default() {
watcherlog.Info("default", "name", r.Name)

r.Spec.Default()
}

// Default - set defaults for this WatcherCore spec.
func (spec *WatcherSpec) Default() {
spec.WatcherImages.Default(watcherDefaults)
}

//+kubebuilder:webhook:path=/validate-watcher-openstack-org-v1beta1-watcher,mutating=false,failurePolicy=fail,sideEffects=None,groups=watcher.openstack.org,resources=watchers,verbs=create;update,versions=v1beta1,name=vwatcher.kb.io,admissionReviewVersions=v1
Expand Down
2 changes: 2 additions & 0 deletions api/v1beta1/watcherapi_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ type WatcherAPISpec struct {
// Important: Run "make" to regenerate code after modifying this file

WatcherCommon `json:",inline"`

WatcherContainerImages `json:",inline"`
}

// WatcherAPIStatus defines the observed state of WatcherAPI
Expand Down
3 changes: 1 addition & 2 deletions api/v1beta1/watcherapplier_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ type WatcherApplierSpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "make" to regenerate code after modifying this file

// Foo is an example field of WatcherApplier. Edit watcherapplier_types.go to remove/update
Foo string `json:"foo,omitempty"`
WatcherContainerImages `json:",inline"`
}

// WatcherApplierStatus defines the observed state of WatcherApplier
Expand Down
3 changes: 1 addition & 2 deletions api/v1beta1/watcherdecisionengine_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ type WatcherDecisionEngineSpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "make" to regenerate code after modifying this file

// Foo is an example field of WatcherDecisionEngine. Edit watcherdecisionengine_types.go to remove/update
Foo string `json:"foo,omitempty"`
WatcherContainerImages `json:",inline"`
}

// WatcherDecisionEngineStatus defines the observed state of WatcherDecisionEngine
Expand Down
49 changes: 49 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.

4 changes: 4 additions & 0 deletions config/crd/bases/watcher.openstack.org_watcherapis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ spec:
spec:
description: WatcherAPISpec defines the desired state of WatcherAPI
properties:
containerImage:
description: The service specific Container Image URL (will be set
to environmental default if empty)
type: string
databaseAccount:
default: watcher
description: DatabaseAccount - MariaDBAccount CR name used for watcher
Expand Down
6 changes: 3 additions & 3 deletions config/crd/bases/watcher.openstack.org_watcherappliers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ spec:
spec:
description: WatcherApplierSpec defines the desired state of WatcherApplier
properties:
foo:
description: Foo is an example field of WatcherApplier. Edit watcherapplier_types.go
to remove/update
containerImage:
description: The service specific Container Image URL (will be set
to environmental default if empty)
type: string
type: object
status:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ spec:
spec:
description: WatcherDecisionEngineSpec defines the desired state of WatcherDecisionEngine
properties:
foo:
description: Foo is an example field of WatcherDecisionEngine. Edit
watcherdecisionengine_types.go to remove/update
containerImage:
description: The service specific Container Image URL (will be set
to environmental default if empty)
type: string
type: object
status:
Expand Down
12 changes: 12 additions & 0 deletions config/crd/bases/watcher.openstack.org_watchers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ spec:
spec:
description: WatcherSpec defines the desired state of Watcher
properties:
apiContainerImageURL:
description: APIContainerImageURL
type: string
applierContainerImageURL:
description: ApplierContainerImageURL
type: string
databaseAccount:
default: watcher
description: DatabaseAccount - MariaDBAccount CR name used for watcher
Expand All @@ -49,6 +55,9 @@ spec:
MariaDB instance name
Required to use the mariadb-operator instance to create the DB and user
type: string
decisionengineContainerImageURL:
description: DecisionEngineContainerImageURL
type: string
passwordSelectors:
default:
service: WatcherPassword
Expand All @@ -72,7 +81,10 @@ spec:
description: Secret containing all passwords / keys needed
type: string
required:
- apiContainerImageURL
- applierContainerImageURL
- databaseInstance
- decisionengineContainerImageURL
- rabbitMqClusterName
type: object
status:
Expand Down
3 changes: 3 additions & 0 deletions config/default/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ patches:
# 'CERTMANAGER' needs to be enabled to use ca injection
#- path: webhookcainjection_patch.yaml

# Injects our custom images (ENV variable settings)
- path: manager_default_images.yaml

# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER' prefix.
# Uncomment the following replacements to add the cert-manager CA injection annotations
#replacements:
Expand Down
Loading

0 comments on commit f456227

Please sign in to comment.