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

feat: add capkk #2478

Draft
wants to merge 1 commit into
base: feature-gitops
Choose a base branch
from
Draft
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
42 changes: 0 additions & 42 deletions .github/workflows/sync-vendor.yml

This file was deleted.

15 changes: 12 additions & 3 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -275,12 +275,21 @@ linters-settings:
# kubekey
- pkg: "github.com/kubesphere/kubekey/v4/pkg/const"
alias: _const
- pkg: "github.com/kubesphere/kubekey/v4/pkg/apis/core/v1"
- pkg: "github.com/kubesphere/kubekey/api/core/v1"
alias: kkcorev1
- pkg: "github.com/kubesphere/kubekey/v4/pkg/apis/core/v1alpha1"
- pkg: "github.com/kubesphere/kubekey/api/core/v1alpha1"
alias: kkcorev1alpha1
- pkg: "github.com/kubesphere/kubekey/v4/pkg/apis/project/v1"
- pkg: "github.com/kubesphere/kubekey/api/project/v1"
alias: kkprojectv1
- pkg: "github.com/kubesphere/kubekey/api/capkk/infrastructure/v1beta1"
alias: capkkinfrav1beta1
# cluster-api
- pkg: "sigs.k8s.io/cluster-api/api/v1beta1"
alias: clusterv1beta1
- pkg: "sigs.k8s.io/cluster-api/util"
alias: clusterutil
- pkg: "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1"
alias: kubeadmcpv1beta1
nestif:
# Minimal complexity of if statements to report.
# Default: 5
Expand Down
13 changes: 6 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ SETUP_ENVTEST_BIN := setup-envtest
SETUP_ENVTEST := $(abspath $(OUTPUT_TOOLS_DIR)/$(SETUP_ENVTEST_BIN)-$(SETUP_ENVTEST_VER))
SETUP_ENVTEST_PKG := sigs.k8s.io/controller-runtime/tools/setup-envtest

CONTROLLER_GEN_VER := v0.15.0
CONTROLLER_GEN_VER := v0.16.5
CONTROLLER_GEN_BIN := controller-gen
CONTROLLER_GEN := $(abspath $(OUTPUT_TOOLS_DIR)/$(CONTROLLER_GEN_BIN)-$(CONTROLLER_GEN_VER))
CONTROLLER_GEN_PKG := sigs.k8s.io/controller-tools/cmd/controller-gen
Expand Down Expand Up @@ -163,30 +163,29 @@ generate: ## Run all generate-manifests-*, generate-go-deepcopy-* targets

.PHONY: generate-go-deepcopy-kubekey
generate-go-deepcopy-kubekey: $(CONTROLLER_GEN) ## Generate deepcopy object
$(MAKE) clean-generated-deepcopy SRC_DIRS="./pkg/apis/"
$(MAKE) clean-generated-deepcopy SRC_DIRS="./api/"
$(CONTROLLER_GEN) \
object:headerFile=./hack/boilerplate.go.txt \
paths=./pkg/apis/...
paths=./api/...

.PHONY: generate-manifests-kubekey
generate-manifests-kubekey: $(CONTROLLER_GEN) ## Generate kubekey manifests e.g. CRD, RBAC etc.
$(CONTROLLER_GEN) \
paths=./pkg/apis/core/... \
paths=./api/core/... \
crd \
output:crd:dir=./config/kubekey/crds/

.PHONY: generate-manifests-capkk
generate-manifests-capkk: $(CONTROLLER_GEN) ## Generate capkk manifests e.g. CRD, RBAC etc.
$(CONTROLLER_GEN) \
paths=./pkg/apis/capkk/... \
paths=./api/capkk/... \
crd webhook \
output:crd:dir=./config/capkk/crds/ \
output:webhook:dir=./config/capkk/webhook/

.PHONY: generate-modules
generate-modules: ## Run go mod tidy to ensure modules are up to date
@go mod tidy && go mod vendor

@go mod tidy
.PHONY: generate-goimports
generate-goimports: ## Format all import, `goimports` is required.
@hack/update-goimports.sh
Expand Down
174 changes: 174 additions & 0 deletions api/capkk/infrastructure/v1beta1/kkcluster_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
/*
Copyright 2024 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 (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
clusterv1beta1 "sigs.k8s.io/cluster-api/api/v1beta1"
)

const (
// ClusterFinalizer allows ReconcileKKCluster to clean up KK resources associated with KKCluster before
// removing it from the apiserver.
KKClusterFinalizer = "kkcluster.infrastructure.cluster.x-k8s.io"
)

const (
// KKClusterNodeReachedCondition represents the condition type indicating whether the hosts
// defined in the inventory are reachable.
KKClusterNodeReachedCondition clusterv1beta1.ConditionType = "NodeReached"
// KKClusterNodeReachedConditionReasonWaiting indicates that the node reachability check is pending.
// This check is triggered when the corresponding inventory host's configuration changes.
KKClusterNodeReachedConditionReasonWaiting = "waiting for node status check"
// KKClusterNodeReachedConditionReasonUnreached indicates that the node reachability check has failed.
// This means the node is currently offline or inaccessible.
KKClusterNodeReachedConditionReasonUnreached = "node is unreachable"

// KKClusterKKMachineConditionReady represents the condition type indicating whether the associated inventory
// has been successfully marked as ready.
KKClusterKKMachineConditionReady clusterv1beta1.ConditionType = "KKClusterReady"
// KKClusterKKMachineConditionReadyReasonWaiting indicates that the associated inventory is still being synchronized.
KKClusterKKMachineConditionReadyReasonWaiting = "waiting for kkmachine sync"
// KKMachineKKMachineConditionReasonSyncing indicates that the associated inventory has been successfully synchronized.
KKMachineKKMachineConditionReasonSyncing = "syncing for kkmachine"
// KKMachineKKMachineConditionReasonFailed indicates that the associated inventory synchronization process has failed.
KKMachineKKMachineConditionReasonFailed = "kkmachine run failed"
)

// HostSelectorPolicy defines the strategy for synchronizing hosts to a specific group.
type HostSelectorPolicy string

const (
// HostSelectorRandom the host will be selected randomly.
HostSelectorRandom HostSelectorPolicy = "Random"
// HostSelectorSequence the host will be selected sequentially.
HostSelectorSequence HostSelectorPolicy = "Sequence"
)

type KKClusterFailedReason string

const (
// KKClusterFailedReasonUnknown like cannot get resource from kubernetes.
KKClusterFailedUnknown KKClusterFailedReason = "unknown"
// KKClusterFailedReasonInvalidHosts like hosts defined in kkcluster is invalid.
KKClusterFailedInvalidHosts KKClusterFailedReason = "hosts defined in kkcluster is invalid."
// KKClusterFailedReasonSyncInventory like failed to sync inventory.
KKClusterFailedSyncInventory KKClusterFailedReason = "failed to sync inventory"
// KKClusterFailedReasonSyncCPKKMachine like failed to sync control_plane kkmachine.
KKClusterFailedSyncCPKKMachine KKClusterFailedReason = "sync control_plane kkmachine failed."
// KKClusterFailedReasonSyncWorkerKKMachine like failed to sync worker kkmachine.
KKClusterFailedSyncWorkerKKMachine KKClusterFailedReason = "sync worker kkmachine failed."
)

type InventoryHostConnector struct {
// Type to connector the host. de
Type string `json:"type"`
// Host address.
Host string `json:"host"`
// User is the user name of the host. default is root.
// +optional
User string `json:"user,omitempty"`
// Password is the password of the host.
// +optional
Password string `json:"password,omitempty"`
// PrivateKey is the private key of the host. default is ~/.ssh/id_rsa.
// +optional
PrivateKey string `json:"privateKey,omitempty"`
}
type InventoryHost struct {
// Name of the host.
Name string `json:"name"`
// Vars for the host.
// +optional
// +kubebuilder:pruning:PreserveUnknownFields
Vars runtime.RawExtension `json:"vars,omitempty"`
// Connector to connect the host.
Connector InventoryHostConnector `json:"connector"`
}

// KKClusterSpec defines the desired state of KKCluster.
type KKClusterSpec struct {
// InventoryHosts contains all hosts of the cluster.
InventoryHosts []InventoryHost `json:"hosts,omitempty"`
// which Group defined in Inventory will be checked. there is some default group by system:
// - all: contains all hosts
// - ungrouped: contains hosts which do not belong to any groups.
// if the value is empty, "ungrouped" will be used.
HostCheckGroup string `json:"hostCheckGroup,omitempty"`
// tolerate defines if tolerate host check if failed.
Tolerate bool `json:"tolerate,omitempty"`
// HostSelectorPolicy defines the strategy for synchronizing hosts to a specific group.
HostSelectorPolicy HostSelectorPolicy `json:"hostSelectorPolicy,omitempty"`
}

// KKClusterStatus defines the observed state of KKCluster.
type KKClusterStatus struct {
// if Ready to create cluster. usage after inventory is ready.
Ready bool `json:"ready"`

// FailureReason
FailureReason KKClusterFailedReason `json:"failureReason,omitempty"`

FailureMessage string `json:"failureMessage,omitempty"`

// Conditions defines current service state of the KKCluster.
// +optional
Conditions clusterv1beta1.Conditions `json:"conditions,omitempty"`
}

// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +k8s:openapi-gen=true
// +kubebuilder:resource:scope=Namespaced,categories=cluster-api,shortName=kkc
// +kubebuilder:subresource:status
// +kubebuilder:metadata:labels="cluster.x-k8s.io/v1beta1=v1beta1"
// +kubebuilder:printcolumn:name="Cluster",type="string",JSONPath=".metadata.labels.cluster\\.x-k8s\\.io/cluster-name",description="Cluster to which this KKClusters belongs"
// +kubebuilder:printcolumn:name="Ready",type="string",JSONPath=".status.ready",description="Cluster infrastructure is ready for SSH instances"
// +kubebuilder:printcolumn:name="Endpoint",type="string",JSONPath=".spec.controlPlaneEndpoint",description="API Endpoint",priority=1

// KKCluster resource maps a kubernetes cluster, manage and reconcile cluster status.
type KKCluster struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec KKClusterSpec `json:"spec,omitempty"`
Status KKClusterStatus `json:"status,omitempty"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

// KKClusterList of KKCluster
type KKClusterList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []KKCluster `json:"items"`
}

func init() {
SchemeBuilder.Register(&KKCluster{}, &KKClusterList{})
}

// GetConditions returns the observations of the operational state of the KKCluster resource.
func (k *KKCluster) GetConditions() clusterv1beta1.Conditions {
return k.Status.Conditions
}

// SetConditions sets the underlying service state of the KKCluster to the predescribed clusterv1beta1.Conditions.
func (k *KKCluster) SetConditions(conditions clusterv1beta1.Conditions) {
k.Status.Conditions = conditions
}
Loading
Loading