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

Fix Cluster Manager Duplication #91

Merged
merged 1 commit into from
Jun 20, 2024
Merged
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions charts/kubernetes/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ description: A Helm chart for deploying Unikorn Kubernetes Service

type: application

version: v0.2.17
appVersion: v0.2.17
version: v0.2.18
appVersion: v0.2.18

icon: https://raw.githubusercontent.com/unikorn-cloud/assets/main/images/logos/dark-on-light/icon.png

Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/unikorn/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ type KubernetesClusterSpec struct {
// Region to provision the cluster in.
RegionID string `json:"regionId"`
// ClusterManager that provides lifecycle management for the cluster.
ClusterManager string `json:"clusterManager"`
ClusterManagerID string `json:"clusterManagerId"`
// Version is the Kubernetes version to install. For performance
// reasons this should match what is already pre-installed on the
// provided image.
Expand Down
197 changes: 99 additions & 98 deletions pkg/openapi/schema.go

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions pkg/openapi/server.spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ components:
regionId:
description: The region to provision the cluster in.
type: string
clusterManager:
clusterManagerId:
description: |-
The name of the cluster manager to use, if one is not specified
the system will create one for you.
Expand Down Expand Up @@ -616,7 +616,9 @@ components:
id: c7568e2d-f9ab-453d-9a3a-51375f78426b
name: default
organizationId: d4600d6e-e965-4b44-a808-84fb2fa36702
organizationName: acme-corp
projectId: cae219d7-10e5-4601-8c2c-ee7e066b93ce
projectName: top-secret
creationTime: 2023-07-31T10:45:45Z
provisioningStatus: provisioned
clusterManagersResponse:
Expand All @@ -630,7 +632,9 @@ components:
id: c7568e2d-f9ab-453d-9a3a-51375f78426b
name: default
organizationId: d4600d6e-e965-4b44-a808-84fb2fa36702
organizationName: acme-corp
projectId: cae219d7-10e5-4601-8c2c-ee7e066b93ce
projectName: top-secret
creationTime: 2023-07-31T10:45:45Z
provisioningStatus: provisioned
kubernetesClusterKubeconfigResponse:
Expand All @@ -654,7 +658,7 @@ components:
spec:
regionId: b059b3e6-9ae5-42b7-94b4-f42fb7a6baee
version: v1.27.2
clusterManager: b85525fc-40ca-482b-ac07-eca8b01b09c2
clusterManagerId: b85525fc-40ca-482b-ac07-eca8b01b09c2
workloadPools:
- name: default
machine:
Expand Down
4 changes: 2 additions & 2 deletions pkg/openapi/types.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/provisioners/managers/cluster/provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func (p *Provisioner) getClusterManager(ctx context.Context) (*unikornv1.Cluster

key := client.ObjectKey{
Namespace: projectNamespace.Name,
Name: p.cluster.Spec.ClusterManager,
Name: p.cluster.Spec.ClusterManagerID,
}

if err := coreclient.StaticClientFromContext(ctx).Get(ctx, key, &clusterManager); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/provisioners/managers/clustermanager/provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func (p *Provisioner) Deprovision(ctx context.Context) error {
}

clusters.Items = slices.DeleteFunc(clusters.Items, func(cluster unikornv1.KubernetesCluster) bool {
return cluster.Spec.ClusterManager != p.clusterManager.Name
return cluster.Spec.ClusterManagerID != p.clusterManager.Name
})

if len(clusters.Items) != 0 {
Expand Down
4 changes: 2 additions & 2 deletions pkg/server/handler/cluster/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,13 +287,13 @@ func (c *Client) Create(ctx context.Context, organizationID, projectID string, r
}

// Implicitly create the controller manager.
if request.Spec.ClusterManager == nil {
if request.Spec.ClusterManagerId == nil {
clusterManager, err := clustermanager.NewClient(c.client).CreateImplicit(ctx, organizationID, projectID)
if err != nil {
return err
}

request.Spec.ClusterManager = util.ToPointer(clusterManager.Name)
request.Spec.ClusterManagerId = util.ToPointer(clusterManager.Name)
}

cluster, err := c.generate(ctx, namespace, organizationID, projectID, request)
Expand Down
10 changes: 5 additions & 5 deletions pkg/server/handler/cluster/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ func (c *Client) convert(in *unikornv1.KubernetesCluster) *openapi.KubernetesClu
out := &openapi.KubernetesClusterRead{
Metadata: conversion.ProjectScopedResourceReadMetadata(in, provisioningStatus),
Spec: openapi.KubernetesClusterSpec{
RegionId: in.Spec.RegionID,
ClusterManager: &in.Spec.ClusterManager,
Version: string(*in.Spec.Version),
WorkloadPools: convertWorkloadPools(in),
RegionId: in.Spec.RegionID,
ClusterManagerId: &in.Spec.ClusterManagerID,
Version: string(*in.Spec.Version),
WorkloadPools: convertWorkloadPools(in),
},
}

Expand Down Expand Up @@ -401,7 +401,7 @@ func (c *Client) generate(ctx context.Context, namespace *corev1.Namespace, orga
ObjectMeta: conversion.ProjectScopedObjectMetadata(&request.Metadata, namespace.Name, organizationID, projectID),
Spec: unikornv1.KubernetesClusterSpec{
RegionID: request.Spec.RegionId,
ClusterManager: *request.Spec.ClusterManager,
ClusterManagerID: *request.Spec.ClusterManagerId,
Version: util.ToPointer(unikornv1.SemanticVersion(request.Spec.Version)),
ApplicationBundle: &applicationBundle.Name,
ApplicationBundleAutoUpgrade: &unikornv1.ApplicationBundleAutoUpgradeSpec{},
Expand Down
22 changes: 22 additions & 0 deletions pkg/server/handler/clustermanager/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"slices"

unikornv1core "github.com/unikorn-cloud/core/pkg/apis/unikorn/v1alpha1"
"github.com/unikorn-cloud/core/pkg/constants"
coreopenapi "github.com/unikorn-cloud/core/pkg/openapi"
"github.com/unikorn-cloud/core/pkg/server/conversion"
"github.com/unikorn-cloud/core/pkg/server/errors"
Expand All @@ -37,6 +38,7 @@ import (
corev1 "k8s.io/api/core/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"

"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
Expand Down Expand Up @@ -72,6 +74,26 @@ var (
func (c *Client) CreateImplicit(ctx context.Context, organizationID, projectID string) (*unikornv1.ClusterManager, error) {
log := log.FromContext(ctx)

namespace, err := common.New(c.client).ProjectNamespace(ctx, organizationID, projectID)
if err != nil {
return nil, err
}

var existing unikornv1.ClusterManagerList

options := &client.ListOptions{
Namespace: namespace.Name,
LabelSelector: labels.SelectorFromSet(labels.Set{constants.NameLabel: "default"}),
}

if err := c.client.List(ctx, &existing, options); err != nil {
return nil, err
}

if len(existing.Items) != 0 {
return &existing.Items[0], nil
}

log.Info("creating implicit control plane")

request := &openapi.ClusterManagerWrite{
Expand Down
Loading