Skip to content

Commit

Permalink
minor edit
Browse files Browse the repository at this point in the history
  • Loading branch information
jmnote committed Aug 11, 2024
1 parent 354b2de commit ed97733
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 35 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
2 changes: 1 addition & 1 deletion config/manager/manager.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ spec:
cpu: 10m
memory: 64Mi
env:
- name: CONTROLLER_NAMESPACE
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
Expand Down
7 changes: 3 additions & 4 deletions internal/controller/configmap_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
47 changes: 19 additions & 28 deletions internal/controller/configmap_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,42 +24,43 @@ 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{
ObjectMeta: ctrl.ObjectMeta{
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{
ObjectMeta: ctrl.ObjectMeta{
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{
Expand All @@ -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)
})
}
}
Expand Down

0 comments on commit ed97733

Please sign in to comment.