Skip to content

Commit

Permalink
Implemented the unmanaged cluster CRD for adopting existing k8s clusters
Browse files Browse the repository at this point in the history
  • Loading branch information
kylewuolle committed Nov 11, 2024
1 parent 55bf573 commit 91870b8
Show file tree
Hide file tree
Showing 24 changed files with 2,007 additions and 2 deletions.
16 changes: 14 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,12 @@ FLUX_SOURCE_REPO_NAME ?= source-helmrepositories
FLUX_SOURCE_REPO_CRD ?= $(EXTERNAL_CRD_DIR)/$(FLUX_SOURCE_REPO_NAME)-$(FLUX_SOURCE_VERSION).yaml
FLUX_SOURCE_CHART_NAME ?= source-helmchart
FLUX_SOURCE_CHART_CRD ?= $(EXTERNAL_CRD_DIR)/$(FLUX_SOURCE_CHART_NAME)-$(FLUX_SOURCE_VERSION).yaml

FLUX_HELM_VERSION ?= $(shell go mod edit -json | jq -r '.Require[] | select(.Path == "github.com/fluxcd/helm-controller/api") | .Version')
FLUX_HELM_CRD ?= $(EXTERNAL_CRD_DIR)/helm-$(FLUX_HELM_VERSION).yaml
CAPI_VERSION ?= v1.8.4
CAPI_CRD ?= $(EXTERNAL_CRD_DIR)/capi-$(CAPI_VERSION).yaml
K0SMOTRON_VERSION ?= $(shell go mod edit -json | jq -r '.Require[] | select(.Path == "github.com/k0sproject/k0smotron") | .Version')
K0SMOTRON_CRD ?= $(EXTERNAL_CRD_DIR)/k0smotron-$(K0SMOTRON_VERSION).yaml
FLUX_HELM_NAME ?= helm
FLUX_HELM_CRD ?= $(EXTERNAL_CRD_DIR)/$(FLUX_HELM_NAME)-$(FLUX_HELM_VERSION).yaml

Expand Down Expand Up @@ -478,8 +482,16 @@ $(SVELTOS_CRD): | yq $(EXTERNAL_CRD_DIR)
rm -f $(EXTERNAL_CRD_DIR)/$(SVELTOS_NAME)*
curl -s --fail https://raw.githubusercontent.com/projectsveltos/sveltos/$(SVELTOS_VERSION)/manifest/crds/sveltos_crds.yaml > $(SVELTOS_CRD)

$(K0SMOTRON_CRD): $(EXTERNAL_CRD_DIR)
rm -f $(K0SMOTRON_CRD)
curl -s https://raw.githubusercontent.com/k0sproject/k0smotron/$(K0SMOTRON_VERSION)/config/crd/bases/infrastructure.cluster.x-k8s.io_remoteclusters.yaml > $(K0SMOTRON_CRD)

$(CAPI_CRD): $(EXTERNAL_CRD_DIR)
rm -f $(CAPI_CRD)
curl -s https://raw.githubusercontent.com/kubernetes-sigs/cluster-api/$(CAPI_VERSION)/config/crd/bases/cluster.x-k8s.io_clusters.yaml > $(CAPI_CRD)

.PHONY: external-crd
external-crd: $(FLUX_HELM_CRD) $(FLUX_SOURCE_CHART_CRD) $(FLUX_SOURCE_REPO_CRD) $(SVELTOS_CRD)
external-crd: $(FLUX_HELM_CRD) $(FLUX_SOURCE_CHART_CRD) $(FLUX_SOURCE_REPO_CRD) $(SVELTOS_CRD) $(K0SMOTRON_CRD) $(CAPI_CRD)

.PHONY: kind
kind: $(KIND) ## Download kind locally if necessary.
Expand Down
18 changes: 18 additions & 0 deletions PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,22 @@ resources:
kind: MultiClusterService
path: github.com/Mirantis/hmc/api/v1alpha1
version: v1alpha1
- api:
crdVersion: v1
namespaced: true
controller: true
domain: hmc.mirantis.com
group: hmc.mirantis.com
kind: UnmanagedCluster
path: github.com/Mirantis/hmc/api/v1alpha
version: v1alpha1
- api:
crdVersion: v1
namespaced: true
controller: true
domain: hmc.mirantis.com
group: hmc.mirantis.com
kind: UnmanagedMachine
path: github.com/Mirantis/hmc/api/v1alpha1
version: v1alpha1
version: "3"
95 changes: 95 additions & 0 deletions api/v1alpha1/unmanagedcluster_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright 2024
//
// 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"
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.

const (
UnmanagedClusterKind = "UnmanagedCluster"
UnmanagedClusterFinalizer = "hmc.mirantis.com/unmanage-dcluster"
AllNodesCondition = "AllNodesCondition"
NodeCondition = "NodeCondition"
HelmChart = "HelmChart"
)

// UnmanagedClusterSpec defines the desired state of UnmanagedCluster
type UnmanagedClusterSpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "make" to regenerate code after modifying this file

Name string `json:"name,omitempty"`
// Services is a list of services created via ServiceTemplates
// that could be installed on the target cluster.
Services []ServiceSpec `json:"services,omitempty"`

// ServicesPriority sets the priority for the services defined in this spec.
// Higher value means higher priority and lower means lower.
// In case of conflict with another object managing the service,
// the one with higher priority will get to deploy its services.
ServicesPriority int32 `json:"servicesPriority,omitempty"`
// DryRun specifies whether the template should be applied after validation or only validated.
// DryRun bool `json:"dryRun,omitempty"`

// +kubebuilder:default:=false

// StopOnConflict specifies what to do in case of a conflict.
// E.g. If another object is already managing a service.
// By default the remaining services will be deployed even if conflict is detected.
// If set to true, the deployment will stop after encountering the first conflict.
StopOnConflict bool `json:"stopOnConflict,omitempty"`
}

// UnmanagedClusterStatus defines the observed state of UnmanagedCluster
type UnmanagedClusterStatus struct {
// Flag indicating whether the unmanaged cluster is in the ready state or not
Ready bool `json:"ready"`

// Conditions contains details for the current state of the ManagedCluster.
Conditions []metav1.Condition `json:"conditions,omitempty"`
}

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:metadata:labels=cluster.x-k8s.io/v1beta1=v1alpha1
// UnmanagedCluster is the Schema for the unmanagedclusters API
type UnmanagedCluster struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec UnmanagedClusterSpec `json:"spec,omitempty"`
Status UnmanagedClusterStatus `json:"status,omitempty"`
}

// +kubebuilder:object:root=true

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

func init() {
SchemeBuilder.Register(&UnmanagedCluster{}, &UnmanagedClusterList{})
}

func (in *UnmanagedCluster) GetConditions() *[]metav1.Condition {
return &in.Status.Conditions
}
69 changes: 69 additions & 0 deletions api/v1alpha1/unmanagedmachine_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright 2024
//
// 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"
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.

// UnmanagedMachineSpec defines the desired state of UnmanagedMachine
type UnmanagedMachineSpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "make" to regenerate code after modifying this file
ProviderID string `json:"providerID,omitempty"`
ClusterName string `json:"clusterName,omitempty"`
}

// UnmanagedMachineStatus defines the observed state of UnmanagedMachine
type UnmanagedMachineStatus struct {
// Flag indicating whether the machine is in the ready state or not
Ready bool `json:"ready,omitempty"`
// Conditions contains details for the current state of the ManagedCluster
Conditions []metav1.Condition `json:"conditions,omitempty"`
}

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:printcolumn:name="Ready",type="string",JSONPath=".status.ready",description="Machine ready status"
// +kubebuilder:metadata:labels=cluster.x-k8s.io/v1beta1=v1alpha1

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

Spec UnmanagedMachineSpec `json:"spec,omitempty"`
Status UnmanagedMachineStatus `json:"status,omitempty"`
}

// +kubebuilder:object:root=true

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

func init() {
SchemeBuilder.Register(&UnmanagedMachine{}, &UnmanagedMachineList{})
}

func (in *UnmanagedMachine) GetConditions() *[]metav1.Condition {
return &in.Status.Conditions
}
Loading

0 comments on commit 91870b8

Please sign in to comment.