Skip to content

Commit

Permalink
Merge pull request #21 from spectrocloud/microk8s-68
Browse files Browse the repository at this point in the history
Hotfix: cherry-pick the changes of v0.6.8 from v0.6.6
  • Loading branch information
Kun483 authored May 13, 2024
2 parents 5f7a7f1 + 2a35e3a commit 8f4981b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 48 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: "1.19"
go-version: "1.21"

- name: go fmt
run: make fmt
Expand All @@ -37,7 +37,7 @@ jobs:
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: "1.19"
go-version: "1.21"

- name: Run tests
run: make test
Expand All @@ -53,7 +53,7 @@ jobs:
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: "1.19"
go-version: "1.21"

- name: Try build
run: make
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ docker-push-manifest: ## Push the fat manifest docker image.

.PHONY: lint
lint: golangci-lint ## Lint the codebase
$(GOLANGCI_LINT) run -v --go=1.19 --timeout 3m0s
$(GOLANGCI_LINT) run -v --go=1.21 --timeout 3m0s

##@ Deployment

Expand Down Expand Up @@ -145,7 +145,7 @@ undeploy: ## Undeploy controller from the K8s cluster specified in ~/.kube/confi
GOLANGCI_LINT = $(shell pwd)/bin/golangci-lint
.PHONY: golangci-lint
golangci-lint: ## Download golangci-lint locally if necessary.
$(call go-get-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/cmd/golangci-lint@v1.50.1)
$(call go-get-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/cmd/golangci-lint@v1.57.2)

CONTROLLER_GEN = $(shell pwd)/bin/controller-gen
.PHONY: controller-gen
Expand Down
84 changes: 41 additions & 43 deletions controllers/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"crypto/x509/pkix"
"encoding/base64"
"encoding/pem"
"fmt"
"math/big"
"net"
"strings"
Expand All @@ -20,6 +21,7 @@ import (

"github.com/pkg/errors"
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"
"k8s.io/apiserver/pkg/storage/names"
Expand All @@ -37,7 +39,7 @@ apiVersion: v1
clusters:
- cluster:
certificate-authority-data: <CACERT>
server: https://<HOST>:6443
server: https://<HOST>:<PORT>
name: microk8s-cluster
contexts:
- context:
Expand Down Expand Up @@ -75,62 +77,53 @@ func newDialer() *connrotation.Dialer {
// kubeconfigForCluster will fetch a kubeconfig secret based on cluster name/namespace,
// use it to create a clientset, and return it.
func (r *MicroK8sControlPlaneReconciler) kubeconfigForCluster(ctx context.Context, cluster client.ObjectKey) (*kubernetesClient, error) {
kubeconfigSecret := &corev1.Secret{}
secret := &corev1.Secret{}

// See if the kubeconfig exists. If not create it.
secrets := &corev1.SecretList{}
err := r.Client.List(ctx, secrets)
if err != nil {
err := r.Client.Get(ctx, types.NamespacedName{
Namespace: cluster.Namespace,
Name: fmt.Sprintf("%s-kubeconfig", cluster.Name),
}, secret)
switch {
case err == nil:
return clientFromKubeconfig(secret.Data["value"])
case apierrors.IsNotFound(err):
default:
return nil, err
}

found := false
for _, s := range secrets.Items {
if s.Name == cluster.Name+"-kubeconfig" {
found = true
}
}

c := &clusterv1.Cluster{}
err = r.Client.Get(ctx, cluster, c)
if err != nil {
if err := r.Client.Get(ctx, cluster, c); err != nil {
return nil, err
}
if !found && c.Spec.ControlPlaneEndpoint.IsValid() {
kubeconfig, err := r.genarateKubeconfig(ctx, cluster, c.Spec.ControlPlaneEndpoint.Host)
if err != nil {
return nil, err
}
configsecret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Namespace: cluster.Namespace,
Name: cluster.Name + "-kubeconfig",
Labels: map[string]string{
clusterv1.ClusterLabelName: cluster.Name,
},
},
Data: map[string][]byte{
"value": []byte(*kubeconfig),
},
}
err = r.Client.Create(ctx, configsecret)
if err != nil {
return nil, err
}
if !c.Spec.ControlPlaneEndpoint.IsValid() {
return nil, fmt.Errorf("ControlPlaneEndpoint is not set yet, cannot generate kubeconfig yet")
}

err = r.Client.Get(ctx,
types.NamespacedName{
kubeconfig, err := r.generateKubeconfig(ctx, cluster, c.Spec.ControlPlaneEndpoint.Host, c.Spec.ControlPlaneEndpoint.Port)
if err != nil {
return nil, err
}
secret = &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Namespace: cluster.Namespace,
Name: cluster.Name + "-kubeconfig",
Labels: map[string]string{
clusterv1.ClusterLabelName: cluster.Name,
},
},
kubeconfigSecret,
)
if err != nil {
Data: map[string][]byte{
"value": []byte(*kubeconfig),
},
}
if err := r.Client.Create(ctx, secret); err != nil {
return nil, err
}
return clientFromKubeconfig([]byte(*kubeconfig))
}

config, err := clientcmd.RESTConfigFromKubeConfig(kubeconfigSecret.Data["value"])
func clientFromKubeconfig(kubeconfigBytes []byte) (*kubernetesClient, error) {
config, err := clientcmd.RESTConfigFromKubeConfig(kubeconfigBytes)
if err != nil {
return nil, err
}
Expand All @@ -148,8 +141,7 @@ func (r *MicroK8sControlPlaneReconciler) kubeconfigForCluster(ctx context.Contex
dialer: dialer,
}, nil
}

func (r *MicroK8sControlPlaneReconciler) genarateKubeconfig(ctx context.Context, cluster client.ObjectKey, host string) (kubeconfig *string, err error) {
func (r *MicroK8sControlPlaneReconciler) generateKubeconfig(ctx context.Context, cluster client.ObjectKey, host string, port int32) (kubeconfig *string, err error) {
// Get the secret with the CA
readCASecret := &corev1.Secret{}
err = r.Client.Get(ctx,
Expand Down Expand Up @@ -228,7 +220,13 @@ func (r *MicroK8sControlPlaneReconciler) genarateKubeconfig(ctx context.Context,
return nil, err
}

// handle unset port value
if port == 0 {
port = 6443
}

config := strings.Replace(templateConfig, "<HOST>", host, -1)
config = strings.Replace(config, "<PORT>", fmt.Sprintf("%d", port), -1)
config = strings.Replace(config, "<CACERT>", base64.StdEncoding.EncodeToString(readCASecret.Data["crt"]), -1)
config = strings.Replace(config, "<CERT>", base64.StdEncoding.EncodeToString(certPEM.Bytes()), -1)
config = strings.Replace(config, "<KEY>", base64.StdEncoding.EncodeToString(keyPEM.Bytes()), -1)
Expand Down

0 comments on commit 8f4981b

Please sign in to comment.