Skip to content

Commit 14d9900

Browse files
Spawn a pod to make request to cluster-agent (#71)
1 parent ac3d9e3 commit 14d9900

File tree

12 files changed

+442
-206
lines changed

12 files changed

+442
-206
lines changed

controllers/configs.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828
"k8s.io/client-go/kubernetes"
2929
"k8s.io/client-go/tools/clientcmd"
3030
"k8s.io/client-go/util/connrotation"
31-
"k8s.io/utils/pointer"
31+
"k8s.io/utils/ptr"
3232
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
3333
"sigs.k8s.io/controller-runtime/pkg/client"
3434
)
@@ -241,7 +241,7 @@ func (r *MicroK8sControlPlaneReconciler) generateMicroK8sConfig(ctx context.Cont
241241
Kind: "MicroK8sControlPlane",
242242
Name: tcp.Name,
243243
UID: tcp.UID,
244-
BlockOwnerDeletion: pointer.BoolPtr(true),
244+
BlockOwnerDeletion: ptr.To(true),
245245
}
246246

247247
bootstrapConfig := &bootstrapv1beta1.MicroK8sConfig{

controllers/reconcile.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import (
1111

1212
clusterv1beta1 "github.com/canonical/cluster-api-control-plane-provider-microk8s/api/v1beta1"
1313
"github.com/canonical/cluster-api-control-plane-provider-microk8s/pkg/clusteragent"
14+
"github.com/canonical/cluster-api-control-plane-provider-microk8s/pkg/images"
1415
"github.com/canonical/cluster-api-control-plane-provider-microk8s/pkg/token"
16+
"github.com/go-logr/logr"
1517
"golang.org/x/mod/semver"
1618

1719
"github.com/pkg/errors"
@@ -33,9 +35,8 @@ import (
3335
)
3436

3537
const (
36-
defaultClusterAgentPort string = "25000"
37-
defaultDqlitePort string = "19001"
38-
defaultClusterAgentClientTimeout time.Duration = 10 * time.Second
38+
defaultClusterAgentPort string = "25000"
39+
defaultDqlitePort string = "19001"
3940
)
4041

4142
type errServiceUnhealthy struct {
@@ -601,7 +602,14 @@ func (r *MicroK8sControlPlaneReconciler) scaleDownControlPlane(ctx context.Conte
601602
if len(machines) > 2 {
602603
portRemap := tcp != nil && tcp.Spec.ControlPlaneConfig.ClusterConfiguration != nil && tcp.Spec.ControlPlaneConfig.ClusterConfiguration.PortCompatibilityRemap
603604

604-
if clusterAgentClient, err := getClusterAgentClient(machines, deleteMachine, portRemap); err == nil {
605+
kubeclient, err := r.kubeconfigForCluster(ctx, cluster)
606+
if err != nil {
607+
return ctrl.Result{RequeueAfter: 5 * time.Second}, fmt.Errorf("failed to get kubeconfig for cluster: %w", err)
608+
}
609+
610+
defer kubeclient.Close() //nolint:errcheck
611+
612+
if clusterAgentClient, err := getClusterAgentClient(kubeclient, logger, machines, deleteMachine, portRemap); err == nil {
605613
if err := r.removeNodeFromDqlite(ctx, clusterAgentClient, cluster, deleteMachine, portRemap); err != nil {
606614
logger.Error(err, "failed to remove node from dqlite: %w", "machineName", deleteMachine.Name, "nodeName", node.Name)
607615
}
@@ -627,7 +635,7 @@ func (r *MicroK8sControlPlaneReconciler) scaleDownControlPlane(ctx context.Conte
627635
return ctrl.Result{Requeue: true}, nil
628636
}
629637

630-
func getClusterAgentClient(machines []clusterv1.Machine, delMachine clusterv1.Machine, portRemap bool) (*clusteragent.Client, error) {
638+
func getClusterAgentClient(kubeclient *kubernetesClient, logger logr.Logger, machines []clusterv1.Machine, delMachine clusterv1.Machine, portRemap bool) (*clusteragent.Client, error) {
631639
opts := clusteragent.Options{
632640
// NOTE(hue): We want to pick a random machine's IP to call POST /dqlite/remove on its cluster agent endpoint.
633641
// This machine should preferably not be the <delMachine> itself, although this is not forced by Microk8s.
@@ -640,7 +648,7 @@ func getClusterAgentClient(machines []clusterv1.Machine, delMachine clusterv1.Ma
640648
port = "30000"
641649
}
642650

643-
clusterAgentClient, err := clusteragent.NewClient(machines, port, defaultClusterAgentClientTimeout, opts)
651+
clusterAgentClient, err := clusteragent.NewClient(kubeclient, logger, machines, port, opts)
644652
if err != nil {
645653
return nil, fmt.Errorf("failed to initialize cluster agent client: %w", err)
646654
}
@@ -696,7 +704,7 @@ func createUpgradePod(ctx context.Context, kubeclient *kubernetesClient, nodeNam
696704
Containers: []corev1.Container{
697705
{
698706
Name: "upgrade",
699-
Image: "curlimages/curl:7.87.0",
707+
Image: images.CurlImage,
700708
Command: []string{
701709
"su",
702710
"-c",

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ require (
4141
github.com/cespare/xxhash/v2 v2.1.2 // indirect
4242
github.com/davecgh/go-spew v1.1.1 // indirect
4343
github.com/fsnotify/fsnotify v1.6.0 // indirect
44-
github.com/go-logr/logr v1.2.3 // indirect
44+
github.com/go-logr/logr v1.2.3
4545
github.com/go-logr/zapr v1.2.3 // indirect
4646
github.com/gogo/protobuf v1.3.2 // indirect
4747
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
@@ -83,7 +83,7 @@ require (
8383
k8s.io/component-base v0.25.3 // indirect
8484
k8s.io/klog/v2 v2.80.1 // indirect
8585
k8s.io/kube-openapi v0.0.0-20221012153701-172d655c2280 // indirect
86-
k8s.io/utils v0.0.0-20221012122500-cfd413dd9e85
86+
k8s.io/utils v0.0.0-20240921022957-49e7df575cb6
8787
sigs.k8s.io/cluster-api v1.2.4
8888
sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 // indirect
8989
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -745,8 +745,8 @@ k8s.io/klog/v2 v2.80.1 h1:atnLQ121W371wYYFawwYx1aEY2eUfs4l3J72wtgAwV4=
745745
k8s.io/klog/v2 v2.80.1/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0=
746746
k8s.io/kube-openapi v0.0.0-20221012153701-172d655c2280 h1:+70TFaan3hfJzs+7VK2o+OGxg8HsuBr/5f6tVAjDu6E=
747747
k8s.io/kube-openapi v0.0.0-20221012153701-172d655c2280/go.mod h1:+Axhij7bCpeqhklhUTe3xmOn6bWxolyZEeyaFpjGtl4=
748-
k8s.io/utils v0.0.0-20221012122500-cfd413dd9e85 h1:cTdVh7LYu82xeClmfzGtgyspNh6UxpwLWGi8R4sspNo=
749-
k8s.io/utils v0.0.0-20221012122500-cfd413dd9e85/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
748+
k8s.io/utils v0.0.0-20240921022957-49e7df575cb6 h1:MDF6h2H/h4tbzmtIKTuctcwZmY0tY9mD9fNT47QO6HI=
749+
k8s.io/utils v0.0.0-20240921022957-49e7df575cb6/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
750750
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
751751
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
752752
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=

0 commit comments

Comments
 (0)