Skip to content

Commit

Permalink
Merge pull request #279 from richardcase/providers_api
Browse files Browse the repository at this point in the history
feat: add api for capi provider
  • Loading branch information
alexander-demicev authored Nov 29, 2023
2 parents 27dfe13 + ddfeae2 commit 32b61c6
Show file tree
Hide file tree
Showing 19 changed files with 3,987 additions and 47 deletions.
41 changes: 31 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,42 @@ help: ## Display this help.
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)

##@ Development
.PHONY: generate
generate: vendor ## Run all generators
$(MAKE) vendor
$(MAKE) generate-modules
$(MAKE) generate-manifests-api
$(MAKE) generate-manifests-external
$(MAKE) generate-go-deepcopy
$(MAKE) vendor-clean

.PHONY: manifests
manifests: vendor controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
manifests: generate

.PHONY: generate-manifests-external
generate-manifests-external: vendor controller-gen ## Generate ClusterRole and CustomResourceDefinition objects.
$(CONTROLLER_GEN) rbac:roleName=manager-role crd paths="./internal/rancher/..." output:crd:artifacts:config=hack/crd/bases
$(CONTROLLER_GEN) rbac:roleName=manager-role crd paths="./vendor/sigs.k8s.io/cluster-api/..." output:crd:artifacts:config=hack/crd/bases
# Vendor is only required for pulling latest CRDs from the dependencies
$(MAKE) vendor-clean

.PHONY: generate-manifests-api
generate-manifests-api: controller-gen ## Generate ClusterRole and CustomResourceDefinition objects.
$(CONTROLLER_GEN) rbac:roleName=manager-role crd paths="./api/..." \
output:crd:artifacts:config=./config/crd/bases \
output:rbac:dir=./config/rbac \

.PHONY: generate-modules
generate-modules: ## Run go mod tidy to ensure modules are up to date
go mod tidy
cd $(TEST_DIR); go mod tidy

.PHONY: generate-go-deepcopy
generate-go-deepcopy: ## Run deepcopy generation
$(CONTROLLER_GEN) \
object:headerFile=./hack/boilerplate.go.txt \
paths=./api/...

# Run go mod
.PHONY: vendor
vendor:
Expand All @@ -196,14 +225,6 @@ vendor:
vendor-clean:
rm -rf vendor

.PHONY: generate
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.

.PHONY: generate-modules
generate-modules: ## Run go mod tidy to ensure modules are up to date
go mod tidy
cd $(TEST_DIR); go mod tidy

.PHOHY: dev-env
dev-env: ## Create a local development environment
./scripts/turtles-dev.sh ${RANCHER_HOSTNAME}
Expand Down Expand Up @@ -313,7 +334,7 @@ uninstall: manifests kustomize ## Uninstall CRDs from the K8s cluster specified

.PHONY: deploy
deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
cd config/manager && $(KUSTOMIZE) edit set image controller=$(CONTROLLER_IMG)
$(KUSTOMIZE) build config/default | kubectl apply -f -

.PHONY: undeploy
Expand Down
14 changes: 12 additions & 2 deletions PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,19 @@
# This file is used to track the info used to scaffold your project
# and allow the plugins properly work.
# More info: https://book.kubebuilder.io/reference/project-config.html
domain: capi.cattle.io
domain: cattle.io
layout:
- go.kubebuilder.io/v4-alpha
- go.kubebuilder.io/v4
projectName: rancher-turtles
repo: github.com/rancher-sandbox/rancher-turtles
resources:
- api:
crdVersion: v1
namespaced: true
controller: true
domain: cattle.io
group: turtles-capi
kind: CAPIProvider
path: github.com/rancher-sandbox/rancher-turtles/api/v1alpha1
version: v1alpha1
version: "3"
142 changes: 142 additions & 0 deletions api/v1alpha1/capiprovider_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*
Copyright SUSE 2023.
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 v1alpha1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

operatorv1 "sigs.k8s.io/cluster-api-operator/api/v1alpha2"
)

const (
// ProviderFinalizer is the finalizer apply on the CAPI Provider resource.
ProviderFinalizer = "capiprovider.turtles.cattle.io"
)

// CAPIProviderSpec defines the desired state of CAPIProvider.
// +kubebuilder:validation:XValidation:message="CAPI Provider version should be in the semver format",rule="!has(self.version) || self.version.matches(r\"\"\"^([0-9]+)\\.([0-9]+)\\.([0-9]+)(?:-([0-9A-Za-z-]+(?:\\.[0-9A-Za-z-]+)*))?(?:\\+[0-9A-Za-z-]+)?$\"\"\")"
//
//nolint:lll
type CAPIProviderSpec struct {
// Name is the name of the provider to enable
// +required
// +kubebuilder:validation:Enum=aws;azure;gcp;docker;rke2
// +kubebuilder:example=aws
Name ProviderName `json:"name"`

// Type is the type of the provider to enable
// +required
// +kubebuilder:validation:Enum=infrastructure;core;controlPlane;bootstrap;addon
// +kubebuilder:example=infrastructure
Type ProviderType `json:"type"`

// Credentials is the structure holding the credentials to use for the provider. Only one credential type could be set at a time.
// +kubebuilder:example={rancherCloudCredential: user-credential}
// +optional
Credentials *ProviderCredentials `json:"credentials,omitempty"`

// Features is a collection of features to enable.
// +optional
// +kubebuilder:example={machinePool: true, clusterResourceSet: true, clusterTopology: true}
Features *Features `json:"features,omitempty"`

// Variables is a map of environment variables to add to the content of the ConfigSecret
// +optional
// +kubebuilder:example={CLUSTER_TOPOLOGY:"true",EXP_CLUSTER_RESOURCE_SET:"true",EXP_MACHINE_POOL: "true"}
Variables map[string]string `json:"variables"`

// ProviderSpec is the spec of the underlying CAPI Provider resource.
ProviderSpec *operatorv1.ProviderSpec `json:",inline"`
}

// Features defines a collection of features for the CAPI Provider to apply.
type Features struct {
// MachinePool if set to true will enable the machine pool feature.
MachinePool bool `json:"machinePool,omitempty"`

// ClusterResourceSet if set to true will enable the cluster resource set feature.
ClusterResourceSet bool `json:"clusterResourceSet,omitempty"`

// ClusterTopology if set to true will enable the clusterclass feature.
ClusterTopology bool `json:"clusterTopology,omitempty"`
}

// ProviderCredentials defines the external credentials information for the provider.
// +kubebuilder:validation:MaxProperties=1
// +kubebuilder:validation:MinProperties=1
// +structType=atomic
//
//nolint:godot
type ProviderCredentials struct {
// RancherCloudCredential is the Rancher Cloud Credential name
RancherCloudCredential string `json:"rancherCloudCredential,omitempty"`

// +optional
// TODO: decide how to handle workload identity
// WorkloadIdentityRef *WorkloadIdentityRef `json:"workloadIdentityRef,omitempty"`
}

// WorkloadIdentityRef is a reference to an identity to be used when reconciling the cluster.
type WorkloadIdentityRef struct {
// Name of the identity
// +kubebuilder:validation:MinLength=1
Name string `json:"name"`

// Kind of the identity
Kind string `json:"kind"`
}

// CAPIProviderStatus defines the observed state of CAPIProvider.
type CAPIProviderStatus struct {
// Indicates the provider status
// +kubebuilder:default=Pending
State ProviderState `json:"state,omitempty"`

// Variables is a map of environment variables added to the content of the ConfigSecret
// +kubebuilder:default={CLUSTER_TOPOLOGY:"true",EXP_CLUSTER_RESOURCE_SET:"true",EXP_MACHINE_POOL: "true"}
Variables map[string]string `json:"variables,omitempty"`

ProviderStatus *operatorv1.ProviderStatus `json:",inline"`
}

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status

// CAPIProvider is the Schema for the CAPI Providers API.
type CAPIProvider struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

// +kubebuilder:example={name: aws, version: "v2.3.0", type: infrastructure, credentials: {rancherCloudCredential: user-credential}}
Spec CAPIProviderSpec `json:"spec,omitempty"`

// +kubebuilder:default={}
Status CAPIProviderStatus `json:"status,omitempty"`
}

//+kubebuilder:object:root=true

// CAPIProviderList contains a list of CAPIProviders.
type CAPIProviderList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []CAPIProvider `json:"items"`
}

func init() {
SchemeBuilder.Register(&CAPIProvider{}, &CAPIProviderList{})
}
36 changes: 36 additions & 0 deletions api/v1alpha1/groupversion_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
Copyright SUSE 2023.
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 v1alpha1 contains API Schema definitions for the turtles-capi.cattle.io v1alpha1 API group
// +kubebuilder:object:generate=true
// +groupName=turtles-capi.cattle.io
package v1alpha1

import (
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/scheme"
)

var (
// GroupVersion is group version used to register these objects.
GroupVersion = schema.GroupVersion{Group: "turtles-capi.cattle.io", Version: "v1alpha1"}

// SchemeBuilder is used to add go types to the GroupVersionKind scheme.
SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion}

// AddToScheme adds the types in this group-version to the given scheme.
AddToScheme = SchemeBuilder.AddToScheme
)
47 changes: 47 additions & 0 deletions api/v1alpha1/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package v1alpha1

// ProviderName defines the designated provider infrastructure provider name.
type ProviderName string

const (
// AWSProvider is the name for the aws provider.
AWSProvider ProviderName = "aws"
// AzureProvider is the name for Azure provider.
AzureProvider ProviderName = "azure"
// GCPProvider is the name for the GCP provider.
GCPProvider ProviderName = "gcp"
// DockerProvider is the name for the docker provider.
DockerProvider ProviderName = "docker"
// RKE2Provider is the name for the RKE2 provider.
RKE2Provider ProviderName = "rke2"
)

// ProviderType defines the type of the CAPI Provider.
type ProviderType string

const (
// InfrastructureProvider is the name for the infrastructure CAPI Provider.
InfrastructureProvider ProviderType = "infrastructure"
// CoreProvider is the name for core CAPI Provider.
CoreProvider ProviderType = "core"
// ControlPlaneProvider is the name for the controlPlane CAPI Provider.
ControlPlaneProvider ProviderType = "controlPlane"
// BootstrapProvider is the name for the bootstrap CAPI Provider.
BootstrapProvider ProviderType = "bootstrap"
// AddonProvider is the name for the addon CAPI Provider.
AddonProvider ProviderType = "addon"
)

// ProviderState defines the current state of the CAPI Provider resource.
type ProviderState string

const (
// Pending status identifies a provder which has not yet started provisioning.
Pending ProviderState = "Pending"
// Provisioning status defines provider in a provisioning state.
Provisioning ProviderState = "Provisioning"
// Ready status identifies that the provider is ready to be used.
Ready ProviderState = "Ready"
// Failed status defines a failed state of provider provisioning.
Failed ProviderState = "Failed"
)
Loading

0 comments on commit 32b61c6

Please sign in to comment.