Skip to content

Commit

Permalink
tidy message undone, plus some new script changes
Browse files Browse the repository at this point in the history
Signed-off-by: Dhruv-J <[email protected]>
  • Loading branch information
Dhruv-J committed Dec 30, 2024
1 parent 6a561ee commit ae05e95
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 19 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ require (
gopkg.in/natefinch/lumberjack.v2 v2.2.1
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.1
gotest.tools v2.2.0+incompatible
k8s.io/api v0.31.1
k8s.io/apiextensions-apiserver v0.31.1
k8s.io/apimachinery v0.31.1
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,7 @@ gopkg.in/yaml.v3 v3.0.0-20200121175148-a6ecf24a6d71/go.mod h1:K4uyk7z7BCEPqu6E+C
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
2 changes: 1 addition & 1 deletion hack/update-codegen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ IMAGE_NAME="antrea/codegen:kubernetes-1.31.1-build.1"
# speed (bind mounts are slow) and to avoid having to add the source repository
# to the "safe" list in the Git config when starting the container (required
# because of user mismatch).
if git_status=$(git status --porcelain --untracked=no 2>/dev/null) && [[ -n "${git_status}" ]]; then
if git_status=$(git status --porcelain --untracked=no) && [[ -n "${git_status}" ]]; then
echoerr "!!! Dirty tree. Clean up and try again."
exit 1
fi
Expand Down
13 changes: 5 additions & 8 deletions pkg/agent/apis/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@
package apis

import (
"net"
"strconv"
"strings"
"time"

corev1 "k8s.io/api/core/v1"
"k8s.io/klog/v2"

"antrea.io/antrea/pkg/agent/types"
"antrea.io/antrea/pkg/apis/crd/v1beta1"
"antrea.io/antrea/pkg/util/printers"
)
Expand Down Expand Up @@ -76,9 +75,7 @@ func (r AntreaAgentInfoResponse) SortRows() bool {
}

type FqdnCacheResponse struct {
fqdnName string
ipAddress net.IP
expirationTime time.Time
*types.DnsCacheEntry
}

func (r FqdnCacheResponse) GetTableHeader() []string {
Expand All @@ -88,9 +85,9 @@ func (r FqdnCacheResponse) GetTableHeader() []string {
func (r FqdnCacheResponse) GetTableRow(maxColumn int) []string {
klog.InfoS("DBUG: types.go GetTableRow() called")
return []string{
r.fqdnName,
r.ipAddress.String(),
r.expirationTime.String(),
r.FqdnName,
r.IpAddress.String(),
r.ExpirationTime.String(),
}
}

Expand Down
10 changes: 0 additions & 10 deletions pkg/agent/apiserver/handlers/fqdncache/handler.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package fqdncache

import (
"bytes"
"encoding/json"
"net/http"
"reflect"

"k8s.io/klog/v2"

Expand All @@ -13,21 +11,13 @@ import (

func HandleFunc(aq agentquerier.AgentQuerier) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
klog.InfoS("DBUG: fqdn cache handler HandleFunc func called")
dnsEntryCache := aq.GetFqdnCache()
if dnsEntryCache == nil {
return
}
klog.InfoS("DBUG: dns entry cache", "obj", dnsEntryCache)
if err := json.NewEncoder(w).Encode(dnsEntryCache); err != nil {
http.Error(w, "Failed to encode response: "+err.Error(), http.StatusInternalServerError)
klog.ErrorS(err, "Failed to encode response")
}
buf := bytes.Buffer{}
if err := json.NewEncoder(&buf).Encode(dnsEntryCache); err != nil {
http.Error(w, "Failed to encode response: "+err.Error(), http.StatusInternalServerError)
klog.ErrorS(err, "Failed to encode response")
}
klog.InfoS("DBUG:", "type", reflect.TypeOf(dnsEntryCache), "buf", buf.String())
}
}
64 changes: 64 additions & 0 deletions pkg/agent/apiserver/handlers/fqdncache/handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package fqdncache

import (
"encoding/json"
"net"
"net/http"
"net/http/httptest"
"testing"
"time"

"antrea.io/antrea/pkg/agent/apis"
queriertest "antrea.io/antrea/pkg/agent/querier/testing"
"antrea.io/antrea/pkg/agent/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
)

func TestFqdnCacheQuery(t *testing.T) {
tests := []struct {
name string
expectedStatus int
expectedResponse []types.DnsCacheEntry
}{
{
name: "FQDN cache exists",
expectedStatus: http.StatusOK,
expectedResponse: []types.DnsCacheEntry{
{
FqdnName: "google.com",
IpAddress: net.ParseIP("10.0.0.1"),
ExpirationTime: time.Date(2025, 12, 25, 15, 0, 0, 0, time.Now().Location()),
},
},
},
{
name: "FQDN cache does not exist",
expectedStatus: http.StatusNotFound,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctrl := gomock.NewController(t)
q := queriertest.NewMockAgentQuerier(ctrl)
q.EXPECT().GetFqdnCache().Return(tt.expectedResponse[0])
handler := HandleFunc(q)

req, err := http.NewRequest(http.MethodGet, "", nil)
require.NoError(t, err)

recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, req)
assert.Equal(t, tt.expectedStatus, recorder.Code)

if tt.expectedStatus == http.StatusOK {
var received []apis.FqdnCacheResponse
err = json.Unmarshal(recorder.Body.Bytes(), &received)
require.NoError(t, err)
assert.Equal(t, tt.expectedResponse, received)
}
})
}
}
7 changes: 7 additions & 0 deletions pkg/agent/querier/testing/mock_querier.go

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

94 changes: 94 additions & 0 deletions pkg/antctl/transform/fqdncache/response_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package fqdncache

import (
"bytes"
"encoding/json"
"net"
"testing"
"time"

"antrea.io/antrea/pkg/agent/types"
"github.com/stretchr/testify/require"
"gotest.tools/assert"
)

func TestTrasnform(t *testing.T) {
var fqdn1 = types.DnsCacheEntry{
FqdnName: "google.com",
IpAddress: net.ParseIP("10.0.0.1"),
ExpirationTime: time.Date(2025, 12, 25, 15, 0, 0, 0, time.Now().Location()),
}
var fqdn2 = types.DnsCacheEntry{
FqdnName: "google.com",
IpAddress: net.ParseIP("10.0.0.2"),
ExpirationTime: time.Date(2025, 12, 25, 15, 0, 0, 0, time.Now().Location()),
}
var fqdn3 = types.DnsCacheEntry{
FqdnName: "google.com",
IpAddress: net.ParseIP("10.0.0.3"),
ExpirationTime: time.Date(2025, 12, 25, 15, 0, 0, 0, time.Now().Location()),
}
var fqdn4 = types.DnsCacheEntry{
FqdnName: "example.com",
IpAddress: net.ParseIP("10.0.0.4"),
ExpirationTime: time.Date(2025, 12, 25, 15, 0, 0, 0, time.Now().Location()),
}
var fqdn5 = types.DnsCacheEntry{
FqdnName: "antrea.io",
IpAddress: net.ParseIP("10.0.0.5"),
ExpirationTime: time.Date(2025, 12, 25, 15, 0, 0, 0, time.Now().Location()),
}
var fqdnList = []types.DnsCacheEntry{fqdn1, fqdn2, fqdn3, fqdn4, fqdn5}

tests := []struct {
name string
opts map[string]string
fqdnList []types.DnsCacheEntry
expectedResponse interface{}
expectedError string
}{
{
name: "all",
fqdnList: fqdnList,
expectedResponse: []Response{{&fqdn1}, {&fqdn2}, {&fqdn3}, {&fqdn4}, {&fqdn5}},
},
{
name: "only google.com domain name",
opts: map[string]string{
"domain": "google.com",
},
fqdnList: fqdnList,
expectedResponse: []Response{{&fqdn1}, {&fqdn2}, {&fqdn3}},
},
{
name: "only antrea.io domain name",
opts: map[string]string{
"domain": "antrea.io",
},
fqdnList: fqdnList,
expectedResponse: []Response{{&fqdn5}},
},
{
name: "domain name that doesn't exist",
opts: map[string]string{
"domain": "bing.com",
},
fqdnList: fqdnList,
expectedResponse: []Response{},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
reqByte, _ := json.Marshal(tt.fqdnList)
reqReader := bytes.NewReader(reqByte)
result, err := Transform(reqReader, false, tt.opts)
if tt.expectedError == "" {
require.NoError(t, err)
assert.Equal(t, tt.expectedResponse, result)
} else {
assert.ErrorContains(t, err, tt.expectedError)
}
})
}
}

0 comments on commit ae05e95

Please sign in to comment.