From 93d5521e5c6c572aea658ca7173130f966b4f5a2 Mon Sep 17 00:00:00 2001 From: Jmnote Date: Tue, 6 Aug 2024 01:50:20 +0900 Subject: [PATCH] workflows-again (#4) * workflows --- .github/workflows/pull-request.yml | 3 +- Makefile | 2 +- internal/controller/configmap_controller.go | 4 +- .../controller/configmap_controller_test.go | 16 ++++---- internal/controller/ingress_controller.go | 2 +- pkg/rulesstore/fake/fake.go | 33 +++++++++++++++ pkg/rulesstore/rulesstore.go | 41 +++++++++++++------ pkg/rulesstore/rulesstore_test.go | 25 ++++++----- 8 files changed, 86 insertions(+), 40 deletions(-) create mode 100644 pkg/rulesstore/fake/fake.go diff --git a/.github/workflows/pull-request.yml b/.github/workflows/pull-request.yml index f8f5ccc..6c56519 100644 --- a/.github/workflows/pull-request.yml +++ b/.github/workflows/pull-request.yml @@ -20,9 +20,8 @@ jobs: - uses: actions/setup-go@v4 - uses: actions/checkout@v3 - run: make test - - run: go test ./... -coverprofile=coverprofile - run: go install github.com/mattn/goveralls@latest - - run: goveralls -coverprofile=coverage.out -service=github + - run: goveralls -coverprofile=cover.out -service=github env: COVERALLS_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/Makefile b/Makefile index 7ffc050..371e181 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ # Image URL to use all building/pushing image targets -IMG ?= controller:latest +IMG ?= ghcr.io/kuoss/ingress-annotator:latest # ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary. ENVTEST_K8S_VERSION = 1.30.0 diff --git a/internal/controller/configmap_controller.go b/internal/controller/configmap_controller.go index ff543af..ad7acc8 100644 --- a/internal/controller/configmap_controller.go +++ b/internal/controller/configmap_controller.go @@ -34,7 +34,7 @@ import ( type ConfigMapReconciler struct { Client client.Client Scheme *runtime.Scheme - RulesStore *rulesstore.RulesStore + RulesStore rulesstore.IRulesStore } // +kubebuilder:rbac:groups=core,resources=configmaps,verbs=get;list;watch;create;update;patch;delete @@ -61,7 +61,7 @@ func (r *ConfigMapReconciler) Reconcile(ctx context.Context, req ctrl.Request) ( logger := log.FromContext(ctx) logger = logger.WithValues("namespace", req.Namespace, "name", req.Name) - if req.Namespace != r.RulesStore.ConfigMapNamespace || req.Name != r.RulesStore.ConfigMapName { + if req.Namespace != r.RulesStore.ConfigMapNamespace() || req.Name != r.RulesStore.ConfigMapName() { return ctrl.Result{}, nil } diff --git a/internal/controller/configmap_controller_test.go b/internal/controller/configmap_controller_test.go index 19c5ac4..91bca19 100644 --- a/internal/controller/configmap_controller_test.go +++ b/internal/controller/configmap_controller_test.go @@ -21,6 +21,7 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" + "github.com/stretchr/testify/assert" corev1 "k8s.io/api/core/v1" "k8s.io/api/node/v1alpha1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -30,7 +31,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client/fake" "sigs.k8s.io/controller-runtime/pkg/reconcile" - "github.com/kuoss/ingress-annotator/pkg/rulesstore" + fakerulesstore "github.com/kuoss/ingress-annotator/pkg/rulesstore/fake" ) var _ = Describe("ConfigMap Controller", func() { @@ -57,12 +58,9 @@ var _ = Describe("ConfigMap Controller", func() { // Set up the reconciler reconciler := &ConfigMapReconciler{ - Client: fakeClient, - Scheme: scheme, - RulesStore: &rulesstore.RulesStore{ // Mock - ConfigMapNamespace: "default", - ConfigMapName: "example-configmap", - }, + Client: fakeClient, + Scheme: scheme, + RulesStore: &fakerulesstore.RulesStore{}, } // Create a request for reconciliation @@ -81,7 +79,9 @@ var _ = Describe("ConfigMap Controller", func() { // Add more specific assertions depending on your controller's reconciliation logic // Example: Verify that the data in RulesStore has been updated updatedData := reconciler.RulesStore.GetData() - Expect(updatedData).ToNot(BeEmpty()) + + t := GinkgoT() + assert.NotEmpty(t, updatedData) }) }) }) diff --git a/internal/controller/ingress_controller.go b/internal/controller/ingress_controller.go index 80d48e6..ea2c7c1 100644 --- a/internal/controller/ingress_controller.go +++ b/internal/controller/ingress_controller.go @@ -34,7 +34,7 @@ import ( type IngressReconciler struct { client.Client Scheme *runtime.Scheme - RulesStore *rulesstore.RulesStore + RulesStore rulesstore.IRulesStore } // +kubebuilder:rbac:groups=networking.k8s.io,resources=ingresses,verbs=get;list;watch;create;update;patch;delete diff --git a/pkg/rulesstore/fake/fake.go b/pkg/rulesstore/fake/fake.go new file mode 100644 index 0000000..2c8918e --- /dev/null +++ b/pkg/rulesstore/fake/fake.go @@ -0,0 +1,33 @@ +package fake + +import ( + "github.com/kuoss/ingress-annotator/pkg/rulesstore" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +type RulesStore struct { +} + +func (s RulesStore) ConfigMapNamespace() string { + return "fake-namespace" +} + +func (s RulesStore) ConfigMapName() string { + return "fake-name" +} + +func (s RulesStore) GetData() *rulesstore.Data { + return &rulesstore.Data{ + ConfigMap: corev1.ConfigMap{ + ObjectMeta: metav1.ObjectMeta{}, + Data: map[string]string{}, + BinaryData: map[string][]byte{}, + }, + Rules: map[string]rulesstore.Annotations{"rules1": {"foo": "bar"}}, + } +} + +func (s RulesStore) UpdateData() error { + return nil +} diff --git a/pkg/rulesstore/rulesstore.go b/pkg/rulesstore/rulesstore.go index 7723c02..06081d4 100644 --- a/pkg/rulesstore/rulesstore.go +++ b/pkg/rulesstore/rulesstore.go @@ -9,6 +9,7 @@ import ( "gopkg.in/yaml.v3" corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/controller-runtime/pkg/client" ) @@ -21,26 +22,32 @@ type Data struct { Rules Rules } +type IRulesStore interface { + GetData() *Data + UpdateData() error + + ConfigMapNamespace() string + ConfigMapName() string +} + type RulesStore struct { - Client client.Client - ConfigMapNamespace string - ConfigMapName string - Data Data - DataMutex *sync.Mutex + Client client.Client + Meta types.NamespacedName + Data Data + DataMutex *sync.Mutex } -func New(client client.Client) (*RulesStore, error) { +func New(client client.Client) (IRulesStore, error) { ns, exists := os.LookupEnv("POD_NAMESPACE") if !exists || ns == "" { return nil, errors.New("POD_NAMESPACE environment variable is not set or is empty") } - rulesStore := &RulesStore{ - Client: client, - ConfigMapNamespace: ns, - ConfigMapName: configMapName, - Data: Data{}, - DataMutex: &sync.Mutex{}, + var rulesStore IRulesStore = &RulesStore{ + Client: client, + Meta: types.NamespacedName{Namespace: ns, Name: configMapName}, + Data: Data{}, + DataMutex: &sync.Mutex{}, } if err := rulesStore.UpdateData(); err != nil { @@ -50,6 +57,14 @@ func New(client client.Client) (*RulesStore, error) { return rulesStore, nil } +func (s *RulesStore) ConfigMapNamespace() string { + return s.Meta.Namespace +} + +func (s *RulesStore) ConfigMapName() string { + return s.Meta.Name +} + func (s *RulesStore) GetData() *Data { s.DataMutex.Lock() defer s.DataMutex.Unlock() @@ -61,7 +76,7 @@ func (s *RulesStore) UpdateData() error { var cm corev1.ConfigMap if err := s.Client.Get( context.Background(), - client.ObjectKey{Namespace: s.ConfigMapNamespace, Name: s.ConfigMapName}, + client.ObjectKey{Namespace: s.ConfigMapNamespace(), Name: s.ConfigMapName()}, &cm, ); err != nil { return fmt.Errorf("failed to get ConfigMap: %w", err) diff --git a/pkg/rulesstore/rulesstore_test.go b/pkg/rulesstore/rulesstore_test.go index b55bc08..0c5d6e1 100644 --- a/pkg/rulesstore/rulesstore_test.go +++ b/pkg/rulesstore/rulesstore_test.go @@ -9,6 +9,7 @@ import ( "github.com/stretchr/testify/assert" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/controller-runtime/pkg/client/fake" ) @@ -27,8 +28,8 @@ func TestNew(t *testing.T) { rulesStore, err := New(fakeClient) assert.NoError(t, err) - assert.Equal(t, "default", rulesStore.ConfigMapNamespace) - assert.Equal(t, configMapName, rulesStore.ConfigMapName) + assert.Equal(t, "default", rulesStore.ConfigMapNamespace()) + assert.Equal(t, configMapName, rulesStore.ConfigMapName()) data := rulesStore.GetData() assert.Equal(t, "annotation1: value1\nannotation2: value2", data.ConfigMap.Data["rule1"]) @@ -38,7 +39,7 @@ func TestNew(t *testing.T) { func TestNew_ErrorMissingPodNamespace(t *testing.T) { err := os.Unsetenv("POD_NAMESPACE") - assert.Error(t, err) + assert.NoError(t, err) fakeClient := fake.NewFakeClient() @@ -100,11 +101,10 @@ func TestUpdateData_ErrorGetConfigMap(t *testing.T) { fakeClient := fake.NewFakeClient() rulesStore := &RulesStore{ - Client: fakeClient, - ConfigMapNamespace: "default", - ConfigMapName: configMapName, - Data: Data{}, - DataMutex: &sync.Mutex{}, + Client: fakeClient, + Meta: types.NamespacedName{Namespace: "default", Name: configMapName}, + Data: Data{}, + DataMutex: &sync.Mutex{}, } err := rulesStore.UpdateData() @@ -127,11 +127,10 @@ func TestUpdateData_ErrorInvalidConfigMap(t *testing.T) { }) rulesStore := &RulesStore{ - Client: fakeClient, - ConfigMapNamespace: "default", - ConfigMapName: configMapName, - Data: Data{}, - DataMutex: &sync.Mutex{}, + Client: fakeClient, + Meta: types.NamespacedName{Namespace: "default", Name: configMapName}, + Data: Data{}, + DataMutex: &sync.Mutex{}, } err := rulesStore.UpdateData()