Skip to content

Commit

Permalink
Merge pull request #51 from detiber/withImages
Browse files Browse the repository at this point in the history
Update to use image-builder based images
  • Loading branch information
detiber authored Apr 16, 2021
2 parents a32255d + 8978ec2 commit 544f586
Show file tree
Hide file tree
Showing 63 changed files with 1,281 additions and 605 deletions.
19 changes: 15 additions & 4 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,18 @@ linters:
enable:
- asciicheck
- bodyclose
- cyclop
- deadcode
- depguard
- dogsled
- dupl
- durationcheck
- errcheck
- errorlint
- exhaustive
- exportloopref
- forbidigo
- forcetypeassert
- funlen
- gochecknoglobals
- gochecknoinits
Expand All @@ -52,23 +55,26 @@ linters:
- gomodguard
- goprintffuncname
- gosec
- gosimple
- ifshort
- importas
- ineffassign
- interfacer
- lll
- makezero
- maligned
- misspell
- nakedret
- nestif
- nilerr
- nlreturn
- noctx
- nolintlint
- paralleltest
- prealloc
- predeclared
- revive
- rowserrcheck
- scopelint
- sqlclosecheck
- staticcheck
- structcheck
- stylecheck
- testpackage
Expand All @@ -77,12 +83,17 @@ linters:
- typecheck
- unconvert
- unparam
- unused
- varcheck
- wastedassign
- whitespace
- wrapcheck
- wsl
disable:
- exhaustivestruct
- gci
- godox
- goerr113
- exhaustivestruct
- interfacer
- maligned
- scopelint
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
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,19 @@ Provider for Tinkerbell.
and Tinkerbell
* It simplifies Kubernetes cluster management using Tinkerbell as underline
infrastructure provider
* Create, update, delete Kubernetes project in a declarative fashion.
* Create, update, delete Kubernetes clusters in a declarative fashion.

## Current state

7th December 2020 marks the first commit for this project, it starts as a
porting from CAPP (cluster api provider packet).

As it is now it does not do anything useful. It starts the infrastructure
manager. Just a go binary with all the boilerplate code and controllers
bootstrapped.
As it is now it is able to bootstrap single Control Plane instance clusters using hardware
managed by Tinkerbell. Support for upgrades and multi-instance Control Plane clusters is
still outstanding.

See [docs/README.md](docs/README.md) for more information on setting up a development
environment.

## Technical preview

Expand Down
30 changes: 30 additions & 0 deletions api/v1alpha3/tinkerbellcluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,36 @@ type TinkerbellClusterSpec struct {
//
// +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 value 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 value 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
}
69 changes: 69 additions & 0 deletions api/v1alpha3/tinkerbellmachinetemplate_webhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
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
}
Loading

0 comments on commit 544f586

Please sign in to comment.