Skip to content

Commit

Permalink
Add Go CI (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
bschimke95 authored Jul 2, 2024
1 parent 7bac9bc commit fc59680
Show file tree
Hide file tree
Showing 15 changed files with 68 additions and 31 deletions.
40 changes: 40 additions & 0 deletions .github/workflows/go.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Go

on:
push:
branches:
- main
pull_request:

permissions:
contents: read

jobs:
test:
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
run: make go-fmt

- name: go vet
run: make go-vet

- name: go lint
run: make lint

- name: unit tests
run: make test-unit
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 5 additions & 0 deletions bootstrap/api/v1beta2/zz_generated.deepcopy.go

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

3 changes: 2 additions & 1 deletion controlplane/controllers/machine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion pkg/ck8s/management_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion pkg/ck8s/workload_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
14 changes: 3 additions & 11 deletions pkg/ck8s/workload_cluster_k8sd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions pkg/cloudinit/controlplane_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 0 additions & 2 deletions test/e2e/cluster_upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 0 additions & 1 deletion test/e2e/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 0 additions & 2 deletions test/e2e/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down
5 changes: 2 additions & 3 deletions test/e2e/e2e_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 4 additions & 5 deletions test/e2e/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 0 additions & 2 deletions test/e2e/md_remediation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 0 additions & 2 deletions test/e2e/node_scale_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down

0 comments on commit fc59680

Please sign in to comment.