diff --git a/.github/workflows/pull-request.yml b/.github/workflows/pull-request.yml index bae0649..4eaff87 100644 --- a/.github/workflows/pull-request.yml +++ b/.github/workflows/pull-request.yml @@ -24,12 +24,12 @@ jobs: ~/go/pkg/mod key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} restore-keys: | - ${{ runner.os }}-go- + ${{ runner.os }}-go- - uses: actions/setup-go@v4 - uses: actions/checkout@v3 - run: go mod download - run: make test - - run: go install github.com/mattn/goveralls@latest + - run: go install github.com/mattn/goveralls@v0.0.12 - run: goveralls -coverprofile=cover.out -service=github env: COVERALLS_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/config/manager/manager.yaml b/config/manager/manager.yaml index fef9712..9d782dc 100644 --- a/config/manager/manager.yaml +++ b/config/manager/manager.yaml @@ -92,7 +92,7 @@ spec: cpu: 10m memory: 64Mi env: - - name: CONTROLLER_NAMESPACE + - name: POD_NAMESPACE valueFrom: fieldRef: fieldPath: metadata.namespace diff --git a/internal/controller/configmap_controller.go b/internal/controller/configmap_controller.go index 38f59b0..1f370bb 100644 --- a/internal/controller/configmap_controller.go +++ b/internal/controller/configmap_controller.go @@ -46,10 +46,9 @@ type ConfigMapReconciler struct { // SetupWithManager sets up the controller with the Manager. func (r *ConfigMapReconciler) SetupWithManager(mgr ctrl.Manager) error { - ns := os.Getenv("CONTROLLER_NAMESPACE") - fmt.Printf("--- ns=[%#v]\n\n", ns) - if ns == "" { - return errors.New("CONTROLLER_NAMESPACE environment variable is not set or is empty") + ns, exists := os.LookupEnv("POD_NAMESPACE") + if !exists || ns == "" { + return errors.New("POD_NAMESPACE environment variable is not set or is empty") } r.ConfigMeta = types.NamespacedName{ Namespace: ns, diff --git a/internal/controller/configmap_controller_test.go b/internal/controller/configmap_controller_test.go index 517ef38..bc0d654 100644 --- a/internal/controller/configmap_controller_test.go +++ b/internal/controller/configmap_controller_test.go @@ -24,7 +24,13 @@ func TestConfigMapReconciler_SetupWithManager(t *testing.T) { wantError string }{ { - name: "successful setup 1", + name: "missing POD_NAMESPACE", + namespaceEnv: "", + objects: []client.Object{}, + wantError: "POD_NAMESPACE environment variable is not set or is empty", + }, + { + name: "unmarshal errors", namespaceEnv: "default", objects: []client.Object{ &corev1.ConfigMap{ @@ -32,12 +38,16 @@ func TestConfigMapReconciler_SetupWithManager(t *testing.T) { Name: "ingress-annotator-rules", Namespace: "default", }, + Data: map[string]string{ + "rule1": "invalid", + }, }, }, - wantError: "", + wantError: "yaml: unmarshal errors", }, + { - name: "successful setup 2", + name: "successful setup 1", namespaceEnv: "default", objects: []client.Object{ &corev1.ConfigMap{ @@ -45,21 +55,12 @@ func TestConfigMapReconciler_SetupWithManager(t *testing.T) { Name: "ingress-annotator-rules", Namespace: "default", }, - Data: map[string]string{ - "rule1": "annotations:\n key1: value1\nnamespace: test-namespace\ningress: test-ingress", - }, }, }, wantError: "", }, { - name: "missing CONTROLLER_NAMESPACE", - namespaceEnv: "", - objects: []client.Object{}, - wantError: "CONTROLLER_NAMESPACE environment variable is not set or is empty", - }, - { - name: "successful setup", + name: "successful setup 2", namespaceEnv: "default", objects: []client.Object{ &corev1.ConfigMap{ @@ -68,39 +69,29 @@ func TestConfigMapReconciler_SetupWithManager(t *testing.T) { Namespace: "default", }, Data: map[string]string{ - "rule1": "invalid", + "rule1": "annotations:\n key1: value1\nnamespace: test-namespace\ningress: test-ingress", }, }, }, - wantError: "yaml: unmarshal errors", + wantError: "", }, } for i, tc := range testCases { t.Run(tester.Name(i, tc.name), func(t *testing.T) { - t.Log(111) - t.Setenv("CONTROLLER_NAMESPACE", tc.namespaceEnv) - t.Log(222) + t.Setenv("POD_NAMESPACE", tc.namespaceEnv) - fakeClient := newFakeClient(tc.objects...) reconciler := &ConfigMapReconciler{ - Client: fakeClient, + Client: newFakeClient(tc.objects...), Scheme: newScheme(), RulesStore: rulesstore.New(), } - t.Log(333) - - mgr := newFakeManager() - t.Log(555) - - err := reconciler.SetupWithManager(mgr) - t.Log(666) + err := reconciler.SetupWithManager(newFakeManager()) if tc.wantError != "" { assert.ErrorContains(t, err, tc.wantError) } else { assert.NoError(t, err) } - t.Log(777) }) } }