From 362425be8a2b9fb1eb2d4e1c9e44bc062e12cbe7 Mon Sep 17 00:00:00 2001 From: Benjamin Schimke Date: Tue, 2 Jul 2024 08:54:41 +0200 Subject: [PATCH 1/7] Add Go CI --- .github/workflows/go.yaml | 42 +++++++++++++++++++ Makefile | 8 ++++ .../api/v1beta2/zz_generated.deepcopy.go | 5 +++ pkg/cloudinit/controlplane_init_test.go | 1 + 4 files changed, 56 insertions(+) create mode 100644 .github/workflows/go.yaml diff --git a/.github/workflows/go.yaml b/.github/workflows/go.yaml new file mode 100644 index 00000000..cf5b4cec --- /dev/null +++ b/.github/workflows/go.yaml @@ -0,0 +1,42 @@ +name: Go + +on: + push: + branches: + - main + pull_request: + +permissions: + contents: read + +jobs: + test: + permissions: + contents: read # for actions/checkout to fetch code + name: Unit Tests & Code Quality + runs-on: ubuntu-latest + + steps: + - name: Harden Runner + uses: step-security/harden-runner@v2 + with: + egress-policy: audit + - name: Check out code + uses: actions/checkout@v4 + + - name: Install Go + uses: actions/setup-go@v5 + with: + go-version: "1.22" + + - name: go fmt + working-directory: src/k8s + run: make go.fmt + + - name: go vet + working-directory: src/k8s + run: make go.vet + + - name: go test + working-directory: src/k8s + run: make test-unit diff --git a/Makefile b/Makefile index 806b8032..780f1e1a 100644 --- a/Makefile +++ b/Makefile @@ -177,6 +177,14 @@ BOOTSTRAP_IMG ?= $(REGISTRY)/bootstrap-controller CONTROLPLANE_IMG_TAG ?= $(RELEASE_TAG) CONTROLPLANE_IMG ?= $(REGISTRY)/controlplane-controller +go-vet: + go vet ./... + +go-fmt: + go fmt ./... + +test-unit: test-common test-bootstrap test-controlplane + test-common: go test $(shell pwd)/pkg/... -coverprofile cover.out diff --git a/bootstrap/api/v1beta2/zz_generated.deepcopy.go b/bootstrap/api/v1beta2/zz_generated.deepcopy.go index cb4dfdda..c3c5217d 100644 --- a/bootstrap/api/v1beta2/zz_generated.deepcopy.go +++ b/bootstrap/api/v1beta2/zz_generated.deepcopy.go @@ -257,6 +257,11 @@ func (in *CK8sControlPlaneConfig) DeepCopyInto(out *CK8sControlPlaneConfig) { *out = make([]string, len(*in)) copy(*out, *in) } + if in.MicroclusterPort != nil { + in, out := &in.MicroclusterPort, &out.MicroclusterPort + *out = new(int) + **out = **in + } if in.ExtraKubeAPIServerArgs != nil { in, out := &in.ExtraKubeAPIServerArgs, &out.ExtraKubeAPIServerArgs *out = make(map[string]*string, len(*in)) diff --git a/pkg/cloudinit/controlplane_init_test.go b/pkg/cloudinit/controlplane_init_test.go index 79935853..5ad86d68 100644 --- a/pkg/cloudinit/controlplane_init_test.go +++ b/pkg/cloudinit/controlplane_init_test.go @@ -52,6 +52,7 @@ func TestNewInitControlPlane(t *testing.T) { "prerun2", "/capi/scripts/install.sh", "/capi/scripts/bootstrap.sh", + "/capi/scripts/load-images.sh", "/capi/scripts/wait-apiserver-ready.sh", "/capi/scripts/deploy-manifests.sh", "/capi/scripts/configure-token.sh", From dbc3f3fb452b0d829ff4d0ec74d6d1b386207f14 Mon Sep 17 00:00:00 2001 From: Benjamin Schimke Date: Tue, 2 Jul 2024 08:59:12 +0200 Subject: [PATCH 2/7] fixup CI workdir --- .github/workflows/go.yaml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.github/workflows/go.yaml b/.github/workflows/go.yaml index cf5b4cec..ddf5d17e 100644 --- a/.github/workflows/go.yaml +++ b/.github/workflows/go.yaml @@ -30,13 +30,10 @@ jobs: go-version: "1.22" - name: go fmt - working-directory: src/k8s run: make go.fmt - name: go vet - working-directory: src/k8s run: make go.vet - name: go test - working-directory: src/k8s run: make test-unit From 951e5857efb8744e0d355145333b6539c53532f1 Mon Sep 17 00:00:00 2001 From: Benjamin Schimke Date: Tue, 2 Jul 2024 09:04:07 +0200 Subject: [PATCH 3/7] fix make target names --- .github/workflows/go.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/go.yaml b/.github/workflows/go.yaml index ddf5d17e..4ac958c5 100644 --- a/.github/workflows/go.yaml +++ b/.github/workflows/go.yaml @@ -30,10 +30,10 @@ jobs: go-version: "1.22" - name: go fmt - run: make go.fmt + run: make go-fmt - name: go vet - run: make go.vet + run: make go-vet - name: go test run: make test-unit From 88bbd136c32d4f12257f6b1d2a9313ba9c7610f8 Mon Sep 17 00:00:00 2001 From: Benjamin Schimke Date: Tue, 2 Jul 2024 09:11:23 +0200 Subject: [PATCH 4/7] fix unit tests --- .github/workflows/go.yaml | 2 +- controlplane/controllers/machine_controller.go | 3 ++- pkg/ck8s/management_cluster.go | 3 ++- pkg/ck8s/workload_cluster.go | 2 +- pkg/ck8s/workload_cluster_k8sd.go | 14 +++----------- test/e2e/cluster_upgrade.go | 2 -- test/e2e/common.go | 1 - test/e2e/create_test.go | 2 -- test/e2e/e2e_suite_test.go | 5 ++--- test/e2e/helpers.go | 9 ++++----- test/e2e/md_remediation_test.go | 2 -- test/e2e/node_scale_test.go | 2 -- 12 files changed, 15 insertions(+), 32 deletions(-) diff --git a/.github/workflows/go.yaml b/.github/workflows/go.yaml index 4ac958c5..f42f6fa1 100644 --- a/.github/workflows/go.yaml +++ b/.github/workflows/go.yaml @@ -35,5 +35,5 @@ jobs: - name: go vet run: make go-vet - - name: go test + - name: unit tests run: make test-unit diff --git a/controlplane/controllers/machine_controller.go b/controlplane/controllers/machine_controller.go index 4c8e95d5..0327c835 100644 --- a/controlplane/controllers/machine_controller.go +++ b/controlplane/controllers/machine_controller.go @@ -4,7 +4,6 @@ import ( "context" "time" - "github.com/canonical/cluster-api-k8s/pkg/ck8s" "github.com/go-logr/logr" "github.com/pkg/errors" apierrors "k8s.io/apimachinery/pkg/api/errors" @@ -15,6 +14,8 @@ import ( "sigs.k8s.io/cluster-api/util/patch" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" + + "github.com/canonical/cluster-api-k8s/pkg/ck8s" ) // CK8sControlPlaneReconciler reconciles a CK8sControlPlane object. diff --git a/pkg/ck8s/management_cluster.go b/pkg/ck8s/management_cluster.go index b862dd9b..2af847f1 100644 --- a/pkg/ck8s/management_cluster.go +++ b/pkg/ck8s/management_cluster.go @@ -5,12 +5,13 @@ import ( "fmt" "time" - "github.com/canonical/cluster-api-k8s/pkg/token" "k8s.io/client-go/kubernetes/scheme" clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" "sigs.k8s.io/cluster-api/controllers/remote" "sigs.k8s.io/cluster-api/util/collections" "sigs.k8s.io/controller-runtime/pkg/client" + + "github.com/canonical/cluster-api-k8s/pkg/token" ) // ManagementCluster defines all behaviors necessary for something to function as a management cluster. diff --git a/pkg/ck8s/workload_cluster.go b/pkg/ck8s/workload_cluster.go index bc7bf87d..2f6f5ae8 100644 --- a/pkg/ck8s/workload_cluster.go +++ b/pkg/ck8s/workload_cluster.go @@ -193,7 +193,7 @@ func (w *Workload) GetK8sdProxyForControlPlane(ctx context.Context, options k8sd continue } - proxy, err := w.K8sdClientGenerator.forNode(ctx, &node) + proxy, err := w.K8sdClientGenerator.forNode(ctx, &node) // #nosec G601 if err != nil { continue } diff --git a/pkg/ck8s/workload_cluster_k8sd.go b/pkg/ck8s/workload_cluster_k8sd.go index af58e4b3..8b0b312d 100644 --- a/pkg/ck8s/workload_cluster_k8sd.go +++ b/pkg/ck8s/workload_cluster_k8sd.go @@ -8,12 +8,13 @@ import ( "net/http" "time" - "github.com/canonical/cluster-api-k8s/pkg/proxy" "github.com/pkg/errors" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" + + "github.com/canonical/cluster-api-k8s/pkg/proxy" ) type K8sdClient struct { @@ -40,15 +41,6 @@ func NewK8sdClientGenerator(restConfig *rest.Config, proxyClientTimeout time.Dur }, nil } -func (g *k8sdClientGenerator) forNodeName(ctx context.Context, nodeName string) (*K8sdClient, error) { - node, err := g.clientset.CoreV1().Nodes().Get(ctx, nodeName, metav1.GetOptions{}) - if err != nil { - return nil, errors.Wrap(err, "unable to get node in target cluster") - } - - return g.forNode(ctx, node) -} - func (g *k8sdClientGenerator) forNode(ctx context.Context, node *corev1.Node) (*K8sdClient, error) { podmap, err := g.getProxyPods(ctx) if err != nil { @@ -121,7 +113,7 @@ func (g *k8sdClientGenerator) NewHTTPClient(ctx context.Context, podName string) ExpectContinueTimeout: http.DefaultTransport.(*http.Transport).ExpectContinueTimeout, // TODO: Workaround for now, address later on // get the certificate fingerprint from the matching node through a resource in the cluster (TBD), and validate it in the TLSClientConfig - TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, // #nosec G402 }, Timeout: g.proxyClientTimeout, }, nil diff --git a/test/e2e/cluster_upgrade.go b/test/e2e/cluster_upgrade.go index 790dc649..0a9ce02b 100644 --- a/test/e2e/cluster_upgrade.go +++ b/test/e2e/cluster_upgrade.go @@ -27,10 +27,8 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" - corev1 "k8s.io/api/core/v1" "k8s.io/utils/ptr" - "sigs.k8s.io/cluster-api/test/framework" "sigs.k8s.io/cluster-api/test/framework/clusterctl" "sigs.k8s.io/cluster-api/util" diff --git a/test/e2e/common.go b/test/e2e/common.go index 6e8668e4..ced12240 100644 --- a/test/e2e/common.go +++ b/test/e2e/common.go @@ -25,7 +25,6 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" - corev1 "k8s.io/api/core/v1" clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" "sigs.k8s.io/cluster-api/test/framework" diff --git a/test/e2e/create_test.go b/test/e2e/create_test.go index 10707f0a..81dd2b8f 100644 --- a/test/e2e/create_test.go +++ b/test/e2e/create_test.go @@ -26,10 +26,8 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" - corev1 "k8s.io/api/core/v1" "k8s.io/utils/pointer" - "sigs.k8s.io/cluster-api/test/framework/clusterctl" "sigs.k8s.io/cluster-api/util" ) diff --git a/test/e2e/e2e_suite_test.go b/test/e2e/e2e_suite_test.go index b702308f..9dd78e93 100644 --- a/test/e2e/e2e_suite_test.go +++ b/test/e2e/e2e_suite_test.go @@ -33,17 +33,16 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/klog/v2" - ctrl "sigs.k8s.io/controller-runtime" - clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" "sigs.k8s.io/cluster-api/test/framework" "sigs.k8s.io/cluster-api/test/framework/bootstrap" "sigs.k8s.io/cluster-api/test/framework/clusterctl" "sigs.k8s.io/cluster-api/test/framework/ginkgoextensions" + dockerinfrav1 "sigs.k8s.io/cluster-api/test/infrastructure/docker/api/v1beta1" + ctrl "sigs.k8s.io/controller-runtime" bootstrapv1 "github.com/canonical/cluster-api-k8s/bootstrap/api/v1beta2" controlplanev1 "github.com/canonical/cluster-api-k8s/controlplane/api/v1beta2" - dockerinfrav1 "sigs.k8s.io/cluster-api/test/infrastructure/docker/api/v1beta1" ) // Test suite flags. diff --git a/test/e2e/helpers.go b/test/e2e/helpers.go index 442c246c..24e8dec1 100644 --- a/test/e2e/helpers.go +++ b/test/e2e/helpers.go @@ -27,18 +27,17 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" "github.com/pkg/errors" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/util/sets" "k8s.io/klog/v2" - + clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" + expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1" "sigs.k8s.io/cluster-api/test/framework" "sigs.k8s.io/cluster-api/test/framework/clusterctl" "sigs.k8s.io/cluster-api/util/patch" "sigs.k8s.io/controller-runtime/pkg/client" controlplanev1 "github.com/canonical/cluster-api-k8s/controlplane/api/v1beta2" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/util/sets" - clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" - expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1" ) // NOTE: the code in this file is largely copied from the cluster-api test framework. diff --git a/test/e2e/md_remediation_test.go b/test/e2e/md_remediation_test.go index ebe06d61..9b2c9d4f 100644 --- a/test/e2e/md_remediation_test.go +++ b/test/e2e/md_remediation_test.go @@ -26,10 +26,8 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" - corev1 "k8s.io/api/core/v1" "k8s.io/utils/pointer" - "sigs.k8s.io/cluster-api/test/framework" "sigs.k8s.io/cluster-api/test/framework/clusterctl" "sigs.k8s.io/cluster-api/util" diff --git a/test/e2e/node_scale_test.go b/test/e2e/node_scale_test.go index 8de028b1..7845c891 100644 --- a/test/e2e/node_scale_test.go +++ b/test/e2e/node_scale_test.go @@ -26,10 +26,8 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" - corev1 "k8s.io/api/core/v1" "k8s.io/utils/pointer" - "sigs.k8s.io/cluster-api/test/framework/clusterctl" "sigs.k8s.io/cluster-api/util" ) From 590e50883a54a7787b72d9ac9a1d6dd7fe7de935 Mon Sep 17 00:00:00 2001 From: Benjamin Schimke Date: Tue, 2 Jul 2024 12:06:04 +0200 Subject: [PATCH 5/7] add golangci-lint --- .github/workflows/go.yaml | 7 +++++-- Makefile | 3 +++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/.github/workflows/go.yaml b/.github/workflows/go.yaml index f42f6fa1..27ead57b 100644 --- a/.github/workflows/go.yaml +++ b/.github/workflows/go.yaml @@ -11,8 +11,6 @@ permissions: jobs: test: - permissions: - contents: read # for actions/checkout to fetch code name: Unit Tests & Code Quality runs-on: ubuntu-latest @@ -35,5 +33,10 @@ jobs: - name: go vet run: make go-vet + - name: golangci-lint + uses: golangci/golangci-lint-action@v6 + with: + version: v1.59 + - name: unit tests run: make test-unit diff --git a/Makefile b/Makefile index 780f1e1a..7cc60fac 100644 --- a/Makefile +++ b/Makefile @@ -183,6 +183,9 @@ go-vet: go-fmt: go fmt ./... +go-lint: + golangci-lint run + test-unit: test-common test-bootstrap test-controlplane test-common: From f10795dc6aac9251f0c993072fcdc271e7748def Mon Sep 17 00:00:00 2001 From: Benjamin Schimke Date: Tue, 2 Jul 2024 16:19:53 +0200 Subject: [PATCH 6/7] use make lint --- .github/workflows/go.yaml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/.github/workflows/go.yaml b/.github/workflows/go.yaml index 27ead57b..2b621e29 100644 --- a/.github/workflows/go.yaml +++ b/.github/workflows/go.yaml @@ -33,10 +33,8 @@ jobs: - name: go vet run: make go-vet - - name: golangci-lint - uses: golangci/golangci-lint-action@v6 - with: - version: v1.59 + - name: go lint + run: make lint - name: unit tests run: make test-unit From 531d6f2040c34fad828a36558984dd740cdbf7da Mon Sep 17 00:00:00 2001 From: Benjamin Schimke Date: Tue, 2 Jul 2024 16:29:35 +0200 Subject: [PATCH 7/7] remove obsolete lint --- Makefile | 3 --- 1 file changed, 3 deletions(-) diff --git a/Makefile b/Makefile index 7cc60fac..780f1e1a 100644 --- a/Makefile +++ b/Makefile @@ -183,9 +183,6 @@ go-vet: go-fmt: go fmt ./... -go-lint: - golangci-lint run - test-unit: test-common test-bootstrap test-controlplane test-common: