Skip to content

Commit

Permalink
workflows-again (#4)
Browse files Browse the repository at this point in the history
* workflows
  • Loading branch information
jmnote authored Aug 5, 2024
1 parent 151a007 commit 93d5521
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 40 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -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

Expand Down
4 changes: 2 additions & 2 deletions internal/controller/configmap_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand Down
16 changes: 8 additions & 8 deletions internal/controller/configmap_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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() {
Expand All @@ -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
Expand All @@ -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)
})
})
})
2 changes: 1 addition & 1 deletion internal/controller/ingress_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions pkg/rulesstore/fake/fake.go
Original file line number Diff line number Diff line change
@@ -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
}
41 changes: 28 additions & 13 deletions pkg/rulesstore/rulesstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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 {
Expand All @@ -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()
Expand All @@ -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)
Expand Down
25 changes: 12 additions & 13 deletions pkg/rulesstore/rulesstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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"])
Expand All @@ -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()

Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down

0 comments on commit 93d5521

Please sign in to comment.