Skip to content
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
2 changes: 1 addition & 1 deletion controllers/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"

Check failure on line 26 in controllers/configs.go

View workflow job for this annotation

GitHub Actions / Code Quality

could not import k8s.io/apimachinery/pkg/types (-: could not load export data: internal error in importing "k8s.io/apimachinery/pkg/types" (unsupported version: 2); please report an issue) (typecheck)
"k8s.io/apiserver/pkg/storage/names"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
Expand Down Expand Up @@ -109,7 +109,7 @@
Namespace: cluster.Namespace,
Name: cluster.Name + "-kubeconfig",
Labels: map[string]string{
clusterv1.ClusterLabelName: cluster.Name,
clusterv1.ClusterNameLabel: cluster.Name,
},
},
Data: map[string][]byte{
Expand Down
9 changes: 4 additions & 5 deletions controllers/microk8scontrolplane_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/source"
)

const requeueDuration = 30 * time.Second
Expand Down Expand Up @@ -203,14 +202,14 @@ func (r *MicroK8sControlPlaneReconciler) SetupWithManager(mgr ctrl.Manager, opti
For(&clusterv1beta1.MicroK8sControlPlane{}).
Owns(&clusterv1.Machine{}).
Watches(
&source.Kind{Type: &clusterv1.Cluster{}},
&clusterv1.Cluster{},
handler.EnqueueRequestsFromMapFunc(r.ClusterToMicroK8sControlPlane),
).
WithOptions(options).
Complete(r)
}

func (r *MicroK8sControlPlaneReconciler) ClusterToMicroK8sControlPlane(o client.Object) []ctrl.Request {
func (r *MicroK8sControlPlaneReconciler) ClusterToMicroK8sControlPlane(ctx context.Context, o client.Object) []ctrl.Request {
c, ok := o.(*clusterv1.Cluster)
if !ok {
fmt.Printf("expected a Cluster but got a %T\n", o)
Expand All @@ -227,8 +226,8 @@ func (r *MicroK8sControlPlaneReconciler) ClusterToMicroK8sControlPlane(o client.

func (r *MicroK8sControlPlaneReconciler) getControlPlaneMachinesForCluster(ctx context.Context, cluster client.ObjectKey, cpName string) ([]clusterv1.Machine, error) {
selector := map[string]string{
clusterv1.ClusterLabelName: cluster.Name,
clusterv1.MachineControlPlaneLabelName: "",
clusterv1.ClusterNameLabel: cluster.Name,
clusterv1.MachineControlPlaneNameLabel: "",
}

machineList := clusterv1.MachineList{}
Expand Down
86 changes: 66 additions & 20 deletions controllers/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,13 @@
"strings"
"time"

clusterv1beta1 "github.com/canonical/cluster-api-control-plane-provider-microk8s/api/v1beta1"
"github.com/canonical/cluster-api-control-plane-provider-microk8s/pkg/clusteragent"
"github.com/canonical/cluster-api-control-plane-provider-microk8s/pkg/images"
"github.com/canonical/cluster-api-control-plane-provider-microk8s/pkg/token"
"github.com/go-logr/logr"
"golang.org/x/mod/semver"

"github.com/pkg/errors"
"golang.org/x/mod/semver"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
kerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apiserver/pkg/storage/names"
Expand All @@ -33,6 +29,11 @@
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/log"

clusterv1beta1 "github.com/canonical/cluster-api-control-plane-provider-microk8s/api/v1beta1"
"github.com/canonical/cluster-api-control-plane-provider-microk8s/pkg/clusteragent"
"github.com/canonical/cluster-api-control-plane-provider-microk8s/pkg/images"
"github.com/canonical/cluster-api-control-plane-provider-microk8s/pkg/token"
)

const (
Expand Down Expand Up @@ -218,7 +219,7 @@
}

// Get the node for the machine
node, err := kubeclient.CoreV1().Nodes().Get(ctx, machine.Status.NodeRef.Name, metav1.GetOptions{})

Check failure on line 222 in controllers/reconcile.go

View workflow job for this annotation

GitHub Actions / Code Quality

kubeclient.CoreV1 undefined (type *kubernetesClient has no field or method CoreV1) (typecheck)
if err != nil {
return ctrl.Result{}, fmt.Errorf("failed to get node: %w", err)
}
Expand Down Expand Up @@ -331,7 +332,8 @@
}

func (r *MicroK8sControlPlaneReconciler) reconcileExternalReference(ctx context.Context, ref corev1.ObjectReference, cluster *clusterv1.Cluster) error {
obj, err := external.Get(ctx, r.Client, &ref, cluster.Namespace)
ref.Namespace = cluster.Namespace
obj, err := external.Get(ctx, r.Client, &ref)
if err != nil {
return err
}
Expand Down Expand Up @@ -362,17 +364,7 @@
}

// Clone the infrastructure template
infraRef, err := external.CloneTemplate(ctx, &external.CloneTemplateInput{
Client: r.Client,
TemplateRef: &mcp.Spec.InfrastructureTemplate,
Namespace: mcp.Namespace,
OwnerRef: infraCloneOwner,
ClusterName: cluster.Name,
Labels: map[string]string{
clusterv1.ClusterLabelName: cluster.Name,
clusterv1.MachineControlPlaneLabelName: "",
},
})
infraRef, err := r.cloneInfrastructureTemplate(ctx, cluster, mcp, infraCloneOwner)
if err != nil {
conditions.MarkFalse(mcp, clusterv1beta1.MachinesCreatedCondition,
clusterv1beta1.InfrastructureTemplateCloningFailedReason,
Expand All @@ -398,8 +390,8 @@
Name: names.SimpleNameGenerator.GenerateName(mcp.Name + "-"),
Namespace: mcp.Namespace,
Labels: map[string]string{
clusterv1.ClusterLabelName: cluster.Name,
clusterv1.MachineControlPlaneLabelName: "",
clusterv1.ClusterNameLabel: cluster.Name,
clusterv1.MachineControlPlaneNameLabel: "",
},
OwnerReferences: []metav1.OwnerReference{
*metav1.NewControllerRef(mcp, clusterv1beta1.GroupVersion.WithKind("MicroK8sControlPlane")),
Expand Down Expand Up @@ -561,7 +553,7 @@
if !machine.ObjectMeta.DeletionTimestamp.IsZero() {
logger.Info("machine is in process of deletion")

node, err := kubeclient.CoreV1().Nodes().Get(ctx, machine.Status.NodeRef.Name, metav1.GetOptions{})

Check failure on line 556 in controllers/reconcile.go

View workflow job for this annotation

GitHub Actions / Code Quality

kubeclient.CoreV1 undefined (type *kubernetesClient has no field or method CoreV1) (typecheck)
if err != nil {
// It's possible for the node to already be deleted in the workload cluster, so we just
// requeue if that's that case instead of throwing a scary error.
Expand All @@ -574,7 +566,7 @@
// TODO: drain and cordon the node
logger.WithValues("nodeName", node.Name).Info("deleting node")

err = kubeclient.CoreV1().Nodes().Delete(ctx, node.Name, metav1.DeleteOptions{})

Check failure on line 569 in controllers/reconcile.go

View workflow job for this annotation

GitHub Actions / Code Quality

kubeclient.CoreV1 undefined (type *kubernetesClient has no field or method CoreV1) (typecheck)
if err != nil {
return ctrl.Result{RequeueAfter: 20 * time.Second}, err
}
Expand Down Expand Up @@ -823,3 +815,57 @@
newVersion = semver.MajorMinor(newVersion) // just being extra careful
return semver.Compare(machineVersion, newVersion) == 0
}

func (r *MicroK8sControlPlaneReconciler) cloneInfrastructureTemplate(ctx context.Context, cluster *clusterv1.Cluster, mcp *clusterv1beta1.MicroK8sControlPlane, owner *metav1.OwnerReference) (*corev1.ObjectReference, error) {
templateRef := &mcp.Spec.InfrastructureTemplate

template := &unstructured.Unstructured{}
template.SetAPIVersion(templateRef.APIVersion)
template.SetKind(templateRef.Kind)
if err := r.Client.Get(ctx, client.ObjectKey{Namespace: mcp.Namespace, Name: templateRef.Name}, template); err != nil {
return nil, errors.Wrapf(err, "failed to get infrastructure template %s", templateRef.Name)
}

// Get the unstructured template from the template's spec
templateSpec, found, err := unstructured.NestedMap(template.Object, "spec", "template")
if err != nil {
return nil, errors.Wrap(err, "failed to get unstructured template spec")
}
if !found {
return nil, errors.New("spec.template not found in infrastructure template")
}

clone := &unstructured.Unstructured{Object: templateSpec}

// Set the APIVersion and Kind for the clone.
clone.SetAPIVersion(templateRef.APIVersion)
clone.SetKind(strings.TrimSuffix(templateRef.Kind, "Template"))

clone.SetName(names.SimpleNameGenerator.GenerateName(mcp.Name + "-"))
clone.SetNamespace(mcp.Namespace)

// Set owner reference.
clone.SetOwnerReferences([]metav1.OwnerReference{*owner})

// Add cluster labels.
labels := clone.GetLabels()
if labels == nil {
labels = make(map[string]string)
}
labels[clusterv1.ClusterNameLabel] = cluster.Name
labels[clusterv1.MachineControlPlaneNameLabel] = ""
clone.SetLabels(labels)

if err := r.Client.Create(ctx, clone); err != nil {
return nil, errors.Wrap(err, "failed to create infrastructure clone")
}

infraRef := &corev1.ObjectReference{
APIVersion: clone.GetAPIVersion(),
Kind: clone.GetKind(),
Name: clone.GetName(),
Namespace: clone.GetNamespace(),
}

return infraRef, nil
}
6 changes: 3 additions & 3 deletions controllers/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"

Check failure on line 12 in controllers/status.go

View workflow job for this annotation

GitHub Actions / Code Quality

could not import k8s.io/apimachinery/pkg/selection (-: could not load export data: internal error in importing "k8s.io/apimachinery/pkg/selection" (unsupported version: 2); please report an issue) (typecheck)
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/conditions"
Expand All @@ -20,8 +20,8 @@
func (r *MicroK8sControlPlaneReconciler) updateStatus(ctx context.Context, mcp *clusterv1beta1.MicroK8sControlPlane, cluster *clusterv1.Cluster) error {
clusterSelector := &metav1.LabelSelector{
MatchLabels: map[string]string{
clusterv1.ClusterLabelName: cluster.Name,
clusterv1.MachineControlPlaneLabelName: "",
clusterv1.ClusterNameLabel: cluster.Name,
clusterv1.MachineControlPlaneNameLabel: "",
},
}

Expand Down Expand Up @@ -115,7 +115,7 @@
}

selector := map[string]string{
clusterv1.ClusterLabelName: cluster.Name,
clusterv1.ClusterNameLabel: cluster.Name,
}

machineList := clusterv1.MachineList{}
Expand Down
5 changes: 1 addition & 4 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
"sigs.k8s.io/controller-runtime/pkg/envtest/printer"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

Expand All @@ -39,15 +38,13 @@
var testEnv *envtest.Environment

func TestAPIs(t *testing.T) {
RegisterFailHandler(Fail)

Check failure on line 41 in controllers/suite_test.go

View workflow job for this annotation

GitHub Actions / Code Quality

undefined: RegisterFailHandler (typecheck)

RunSpecsWithDefaultAndCustomReporters(t,
"Controller Suite",
[]Reporter{printer.NewlineReporter{}})
RunSpecs(t, "Controller Suite")

Check failure on line 43 in controllers/suite_test.go

View workflow job for this annotation

GitHub Actions / Code Quality

undefined: RunSpecs (typecheck)
}

var _ = BeforeSuite(func() {

Check failure on line 46 in controllers/suite_test.go

View workflow job for this annotation

GitHub Actions / Code Quality

undefined: BeforeSuite (typecheck)
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))

Check failure on line 47 in controllers/suite_test.go

View workflow job for this annotation

GitHub Actions / Code Quality

undefined: GinkgoWriter (typecheck)

By("bootstrapping test environment")
testEnv = &envtest.Environment{
Expand All @@ -70,7 +67,7 @@

}, 60)

var _ = AfterSuite(func() {

Check failure on line 70 in controllers/suite_test.go

View workflow job for this annotation

GitHub Actions / Code Quality

undefined: AfterSuite (typecheck)
By("tearing down the test environment")
err := testEnv.Stop()
Expect(err).NotTo(HaveOccurred())
Expand Down
116 changes: 52 additions & 64 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,93 +1,81 @@
module github.com/canonical/cluster-api-control-plane-provider-microk8s

go 1.22
go 1.23.0

toolchain go1.22.4
toolchain go1.24.6

require (
github.com/canonical/cluster-api-bootstrap-provider-microk8s v0.6.9
github.com/onsi/ginkgo v1.16.5
github.com/onsi/gomega v1.22.1
k8s.io/api v0.25.3
k8s.io/apimachinery v0.25.3
k8s.io/apiserver v0.25.3
k8s.io/client-go v0.25.3
sigs.k8s.io/controller-runtime v0.13.0
github.com/onsi/gomega v1.36.3
k8s.io/api v0.32.3
k8s.io/apimachinery v0.32.3
k8s.io/apiserver v0.32.3
k8s.io/client-go v0.32.3
sigs.k8s.io/controller-runtime v0.20.4
)

require (
cloud.google.com/go/compute v1.10.0 // indirect
github.com/blang/semver v3.5.1+incompatible // indirect
github.com/emicklei/go-restful/v3 v3.10.0 // indirect
github.com/evanphx/json-patch v5.6.0+incompatible // indirect
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.20.0 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/gobuffalo/flect v0.3.0 // indirect
github.com/golang-jwt/jwt/v4 v4.4.2 // indirect
github.com/google/gnostic v0.6.9 // indirect
github.com/blang/semver/v4 v4.0.0 // indirect
github.com/emicklei/go-restful/v3 v3.12.2 // indirect
github.com/evanphx/json-patch/v5 v5.9.11 // indirect
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
github.com/go-openapi/jsonpointer v0.21.0 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.23.0 // indirect
github.com/gobuffalo/flect v1.0.3 // indirect
github.com/google/btree v1.1.3 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/x448/float16 v0.8.4 // indirect
golang.org/x/sync v0.12.0 // indirect
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
)

require (
github.com/Azure/go-autorest v14.2.0+incompatible // indirect
github.com/Azure/go-autorest/autorest v0.11.28 // indirect
github.com/Azure/go-autorest/autorest/adal v0.9.21 // indirect
github.com/Azure/go-autorest/autorest/date v0.3.0 // indirect
github.com/Azure/go-autorest/logger v0.2.1 // indirect
github.com/Azure/go-autorest/tracing v0.6.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-logr/logr v1.2.3
github.com/go-logr/zapr v1.2.3 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/fsnotify/fsnotify v1.8.0 // indirect
github.com/go-logr/logr v1.4.2
github.com/go-logr/zapr v1.3.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/go-cmp v0.7.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/nxadm/tail v1.4.8 // indirect
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.13.0 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
go.uber.org/zap v1.23.0 // indirect
golang.org/x/crypto v0.31.0 // indirect
golang.org/x/mod v0.17.0
golang.org/x/net v0.33.0 // indirect
golang.org/x/oauth2 v0.1.0 // indirect
golang.org/x/sys v0.28.0 // indirect
golang.org/x/term v0.27.0 // indirect
golang.org/x/text v0.21.0 // indirect
golang.org/x/time v0.1.0 // indirect
gomodules.xyz/jsonpatch/v2 v2.2.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.1 // indirect
github.com/prometheus/client_golang v1.19.1 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.55.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/spf13/pflag v1.0.6 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/mod v0.21.0
golang.org/x/net v0.38.0 // indirect
golang.org/x/oauth2 v0.28.0 // indirect
golang.org/x/sys v0.31.0 // indirect
golang.org/x/term v0.30.0 // indirect
golang.org/x/text v0.23.0 // indirect
golang.org/x/time v0.8.0 // indirect
gomodules.xyz/jsonpatch/v2 v2.5.0 // indirect
google.golang.org/protobuf v1.36.5 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/apiextensions-apiserver v0.25.3 // indirect
k8s.io/component-base v0.25.3 // indirect
k8s.io/klog/v2 v2.80.1 // indirect
k8s.io/kube-openapi v0.0.0-20221012153701-172d655c2280 // indirect
k8s.io/apiextensions-apiserver v0.32.3 // indirect
k8s.io/klog/v2 v2.130.1 // indirect
k8s.io/kube-openapi v0.0.0-20241105132330-32ad38e42d3f // indirect
k8s.io/utils v0.0.0-20241210054802-24370beab758
sigs.k8s.io/cluster-api v1.2.4
sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
sigs.k8s.io/cluster-api v1.10.4
sigs.k8s.io/json v0.0.0-20241010143419-9aa6b5e7a4b3 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.2 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
)
Loading
Loading