Skip to content

Commit

Permalink
Merge pull request #1510 from 24sama/master
Browse files Browse the repository at this point in the history
feat: support using os repository to install packages.
  • Loading branch information
ks-ci-bot committed Sep 16, 2022
2 parents be458fa + 6506cc9 commit b731d2f
Show file tree
Hide file tree
Showing 57 changed files with 4,429 additions and 2,511 deletions.
8 changes: 8 additions & 0 deletions exp/cluster-api-provider-kubekey/PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ resources:
kind: KKMachine
path: github.com/kubesphere/kubekey/exp/cluster-api-provider-kubekey/api/v1beta1
version: v1beta1
webhooks:
defaulting: true
validation: true
webhookVersion: v1
- api:
crdVersion: v1
namespaced: true
Expand All @@ -49,6 +53,10 @@ resources:
kind: KKMachineTemplate
path: github.com/kubesphere/kubekey/exp/cluster-api-provider-kubekey/api/v1beta1
version: v1beta1
webhooks:
defaulting: true
validation: true
webhookVersion: v1
- api:
crdVersion: v1
namespaced: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@ package v1beta1
type Component struct {
// ZONE is the zone of the KKCluster where can get the binaries.
// If you have problem to access https://storage.googleapis.com, you can set "zone: cn".
// +optional
ZONE string `json:"zone,omitempty"`

// Host is the host to download the binaries.
// +optional
Host string `json:"host,omitempty"`

// Overrides is a list of components download information that need to be overridden.
// +optional
Overrides []Override `json:"overrides,omitempty"`
}

Expand All @@ -47,5 +50,6 @@ type Override struct {
Path string `json:"path,omitempty"`

// Checksum is the SHA256 checksum of the binary.
// +optional
Checksum string `json:"checksum,omitempty"`
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ const (
KKInstanceInitOSFailedReason = "InitOSFailed"
)

const (
// KKInstanceRepositoryReadyCondition reports on whether successful to use repository to install packages.
KKInstanceRepositoryReadyCondition clusterv1.ConditionType = "InstanceRepositoryReady"
// KKInstanceRepositoryFailedReason used when the instance couldn't use repository to install packages.
KKInstanceRepositoryFailedReason = "InstanceRepositoryFailed"
)

const (
// KKInstanceBinariesReadyCondition reports on whether successful to download binaries.
KKInstanceBinariesReadyCondition clusterv1.ConditionType = "InstanceBinariesReady"
Expand Down
32 changes: 26 additions & 6 deletions exp/cluster-api-provider-kubekey/api/v1beta1/kkcluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ type KKClusterSpec struct {
ControlPlaneEndpoint clusterv1.APIEndpoint `json:"controlPlaneEndpoint"`

// ControlPlaneLoadBalancer is optional configuration for customizing control plane behavior.
// +optional
ControlPlaneLoadBalancer *KKLoadBalancerSpec `json:"controlPlaneLoadBalancer,omitempty"`

// Component is optional configuration for modifying the FTP server
Expand All @@ -49,7 +48,7 @@ type KKClusterSpec struct {

// Registry represents the cluster image registry used to pull the images.
// +optional
Registry Registry `json:"registry"`
Registry Registry `json:"registry,omitempty"`
}

// Nodes represents the information about the nodes available to the cluster
Expand All @@ -58,12 +57,33 @@ type Nodes struct {
// +optional
Auth Auth `json:"auth"`

// ContainerManager is the container manager config of all instance. It is a global container manager configuration.
// Instances defines all instance contained in kkcluster.
Instances []InstanceInfo `json:"instances"`
}

// InstanceInfo defines the information about the instance.
type InstanceInfo struct {
// Name is the host name of the machine.
// +kubebuilder:validation:MinLength=1
Name string `json:"name,omitempty"`

// Address is the IP address of the machine.
Address string `json:"address,omitempty"`

// InternalAddress is the internal IP address of the machine.
InternalAddress string `json:"internalAddress,omitempty"`

// Roles is the role of the machine.
// +optional
ContainerManager ContainerManager `json:"containerManager,omitempty"`
Roles []Role `json:"roles,omitempty"`

// Instances defines all instance contained in kkcluster.
Instances []KKInstanceSpec `json:"instances"`
// Arch is the architecture of the machine. e.g. "amd64", "arm64".
// +optional
Arch string `json:"arch,omitempty"`

// Auth is the SSH authentication information of this machine. It will override the global auth configuration.
// +optional
Auth Auth `json:"auth,omitempty"`
}

// KKLoadBalancerSpec defines the desired state of an KK load balancer.
Expand Down
25 changes: 0 additions & 25 deletions exp/cluster-api-provider-kubekey/api/v1beta1/kkcluster_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ func (k *KKCluster) Default() {
kkclusterlog.Info("default", "name", k.Name)

defaultAuth(&k.Spec.Nodes.Auth)
defaultContainerManager(&k.Spec)
defaultInstance(&k.Spec)
}

Expand All @@ -75,30 +74,6 @@ func defaultAuth(auth *Auth) {
}
}

func defaultContainerManager(spec *KKClusterSpec) {
// Direct connection to the user-provided CRI socket
if spec.Nodes.ContainerManager.CRISocket != "" {
return
}

if spec.Nodes.ContainerManager.Type == "" {
spec.Nodes.ContainerManager.Type = ContainerdType
}

switch spec.Nodes.ContainerManager.Type {
case ContainerdType:
if spec.Nodes.ContainerManager.Version == "" {
spec.Nodes.ContainerManager.Version = DefaultContainerdVersion
}
spec.Nodes.ContainerManager.CRISocket = DefaultContainerdCRISocket
case DockerType:
if spec.Nodes.ContainerManager.Version == "" {
spec.Nodes.ContainerManager.Version = DefaultDockerVersion
}
spec.Nodes.ContainerManager.CRISocket = DefaultDockerCRISocket
}
}

func defaultInstance(spec *KKClusterSpec) {
for i := range spec.Nodes.Instances {
instance := &spec.Nodes.Instances[i]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ func (r *KKClusterTemplate) Default() {
kkclustertemplatelog.Info("default", "name", r.Name)

defaultAuth(&r.Spec.Template.Spec.Nodes.Auth)
defaultContainerManager(&r.Spec.Template.Spec)
defaultInstance(&r.Spec.Template.Spec)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ type KKInstanceSpec struct {
// ContainerManager is the container manager config of this machine.
// +optional
ContainerManager ContainerManager `json:"containerManager,omitempty"`

// Repository is the repository config of this machine.
// +optional
Repository *Repository `json:"repository,omitempty"`
}

// KKInstanceStatus defines the observed state of KKInstance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type KKMachineSpec struct {
// ProviderID is the unique identifier as specified by the kubekey provider.
ProviderID *string `json:"providerID,omitempty"`

// InstanceID is the name of the KKInstance.
InstanceID *string `json:"instanceID,omitempty"`

// Roles is the role of the machine.
Expand All @@ -42,6 +43,10 @@ type KKMachineSpec struct {
// ContainerManager is the container manager config of this machine.
// +optional
ContainerManager ContainerManager `json:"containerManager"`

// Repository is the repository config of this machine.
// +optional
Repository *Repository `json:"repository,omitempty"`
}

// KKMachineStatus defines the observed state of KKMachine
Expand Down
104 changes: 104 additions & 0 deletions exp/cluster-api-provider-kubekey/api/v1beta1/kkmachine_webhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
Copyright 2022 The KubeSphere 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 v1beta1

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

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

func (k *KKMachine) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(k).
Complete()
}

//+kubebuilder:webhook:path=/mutate-infrastructure-cluster-x-k8s-io-v1beta1-kkmachine,mutating=true,failurePolicy=fail,sideEffects=None,groups=infrastructure.cluster.x-k8s.io,resources=kkmachines,verbs=create;update,versions=v1beta1,name=default.kkmachine.infrastructure.cluster.x-k8s.io,admissionReviewVersions=v1

var _ webhook.Defaulter = &KKMachine{}

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

defaultContainerManager(&k.Spec)
}

func defaultContainerManager(spec *KKMachineSpec) {
// Direct connection to the user-provided CRI socket
if spec.ContainerManager.CRISocket != "" {
return
}

if spec.ContainerManager.Type == "" {
spec.ContainerManager.Type = ContainerdType
}

switch spec.ContainerManager.Type {
case ContainerdType:
if spec.ContainerManager.Version == "" {
spec.ContainerManager.Version = DefaultContainerdVersion
}
spec.ContainerManager.CRISocket = DefaultContainerdCRISocket
case DockerType:
if spec.ContainerManager.Version == "" {
spec.ContainerManager.Version = DefaultDockerVersion
}
spec.ContainerManager.CRISocket = DefaultDockerCRISocket
}
}

//+kubebuilder:webhook:path=/validate-infrastructure-cluster-x-k8s-io-v1beta1-kkmachine,mutating=false,failurePolicy=fail,sideEffects=None,groups=infrastructure.cluster.x-k8s.io,resources=kkmachines,verbs=create;update,versions=v1beta1,name=validation.kkmachine.infrastructure.cluster.x-k8s.io,admissionReviewVersions=v1

var _ webhook.Validator = &KKMachine{}

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

var allErrs field.ErrorList
allErrs = append(allErrs, validateRepository(k.Spec.Repository)...)
return aggregateObjErrors(k.GroupVersionKind().GroupKind(), k.Name, allErrs)
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (k *KKMachine) ValidateUpdate(old runtime.Object) error {
kkmachinelog.Info("validate update", "name", k.Name)
var allErrs field.ErrorList
allErrs = append(allErrs, validateRepository(k.Spec.Repository)...)
return aggregateObjErrors(k.GroupVersionKind().GroupKind(), k.Name, allErrs)
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (k *KKMachine) ValidateDelete() error {
kkmachinelog.Info("validate delete", "name", k.Name)
return nil
}

func validateRepository(repo *Repository) field.ErrorList { //nolint:unparam
var allErrs field.ErrorList
if repo == nil {
return allErrs
}
return allErrs
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
Copyright 2022 The KubeSphere 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 v1beta1

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

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

func (k *KKMachineTemplate) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(k).
Complete()
}

//+kubebuilder:webhook:path=/mutate-infrastructure-cluster-x-k8s-io-v1beta1-kkmachinetemplate,mutating=true,failurePolicy=fail,sideEffects=None,groups=infrastructure.cluster.x-k8s.io,resources=kkmachinetemplates,verbs=create;update,versions=v1beta1,name=default.kkmachinetemplate.infrastructure.cluster.x-k8s.io,admissionReviewVersions=v1

var _ webhook.Defaulter = &KKMachineTemplate{}

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

defaultContainerManager(&k.Spec.Template.Spec)
}

//+kubebuilder:webhook:path=/validate-infrastructure-cluster-x-k8s-io-v1beta1-kkmachinetemplate,mutating=false,failurePolicy=fail,sideEffects=None,groups=infrastructure.cluster.x-k8s.io,resources=kkmachinetemplates,verbs=create;update,versions=v1beta1,name=validation.kkmachinetemplate.infrastructure.cluster.x-k8s.io,admissionReviewVersions=v1

var _ webhook.Validator = &KKMachineTemplate{}

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

spec := k.Spec.Template.Spec
var allErrs field.ErrorList
allErrs = append(allErrs, validateRepository(spec.Repository)...)
return aggregateObjErrors(k.GroupVersionKind().GroupKind(), k.Name, allErrs)
}

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

spec := k.Spec.Template.Spec
var allErrs field.ErrorList
allErrs = append(allErrs, validateRepository(spec.Repository)...)
return aggregateObjErrors(k.GroupVersionKind().GroupKind(), k.Name, allErrs)
}

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

return nil
}

0 comments on commit b731d2f

Please sign in to comment.