Skip to content

Reconcile capc to cks new #2

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ E2E_TIMEOUT ?= 3h
run-e2e: e2e-essentials ## Run e2e testing. JOB is an optional REGEXP to select certainn test cases to run. e.g. JOB=PR-Blocking, JOB=Conformance
$(KUBECTL) apply -f cloud-config.yaml && \
cd test/e2e && \
$(GINKGO) -v --trace --tags=e2e --focus=$(JOB) --timeout=$(E2E_TIMEOUT) --skip=Conformance --skip-package=kubeconfig_helper --nodes=1 --no-color=false ./... -- \
$(GINKGO) -vv --trace --tags=e2e --focus=$(JOB) --timeout=$(E2E_TIMEOUT) --skip=Conformance --skip-package=kubeconfig_helper --nodes=1 --no-color=false ./... -- \
-e2e.artifacts-folder=${REPO_ROOT}/_artifacts \
-e2e.config=${E2E_CONFIG} \
-e2e.skip-resource-cleanup=false -e2e.use-existing-cluster=true
Expand Down
2 changes: 1 addition & 1 deletion api/v1beta1/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func Convert_v1beta1_CloudStackCluster_To_v1beta3_CloudStackCluster(in *CloudSta
//nolint:golint,revive,stylecheck
func Convert_v1beta3_CloudStackCluster_To_v1beta1_CloudStackCluster(in *v1beta3.CloudStackCluster, out *CloudStackCluster, scope conv.Scope) error {
if len(in.Spec.FailureDomains) < 1 {
return fmt.Errorf("v1beta3 to v1beta1 conversion not supported when < 1 failure domain is provided. Input CloudStackCluster spec %s", in.Spec)
return fmt.Errorf("v1beta3 to v1beta1 conversion not supported when < 1 failure domain is provided. Input CloudStackCluster spec %s", fmt.Sprintf("%v", in.Spec))
}
out.ObjectMeta = in.ObjectMeta
out.Spec = CloudStackClusterSpec{
Expand Down
9 changes: 9 additions & 0 deletions api/v1beta2/cloudstackcluster_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package v1beta2

import (
machineryconversion "k8s.io/apimachinery/pkg/conversion"
"sigs.k8s.io/cluster-api-provider-cloudstack/api/v1beta3"
"sigs.k8s.io/controller-runtime/pkg/conversion"
)
Expand All @@ -30,3 +31,11 @@ func (dst *CloudStackCluster) ConvertFrom(srcRaw conversion.Hub) error { // noli
src := srcRaw.(*v1beta3.CloudStackCluster)
return Convert_v1beta3_CloudStackCluster_To_v1beta2_CloudStackCluster(src, dst, nil)
}

func Convert_v1beta3_CloudStackClusterSpec_To_v1beta2_CloudStackClusterSpec(in *v1beta3.CloudStackClusterSpec, out *CloudStackClusterSpec, s machineryconversion.Scope) error { // nolint
return autoConvert_v1beta3_CloudStackClusterSpec_To_v1beta2_CloudStackClusterSpec(in, out, s)
}

func Convert_v1beta3_CloudStackClusterStatus_To_v1beta2_CloudStackClusterStatus(in *v1beta3.CloudStackClusterStatus, out *CloudStackClusterStatus, s machineryconversion.Scope) error { // nolint
return autoConvert_v1beta3_CloudStackClusterStatus_To_v1beta2_CloudStackClusterStatus(in, out, s)
}
32 changes: 12 additions & 20 deletions api/v1beta2/zz_generated.conversion.go

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

8 changes: 8 additions & 0 deletions api/v1beta3/cloudstackcluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ type CloudStackClusterSpec struct {

// The kubernetes control plane endpoint.
ControlPlaneEndpoint clusterv1.APIEndpoint `json:"controlPlaneEndpoint"`

// SyncWithACS determines if an externalManaged CKS cluster should be created on ACS.
// +optional
SyncWithACS bool `json:"syncWithACS,omitempty"`
}

// The status of the CloudStackCluster object.
Expand All @@ -43,6 +47,10 @@ type CloudStackClusterStatus struct {
// +optional
FailureDomains clusterv1.FailureDomains `json:"failureDomains,omitempty"`

// Id of CAPC managed kubernetes cluster created in CloudStack
// +optional
CloudStackClusterID string `json:"cloudStackClusterId"`

// Reflects the readiness of the CS cluster.
Ready bool `json:"ready"`
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -415,13 +415,20 @@ spec:
- zone
type: object
type: array
syncWithACS:
description: SyncWithACS determines if an externalManaged CKS cluster
should be created on ACS.
type: boolean
required:
- controlPlaneEndpoint
- failureDomains
type: object
status:
description: The actual cluster state reported by CloudStack.
properties:
cloudStackClusterId:
description: Id of CAPC managed kubernetes cluster created in CloudStack
type: string
failureDomains:
additionalProperties:
description: FailureDomainSpec is the Schema for Cluster API failure
Expand Down
124 changes: 124 additions & 0 deletions controllers/cks_cluster_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
Copyright 2022 The Kubernetes 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 controllers

import (
"context"
"fmt"
"strings"

ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

infrav1 "sigs.k8s.io/cluster-api-provider-cloudstack/api/v1beta3"
csCtrlrUtils "sigs.k8s.io/cluster-api-provider-cloudstack/controllers/utils"
)

const CksClusterFinalizer = "ckscluster.infrastructure.cluster.x-k8s.io"

// RBAC permissions for CloudStackCluster.
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=cloudstackclusters,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=cloudstackclusters/status,verbs=create;get;update;patch
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=cloudstackclusters/finalizers,verbs=update

// CksClusterReconciliationRunner is a ReconciliationRunner with extensions specific to CloudStackClusters.
// The runner does the actual reconciliation.
type CksClusterReconciliationRunner struct {
*csCtrlrUtils.ReconciliationRunner
FailureDomains *infrav1.CloudStackFailureDomainList
ReconciliationSubject *infrav1.CloudStackCluster
}

// CksClusterReconciler is the k8s controller manager's interface to reconcile a CloudStackCluster.
// This is primarily to adapt to k8s.
type CksClusterReconciler struct {
csCtrlrUtils.ReconcilerBase
}

// Initialize a new CloudStackCluster reconciliation runner with concrete types and initialized member fields.
func NewCksClusterReconciliationRunner() *CksClusterReconciliationRunner {
// Set concrete type and init pointers.
runner := &CksClusterReconciliationRunner{ReconciliationSubject: &infrav1.CloudStackCluster{}}
runner.FailureDomains = &infrav1.CloudStackFailureDomainList{}
// Setup the base runner. Initializes pointers and links reconciliation methods.
runner.ReconciliationRunner = csCtrlrUtils.NewRunner(runner, runner.ReconciliationSubject, "CKSClusterController")
runner.CSCluster = runner.ReconciliationSubject
return runner
}

// Reconcile is the method k8s will call upon a reconciliation request.
func (reconciler *CksClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (retRes ctrl.Result, retErr error) {
r := NewCksClusterReconciliationRunner()
r.UsingBaseReconciler(reconciler.ReconcilerBase).ForRequest(req).WithRequestCtx(ctx)
r.WithAdditionalCommonStages(r.GetFailureDomains(r.FailureDomains))
return r.RunBaseReconciliationStages()
}

// Reconcile actually reconciles the CloudStackCluster.
func (r *CksClusterReconciliationRunner) Reconcile() (res ctrl.Result, reterr error) {
if r.CSCluster.Spec.SyncWithACS {
// Prevent premature deletion.
controllerutil.AddFinalizer(r.ReconciliationSubject, CksClusterFinalizer)

if len(r.FailureDomains.Items) == 0 {
return r.RequeueWithMessage("No failure domains found")
}

res, err := r.AsFailureDomainUser(&r.FailureDomains.Items[0].Spec)()
if r.ShouldReturn(res, err) {
return res, err
}

r.Log.Info("Creating entry with CKS")
err = r.CSUser.GetOrCreateUnmanagedCluster(r.CAPICluster, r.ReconciliationSubject, &r.FailureDomains.Items[0].Spec)
if err != nil {
if strings.Contains(err.Error(), "Kubernetes Service plugin is disabled") {
r.Log.Info("Kubernetes Service plugin is disabled on CloudStack. Skipping ExternalManaged kubernetes cluster creation")
return ctrl.Result{}, nil
}
r.Log.Info(fmt.Sprintf("Failed creating ExternalManaged kubernetes cluster on CloudStack. Error: %s", err.Error()))
return r.RequeueWithMessage(fmt.Sprintf("Syncing VMs with CloudStack failed. error: %s", err.Error()))
}
}
return ctrl.Result{}, nil
}

// ReconcileDelete cleans up resources used by the cluster and finally removes the CloudStackCluster's finalizers.
func (r *CksClusterReconciliationRunner) ReconcileDelete() (ctrl.Result, error) {
if r.ReconciliationSubject.Status.CloudStackClusterID != "" {
if len(r.FailureDomains.Items) == 0 {
return ctrl.Result{}, fmt.Errorf("no failure domains found")
}
res, err := r.AsFailureDomainUser(&r.FailureDomains.Items[0].Spec)()
if r.ShouldReturn(res, err) {
return res, err
}
err = r.CSUser.DeleteUnmanagedCluster(r.ReconciliationSubject)
if err != nil && !strings.Contains(err.Error(), " not found") {
return r.RequeueWithMessage(fmt.Sprintf("Deleting unmanaged kubernetes cluster on CloudStack failed. error: %s", err.Error()))
}
}
controllerutil.RemoveFinalizer(r.ReconciliationSubject, CksClusterFinalizer)
return ctrl.Result{}, nil
}

// SetupWithManager sets up the controller with the Manager.
func (reconciler *CksClusterReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&infrav1.CloudStackCluster{}).
Complete(reconciler)
}
69 changes: 69 additions & 0 deletions controllers/cks_cluster_controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
Copyright 2022 The Kubernetes 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 controllers_test

import (
"github.com/golang/mock/gomock"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
infrav1 "sigs.k8s.io/cluster-api-provider-cloudstack/api/v1beta3"
"sigs.k8s.io/cluster-api-provider-cloudstack/controllers"
"sigs.k8s.io/cluster-api-provider-cloudstack/pkg/cloud"
dummies "sigs.k8s.io/cluster-api-provider-cloudstack/test/dummies/v1beta3"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
)

var _ = Describe("CksCloudStackClusterReconciler", func() {
Context("With k8s like test environment.", func() {
BeforeEach(func() {
dummies.SetDummyVars()
SetupTestEnvironment()
Ω(ClusterReconciler.SetupWithManager(ctx, k8sManager, controller.Options{})).Should(Succeed()) // Register CloudStack ClusterReconciler.
Ω(FailureDomainReconciler.SetupWithManager(k8sManager, controller.Options{})).Should(Succeed()) // Register CloudStack FailureDomainReconciler.
Ω(CksClusterReconciler.SetupWithManager(k8sManager)).Should(Succeed()) // Register CloudStack Cks ClusterReconciler.
mockCloudClient.EXPECT().GetOrCreateUnmanagedCluster(gomock.Any(), gomock.Any(), gomock.Any()).Do(func(_, arg1, _ interface{}) {
arg1.(*infrav1.CloudStackCluster).Status.CloudStackClusterID = "cluster-id-123"
}).MinTimes(1).Return(nil)
mockCloudClient.EXPECT().ResolveZone(gomock.Any()).AnyTimes()
mockCloudClient.EXPECT().ResolveNetworkForZone(gomock.Any()).AnyTimes().Do(
func(arg1 interface{}) {
arg1.(*infrav1.CloudStackZoneSpec).Network.ID = "SomeID"
arg1.(*infrav1.CloudStackZoneSpec).Network.Type = cloud.NetworkTypeShared
}).MinTimes(1)
})

It("Should create a cluster in CKS.", func() {
Eventually(func() string {
key := client.ObjectKeyFromObject(dummies.CSCluster)
if err := k8sClient.Get(ctx, key, dummies.CSCluster); err != nil {
return ""
}
return dummies.CSCluster.Status.CloudStackClusterID
}, timeout).WithPolling(pollInterval).Should(Equal("cluster-id-123"))

})

})

Context("Without a k8s test environment.", func() {
It("Should create a reconciliation runner with a Cloudstack Cluster as the reconciliation subject.", func() {
reconRunner := controllers.NewCksClusterReconciliationRunner()
Ω(reconRunner.ReconciliationSubject).ShouldNot(BeNil())
})
})
})
Loading
Loading