Skip to content

Commit

Permalink
Add adopted cluster template to adopt existing k8s clusters
Browse files Browse the repository at this point in the history
  • Loading branch information
kylewuolle committed Dec 23, 2024
1 parent 1070b58 commit f96ce95
Show file tree
Hide file tree
Showing 18 changed files with 428 additions and 21 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ or install using `helm`
helm install hmc oci://ghcr.io/mirantis/hmc/charts/hmc --version 0.0.5 -n hmc-system --create-namespace
```

Then follow the [Deploy a managed cluster](#deploy-a-managed-cluster) guide to
create a managed cluster.
Then follow the [Deploy a cluster deployment](#deploy-a-cluster-deployment) guide to
create a cluster deployment.

> [!NOTE]
> The HMC installation using Kubernetes manifests does not allow
Expand All @@ -51,7 +51,7 @@ Mirantis Hybrid Container Cloud requires the following:
Optionally, the following CLIs may be helpful:

1. `helm` (required only when installing HMC using `helm`).
2. `clusterctl` (to handle the lifecycle of the managed clusters).
2. `clusterctl` (to handle the lifecycle of the cluster deployments).

### Providers configuration

Expand Down Expand Up @@ -109,9 +109,9 @@ own `Management` configuration:

`kubectl --kubeconfig <path-to-management-kubeconfig> create -f management.yaml`

## Deploy a managed cluster
## Create a ClusterDeployment

To deploy a managed cluster:
To create a ClusterDeployment:

1. Create `Credential` object with all credentials required.

Expand Down Expand Up @@ -173,7 +173,7 @@ kubectl -n <clusterdeployment-namespace> get cluster <clusterdeployment-name> -o
> cluster <clusterdeployment-name> -n <clusterdeployment-namespace> --show-conditions
> all ```

6. Retrieve the `kubeconfig` of your managed cluster:
6. Retrieve the `kubeconfig` of your cluster deployment:

```
kubectl get secret -n hmc-system <clusterdeployment-name>-kubeconfig -o=jsonpath={.data.value} | base64 -d > kubeconfig
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ type ClusterDeploymentStatus struct {

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:resource:shortName=mcluster;mcl
// +kubebuilder:resource:shortName=clusterd;cld
// +kubebuilder:printcolumn:name="ready",type="string",JSONPath=".status.conditions[?(@.type==\"Ready\")].status",description="Ready",priority=0
// +kubebuilder:printcolumn:name="status",type="string",JSONPath=".status.conditions[?(@.type==\"Ready\")].message",description="Status",priority=0
// +kubebuilder:printcolumn:name="dryRun",type="string",JSONPath=".spec.dryRun",description="Dry Run",priority=1
Expand Down
8 changes: 4 additions & 4 deletions api/v1alpha1/indexers.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func SetupIndexers(ctx context.Context, mgr ctrl.Manager) error {
return merr
}

// managed cluster
// cluster deployment

// ClusterDeploymentTemplateIndexKey indexer field name to extract ClusterTemplate name reference from a ClusterDeployment object.
const ClusterDeploymentTemplateIndexKey = ".spec.template"
Expand Down Expand Up @@ -89,12 +89,12 @@ func ExtractServiceTemplateNamesFromClusterDeployment(rawObj client.Object) []st
const ClusterDeploymentCredentialIndexKey = ".spec.credential"

func setupClusterDeploymentCredentialIndexer(ctx context.Context, mgr ctrl.Manager) error {
return mgr.GetFieldIndexer().IndexField(ctx, &ClusterDeployment{}, ClusterDeploymentCredentialIndexKey, ExtractCredentialNameFromClusterDeployment)
return mgr.GetFieldIndexer().IndexField(ctx, &ClusterDeployment{}, ClusterDeploymentCredentialIndexKey, extractCredentialNameFromClusterDeployment)
}

// ExtractCredentialNameFromClusterDeployment returns referenced Credential name
// extractCredentialNameFromClusterDeployment returns referenced Credential name
// declared in a ClusterDeployment object.
func ExtractCredentialNameFromClusterDeployment(rawObj client.Object) []string {
func extractCredentialNameFromClusterDeployment(rawObj client.Object) []string {
cluster, ok := rawObj.(*ClusterDeployment)
if !ok {
return nil
Expand Down
16 changes: 16 additions & 0 deletions config/dev/adopted-clusterdeployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
apiVersion: hmc.mirantis.com/v1alpha1
kind: ClusterDeployment
metadata:
name: adopted-dev
namespace: ${NAMESPACE}
spec:
template: adopted-cluster-0-0-1
credential: adopted-cluster-cred
config: {}
services:
- template: kyverno-3-2-6
name: kyverno
namespace: kyverno
- template: ingress-nginx-4-11-0
name: ingress-nginx
namespace: ingress-nginx
21 changes: 21 additions & 0 deletions config/dev/adopted-credentials.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
apiVersion: v1
data:
value: ${KUBECONFIG_DATA}
kind: Secret
metadata:
name: adopted-cluster-kubeconf
namespace: ${NAMESPACE}
type: Opaque
---
apiVersion: hmc.mirantis.com/v1alpha1
kind: Credential
metadata:
name: adopted-cluster-cred
namespace: ${NAMESPACE}
spec:
description: Adopted Credentials
identityRef:
apiVersion: v1
kind: Secret
name: adopted-cluster-kubeconf
namespace: ${NAMESPACE}
2 changes: 1 addition & 1 deletion docs/dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ another provider change `DEV_PROVIDER` variable with the name of provider before
running make (e.g. `export DEV_PROVIDER=azure`).

1. Configure your cluster parameters in provider specific file
(for example `config/dev/aws-clusterDeployment.yaml` in case of AWS):
(for example `config/dev/aws-clusterdeployment.yaml` in case of AWS):

* Configure the `name` of the ClusterDeployment
* Change instance type or size for control plane and worker machines
Expand Down
4 changes: 1 addition & 3 deletions internal/controller/clusterdeployment_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,7 @@ func (r *ClusterDeploymentReconciler) Reconcile(ctx context.Context, req ctrl.Re
return r.reconcileUpdate(ctx, clusterDeployment)
}

func (r *ClusterDeploymentReconciler) setStatusFromChildObjects(
ctx context.Context, clusterDeployment *hmc.ClusterDeployment, gvr schema.GroupVersionResource, conditions []string,
) (requeue bool, _ error) {
func (r *ClusterDeploymentReconciler) setStatusFromChildObjects(ctx context.Context, clusterDeployment *hmc.ClusterDeployment, gvr schema.GroupVersionResource, conditions []string) (requeue bool, _ error) {
l := ctrl.LoggerFrom(ctx)

resourceConditions, err := status.GetResourceConditions(ctx, clusterDeployment.Namespace, r.DynamicClient, gvr,
Expand Down
4 changes: 1 addition & 3 deletions internal/controller/management_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,14 @@ func (r *ManagementReconciler) Update(ctx context.Context, management *hmc.Manag
errs error

statusAccumulator = &mgmtStatusAccumulator{
providers: hmc.Providers{},
providers: hmc.Providers{"infrastructure-internal"},
components: make(map[string]hmc.ComponentStatus),
compatibilityContracts: make(map[string]hmc.CompatibilityContracts),
}

requeue bool
)

statusAccumulator.providers = append(statusAccumulator.providers, "infrastructure-internal")

for _, component := range components {
l.V(1).Info("reconciling components", "component", component)
template := new(hmc.ProviderTemplate)
Expand Down
23 changes: 23 additions & 0 deletions templates/cluster/adopted-cluster/.helmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Patterns to ignore when building packages.
# This supports shell glob matching, relative path matching, and
# negation (prefixed with !). Only one pattern per line.
.DS_Store
# Common VCS dirs
.git/
.gitignore
.bzr/
.bzrignore
.hg/
.hgignore
.svn/
# Common backup files
*.swp
*.bak
*.tmp
*.orig
*~
# Various IDEs
.project
.idea/
*.tmproj
.vscode/
11 changes: 11 additions & 0 deletions templates/cluster/adopted-cluster/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: v2
name: adopted-cluster
description: |
An HMC template to adopt an already existing kubernetes cluster
type: application
# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 0.0.1
annotations:
cluster.x-k8s.io/provider: infrastructure-internal
4 changes: 4 additions & 0 deletions templates/cluster/adopted-cluster/templates/_helpers.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{{- define "cluster.name" -}}
{{- .Release.Name | trunc 63 | trimSuffix "-" }}
{{- end }}

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: lib.projectsveltos.io/v1beta1
kind: SveltosCluster
metadata:
name: {{ include "cluster.name" . }}
spec:
consecutiveFailureThreshold: {{ .Values.consecutiveFailureThreshold }}
kubeconfigName: {{ .Values.clusterIdentity.name }}
15 changes: 15 additions & 0 deletions templates/cluster/adopted-cluster/values.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"description": "An HMC template to adopt an existing k8s cluster.",
"type": "object",
"required": [
"clusterIdentity"
],
"properties": {
"consecutiveFailureThreshold": {
"description": "The number of the failures prior to setting the status condition",
"type": "integer",
"minimum": 1
}
}
}
6 changes: 6 additions & 0 deletions templates/cluster/adopted-cluster/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Cluster parameters

clusterIdentity:
name: "adopted-cluster-identity"

consecutiveFailureThreshold: 3
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
apiVersion: hmc.mirantis.com/v1alpha1
kind: ClusterTemplate
metadata:
name: adopted-cluster-0-0-1
annotations:
helm.sh/resource-policy: keep
spec:
helm:
chartSpec:
chart: adopted-cluster
version: 0.0.1
interval: 10m0s
sourceRef:
kind: HelmRepository
name: hmc-templates
Loading

0 comments on commit f96ce95

Please sign in to comment.