Skip to content

Commit

Permalink
Add defaulting/validation, add ability to override imageformat, baseu…
Browse files Browse the repository at this point in the history
…rl, os, os version
  • Loading branch information
detiber committed Mar 24, 2021
1 parent ce6d096 commit f0218c3
Show file tree
Hide file tree
Showing 35 changed files with 787 additions and 93 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ KUSTOMIZE_VER := v3.9.2
KUSTOMIZE_BIN := kustomize
KUSTOMIZE := $(TOOLS_BIN_DIR)/$(KUSTOMIZE_BIN)-$(KUSTOMIZE_VER)

CONTROLLER_GEN_VER := v0.4.1
CONTROLLER_GEN_VER := v0.2.9
CONTROLLER_GEN_BIN := controller-gen
CONTROLLER_GEN := $(TOOLS_BIN_DIR)/$(CONTROLLER_GEN_BIN)-$(CONTROLLER_GEN_VER)

Expand Down
36 changes: 30 additions & 6 deletions api/v1alpha3/tinkerbellcluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,43 @@ const (
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "make" to regenerate code after modifying this file.
type TinkerbellClusterSpec struct {
// ImageBaseURL is the base URL that is used for pulling images, if not set, the default
// will be to use http://<TINKERBELL IP>:8080/
//
// +optional
ImageBaseURL string `json:"imageBaseURL,omitempty"`

// ControlPlaneEndpoint is a required field by ClusterAPI v1alpha3.
//
// See https://cluster-api.sigs.k8s.io/developer/architecture/controllers/cluster.html
// for more details.
//
// +optional
ControlPlaneEndpoint clusterv1.APIEndpoint `json:"controlPlaneEndpoint,omitempty"`

// ImageLookupFormat is the URL naming format to use for machine images when
// a machine does not specify. When set, this will be used for all cluster machines
// unless a machine specifies a different ImageLookupFormat. Supports substitutions
// for {{.BaseURL}}, {{.OSDistro}}, {{.OSVersion}} and {{.KubernetesVersion}} with
// the basse URL, OS distribution, OS version, and kubernetes version, respectively.
// BaseURL will be the vallue in ImageLookupBaseURL or http://$TINKERBELL_IP:8080/
// (the default), OSDistro will be the value in ImageLookupOSDistro or ubuntu (the default),
// OSVersion will be the value in ImageLookupOSVersion or default based on the OSDistro
// (if known), and the kubernetes version as defined by the packages produced by
// kubernetes/release: v1.13.0, v1.12.5-mybuild.1, or v1.17.3. For example, the default
// image format of {{.BaseURL}}{{.OSDistro}}-{{.OSVersion}}-kube-{{.K8sVersion}}.gz will
// attempt to pull the image from that location. See also: https://golang.org/pkg/text/template/
// +optional
ImageLookupFormat string `json:"imageLookupFormat,omitempty"`

// ImageLookupBaseURL is the base URL that is used for pulling images, if not set,
// the default will be to use http://$TINKERBELL_IP:8080/.
// +optional
ImageLookupBaseURL string `json:"imageLookupBaseURL,omitempty"`

// ImageLookupOSDistro is the name of the OS distro to use when fetching machine images,
// if not set it will default to ubuntu.
// +optional
ImageLookupOSDistro string `json:"imageLookupOSDistro,omitempty"`

// ImageLookupOSVersion is the version of the OS distribution to use when fetching machine
// images. If not set it will default based on ImageLookupOSDistro.
// +optional
ImageLookupOSVersion string `json:"imageLookupOSVersion,omitempty"`
}

// TinkerbellClusterStatus defines the observed state of TinkerbellCluster.
Expand Down
85 changes: 85 additions & 0 deletions api/v1alpha3/tinkerbellcluster_webhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
Copyright 2021 The Kubernetes 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 v1alpha3

import (
"fmt"
"os"
"strings"

"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
)

const (
osUbuntu = "ubuntu"
defaultUbuntuVersion = "20.04"
defaultOSDistro = osUbuntu
)

// SetupWebhookWithManager sets up and registers the webhook with the manager.
func (c *TinkerbellCluster) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(c).
Complete()
}

// +kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1alpha3-tinkerbellcluster,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=tinkerbellclusters,versions=v1alpha3,name=validation.tinkerbellcluster.infrastructure.cluster.x-k8s.io,sideEffects=None
// +kubebuilder:webhook:verbs=create;update,path=/mutate-infrastructure-cluster-x-k8s-io-v1alpha3-tinkerbellcluster,mutating=true,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=tinkerbellclusters,versions=v1alpha3,name=default.tinkerbellcluster.infrastructure.cluster.x-k8s.io,sideEffects=None

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
func (c *TinkerbellCluster) ValidateCreate() error {
return nil
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
func (c *TinkerbellCluster) ValidateUpdate(oldRaw runtime.Object) error {
return nil
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
func (c *TinkerbellCluster) ValidateDelete() error {
return nil
}

func defaultVersionForOSDistro(distro string) string {
if strings.ToLower(distro) == osUbuntu {
return defaultUbuntuVersion
}

return ""
}

// Default implements webhookutil.defaulter so a webhook will be registered for the type.
func (c *TinkerbellCluster) Default() {
if c.Spec.ImageLookupFormat == "" {
c.Spec.ImageLookupFormat = "{{.BaseURL}}{{.OSDistro}}-{{.OSVersion}}-kube-{{.KubernetesVersion}}.gz"
}

if c.Spec.ImageLookupBaseURL == "" {
tinkIP := os.Getenv("TINKERBELL_IP")
c.Spec.ImageLookupBaseURL = fmt.Sprintf("http://%s:8080/", tinkIP)
}

if c.Spec.ImageLookupOSDistro == "" {
c.Spec.ImageLookupOSDistro = defaultOSDistro
}

if c.Spec.ImageLookupOSVersion == "" {
c.Spec.ImageLookupOSVersion = defaultVersionForOSDistro(c.Spec.ImageLookupOSDistro)
}
}
30 changes: 30 additions & 0 deletions api/v1alpha3/tinkerbellmachine_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,36 @@ const (

// TinkerbellMachineSpec defines the desired state of TinkerbellMachine.
type TinkerbellMachineSpec struct {
// ImageLookupFormat is the URL naming format to use for machine images when
// a machine does not specify. When set, this will be used for all cluster machines
// unless a machine specifies a different ImageLookupFormat. Supports substitutions
// for {{.BaseURL}}, {{.OSDistro}}, {{.OSVersion}} and {{.KubernetesVersion}} with
// the basse URL, OS distribution, OS version, and kubernetes version, respectively.
// BaseURL will be the vallue in ImageLookupBaseURL or http://$TINKERBELL_IP:8080/
// (the default), OSDistro will be the value in ImageLookupOSDistro or ubuntu (the default),
// OSVersion will be the value in ImageLookupOSVersion or default based on the OSDistro
// (if known), and the kubernetes version as defined by the packages produced by
// kubernetes/release: v1.13.0, v1.12.5-mybuild.1, or v1.17.3. For example, the default
// image format of {{.BaseURL}}{{.OSDistro}}-{{.OSVersion}}-kube-{{.K8sVersion}}.gz will
// attempt to pull the image from that location. See also: https://golang.org/pkg/text/template/
// +optional
ImageLookupFormat string `json:"imageLookupFormat,omitempty"`

// ImageLookupBaseURL is the base URL that is used for pulling images, if not set,
// the default will be to use http://$TINKERBELL_IP:8080/.
// +optional
ImageLookupBaseURL string `json:"imageLookupBaseURL,omitempty"`

// ImageLookupOSDistro is the name of the OS distro to use when fetching machine images,
// if not set it will default to ubuntu.
// +optional
ImageLookupOSDistro string `json:"imageLookupOSDistro,omitempty"`

// ImageLookupOSVersion is the version of the OS distribution to use when fetching machine
// images. If not set it will default based on ImageLookupOSDistro.
// +optional
ImageLookupOSVersion string `json:"imageLookupOSVersion,omitempty"`

// Those fields are set programmatically, but they cannot be re-constructed from "state of the world", so
// we put them in spec instead of status.
HardwareName string `json:"hardwareName,omitempty"`
Expand Down
59 changes: 59 additions & 0 deletions api/v1alpha3/tinkerbellmachine_webhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
Copyright 2021 The Kubernetes 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 v1alpha3

import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation/field"
ctrl "sigs.k8s.io/controller-runtime"
)

// SetupWebhookWithManager sets up and registers the webhook with the manager.
func (m *TinkerbellMachine) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(m).
Complete()
}

// +kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1alpha3-tinkerbellmachine,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=tinkerbellmachines,versions=v1alpha3,name=validation.tinkerbellmachine.infrastructure.cluster.x-k8s.io,sideEffects=None

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
func (m *TinkerbellMachine) ValidateCreate() error {
return nil
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
func (m *TinkerbellMachine) ValidateUpdate(oldRaw runtime.Object) error {
var allErrs field.ErrorList

old, _ := oldRaw.(*TinkerbellMachine)

if old.Spec.HardwareName != "" && m.Spec.HardwareName != old.Spec.HardwareName {
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec", "hardwareName"), "is immutable once set"))
}

if old.Spec.ProviderID != "" && m.Spec.ProviderID != old.Spec.ProviderID {
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec", "providerID"), "is immutable once set"))
}

return aggregateObjErrors(m.GroupVersionKind().GroupKind(), m.Name, allErrs)
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
func (m *TinkerbellMachine) ValidateDelete() error {
return nil
}
66 changes: 66 additions & 0 deletions api/v1alpha3/tinkerbellmachinetemplate_webhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
Copyright 2021 The Kubernetes 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 v1alpha3

import (
"reflect"

apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation/field"
ctrl "sigs.k8s.io/controller-runtime"
)

// SetupWebhookWithManager sets up and registers the webhook with the manager.
func (m *TinkerbellMachineTemplate) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(m).
Complete()
}

// +kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1alpha3-tinkerbellmachinetemplate,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=tinkerbellmachinetemplates,versions=v1alpha3,name=validation.tinkerbellmachinetemplate.infrastructure.x-k8s.io,sideEffects=None

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
func (m *TinkerbellMachineTemplate) ValidateCreate() error {
var allErrs field.ErrorList

spec := m.Spec.Template.Spec
fieldBasePath := field.NewPath("spec", "template", "spec")

if spec.ProviderID != "" {
allErrs = append(allErrs, field.Forbidden(fieldBasePath.Child("providerID"), "cannot be set in templates"))
}

if spec.HardwareName != "" {
allErrs = append(allErrs, field.Forbidden(fieldBasePath.Child("hardwareName"), "cannot be set in templates"))
}

return aggregateObjErrors(m.GroupVersionKind().GroupKind(), m.Name, allErrs)
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
func (m *TinkerbellMachineTemplate) ValidateUpdate(old runtime.Object) error {
oldTinkerbellMachineTemplate, _ := old.(*TinkerbellMachineTemplate)

if !reflect.DeepEqual(m.Spec, oldTinkerbellMachineTemplate.Spec) {
return apierrors.NewBadRequest("TinkerbellMachineTemplate.Spec is immutable")
}

return nil
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
func (m *TinkerbellMachineTemplate) ValidateDelete() error {
return nil
}
35 changes: 35 additions & 0 deletions api/v1alpha3/webhooks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
Copyright 2021 The Kubernetes 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 v1alpha3

import (
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/validation/field"
)

func aggregateObjErrors(gk schema.GroupKind, name string, allErrs field.ErrorList) error {
if len(allErrs) == 0 {
return nil
}

return apierrors.NewInvalid(
gk,
name,
allErrs,
)
}
2 changes: 1 addition & 1 deletion api/v1alpha3/zz_generated.deepcopy.go

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

Loading

0 comments on commit f0218c3

Please sign in to comment.